DispatchService.php 27 KB

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