Parts.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Parts as PartsModel;
  4. use addons\qingdongams\model\Message;
  5. use addons\qingdongams\model\PartsStock;
  6. use addons\qingdongams\model\PartsStockReload;
  7. use think\Db;
  8. use think\Exception;
  9. /**
  10. * 零件管理
  11. */
  12. class Parts extends StaffApi
  13. {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = [];
  16. //获取零件列表
  17. public function getList()
  18. {
  19. $limit = input("limit/d", 10);
  20. $params = $this->request->post();
  21. $where = [];
  22. $stock='';
  23. $order = 'id desc';
  24. if (isset($params['name']) && $params['name']) {//产品名称
  25. $where['name'] = ['like', "%{$params['name']}%"];
  26. }
  27. if(isset($params['type']) && $params['type'] == 1){
  28. $stock='stock <= warning_stock';
  29. }
  30. $list = PartsModel::where($where)->with([
  31. 'createStaff',
  32. ])->where($stock)->order($order)->paginate($limit);
  33. $this->success('请求成功', $list);
  34. }
  35. //获取selelct列表
  36. public function getSelectList()
  37. {
  38. $name = input('name');
  39. $product_id = input('product_id');
  40. $where = [];
  41. if ($name) {
  42. $where['name'] = ['like', "%{$name}%"];
  43. }
  44. $whereProduct = [];
  45. if ($product_id) {//产品
  46. $product_id=intval($product_id);
  47. $whereProduct[] = ['exp', Db::raw('FIND_IN_SET(' . $product_id . ',product_id)')];
  48. }
  49. $list = PartsModel::where($where)
  50. ->where($whereProduct)
  51. ->with(['createStaff'])
  52. ->field('id,name,stock,num,img,unit,price,status')->select();
  53. $this->success('请求成功', $list);
  54. }
  55. //添加零件
  56. public function addParts()
  57. {
  58. $params = $this->request->post();
  59. if (PartsModel::where(['name' => $params['name']])->find()) {
  60. $this->error('零件名称已存在');
  61. }
  62. try {
  63. $result = PartsModel::createParts($params);
  64. Db::commit();
  65. } catch (Exception $e) {
  66. Db::rollback();
  67. $this->error($e->getMessage());
  68. }
  69. if ($result) {
  70. $this->success('新增零件成功');
  71. }
  72. }
  73. //修改零件
  74. public function editParts()
  75. {
  76. $id = input('id');
  77. $params = $this->request->post();
  78. $row = PartsModel::where(['id' => $id])->find();
  79. if (empty($row)) {
  80. $this->error('修改零件信息不存在');
  81. }
  82. $params['product_id']=implode(',',$params['product_id']);
  83. // 表单验证
  84. if (($result = $this->qingdongamsValidate($params,get_class(), 'edit')) !== true) {
  85. $this->error($result);
  86. }
  87. Db::startTrans();
  88. try {
  89. $result = PartsModel::updateParts($params);
  90. Db::commit();
  91. } catch (Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. $this->success('修改零件信息成功');
  96. }
  97. //获取零件详情
  98. public function getPartsDetail()
  99. {
  100. $id = input('id');
  101. $parts = PartsModel::where(['id' => $id])->with(['createStaff'])->find();
  102. if (empty($parts)) {
  103. $this->error('零件信息不存在');
  104. }
  105. if (!empty($parts['product_id'])) {
  106. $product_ids = explode(',', $parts['product_id']);
  107. $names = \addons\qingdongams\model\Product::where(['id' => ['in', $product_ids]])->column('name');
  108. $parts['product_name'] = implode(',', $names);
  109. }
  110. $parts['stocks'] = PartsStock::where(['parts_id' => $id])->with(['createStaff'])->select();
  111. Message::setRead(Message::PARTS_TYPE,$id,$this->auth->id);
  112. $this->success('请求成功', $parts);
  113. }
  114. //获取库存记录
  115. public function getStockRecord()
  116. {
  117. $parts_id = input('parts_id');
  118. $type = input('type');
  119. if (empty($parts_id)) {
  120. $this->error('库存不存在');
  121. }
  122. $where = ['parts_id' => $parts_id];
  123. if ($type) {
  124. $where['type'] = $type;
  125. }
  126. $stocks = PartsStock::where($where)->with(['parts', 'createStaff'])->select();
  127. $this->success('请求成功', $stocks);
  128. }
  129. //添加库存
  130. public function addStocks()
  131. {
  132. $params = $this->request->post();
  133. // 表单验证
  134. if (($result = $this->validate($params, 'addons\qingdongams\validate\PartsStockReload.create')) !== true) {
  135. $this->error($result);
  136. }
  137. Db::startTrans();
  138. try {
  139. $parts = [];
  140. foreach ($params['parts'] as $part) {
  141. $parts[] = ['id' => $part['id'], 'number' => $part['number']];
  142. }
  143. $params['parts'] = json_encode($params['parts']);
  144. PartsStockReload::addRecord($params);
  145. Db::commit();
  146. } catch (Exception $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. }
  150. $this->success('提交成功');
  151. }
  152. //首页-库存审批记录
  153. public function examineStockRecord()
  154. {
  155. $limit = input('limit', 10);
  156. $type = input('type');//1 出库 入库
  157. $check_status = input('check_status'); //审核状态 1审核中、2审核通过、3审核未通过
  158. $where = [];
  159. if ($type) {
  160. $where['type'] = $type;
  161. }
  162. $whereFlow = [];
  163. $where = [];
  164. if ($check_status) {
  165. //0待审核、1审核中、2审核通过、3审核未通过、4撤销
  166. if ($check_status == 1) {
  167. $where['check_status'] = ['in', [0, 1]];
  168. } elseif ($check_status == 2) {
  169. $where['check_status'] = 2;
  170. } elseif ($check_status == 3) {
  171. $where['check_status'] = ['in', [3, 4]];
  172. }
  173. }
  174. $select = PartsStockReload::where($where)->with(['createStaff'])->paginate($limit);
  175. $this->success('请求成功', $select);
  176. }
  177. //我的库存记录
  178. public function myStockRecord()
  179. {
  180. $limit = input('limit', 10);
  181. $times = input('times');
  182. $type = input('type');//
  183. $check_status = input('check_status');//审核状态 1审核中、2审核通过、3审核未通过
  184. $check_type = input('check_type', 1);
  185. $where = [];
  186. $whereFlow = [];
  187. if ($type) {
  188. $where['type'] = $type;
  189. }
  190. if ($check_type == 1) {//我发起的
  191. $where['create_staff_id'] = $this->auth->id;
  192. } elseif ($check_type == 2) {//我审批的
  193. $whereFlow[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',flow_staff_ids)')];
  194. }
  195. if ($check_status) {
  196. //0待审核、1审核中、2审核通过、3审核未通过、4撤销
  197. if ($check_status == 1) {
  198. $where['check_status'] = ['in', [0, 1]];
  199. } elseif ($check_status == 2) {
  200. $where['check_status'] = 2;
  201. } elseif ($check_status == 3) {
  202. $where['check_status'] = ['in', [3, 4]];
  203. }
  204. }
  205. // $where['create_staff_id'] = $this->auth->id;
  206. if ($times) {//创建时间
  207. $times = explode(',', $times);
  208. $where['createtime'] = ['between', [$times[0], $times[1]]];
  209. }
  210. $select = PartsStockReload::where($where)->where($whereFlow)->order('id desc')->with(['createStaff'])->paginate($limit);
  211. $this->success('请求成功', $select);
  212. }
  213. //获取我的 已申请和待我审核
  214. public function getReloadNumber()
  215. {
  216. $check_type = input('check_type', 1);
  217. $where = [
  218. 'check_status' => ['in', [0, 1]],];
  219. $whereFlow = [];
  220. if ($check_type == 1) {//我发起的
  221. $where['create_staff_id'] = $this->auth->id;
  222. } elseif ($check_type == 2) {//我审批的
  223. $whereFlow[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',flow_staff_ids)')];
  224. }
  225. $ruku = PartsStockReload::where([
  226. 'type' => 1,
  227. ])->where($where)->where($whereFlow)->count();
  228. $chuku = PartsStockReload::where([
  229. 'type' => 2,
  230. ])->where($where)->where($whereFlow)->count();
  231. $this->success('请求成功', ['ruku' => $ruku, 'chuku' => $chuku]);
  232. }
  233. //获取零件审批详情
  234. public function getPartsReloadDetail(){
  235. $id=input('id');
  236. $detail = PartsStockReload::where([
  237. 'id' => $id,
  238. ])->with(['createStaff'])->find();
  239. $detail['is_examine'] = 0;
  240. $detail['is_operation'] = 0;
  241. if(in_array($detail['check_status'],[0,1]) && in_array($this->auth->id,explode(',',$detail['flow_staff_ids']))){
  242. $detail['is_examine'] = 1;
  243. $detail['is_operation'] = 1;
  244. }
  245. if($detail['create_staff_id'] == $this->auth->id){
  246. //是否可以操作
  247. $detail['is_operation']=1;
  248. }
  249. Message::setRead(Message::PARTS_TYPE,$id,$this->auth->id);
  250. $this->success('请求成功', $detail);
  251. }
  252. //获取零件编号
  253. public function getPartsNumber()
  254. {
  255. $this->success('请求成功', ['number' => 'KQ' . date('Ymdhis') . rand(1000, 9999)]);
  256. }
  257. }