ScrappService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. ->orderBy('dispatch_sub_id','desc')
  79. ->groupBy('order_number');
  80. if(! empty($data['order_number'])) {
  81. $order_number = str_replace("ZJ","",$data['order_number']);
  82. if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$order_number.'%');
  83. }
  84. if(! empty($data['dispatch_no'])) {
  85. $dispatch_id = DispatchSub::where('del_time',0)
  86. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  87. ->select('id')
  88. ->get()->toArray();
  89. $dispatch_id = array_column($dispatch_id,'id');
  90. $model->whereIn('dispatch_sub_id', $dispatch_id);
  91. }
  92. if(! empty($data['sale_order_number'])) {
  93. $sales_id = SaleOrdersProduct::where('del_time',0)
  94. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  95. ->select('id')
  96. ->get()->toArray();
  97. $sales_id = array_column($sales_id,'id');
  98. $model->whereIn('sale_orders_product_id', $sales_id);
  99. }
  100. if(! empty($data['team_id'])) $model->where('team_id', $data['team_id']);
  101. if(! empty($data['team_man_id'])) {
  102. $team_id = EmployeeTeamPermission::where('employee_id',$data['team_man_id'])
  103. ->select('team_id')
  104. ->get()->toArray();
  105. $model->whereIn('team_id', array_unique(array_column($team_id,'team_id')));
  106. }
  107. if(! empty($data['finished_id'])) $model->where('finished_id', $data['finished_id']);
  108. if(! empty($data['equipment_id'])) $model->where('equipment_id', $data['equipment_id']);
  109. if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
  110. $list = $this->limit($model,'',$data);
  111. $list = $this->fillZjList($list);
  112. return [true,$list];
  113. }
  114. public function fillZjList($data){
  115. if(empty($data['data'])) return $data;
  116. $team_id = array_unique(array_column($data['data'],'team_id'));
  117. $team_maps = Team::whereIn('id',$team_id)
  118. ->pluck('title','id')
  119. ->toArray();
  120. $team_man = EmployeeTeamPermission::whereIn('team_id',$team_id)
  121. ->select('team_id','employee_id')
  122. ->get()->toArray();
  123. $emp_map = Employee::whereIn('id',array_merge_recursive(array_column($data['data'],'finished_id'),array_column($team_man,'employee_id')))
  124. ->pluck('emp_name','id')
  125. ->toArray();
  126. $team_man_maps = [];
  127. foreach ($team_man as $value){
  128. $t = $emp_map[$value['employee_id']] ?? "";
  129. if(empty($t)) continue;
  130. if(isset($team_man_maps[$value['team_id']])){
  131. $team_man_maps[$value['team_id']] .= ',' . $t;
  132. }else{
  133. $team_man_maps[$value['team_id']] = $t;
  134. }
  135. }
  136. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  137. ->pluck('title','id')
  138. ->toArray();
  139. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  140. ->pluck('title','id')
  141. ->toArray();
  142. $sales_number = SaleOrdersProduct::whereIn('id',array_unique(array_column($data['data'],'sale_orders_product_id')))
  143. ->pluck('out_order_no','id')
  144. ->toArray();
  145. $dispatch_no = DispatchSub::whereIn('id',array_column($data['data'], 'dispatch_sub_id'))
  146. ->pluck('dispatch_no','id')
  147. ->toArray();
  148. foreach ($data['data'] as $key => $value){
  149. // $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
  150. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  151. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  152. $data['data'][$key]['team_man'] = $team_man_maps[$value['team_id']] ?? '';
  153. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  154. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  155. $data['data'][$key]['order_number'] = "ZJ" . $value['order_number'];
  156. $data['data'][$key]['sale_order_number'] = $sales_number[$value['sale_orders_product_id']] ?? "";
  157. $dispatch_t = $dispatch_no[$value['dispatch_sub_id']] ?? "";
  158. $data['data'][$key]['dispatch_no'] = $dispatch_t;
  159. $data['data'][$key]['crt_time'] = date("Y-m-d",strtotime(substr($dispatch_t,0,8)));
  160. }
  161. return $data;
  162. }
  163. public function zjDetail($data, $user){
  164. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  165. $order_number = str_replace("ZJ","",$data['order_number']);
  166. $result = ScrappCount::where('del_time',0)
  167. ->where('order_number', $order_number)
  168. ->get()->toArray();
  169. if(empty($result)) return [false, '质检单不存在或已被删除'];
  170. $first = $result[0] ?? [];
  171. $order['order_number'] = "ZJ" . $first['order_number'];
  172. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  173. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  174. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  175. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  176. $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
  177. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  178. $order['dispatch_no'] = $dispatch['dispatch_no'] ?? "";
  179. $order['crt_time'] = date("Y-m-d",strtotime(substr($order['dispatch_no'],0,8)));
  180. $team_man = EmployeeTeamPermission::where('team_id',$first['team_id'])
  181. ->select('team_id','employee_id')
  182. ->get()->toArray();
  183. $emp_map = Employee::whereIn('id',array_column($team_man,'employee_id'))
  184. ->pluck('emp_name','id')
  185. ->toArray();
  186. $team_man_maps = "";
  187. foreach ($team_man as $value){
  188. $t = $emp_map[$value['employee_id']] ?? "";
  189. if(empty($t)) continue;
  190. if(! empty($team_man_maps)){
  191. $team_man_maps .= ',' . $t;
  192. }else{
  193. $team_man_maps = $t;
  194. }
  195. }
  196. $order['team_man'] = $team_man_maps;
  197. $scrapp_quantity = array_sum(array_column($result, 'scrapp_num')); // 不良品数量
  198. $detail = [
  199. 'product_no' => $first['product_no'],
  200. 'product_title' => $first['product_title'],
  201. 'product_size' => $first['product_size'],
  202. 'product_unit' => $first['product_unit'],
  203. 'technology_name' => $first['technology_name'],
  204. 'production_quantity' => $dispatch['production_quantity'] ?? 0, // 生产数量
  205. 'dispatch_quantity' => $dispatch['dispatch_quantity'] ?? 0, // 派工数量
  206. 'quantity' => $first['quantity'], // 完工数量
  207. 'scrapp_quantity' => $scrapp_quantity, // 不良品数量
  208. 'zj_quantity' => $first['quantity'] + $scrapp_quantity, // 质检数量
  209. 'hg_quantity' => $first['quantity'], // 合格数量
  210. ];
  211. $order['detail'][] = $detail;
  212. return [true, $order];
  213. }
  214. //不良品
  215. public function blpList($data, $user){
  216. $model = ScrappCount::where('del_time',0)
  217. ->select('id','dispatch_sub_id','crt_time','team_id','finished_id','equipment_id','order_number','process_id','sale_orders_product_id')
  218. ->orderBy('dispatch_sub_id','desc')
  219. ->groupBy('order_number');
  220. if(! empty($data['order_number'])) {
  221. $order_number = str_replace("BLP","",$data['order_number']);
  222. if(! empty($order_number)) $model->where('order_number', 'LIKE', '%'.$order_number.'%');
  223. }
  224. if(! empty($data['dispatch_no'])) {
  225. $dispatch_id = DispatchSub::where('del_time',0)
  226. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  227. ->select('id')
  228. ->get()->toArray();
  229. $dispatch_id = array_column($dispatch_id,'id');
  230. $model->whereIn('dispatch_sub_id', $dispatch_id);
  231. }
  232. if(! empty($data['sale_order_number'])) {
  233. $sales_id = SaleOrdersProduct::where('del_time',0)
  234. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  235. ->select('id')
  236. ->get()->toArray();
  237. $sales_id = array_column($sales_id,'id');
  238. $model->whereIn('sale_orders_product_id', $sales_id);
  239. }
  240. if(! empty($data['team_id'])) $model->where('team_id', $data['team_id']);
  241. if(! empty($data['finished_id'])) $model->where('finished_id', $data['finished_id']);
  242. if(! empty($data['team_man_id'])) {
  243. $team_id = EmployeeTeamPermission::where('employee_id',$data['team_man_id'])
  244. ->select('team_id')
  245. ->get()->toArray();
  246. $model->whereIn('team_id', array_unique(array_column($team_id,'team_id')));
  247. }
  248. if(! empty($data['equipment_id'])) $model->where('equipment_id', $data['equipment_id']);
  249. if(! empty($data['process_id'])) $model->where('process_id', $data['process_id']);
  250. $list = $this->limit($model,'',$data);
  251. $list = $this->fillBLPList($list);
  252. return [true,$list];
  253. }
  254. public function fillBLPList($data){
  255. if(empty($data['data'])) return $data;
  256. $team_id = array_unique(array_column($data['data'],'team_id'));
  257. $team_maps = Team::whereIn('id',$team_id)
  258. ->pluck('title','id')
  259. ->toArray();
  260. $team_man = EmployeeTeamPermission::whereIn('team_id',$team_id)
  261. ->select('team_id','employee_id')
  262. ->get()->toArray();
  263. $emp_map = Employee::whereIn('id',array_merge_recursive(array_column($data['data'],'finished_id'),array_column($team_man,'employee_id')))
  264. ->pluck('emp_name','id')
  265. ->toArray();
  266. $team_man_maps = [];
  267. foreach ($team_man as $value){
  268. $t = $emp_map[$value['employee_id']] ?? "";
  269. if(empty($t)) continue;
  270. if(isset($team_man_maps[$value['team_id']])){
  271. $team_man_maps[$value['team_id']] .= ',' . $t;
  272. }else{
  273. $team_man_maps[$value['team_id']] = $t;
  274. }
  275. }
  276. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  277. ->pluck('title','id')
  278. ->toArray();
  279. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  280. ->pluck('title','id')
  281. ->toArray();
  282. $sales_number = SaleOrdersProduct::whereIn('id',array_unique(array_column($data['data'],'sale_orders_product_id')))
  283. ->pluck('out_order_no','id')
  284. ->toArray();
  285. $dispatch_no = DispatchSub::whereIn('id',array_column($data['data'], 'dispatch_sub_id'))
  286. ->pluck('dispatch_no','id')
  287. ->toArray();
  288. foreach ($data['data'] as $key => $value){
  289. // $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : '';
  290. $data['data'][$key]['finished_title'] = $emp_map[$value['finished_id']] ?? '';
  291. $data['data'][$key]['team_title'] = $team_maps[$value['team_id']] ?? '';
  292. $data['data'][$key]['team_man'] = $team_man_maps[$value['team_id']] ?? '';
  293. $data['data'][$key]['equipment_title'] = $equipment_map[$value['equipment_id']] ?? '';
  294. $data['data'][$key]['process_title'] = $process_map[$value['process_id']] ?? '';
  295. $data['data'][$key]['order_number'] = "BLP" . $value['order_number'];
  296. $data['data'][$key]['sale_order_number'] = $sales_number[$value['sale_orders_product_id']] ?? "";
  297. $dispatch_t = $dispatch_no[$value['dispatch_sub_id']] ?? "";
  298. $data['data'][$key]['dispatch_no'] = $dispatch_t;
  299. $data['data'][$key]['crt_time'] = date("Y-m-d",strtotime(substr($dispatch_t,0,8)));
  300. }
  301. return $data;
  302. }
  303. public function blpDetail($data, $user){
  304. if(empty($data['order_number'])) return [false, '请选择质检单数据'];
  305. $order_number = str_replace("BLP","",$data['order_number']);
  306. $result = ScrappCount::where('del_time',0)
  307. ->where('order_number', $order_number)
  308. ->get()->toArray();
  309. if(empty($result)) return [false, '不良品单不存在或已被删除'];
  310. $first = $result[0] ?? [];
  311. $order['order_number'] = "BLP" . $first['order_number'];
  312. $order['process_title'] = Process::where('id',$first['process_id'])->value("title");
  313. $order['team_title'] = Team::where('id',$first['team_id'])->value("title");
  314. $order['finished_title'] = Employee::where('id',$first['finished_id'])->value("emp_name");
  315. $order['equipment_title'] = Equipment::where('id',$first['equipment_id'])->value("title");
  316. $dispatch = DispatchSub::where('id',$first['dispatch_sub_id'])->first();
  317. $dispatch = empty($dispatch) ? [] : $dispatch->toArray();
  318. $order['dispatch_no'] = $dispatch['dispatch_no'] ?? "";
  319. $order['crt_time'] = date("Y-m-d",strtotime(substr($order['dispatch_no'],0,8)));
  320. $team_man = EmployeeTeamPermission::where('team_id',$first['team_id'])
  321. ->select('team_id','employee_id')
  322. ->get()->toArray();
  323. $emp_map = Employee::whereIn('id',array_column($team_man,'employee_id'))
  324. ->pluck('emp_name','id')
  325. ->toArray();
  326. $team_man_maps = "";
  327. foreach ($team_man as $value){
  328. $t = $emp_map[$value['employee_id']] ?? "";
  329. if(empty($t)) continue;
  330. if(! empty($team_man_maps)){
  331. $team_man_maps .= ',' . $t;
  332. }else{
  333. $team_man_maps = $t;
  334. }
  335. }
  336. $order['team_man'] = $team_man_maps;
  337. $scrapp = Scrapp::whereIn('id', array_unique(array_column($result,'scrapp_id')))
  338. ->pluck('title','id')
  339. ->toArray();
  340. $detail = [];
  341. foreach ($result as $value){
  342. $tmp = [
  343. 'product_no' => $first['product_no'],
  344. 'product_title' => $first['product_title'],
  345. 'product_size' => $first['product_size'],
  346. 'product_unit' => $first['product_unit'],
  347. 'technology_name' => $first['technology_name'],
  348. 'scrapp_quantity' => $value['scrapp_num'], // 不良品数量
  349. 'scrapp_title' => $scrapp[$value['scrapp_id']], // 不良品原因
  350. ];
  351. $detail[] = $tmp;
  352. }
  353. $order['detail'] = $detail;
  354. return [true, $order];
  355. }
  356. }