DispatchService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Dispatch;
  4. use App\Model\DispatchEmpSub;
  5. use App\Model\DispatchSub;
  6. use App\Model\Employee;
  7. use App\Model\EmployeeTeamPermission;
  8. use App\Model\Equipment;
  9. use App\Model\FinishedOrderSub;
  10. use App\Model\Orders;
  11. use App\Model\OrdersProduct;
  12. use App\Model\OrdersProductProcess;
  13. use App\Model\Process;
  14. use App\Model\Team;
  15. use Illuminate\Support\Facades\DB;
  16. class DispatchService extends Service
  17. {
  18. public function edit($data){}
  19. public function setOrderNO(){
  20. $str = date('Ymd',time());
  21. $order_number = Dispatch::where('dispatch_no','Like','%'. $str . '%')
  22. ->max('dispatch_no');
  23. if(empty($order_number)){
  24. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  25. $number = $str . $number;
  26. }else{
  27. $tmp = substr($order_number, -3);
  28. $tmp = $tmp + 1;
  29. //超过99999
  30. if(strlen($tmp) > 3) return '';
  31. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  32. $number = $str . $number;
  33. }
  34. return $number;
  35. }
  36. public function add($data,$user){
  37. //数据校验以及填充
  38. list($status,$msg) = $this->orderRule($data);
  39. if(! $status) return [$status,$msg];
  40. try{
  41. DB::beginTransaction();
  42. if($data['is_split']){
  43. foreach ($msg as $value){
  44. list($s,$m) = $this->insertDispatch([$value],$data,$user);
  45. if(! $s) return [false,$m];
  46. }
  47. }else{
  48. list($s,$m) = $this->insertDispatch($msg,$data,$user);
  49. if(! $s) return [false, $m];
  50. }
  51. //反写已派工数量
  52. $this->writeDispatchQuantity(array_column($msg,'order_product_id'));
  53. DB::commit();
  54. }catch (\Exception $e){
  55. DB::rollBack();
  56. return [false,$e->getLine().':'.$e->getMessage()];
  57. }
  58. return [true,''];
  59. }
  60. public function makeData($equipment_id, $team_id,$employee_id,$message){
  61. $arr = [];
  62. if(! empty($equipment_id)){
  63. foreach ($equipment_id as $v_e){
  64. if(! empty($team_id)){
  65. foreach ($team_id as $t){
  66. if(! empty($employee_id)){
  67. foreach ($employee_id as $e){
  68. $arr[] = [
  69. 'equipment_id' => $v_e,
  70. 'team_id' => $t,
  71. 'employee_id' => $e,
  72. 'order_product_id' => $message['order_product_id'],
  73. 'dispatch_no' => $message['dispatch_no'],
  74. ];
  75. }
  76. }else{
  77. $arr[] = [
  78. 'equipment_id' => $v_e,
  79. 'team_id' => $t,
  80. 'order_product_id' => $message['order_product_id'],
  81. 'dispatch_no' => $message['dispatch_no'],
  82. ];
  83. }
  84. }
  85. }elseif(! empty($employee_id)){
  86. foreach ($employee_id as $e){
  87. $arr[] = [
  88. 'equipment_id' => $v_e,
  89. 'employee_id' => $e,
  90. 'order_product_id' => $message['order_product_id'],
  91. 'dispatch_no' => $message['dispatch_no'],
  92. ];
  93. }
  94. }else{
  95. $arr[] = [
  96. 'equipment_id' => $v_e,
  97. 'order_product_id' => $message['order_product_id'],
  98. 'dispatch_no' => $message['dispatch_no'],
  99. ];
  100. }
  101. }
  102. }elseif (! empty($team_id)){
  103. foreach ($team_id as $t){
  104. if(! empty($employee_id)){
  105. foreach ($employee_id as $e){
  106. $arr[] = [
  107. 'team_id' => $t,
  108. 'employee_id' => $e,
  109. 'order_product_id' => $message['order_product_id'],
  110. 'dispatch_no' => $message['dispatch_no'],
  111. ];
  112. }
  113. }else{
  114. $arr[] = [
  115. 'team_id' => $t,
  116. 'order_product_id' => $message['order_product_id'],
  117. 'dispatch_no' => $message['dispatch_no'],
  118. ];
  119. }
  120. }
  121. }elseif(! empty($employee_id)){
  122. foreach ($employee_id as $e){
  123. $arr[] = [
  124. 'employee_id' => $e,
  125. 'order_product_id' => $message['order_product_id'],
  126. 'dispatch_no' => $message['dispatch_no'],
  127. ];
  128. }
  129. }
  130. return $arr;
  131. }
  132. public function insertDispatch($msg, $data, $user){
  133. //生产数据的源数据
  134. $result = $msg;
  135. $time = time();
  136. $dispatch_no = $this->setOrderNO();
  137. if(! $dispatch_no) return [false,'单号生成失败!'];
  138. //主表
  139. Dispatch::insert(['dispatch_no' => $dispatch_no,'crt_time' => $time]);
  140. $time_tmp = date("Ymd", $data['out_order_no_time'][0]);
  141. $process_model = new OrdersProductProcess(['channel' => $time_tmp]);
  142. $equipment_id = is_array($data['equipment_id']) ? $data['equipment_id'] : [$data['equipment_id']];
  143. $equipment_id = array_filter($equipment_id);
  144. $team_id = is_array($data['team_id']) ? $data['team_id'] : [$data['team_id']];
  145. $team_id = array_filter($team_id);
  146. $insert_emp_sub = [];
  147. foreach ($result as $key => $value){
  148. $result[$key]['dispatch_no'] = $dispatch_no;
  149. $result[$key]['process_id'] = $data['process_id'];
  150. $result[$key]['dispatch_time_start'] = $data['dispatch_time'][0];
  151. $result[$key]['dispatch_time_end'] = $data['dispatch_time'][1];
  152. $result[$key]['dispatch_quantity'] = $value['quantity'];
  153. $result[$key]['crt_time'] = $time;
  154. $result[$key]['crt_id'] = $user['id'];
  155. $tmp = $this->makeData($equipment_id,$team_id,$data['employee_id'],$result[$key]);
  156. $insert_emp_sub = array_merge_recursive($insert_emp_sub,$tmp);
  157. $process_model->where('order_product_id',$value['order_product_id'])
  158. ->where('process_id',$data['process_id'])
  159. ->where('dispatch_no','')
  160. ->take($value['quantity'])
  161. ->update([
  162. 'dispatch_no' => $dispatch_no,
  163. 'status' => 1
  164. ]);
  165. unset($result[$key]['quantity']);
  166. }
  167. DispatchSub::insert($result);
  168. if(! empty($insert_emp_sub)) DispatchEmpSub::insert($insert_emp_sub);
  169. return [true,''];
  170. }
  171. public function del($data){
  172. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  173. return [true,'删除成功'];
  174. }
  175. public function orderDetail($data){
  176. return [200,''];
  177. }
  178. public function is_same_month($timestamp1,$timestamp2){
  179. // 格式化时间戳为年份和月份
  180. $year1 = date('Y', $timestamp1);
  181. $month1 = date('m', $timestamp1);
  182. $year2 = date('Y', $timestamp2);
  183. $month2 = date('m', $timestamp2);
  184. if ($year1 === $year2 && $month1 === $month2) {
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190. public function orderList($data){
  191. list($status,$msg) = $this->orderListRule($data);
  192. if(! $status) return [false, $msg];
  193. $model = OrdersProduct::where('del_time',0)
  194. ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','production_time','production_no','status','crt_id','dispatch_complete_quantity')
  195. ->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]])
  196. ->whereIn('id',$msg)
  197. ->orderBy('id','desc');
  198. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  199. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  200. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  201. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  202. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  203. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  204. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  205. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  206. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  207. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  208. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  209. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  210. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  211. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  212. if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]);
  213. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  214. if(isset($data['status'])) $model->where('status',$data['status']);
  215. if(isset($data['is_create'])) {
  216. if($data['is_create']){
  217. $model->whereColumn('dispatch_complete_quantity', '>=', 'production_quantity');
  218. }else{
  219. $model->whereColumn('production_quantity', '>', 'dispatch_complete_quantity');
  220. }
  221. }
  222. $list = $this->limit($model,'',$data);
  223. $list = $this->fillData($list);
  224. return [true,$list];
  225. }
  226. public function orderListRule($data){
  227. if(empty($data['process_id'])) return [false, '工序必须选择!'];
  228. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期必须选择'];
  229. $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
  230. if(! $bool) return [false,'制单日期必须同月!'];
  231. $time = date("Ymd",$data['out_order_no_time'][0]);
  232. $process_model = new OrdersProductProcess(['channel' => $time]);
  233. if(! $process_model->is_table_isset()) return [true, []];//不存在process子表 返回空数据
  234. $process_array = $process_model->where('del_time',0)
  235. ->where('process_id',$data['process_id'])
  236. ->distinct()
  237. ->select('order_product_id')
  238. ->get()->toArray();
  239. $order_product_id = array_column($process_array,'order_product_id');
  240. if(empty($order_product_id)) return [true,[]];//process子表无数据 返回空数据
  241. return [true, $order_product_id];
  242. }
  243. public function orderRule($data){
  244. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  245. if($this->isEmpty($data,'quantity')) return [false,'请填写数量!'];
  246. if(in_array(false, $data['quantity'], true) || in_array(0, $data['quantity'], true) || in_array('', $data['quantity'], true))return [false,'数量不能为空!'];
  247. if(empty($data['dispatch_time'][0]) || empty($data['dispatch_time'][1])) return [false,'计划生产时间不能为空!'];
  248. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单时间不能为空!'];
  249. if($this->isEmpty($data,'process_id')) return [false,'工序不能为空!'];
  250. if(! isset($data['is_split'])) return [false,'是否拆分标识不能为空!'];
  251. $result = OrdersProduct::whereIn('id',$data['id'])
  252. ->select('id as order_product_id','sale_orders_product_id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','production_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','sale_orders_product_id','out_order_no_time','price','customer_name','out_order_no','customer_no')
  253. ->orderBy('id','desc')
  254. ->get()->toArray();
  255. $result_map = array_column($result,null,'order_product_id');
  256. //已派工数据
  257. $map = $this->getDispatchQuantity($data['id']);
  258. $total_map = $return = [];
  259. foreach ($data['id'] as $key => $value){
  260. if(isset($total_map[$value])){
  261. $total_map[$value] += $data['quantity'][$key];
  262. }else{
  263. $total_map[$value] = $data['quantity'][$key];
  264. }
  265. $tmp = $result_map[$value];
  266. $tmp['quantity'] = $data['quantity'][$key];
  267. $return[] = $tmp;
  268. }
  269. foreach ($result as $key => $value){
  270. //数据校验
  271. $quantity_tmp = $total_map[$value['order_product_id']];
  272. if(isset($map[$value['order_product_id']])){
  273. if($map[$value['order_product_id']] + $quantity_tmp > $value['production_quantity']) return [false,'派单数量不能大于生产订单数量'];
  274. }else{
  275. if($quantity_tmp > $value['production_quantity']) return [false,'派单数量不能大于生产订单数量'];
  276. }
  277. }
  278. return [true, $return];
  279. }
  280. public function fillData($data){
  281. if(empty($data['data'])) return $data;
  282. $map = $this->getDispatchQuantity(array_column($data['data'],'id'));
  283. $emp_map = Employee::whereIn('id',array_column($data['data'],'crt_id'))
  284. ->pluck('emp_name','id')
  285. ->toArray();
  286. foreach ($data['data'] as $key => $value){
  287. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  288. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  289. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  290. $data['data'][$key]['dispatch_quantity'] = $map[$value['id']] ?? 0;
  291. $data['data'][$key]['not_dispatch_quantity'] = $value['production_quantity'] - ($map[$value['id']] ?? 0);
  292. $data['data'][$key]['order_product_man'] = $emp_map[$value['crt_id']] ?? '';
  293. if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
  294. $data['data'][$key]['is_create'] = 1;
  295. }else{
  296. $data['data'][$key]['is_create'] = 0;
  297. }
  298. }
  299. $data['production_quantity'] = array_sum(array_column($data['data'], 'production_quantity'));
  300. $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
  301. $data['not_dispatch_quantity'] = array_sum(array_column($data['data'], 'not_dispatch_quantity'));
  302. return $data;
  303. }
  304. //返回已派工数量
  305. public function getDispatchQuantity($order_product_id = []){
  306. if(empty($order_product_id)) return [];
  307. $result = DispatchSub::where('del_time',0)
  308. ->whereIn("order_product_id",$order_product_id)
  309. ->select(DB::raw("sum(dispatch_quantity) as dispatch_quantity"),'order_product_id')
  310. ->groupby('order_product_id')
  311. ->pluck('dispatch_quantity','order_product_id')
  312. ->toArray();
  313. return $result;
  314. }
  315. public function dispatchOrderList($data){
  316. $model = DispatchSub::where('del_time',0)
  317. ->select('id','order_no','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','production_quantity','dispatch_no','status','crt_id','process_id','dispatch_time_start','dispatch_time_end','crt_time','finished_num','waste_num','customer_name','order_product_id','out_order_no')
  318. ->orderBy('id','desc');
  319. if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
  320. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  321. if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  322. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  323. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  324. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  325. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  326. $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
  327. $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
  328. }
  329. if(! empty($data['team_id'])) {
  330. $res = DispatchEmpSub::where('del_time',0)
  331. ->where('team_id',$data['team_id'])
  332. ->distinct()
  333. ->select('dispatch_no')
  334. ->get()->toArray();
  335. $model->whereIn('dispatch_no',array_column($res,'dispatch_no'));
  336. }
  337. if(! empty($data['equipment_id'])) {
  338. $res = DispatchEmpSub::where('del_time',0)
  339. ->where('equipment_id',$data['equipment_id'])
  340. ->distinct()
  341. ->select('dispatch_no')
  342. ->get()->toArray();
  343. $model->whereIn('dispatch_no',array_column($res,'dispatch_no'));
  344. }
  345. if(! empty($data['employee_id'])) {
  346. $res = DispatchEmpSub::where('del_time',0)
  347. ->where('employee_id',$data['employee_id'])
  348. ->distinct()
  349. ->select('dispatch_no')
  350. ->get()->toArray();
  351. $model->whereIn('dispatch_no',array_column($res,'dispatch_no'));
  352. }
  353. $list = $this->limit($model,'',$data);
  354. $list = $this->fillDispatchOrderListData($list);
  355. return [true,$list];
  356. }
  357. public function fillDispatchOrderListData($data){
  358. if(empty($data['data'])) return $data;
  359. $emp_sub = DispatchEmpSub::where('del_time',0)
  360. ->whereIn('dispatch_no',array_column($data['data'],'dispatch_no'))
  361. ->select('dispatch_no','equipment_id','team_id','employee_id')
  362. ->get()->toArray();
  363. if(! empty($emp_sub)){
  364. $team_map = Team::whereIn('id',array_unique(array_column($emp_sub,'team_id')))
  365. ->pluck('title','id')
  366. ->toArray();
  367. $equipment_map = Equipment::whereIn('id',array_unique(array_column($emp_sub,'equipment_id')))
  368. ->pluck('title','id')
  369. ->toArray();
  370. $emp_map = Employee::whereIn('id',array_unique(array_column($emp_sub,'employee_id')))
  371. ->pluck('emp_name','id')
  372. ->toArray();
  373. $equipment_map1 = $team_map1 = $employee_map1 = $employee_map2 = $equipment_map2 = $team_map2 = [];
  374. foreach ($emp_sub as $value){
  375. $equipment_name = $equipment_map[$value['equipment_id']] ?? '';
  376. $team_name = $team_map[$value['team_id']] ?? '';
  377. $employee_name = $emp_map[$value['employee_id']] ?? '';
  378. $equipment_map1[$value['dispatch_no']][] = $equipment_name;
  379. $equipment_map2[$value['dispatch_no']][] = $value['equipment_id'];
  380. $team_map1[$value['dispatch_no']][] = $team_name;
  381. $team_map2[$value['dispatch_no']][] = $value['team_id'];
  382. $employee_map1[$value['dispatch_no']][] = $employee_name;
  383. $employee_map2[$value['dispatch_no']][] = $value['employee_id'];
  384. }
  385. }
  386. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  387. ->pluck('title','id')
  388. ->toArray();
  389. foreach ($data['data'] as $key => $value){
  390. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  391. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  392. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  393. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  394. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  395. $tmp = $employee_map1[$value['dispatch_no']] ?? [];
  396. $data['data'][$key]['team_man'] = implode(',',array_unique($tmp));
  397. $tmp = $employee_map2[$value['dispatch_no']] ?? [];
  398. $data['data'][$key]['team_man_id'] = array_unique($tmp);
  399. $tmp = $team_map1[$value['dispatch_no']] ?? [];
  400. $data['data'][$key]['team_name'] = implode(',',array_unique($tmp));
  401. $tmp = $team_map2[$value['dispatch_no']] ?? [];
  402. $data['data'][$key]['team_id'] = implode(',',array_unique($tmp));
  403. $tmp = $equipment_map1[$value['dispatch_no']] ?? [];
  404. $data['data'][$key]['equipment_name'] = implode(',',array_unique($tmp));
  405. $tmp = $equipment_map2[$value['dispatch_no']] ?? [];
  406. $data['data'][$key]['equipment_id'] = implode(',',array_unique($tmp));
  407. $data['data'][$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'] - $value['waste_num'];
  408. }
  409. $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
  410. return $data;
  411. }
  412. //反写写派工数量(增加)
  413. public function writeDispatchQuantity($order_product_id){
  414. if(empty($order_product_id)) return;
  415. $result = DispatchSub::where('del_time',0)
  416. ->whereIn('order_product_id',$order_product_id)
  417. ->select(DB::raw("sum(dispatch_quantity) as dispatch_quantity"),'order_product_id')
  418. ->groupby('order_product_id')
  419. ->pluck('dispatch_quantity','order_product_id')
  420. ->toArray();
  421. if(empty($result)) return;
  422. foreach ($result as $key => $value){
  423. OrdersProduct::where('id',$key)->update([
  424. 'dispatch_complete_quantity' => $value
  425. ]);
  426. }
  427. }
  428. //反写写派工数量(删除)
  429. public function writeDispatchQuantityDEL($order_product_id){
  430. if(empty($order_product_id)) return;
  431. $result = DispatchSub::where('del_time',0)
  432. ->whereIn('order_product_id',$order_product_id)
  433. ->select(DB::raw("sum(dispatch_quantity) as dispatch_quantity"),'order_product_id')
  434. ->groupby('order_product_id')
  435. ->pluck('dispatch_quantity','order_product_id')
  436. ->toArray();
  437. foreach ($order_product_id as $value){
  438. $quantity = $result[$value] ?? 0;
  439. OrdersProduct::where('id',$value)->update([
  440. 'dispatch_complete_quantity' => $quantity
  441. ]);
  442. }
  443. }
  444. //设备上的去完工列表
  445. public function dispatchMobileOrderList($data){
  446. $model = DispatchSub::where('del_time',0)
  447. ->select('id','product_title','product_no','dispatch_quantity','finished_num','dispatch_no','waste_num')
  448. ->whereRaw('dispatch_quantity > finished_num')
  449. ->orderBy('id','desc');
  450. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  451. if(! empty($data['order_number'])) $model->where('dispatch_no',$data['dispatch_no']);
  452. $list = $model->get()->toArray();
  453. $list = $this->fillDispatchMobileOrderList($list);
  454. return [true,$list];
  455. }
  456. public function fillDispatchMobileOrderList($data){
  457. if(empty($data)) return $data;
  458. foreach ($data as $key => $value){
  459. $data[$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'] - $value['waste_num'];
  460. }
  461. $return['product_num'] = count($data);
  462. $return['finished_num'] = array_sum(array_column($data, 'finished_num'));
  463. $return['un_finished_quantity'] = array_sum(array_column($data, 'un_finished_quantity'));
  464. $return['data'] = $data;
  465. unset($data);
  466. return $return;
  467. }
  468. //设备上完工填写数据的页面
  469. public function dispatchMobileOrderDetailsList($data){
  470. if(empty($data['id'])) return [false,'派工单ID不能为空!'];
  471. $dispatch = DispatchSub::whereIn('id',$data['id'])
  472. ->where('del_time',0)
  473. ->select('id','product_title','product_no','dispatch_no',DB::raw('(dispatch_quantity - finished_num) as quantity'))
  474. ->get()->toArray();
  475. $sub = DispatchEmpSub::where('del_time',0)
  476. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  477. ->select('dispatch_no','equipment_id','team_id','employee_id')
  478. ->get()->toArray();
  479. $sub_map = [];
  480. foreach ($sub as $s){
  481. $array = [
  482. 'equipment_id' => $s['equipment_id'],
  483. 'team_id' => $s['team_id'],
  484. 'employee_id' => $s['employee_id'],
  485. ];
  486. if(empty($sub_map[$s['dispatch_no']])){
  487. $sub_map[$s['dispatch_no']][] = $array;
  488. }else{
  489. if(! in_array($array,$sub_map[$s['dispatch_no']])) {
  490. $sub_map[$s['dispatch_no']][] = $array;
  491. }
  492. }
  493. }
  494. $return_list = $return_details = [];
  495. foreach ($dispatch as $value){
  496. $dispatch_tmp = $sub_map[$value['dispatch_no']];
  497. $counts = count($dispatch_tmp) ?? 1;
  498. // 计算每个人应该得到的数量
  499. $per_person = floor($value['quantity'] / $counts);
  500. // 计算最后一个人拿到的数量
  501. $last_person = $value['quantity'] - ($per_person * ($counts - 1));
  502. foreach ($dispatch_tmp as $k => $t){
  503. $dispatch_tmp[$k]['id'] = $value['id'];
  504. $dispatch_tmp[$k]['product_title'] = $value['product_title'];
  505. $dispatch_tmp[$k]['product_no'] = $value['product_no'];
  506. $dispatch_tmp[$k]['dispatch_no'] = $value['dispatch_no'];
  507. if ($k == $counts - 1) {
  508. $dispatch_tmp[$k]['quantity'] = (int)$last_person;
  509. }else{
  510. $dispatch_tmp[$k]['quantity'] = (int)$per_person;
  511. }
  512. }
  513. //列数据
  514. $return_list = array_merge_recursive($return_list,$dispatch_tmp);
  515. //总数量
  516. $return_details[$value['id']] = $value['quantity'];
  517. }
  518. $return['list'] = $return_list;
  519. $return['list_details'] = $return_details;
  520. return [true, $return];
  521. }
  522. }