ScrappService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. $list = $this->limit($model,'',$data);
  82. $list = $this->fillZjList($list);
  83. return [true,$list];
  84. }
  85. public function fillZjList($data){
  86. if(empty($data['data'])) return $data;
  87. $emp_map = Employee::whereIn('id',array_column($data['data'],'finished_id'))
  88. ->pluck('emp_name','id')
  89. ->toArray();
  90. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  91. ->pluck('title','id')
  92. ->toArray();
  93. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  94. ->pluck('title','id')
  95. ->toArray();
  96. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  97. ->pluck('title','id')
  98. ->toArray();
  99. foreach ($data['data'] as $key => $value){
  100. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
  101. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  102. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  103. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  104. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  105. $data['data'][$key]['order_number'] = "ZJ" . $value['order_number'];
  106. }
  107. return $data;
  108. }
  109. public function zjDetail($data, $user){
  110. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  111. $order_number = str_replace("ZJ","",$data['order_number']);
  112. $result = ScrappCount::where('del_time',0)
  113. ->where('order_number', $order_number)
  114. ->get()->toArray();
  115. if(empty($result)) return [false, '质检单不存在或已被删除'];
  116. $first = $result[0] ?? [];
  117. $order['order_number'] = "ZJ" . $first['order_number'];
  118. $order['crt_time'] = $first['crt_time'] ? date("Y-m-d H:i:s", $first['crt_time']) : '';
  119. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  120. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  121. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  122. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  123. $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
  124. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  125. $order['dispatch_no'] = $dispatch['order_no'] ?? "";
  126. $scrapp_quantity = array_sum(array_column($result, 'scrapp_num')); // 不良品数量
  127. $detail = [
  128. 'product_no' => $first['product_no'],
  129. 'product_title' => $first['product_title'],
  130. 'product_size' => $first['product_size'],
  131. 'product_unit' => $first['product_unit'],
  132. 'technology_material' => $first['technology_material'],
  133. 'wood_name' => $first['wood_name'],
  134. 'production_quantity' => $dispatch['production_quantity'] ?? 0, // 生产数量
  135. 'dispatch_quantity' => $dispatch['dispatch_quantity'] ?? 0, // 派工数量
  136. 'quantity' => $first['quantity'], // 完工数量
  137. 'scrapp_quantity' => $scrapp_quantity, // 不良品数量
  138. 'zj_quantity' => $first['quantity'] + $scrapp_quantity, // 质检数量
  139. 'hg_quantity' => $first['quantity'], // 合格数量
  140. ];
  141. $order['detail'][] = $detail;
  142. return [true, $order];
  143. }
  144. //不良品
  145. public function blpList($data, $user){
  146. $model = ScrappCount::where('del_time',0)
  147. ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id')
  148. ->groupBy('order_number');
  149. if(! empty($data['order_number'])) {
  150. $order_number = str_replace("BLP","",$data['order_number']);
  151. if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  152. }
  153. $list = $this->limit($model,'',$data);
  154. $list = $this->fillBLPList($list);
  155. return [true,$list];
  156. }
  157. public function fillBLPList($data){
  158. if(empty($data['data'])) return $data;
  159. $emp_map = Employee::whereIn('id',array_column($data['data'],'finished_id'))
  160. ->pluck('emp_name','id')
  161. ->toArray();
  162. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  163. ->pluck('title','id')
  164. ->toArray();
  165. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  166. ->pluck('title','id')
  167. ->toArray();
  168. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  169. ->pluck('title','id')
  170. ->toArray();
  171. foreach ($data['data'] as $key => $value){
  172. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
  173. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  174. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  175. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  176. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  177. $data['data'][$key]['order_number'] = "BLP" . $value['order_number'];
  178. }
  179. return $data;
  180. }
  181. public function blpDetail($data, $user){
  182. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  183. $order_number = str_replace("BLP","",$data['order_number']);
  184. $result = ScrappCount::where('del_time',0)
  185. ->where('order_number', $order_number)
  186. ->get()->toArray();
  187. if(empty($result)) return [false, '不良品单不存在或已被删除'];
  188. $first = $result[0] ?? [];
  189. $order['order_number'] = "BLP" . $first['order_number'];
  190. $order['crt_time'] = $first['crt_time'] ? date("Y-m-d H:i:s", $first['crt_time']) : '';
  191. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  192. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  193. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  194. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  195. $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
  196. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  197. $order['dispatch_no'] = $dispatch['order_no'] ?? "";
  198. $scrapp = Scrapp::whereIn('id', array_unique(array_column($result,'scrapp_id')))
  199. ->pluck('title','id')
  200. ->toArray();
  201. $detail = [];
  202. foreach ($result as $value){
  203. $tmp = [
  204. 'product_no' => $first['product_no'],
  205. 'product_title' => $first['product_title'],
  206. 'product_size' => $first['product_size'],
  207. 'product_unit' => $first['product_unit'],
  208. 'technology_material' => $first['technology_material'],
  209. 'wood_name' => $first['wood_name'],
  210. 'scrapp_quantity' => $value['scrapp_num'], // 不良品数量
  211. 'scrapp_title' => $scrapp[$value['scrapp_id']], // 不良品原因
  212. ];
  213. $detail[] = $tmp;
  214. }
  215. $order['detail'] = $detail;
  216. return [true, $order];
  217. }
  218. }