ScrappService.php 18 KB

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