ScrappService.php 17 KB

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