ScrappService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DispatchSub;
  4. use App\Model\Employee;
  5. use App\Model\Equipment;
  6. use App\Model\Process;
  7. use App\Model\Scrapp;
  8. use App\Model\ScrappCount;
  9. use App\Model\Team;
  10. /**
  11. * 报废原因
  12. * @package App\Models
  13. */
  14. class ScrappService extends Service
  15. {
  16. public function scrappEdit($data){
  17. list($status,$msg) = $this->scrappRule($data,false);
  18. if(!$status) return [$status,$msg];
  19. $update = $msg['data'][0];
  20. Scrapp::where('id',$data['id'])->update($update);
  21. return [true,'保存成功!'];
  22. }
  23. public function scrappAdd($data){
  24. list($status,$msg) = $this->scrappRule($data);
  25. if(!$status) return [$status,$msg];
  26. Scrapp::insert($msg['data']);
  27. return [true,'保存成功!'];
  28. }
  29. public function scrappDel($data){
  30. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  31. Scrapp::whereIn('id',$data['id'])->update([
  32. 'del_time' => time()
  33. ]);
  34. return [true,'删除成功'];
  35. }
  36. public function scrappList($data){
  37. $model = Scrapp::where('del_time',0)
  38. ->select('*');
  39. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  40. $list = $this->limit($model,'',$data);
  41. return [200,$list];
  42. }
  43. public function scrappRule($data,$is_add = true){
  44. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  45. $title = array_column($data['data'],'title');
  46. $title = array_map(function($val) {
  47. return $val !== null ? $val : 0;
  48. }, $title);
  49. $title_count = array_count_values($title);
  50. foreach ($title as $value){
  51. if(empty($value)) return [false,'名称不能为空!'];
  52. if($title_count[$value] > 1) return [false,'名称不能重复'];
  53. }
  54. foreach ($data['data'] as $key => $value){
  55. $data['data'][$key]['upd_time'] = time();
  56. if($is_add){
  57. $bool = Scrapp::whereRaw("title = '{$value['title']}'")
  58. ->where('del_time',0)
  59. ->exists();
  60. $data['data'][$key]['crt_time'] = time();
  61. }else{
  62. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  63. $bool = Scrapp::whereRaw("title = '{$value['title']}'")
  64. ->where('id','<>',$data['id'])
  65. ->where('del_time',0)
  66. ->exists();
  67. }
  68. if($bool) return [false,'名称不能重复'];
  69. }
  70. return [true,$data];
  71. }
  72. //质检单
  73. public function zjList($data, $user){
  74. $model = ScrappCount::where('del_time',0)
  75. ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id')
  76. ->groupBy('order_number');
  77. if(! empty($data['order_number'])) {
  78. $order_number = str_replace("ZJ","",$data['order_number']);
  79. if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  80. }
  81. if(! empty($data['dispatch_no'])) {
  82. $dispatch_id = DispatchSub::where('del_time',0)
  83. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  84. ->select('id')
  85. ->get()->toArray();
  86. $dispatch_id = array_column($dispatch_id,'id');
  87. $model->whereIn('dispatch_sub_id', $dispatch_id);
  88. }
  89. if(! empty($data['team_id'])) $model->where('team_id', $data['team_id']);
  90. if(! empty($data['finished_id'])) $model->where('finished_id', $data['finished_id']);
  91. if(! empty($data['equipment_id'])) $model->where('equipment_id', $data['equipment_id']);
  92. if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
  93. $list = $this->limit($model,'',$data);
  94. $list = $this->fillZjList($list);
  95. return [true,$list];
  96. }
  97. public function fillZjList($data){
  98. if(empty($data['data'])) return $data;
  99. $emp_map = Employee::whereIn('id',array_column($data['data'],'finished_id'))
  100. ->pluck('emp_name','id')
  101. ->toArray();
  102. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  103. ->pluck('title','id')
  104. ->toArray();
  105. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  106. ->pluck('title','id')
  107. ->toArray();
  108. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  109. ->pluck('title','id')
  110. ->toArray();
  111. foreach ($data['data'] as $key => $value){
  112. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
  113. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  114. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  115. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  116. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  117. $data['data'][$key]['order_number'] = "ZJ" . $value['order_number'];
  118. }
  119. return $data;
  120. }
  121. public function zjDetail($data, $user){
  122. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  123. $order_number = str_replace("ZJ","",$data['order_number']);
  124. $result = ScrappCount::where('del_time',0)
  125. ->where('order_number', $order_number)
  126. ->get()->toArray();
  127. if(empty($result)) return [false, '质检单不存在或已被删除'];
  128. $first = $result[0] ?? [];
  129. $order['order_number'] = "ZJ" . $first['order_number'];
  130. $order['crt_time'] = $first['crt_time'] ? date("Y-m-d H:i:s", $first['crt_time']) : '';
  131. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  132. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  133. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  134. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  135. $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
  136. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  137. $order['dispatch_no'] = $dispatch['order_no'] ?? "";
  138. $scrapp_quantity = array_sum(array_column($result, 'scrapp_num')); // 不良品数量
  139. $detail = [
  140. 'product_no' => $first['product_no'],
  141. 'product_title' => $first['product_title'],
  142. 'product_size' => $first['product_size'],
  143. 'product_unit' => $first['product_unit'],
  144. 'technology_material' => $first['technology_material'],
  145. 'wood_name' => $first['wood_name'],
  146. 'production_quantity' => $dispatch['production_quantity'] ?? 0, // 生产数量
  147. 'dispatch_quantity' => $dispatch['dispatch_quantity'] ?? 0, // 派工数量
  148. 'quantity' => $first['quantity'], // 完工数量
  149. 'scrapp_quantity' => $scrapp_quantity, // 不良品数量
  150. 'zj_quantity' => $first['quantity'] + $scrapp_quantity, // 质检数量
  151. 'hg_quantity' => $first['quantity'], // 合格数量
  152. ];
  153. $order['detail'][] = $detail;
  154. return [true, $order];
  155. }
  156. //不良品
  157. public function blpList($data, $user){
  158. $model = ScrappCount::where('del_time',0)
  159. ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id')
  160. ->groupBy('order_number');
  161. if(! empty($data['order_number'])) {
  162. $order_number = str_replace("BLP","",$data['order_number']);
  163. if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  164. }
  165. if(! empty($data['dispatch_no'])) {
  166. $dispatch_id = DispatchSub::where('del_time',0)
  167. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  168. ->select('id')
  169. ->get()->toArray();
  170. $dispatch_id = array_column($dispatch_id,'id');
  171. $model->whereIn('dispatch_sub_id', $dispatch_id);
  172. }
  173. if(! empty($data['team_id'])) $model->where('team_id', $data['team_id']);
  174. if(! empty($data['finished_id'])) $model->where('finished_id', $data['finished_id']);
  175. if(! empty($data['equipment_id'])) $model->where('equipment_id', $data['equipment_id']);
  176. if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
  177. $list = $this->limit($model,'',$data);
  178. $list = $this->fillBLPList($list);
  179. return [true,$list];
  180. }
  181. public function fillBLPList($data){
  182. if(empty($data['data'])) return $data;
  183. $emp_map = Employee::whereIn('id',array_column($data['data'],'finished_id'))
  184. ->pluck('emp_name','id')
  185. ->toArray();
  186. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  187. ->pluck('title','id')
  188. ->toArray();
  189. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  190. ->pluck('title','id')
  191. ->toArray();
  192. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  193. ->pluck('title','id')
  194. ->toArray();
  195. foreach ($data['data'] as $key => $value){
  196. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
  197. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  198. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  199. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  200. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  201. $data['data'][$key]['order_number'] = "BLP" . $value['order_number'];
  202. }
  203. return $data;
  204. }
  205. public function blpDetail($data, $user){
  206. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  207. $order_number = str_replace("BLP","",$data['order_number']);
  208. $result = ScrappCount::where('del_time',0)
  209. ->where('order_number', $order_number)
  210. ->get()->toArray();
  211. if(empty($result)) return [false, '不良品单不存在或已被删除'];
  212. $first = $result[0] ?? [];
  213. $order['order_number'] = "BLP" . $first['order_number'];
  214. $order['crt_time'] = $first['crt_time'] ? date("Y-m-d H:i:s", $first['crt_time']) : '';
  215. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  216. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  217. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  218. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  219. $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
  220. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  221. $order['dispatch_no'] = $dispatch['order_no'] ?? "";
  222. $scrapp = Scrapp::whereIn('id', array_unique(array_column($result,'scrapp_id')))
  223. ->pluck('title','id')
  224. ->toArray();
  225. $detail = [];
  226. foreach ($result as $value){
  227. $tmp = [
  228. 'product_no' => $first['product_no'],
  229. 'product_title' => $first['product_title'],
  230. 'product_size' => $first['product_size'],
  231. 'product_unit' => $first['product_unit'],
  232. 'technology_material' => $first['technology_material'],
  233. 'wood_name' => $first['wood_name'],
  234. 'scrapp_quantity' => $value['scrapp_num'], // 不良品数量
  235. 'scrapp_title' => $scrapp[$value['scrapp_id']], // 不良品原因
  236. ];
  237. $detail[] = $tmp;
  238. }
  239. $order['detail'] = $detail;
  240. return [true, $order];
  241. }
  242. }