Parts.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace app\admin\controller\qingdongams\parts;
  3. use addons\qingdongams\model\Account;
  4. use addons\qingdongams\model\Contacts;
  5. use addons\qingdongams\model\Customer;
  6. use addons\qingdongams\model\ExamineRecord;
  7. use addons\qingdongams\model\File;
  8. use addons\qingdongams\model\Form;
  9. use addons\qingdongams\model\Message;
  10. use addons\qingdongams\model\Parts as PartsModel;
  11. use addons\qingdongams\model\PartsStock;
  12. use addons\qingdongams\model\PartsStockReload;
  13. use addons\qingdongams\model\Staff;
  14. use app\admin\controller\qingdongams\Base;
  15. use addons\qingdongams\model\Product;
  16. use think\Db;
  17. use think\Exception;
  18. /**
  19. * 备件管理
  20. * @icon fa fa-user
  21. */
  22. class Parts extends Base
  23. {
  24. protected $relationSearch = true;
  25. protected $searchFields = 'id,name,num,parts.odd_numbers,parts.create_staff.name';
  26. /**
  27. * @var \addons\qingdongams\model\Parts
  28. */
  29. protected $model = null;
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. $this->model = new \addons\qingdongams\model\Parts;
  34. }
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. //
  45. //负责人为空
  46. $list = $this->model->where($where)->order($sort, $order)->paginate($limit);
  47. $result = array("total" => $list->total(), "rows" => $list->items());
  48. return json($result);
  49. }
  50. return $this->view->fetch();
  51. }
  52. /**
  53. * 添加
  54. */
  55. public function add()
  56. {
  57. if ($this->request->isPost()) {
  58. $params = $this->request->post("row/a");
  59. if ($params) {
  60. $params = $this->preExcludeFields($params);
  61. $params['product_id'] = implode(',', $params['product_id']);
  62. $result = false;
  63. Db::startTrans();
  64. try {
  65. $result = $this->model->allowField(true)->save($params);
  66. Db::commit();
  67. } catch (Exception $e) {
  68. Db::rollback();
  69. $this->error($e->getMessage());
  70. }
  71. if ($result !== false) {
  72. $this->success();
  73. } else {
  74. $this->error(__('No rows were inserted'));
  75. }
  76. }
  77. $this->error(__('Parameter %s can not be empty', ''));
  78. }
  79. $product = Product::where([])->column('name', 'id');
  80. $this->view->assign('product', $product);
  81. return $this->view->fetch();
  82. }
  83. //修改
  84. public function edit($ids = null)
  85. {
  86. $map['id'] = $ids;
  87. if ($this->request->isAjax()) {
  88. $params = $this->request->post('row/a');
  89. $params = $this->preExcludeFields($params);
  90. $params['product_id'] = implode(',', $params['product_id']);
  91. $result = false;
  92. Db::startTrans();
  93. try {
  94. $result = $this->model->allowField(true)->update($params, $map);
  95. Db::commit();
  96. } catch (Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. if ($result !== false) {
  101. $this->success('修改成功');
  102. } else {
  103. $this->error('修改失败');
  104. }
  105. }
  106. $data = $this->model->where($map)->find()->getData();
  107. $this->view->assign("row", $data);
  108. $product = Product::where([])->column('name', 'id');
  109. $this->view->assign('product', $product);
  110. return $this->view->fetch();
  111. }
  112. public function detail($ids = null)
  113. {
  114. $row = $this->model->where(['id' => $ids])->find();
  115. $this->assign('row', $row);
  116. $this->assign('ids', $ids);
  117. return $this->view->fetch();
  118. }
  119. /**
  120. * 删除
  121. */
  122. public function del($ids = "")
  123. {
  124. if (!$this->request->isPost()) {
  125. $this->error(__("Invalid parameters"));
  126. }
  127. $ids = $ids ? $ids : $this->request->post("ids");
  128. $row = $this->model->get($ids);
  129. $this->modelValidate = true;
  130. if (!$row) {
  131. $this->error(__('No Results were found'));
  132. }
  133. $row->delete();
  134. $this->success();
  135. }
  136. /**
  137. * 出库
  138. */
  139. public function export($ids = null)
  140. {
  141. if ($this->request->isPost()) {
  142. $params = $this->request->post("row/a");
  143. if ($params) {
  144. $params = $this->preExcludeFields($params);
  145. $parts = PartsModel::where(['id' => $ids])->with(['createStaff'])->find();
  146. $result = false;
  147. Db::startTrans();
  148. try {
  149. $parts['number'] = $params['number'];
  150. unset($params['number']);
  151. $params['parts'][] = $parts;
  152. $params['type'] = 2;
  153. $params['parts'] = json_encode($params['parts']);
  154. PartsStockReload::addRecord($params);
  155. Db::commit();
  156. $this->success();
  157. } catch (Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. }
  162. $this->error(__('Parameter %s can not be empty', ''));
  163. }
  164. $parts = PartsModel::where(['id' => $ids])->with(['createStaff'])->find();
  165. if (!empty($parts['product_id'])) {
  166. $product_ids = explode(',', $parts['product_id']);
  167. $names = \addons\qingdongams\model\Product::where(['id' => ['in', $product_ids]])->column('name');
  168. $parts['product_name'] = implode(',', $names);
  169. }
  170. $parts['stocks'] = PartsStock::where(['parts_id' => $ids])->with(['createStaff'])->select();
  171. $this->assign('parts', $parts);
  172. $this->assign('ids', $ids);
  173. $this->assign('staffs', Staff::getList());
  174. $this->assign('number', getItemNumber('export'));
  175. return $this->view->fetch();
  176. }
  177. /**
  178. * 入库
  179. */
  180. public function import($ids = null)
  181. {
  182. if ($this->request->isPost()) {
  183. $params = $this->request->post("row/a");
  184. if ($params) {
  185. $params = $this->preExcludeFields($params);
  186. $parts = PartsModel::where(['id' => $ids])->with(['createStaff'])->find();
  187. Db::startTrans();
  188. try {
  189. $parts['number'] = $params['number'];
  190. unset($params['number']);
  191. $params['parts'][] = $parts;
  192. $params['type'] = 1;
  193. $params['parts'] = json_encode($params['parts']);
  194. $params['desc'] = $params['remarks'];
  195. PartsStockReload::addRecord($params);
  196. Db::commit();
  197. $this->success();
  198. } catch (Exception $e) {
  199. Db::rollback();
  200. $this->error($e->getMessage());
  201. }
  202. }
  203. $this->error(__('Parameter %s can not be empty', ''));
  204. }
  205. $parts = PartsModel::where(['id' => $ids])->with(['createStaff'])->find();
  206. if (!empty($parts['product_id'])) {
  207. $product_ids = explode(',', $parts['product_id']);
  208. $names = \addons\qingdongams\model\Product::where(['id' => ['in', $product_ids]])->column('name');
  209. $parts['product_name'] = implode(',', $names);
  210. }
  211. $parts['stocks'] = PartsStock::where(['parts_id' => $ids])->with(['createStaff'])->select();
  212. $this->assign('parts', $parts);
  213. $this->assign('ids', $ids);
  214. $this->assign('staffs', Staff::getList());
  215. $this->assign('number', getItemNumber('import'));
  216. return $this->view->fetch();
  217. }
  218. /**
  219. * 审批记录
  220. */
  221. public function logs()
  222. {
  223. $this->relationSearch = true;
  224. $this->request->filter(['strip_tags']);
  225. if ($this->request->isAjax()) {
  226. list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, null);
  227. $filter = $this->request->get("filter", '');
  228. $filter = (array)json_decode($filter, true);
  229. $filter = $filter ? $filter : [];
  230. if (array_key_exists('status', $filter)) {
  231. $wheres['status'] = $filter['status'];
  232. }
  233. if (array_key_exists('createtime', $filter)) {
  234. $time = explode(' - ', $filter['createtime']);
  235. $wheres['createtime'] = ['between', [strtotime($time[0]), strtotime($time[1])]];
  236. }
  237. $ids = input('ids');
  238. if ($ids) {
  239. $reloads = PartsStockReload::where(['create_staff_id' => $this->_staff->id])->column('id');
  240. $wheres['relation_id'] = ['in', $reloads];
  241. }
  242. $wheres['relation_type'] = 'parts';
  243. $model = new ExamineRecord();
  244. $list = $model
  245. ->with([
  246. 'checkStaff',
  247. 'parts' => ['createStaff']]
  248. )
  249. // ->where($where)
  250. ->where($wheres)
  251. ->order('id', $order)
  252. ->paginate($limit);
  253. $row = $list->items();
  254. $result = array("total" => $list->total(), "rows" => $row);
  255. return json($result);
  256. }
  257. return $this->view->fetch();
  258. }
  259. /**
  260. * 出入库记录
  261. */
  262. public function get_stock_record()
  263. {
  264. if ($this->request->isAjax()) {
  265. $limit = 10;
  266. $page = $this->request->post('pageNumber');
  267. $ids = input('ids');
  268. $type = input('type');
  269. if (empty($ids)) {
  270. $this->error('库存不存在');
  271. }
  272. $where = [];
  273. if ($type) {
  274. $where['type'] = $type;
  275. }
  276. if ($ids) {
  277. $where['parts_id'] = $ids;
  278. }
  279. $stocks = PartsStock::where($where)->with(['parts', 'createStaff'])->paginate($limit, false, ['page' => $page]);
  280. $stocks = $stocks->toArray();
  281. return json(['rows' => $stocks['data'], 'total' => $stocks['total']]);
  282. }
  283. }
  284. public function log_info($ids = null)
  285. {
  286. $types = input('types', 'parts');
  287. $row = PartsStockReload::where(['id' => $ids])->find();
  288. if ($row['check_status'] == 0 || $row['check_status'] == 1) {
  289. $row['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::PARTS_TYPE, $ids);
  290. } else {
  291. $row['is_examine'] = 0;
  292. }
  293. $this->assign('row', $row);
  294. $this->assign('ids', $ids);
  295. $examin = ExamineRecord::with('checkStaff')->where(array('relation_id' => $ids, 'relation_type' => 'parts'))->select();
  296. foreach ($examin as $k => $v) {
  297. switch ($v['status']) {
  298. case 0:
  299. $examin[$k]['status_name'] = '待审批';
  300. break;
  301. case 1:
  302. $examin[$k]['status_name'] = '审核通过';
  303. break;
  304. case 2:
  305. $examin[$k]['status_name'] = '审核拒绝';
  306. break;
  307. case 3:
  308. $examin[$k]['status_name'] = '撤销';
  309. break;
  310. default:
  311. $examin[$k]['status_name'] = '待审批';
  312. break;
  313. }
  314. }
  315. Message::setRead($types, $ids, $this->_staff->id);
  316. $this->assign('examin', $examin);
  317. return $this->view->fetch();
  318. }
  319. }