ScrappService.php 17 KB

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