DispatchService.php 25 KB

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