ScrappService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DispatchSub;
  4. use App\Model\Employee;
  5. use App\Model\EmployeeTeamPermission;
  6. use App\Model\Equipment;
  7. use App\Model\Process;
  8. use App\Model\SaleOrdersProduct;
  9. use App\Model\Scrapp;
  10. use App\Model\ScrappCount;
  11. use App\Model\Team;
  12. /**
  13. * 报废原因
  14. * @package App\Models
  15. */
  16. class ScrappService extends Service
  17. {
  18. public function scrappEdit($data){
  19. list($status,$msg) = $this->scrappRule($data,false);
  20. if(!$status) return [$status,$msg];
  21. $update = $msg['data'][0];
  22. Scrapp::where('id',$data['id'])->update($update);
  23. return [true,'保存成功!'];
  24. }
  25. public function scrappAdd($data){
  26. list($status,$msg) = $this->scrappRule($data);
  27. if(!$status) return [$status,$msg];
  28. Scrapp::insert($msg['data']);
  29. return [true,'保存成功!'];
  30. }
  31. public function scrappDel($data){
  32. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  33. Scrapp::whereIn('id',$data['id'])->update([
  34. 'del_time' => time()
  35. ]);
  36. return [true,'删除成功'];
  37. }
  38. public function scrappList($data){
  39. $model = Scrapp::where('del_time',0)
  40. ->select('*');
  41. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  42. $list = $this->limit($model,'',$data);
  43. return [200,$list];
  44. }
  45. public function scrappRule($data,$is_add = true){
  46. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  47. $title = array_column($data['data'],'title');
  48. $title = array_map(function($val) {
  49. return $val !== null ? $val : 0;
  50. }, $title);
  51. $title_count = array_count_values($title);
  52. foreach ($title as $value){
  53. if(empty($value)) return [false,'名称不能为空!'];
  54. if($title_count[$value] > 1) return [false,'名称不能重复'];
  55. }
  56. foreach ($data['data'] as $key => $value){
  57. $data['data'][$key]['upd_time'] = time();
  58. if($is_add){
  59. $bool = Scrapp::whereRaw("title = '{$value['title']}'")
  60. ->where('del_time',0)
  61. ->exists();
  62. $data['data'][$key]['crt_time'] = time();
  63. }else{
  64. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  65. $bool = Scrapp::whereRaw("title = '{$value['title']}'")
  66. ->where('id','<>',$data['id'])
  67. ->where('del_time',0)
  68. ->exists();
  69. }
  70. if($bool) return [false,'名称不能重复'];
  71. }
  72. return [true,$data];
  73. }
  74. //质检单
  75. public function zjList($data, $user){
  76. $model = ScrappCount::where('del_time',0)
  77. ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id','sale_orders_product_id')
  78. ->groupBy('order_number');
  79. if(! empty($data['order_number'])) {
  80. $order_number = str_replace("ZJ","",$data['order_number']);
  81. if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$order_number.'%');
  82. }
  83. if(! empty($data['dispatch_no'])) {
  84. $dispatch_id = DispatchSub::where('del_time',0)
  85. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  86. ->select('id')
  87. ->get()->toArray();
  88. $dispatch_id = array_column($dispatch_id,'id');
  89. $model->whereIn('dispatch_sub_id', $dispatch_id);
  90. }
  91. if(! empty($data['sale_order_number'])) {
  92. $sales_id = SaleOrdersProduct::where('del_time',0)
  93. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  94. ->select('id')
  95. ->get()->toArray();
  96. $sales_id = array_column($sales_id,'id');
  97. $model->whereIn('sale_orders_product_id', $sales_id);
  98. }
  99. if(! empty($data['team_id'])) $model->where('team_id', $data['team_id']);
  100. if(! empty($data['team_man_id'])) {
  101. $team_id = EmployeeTeamPermission::where('employee_id',$data['team_man_id'])
  102. ->select('team_id')
  103. ->get()->toArray();
  104. $model->whereIn('team_id', array_unique(array_column($team_id,'team_id')));
  105. }
  106. if(! empty($data['finished_id'])) $model->where('finished_id', $data['finished_id']);
  107. if(! empty($data['equipment_id'])) $model->where('equipment_id', $data['equipment_id']);
  108. if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
  109. $list = $this->limit($model,'',$data);
  110. $list = $this->fillZjList($list);
  111. return [true,$list];
  112. }
  113. public function fillZjList($data){
  114. if(empty($data['data'])) return $data;
  115. $team_id = array_unique(array_column($data['data'],'team_id'));
  116. $team_maps = Team::whereIn('id',$team_id)
  117. ->pluck('title','id')
  118. ->toArray();
  119. $team_man = EmployeeTeamPermission::whereIn('team_id',$team_id)
  120. ->select('team_id','employee_id')
  121. ->get()->toArray();
  122. $emp_map = Employee::whereIn('id',array_merge_recursive(array_column($data['data'],'finished_id'),array_column($team_man,'employee_id')))
  123. ->pluck('emp_name','id')
  124. ->toArray();
  125. $team_man_maps = [];
  126. foreach ($team_man as $value){
  127. $t = $emp_map[$value['employee_id']] ?? "";
  128. if(empty($t)) continue;
  129. if(isset($team_man_maps[$value['team_id']])){
  130. $team_man_maps[$value['team_id']] .= ',' . $t;
  131. }else{
  132. $team_man_maps[$value['team_id']] = $t;
  133. }
  134. }
  135. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  136. ->pluck('title','id')
  137. ->toArray();
  138. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  139. ->pluck('title','id')
  140. ->toArray();
  141. $sales_number = SaleOrdersProduct::whereIn('id',array_unique(array_column($data['data'],'sale_orders_product_id')))
  142. ->pluck('out_order_no','id')
  143. ->toArray();
  144. $dispatch_no = DispatchSub::whereIn('id',array_column($data['data'], 'dispatch_sub_id'))
  145. ->pluck('dispatch_no','id')
  146. ->toArray();
  147. foreach ($data['data'] as $key => $value){
  148. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
  149. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  150. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  151. $data['data'][$key]['team_man'] = $team_man_maps[$value['team_id']] ?? '';
  152. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  153. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  154. $data['data'][$key]['order_number'] = "ZJ" . $value['order_number'];
  155. $data['data'][$key]['sale_order_number'] = $sales_number[$value['sale_orders_product_id']] ?? "";
  156. $data['data'][$key]['dispatch_no'] = $dispatch_no[$value['dispatch_sub_id']] ?? "";
  157. }
  158. return $data;
  159. }
  160. public function zjDetail($data, $user){
  161. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  162. $order_number = str_replace("ZJ","",$data['order_number']);
  163. $result = ScrappCount::where('del_time',0)
  164. ->where('order_number', $order_number)
  165. ->get()->toArray();
  166. if(empty($result)) return [false, '质检单不存在或已被删除'];
  167. $first = $result[0] ?? [];
  168. $order['order_number'] = "ZJ" . $first['order_number'];
  169. $order['crt_time'] = $first['crt_time'] ? date("Y-m-d H:i:s", $first['crt_time']) : '';
  170. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  171. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  172. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  173. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  174. $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
  175. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  176. $order['dispatch_no'] = $dispatch['dispatch_no'] ?? "";
  177. $team_man = EmployeeTeamPermission::where('team_id',$first['team_id'])
  178. ->select('team_id','employee_id')
  179. ->get()->toArray();
  180. $emp_map = Employee::whereIn('id',array_column($team_man,'employee_id'))
  181. ->pluck('emp_name','id')
  182. ->toArray();
  183. $team_man_maps = "";
  184. foreach ($team_man as $value){
  185. $t = $emp_map[$value['employee_id']] ?? "";
  186. if(empty($t)) continue;
  187. if(! empty($team_man_maps)){
  188. $team_man_maps .= ',' . $t;
  189. }else{
  190. $team_man_maps = $t;
  191. }
  192. }
  193. $order['team_man'] = $team_man_maps;
  194. $scrapp_quantity = array_sum(array_column($result, 'scrapp_num')); // 不良品数量
  195. $detail = [
  196. 'product_no' => $first['product_no'],
  197. 'product_title' => $first['product_title'],
  198. 'product_size' => $first['product_size'],
  199. 'product_unit' => $first['product_unit'],
  200. 'technology_name' => $first['technology_name'],
  201. 'production_quantity' => $dispatch['production_quantity'] ?? 0, // 生产数量
  202. 'dispatch_quantity' => $dispatch['dispatch_quantity'] ?? 0, // 派工数量
  203. 'quantity' => $first['quantity'], // 完工数量
  204. 'scrapp_quantity' => $scrapp_quantity, // 不良品数量
  205. 'zj_quantity' => $first['quantity'] + $scrapp_quantity, // 质检数量
  206. 'hg_quantity' => $first['quantity'], // 合格数量
  207. ];
  208. $order['detail'][] = $detail;
  209. return [true, $order];
  210. }
  211. //不良品
  212. public function blpList($data, $user){
  213. $model = ScrappCount::where('del_time',0)
  214. ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id','sale_orders_product_id')
  215. ->groupBy('order_number');
  216. if(! empty($data['order_number'])) {
  217. $order_number = str_replace("BLP","",$data['order_number']);
  218. if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$order_number.'%');
  219. }
  220. if(! empty($data['dispatch_no'])) {
  221. $dispatch_id = DispatchSub::where('del_time',0)
  222. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  223. ->select('id')
  224. ->get()->toArray();
  225. $dispatch_id = array_column($dispatch_id,'id');
  226. $model->whereIn('dispatch_sub_id', $dispatch_id);
  227. }
  228. if(! empty($data['sale_order_number'])) {
  229. $sales_id = SaleOrdersProduct::where('del_time',0)
  230. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  231. ->select('id')
  232. ->get()->toArray();
  233. $sales_id = array_column($sales_id,'id');
  234. $model->whereIn('sale_orders_product_id', $sales_id);
  235. }
  236. if(! empty($data['team_id'])) $model->where('team_id', $data['team_id']);
  237. if(! empty($data['finished_id'])) $model->where('finished_id', $data['finished_id']);
  238. if(! empty($data['team_man_id'])) {
  239. $team_id = EmployeeTeamPermission::where('employee_id',$data['team_man_id'])
  240. ->select('team_id')
  241. ->get()->toArray();
  242. $model->whereIn('team_id', array_unique(array_column($team_id,'team_id')));
  243. }
  244. if(! empty($data['equipment_id'])) $model->where('equipment_id', $data['equipment_id']);
  245. if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
  246. $list = $this->limit($model,'',$data);
  247. $list = $this->fillBLPList($list);
  248. return [true,$list];
  249. }
  250. public function fillBLPList($data){
  251. if(empty($data['data'])) return $data;
  252. $team_id = array_unique(array_column($data['data'],'team_id'));
  253. $team_maps = Team::whereIn('id',$team_id)
  254. ->pluck('title','id')
  255. ->toArray();
  256. $team_man = EmployeeTeamPermission::whereIn('team_id',$team_id)
  257. ->select('team_id','employee_id')
  258. ->get()->toArray();
  259. $emp_map = Employee::whereIn('id',array_merge_recursive(array_column($data['data'],'finished_id'),array_column($team_man,'employee_id')))
  260. ->pluck('emp_name','id')
  261. ->toArray();
  262. $team_man_maps = [];
  263. foreach ($team_man as $value){
  264. $t = $emp_map[$value['employee_id']] ?? "";
  265. if(empty($t)) continue;
  266. if(isset($team_man_maps[$value['team_id']])){
  267. $team_man_maps[$value['team_id']] .= ',' . $t;
  268. }else{
  269. $team_man_maps[$value['team_id']] = $t;
  270. }
  271. }
  272. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  273. ->pluck('title','id')
  274. ->toArray();
  275. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  276. ->pluck('title','id')
  277. ->toArray();
  278. $sales_number = SaleOrdersProduct::whereIn('id',array_unique(array_column($data['data'],'sale_orders_product_id')))
  279. ->pluck('out_order_no','id')
  280. ->toArray();
  281. $dispatch_no = DispatchSub::whereIn('id',array_column($data['data'], 'dispatch_sub_id'))
  282. ->pluck('dispatch_no','id')
  283. ->toArray();
  284. foreach ($data['data'] as $key => $value){
  285. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
  286. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  287. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  288. $data['data'][$key]['team_man'] = $team_man_maps[$value['team_id']] ?? '';
  289. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  290. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  291. $data['data'][$key]['order_number'] = "BLP" . $value['order_number'];
  292. $data['data'][$key]['sale_order_number'] = $sales_number[$value['sale_orders_product_id']] ?? "";
  293. $data['data'][$key]['dispatch_no'] = $dispatch_no[$value['dispatch_sub_id']] ?? "";
  294. }
  295. return $data;
  296. }
  297. public function blpDetail($data, $user){
  298. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  299. $order_number = str_replace("BLP","",$data['order_number']);
  300. $result = ScrappCount::where('del_time',0)
  301. ->where('order_number', $order_number)
  302. ->get()->toArray();
  303. if(empty($result)) return [false, '不良品单不存在或已被删除'];
  304. $first = $result[0] ?? [];
  305. $order['order_number'] = "BLP" . $first['order_number'];
  306. $order['crt_time'] = $first['crt_time'] ? date("Y-m-d H:i:s", $first['crt_time']) : '';
  307. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  308. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  309. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  310. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  311. $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
  312. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  313. $order['dispatch_no'] = $dispatch['dispatch_no'] ?? "";
  314. $team_man = EmployeeTeamPermission::where('team_id',$first['team_id'])
  315. ->select('team_id','employee_id')
  316. ->get()->toArray();
  317. $emp_map = Employee::whereIn('id',array_column($team_man,'employee_id'))
  318. ->pluck('emp_name','id')
  319. ->toArray();
  320. $team_man_maps = "";
  321. foreach ($team_man as $value){
  322. $t = $emp_map[$value['employee_id']] ?? "";
  323. if(empty($t)) continue;
  324. if(! empty($team_man_maps)){
  325. $team_man_maps .= ',' . $t;
  326. }else{
  327. $team_man_maps = $t;
  328. }
  329. }
  330. $order['team_man'] = $team_man_maps;
  331. $scrapp = Scrapp::whereIn('id', array_unique(array_column($result,'scrapp_id')))
  332. ->pluck('title','id')
  333. ->toArray();
  334. $detail = [];
  335. foreach ($result as $value){
  336. $tmp = [
  337. 'product_no' => $first['product_no'],
  338. 'product_title' => $first['product_title'],
  339. 'product_size' => $first['product_size'],
  340. 'product_unit' => $first['product_unit'],
  341. 'technology_name' => $first['technology_name'],
  342. 'scrapp_quantity' => $value['scrapp_num'], // 不良品数量
  343. 'scrapp_title' => $scrapp[$value['scrapp_id']], // 不良品原因
  344. ];
  345. $detail[] = $tmp;
  346. }
  347. $order['detail'] = $detail;
  348. return [true, $order];
  349. }
  350. }