ScrappService.php 19 KB

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