DispatchService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. $time = time();
  43. if($data['is_split']){
  44. foreach ($msg as $value){
  45. list($s,$m) = $this->insertDispatch([$value],$data,$user,$time);
  46. if(! $s) return [false,$m];
  47. }
  48. }else{
  49. list($s,$m) = $this->insertDispatch($msg,$data,$user,$time);
  50. if(! $s) return [false, $m];
  51. }
  52. //反写已派工数量
  53. $this->writeDispatchQuantity(array_column($msg,'order_product_id'));
  54. DB::commit();
  55. }catch (\Exception $e){
  56. DB::rollBack();
  57. return [false,$e->getFile() . ':' .$e->getLine().':'.$e->getMessage()];
  58. }
  59. return [true,''];
  60. }
  61. public function makeData($equipment_id, $team_id,$employee_id,$message){
  62. $arr = [];
  63. if(! empty($equipment_id)){
  64. foreach ($equipment_id as $v_e){
  65. if(! empty($team_id)){
  66. foreach ($team_id as $t){
  67. if(! empty($employee_id)){
  68. foreach ($employee_id as $e){
  69. $arr[] = [
  70. 'equipment_id' => $v_e,
  71. 'team_id' => $t,
  72. 'employee_id' => $e,
  73. 'order_product_id' => $message['order_product_id'],
  74. 'dispatch_no' => $message['dispatch_no'],
  75. ];
  76. }
  77. }else{
  78. $arr[] = [
  79. 'equipment_id' => $v_e,
  80. 'team_id' => $t,
  81. 'order_product_id' => $message['order_product_id'],
  82. 'dispatch_no' => $message['dispatch_no'],
  83. ];
  84. }
  85. }
  86. }elseif(! empty($employee_id)){
  87. foreach ($employee_id as $e){
  88. $arr[] = [
  89. 'equipment_id' => $v_e,
  90. 'employee_id' => $e,
  91. 'order_product_id' => $message['order_product_id'],
  92. 'dispatch_no' => $message['dispatch_no'],
  93. ];
  94. }
  95. }else{
  96. $arr[] = [
  97. 'equipment_id' => $v_e,
  98. 'order_product_id' => $message['order_product_id'],
  99. 'dispatch_no' => $message['dispatch_no'],
  100. ];
  101. }
  102. }
  103. }elseif (! empty($team_id)){
  104. foreach ($team_id as $t){
  105. if(! empty($employee_id)){
  106. foreach ($employee_id as $e){
  107. $arr[] = [
  108. 'team_id' => $t,
  109. 'employee_id' => $e,
  110. 'order_product_id' => $message['order_product_id'],
  111. 'dispatch_no' => $message['dispatch_no'],
  112. ];
  113. }
  114. }else{
  115. $arr[] = [
  116. 'team_id' => $t,
  117. 'order_product_id' => $message['order_product_id'],
  118. 'dispatch_no' => $message['dispatch_no'],
  119. ];
  120. }
  121. }
  122. }elseif(! empty($employee_id)){
  123. foreach ($employee_id as $e){
  124. $arr[] = [
  125. 'employee_id' => $e,
  126. 'order_product_id' => $message['order_product_id'],
  127. 'dispatch_no' => $message['dispatch_no'],
  128. ];
  129. }
  130. }
  131. return $arr;
  132. }
  133. public function insertDispatch($msg, $data, $user, $time){
  134. //生产数据的源数据
  135. $result = $msg;
  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. foreach ($result as $key => $value){
  143. $result[$key]['dispatch_no'] = $dispatch_no;
  144. $result[$key]['crt_time'] = $time;
  145. $result[$key]['crt_id'] = $user['id'];
  146. $process_model->where('order_product_id',$value['order_product_id'])
  147. ->where('process_id',$value['process_id'])
  148. ->where('dispatch_no','')
  149. ->take($value['dispatch_quantity'])
  150. ->update([
  151. 'dispatch_no' => $dispatch_no,
  152. 'status' => 1
  153. ]);
  154. }
  155. DispatchSub::insert($result);
  156. return [true,''];
  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. // 格式化时间戳为年份和月份
  167. $year1 = date('Y', $timestamp1);
  168. $month1 = date('m', $timestamp1);
  169. $year2 = date('Y', $timestamp2);
  170. $month2 = date('m', $timestamp2);
  171. if ($year1 === $year2 && $month1 === $month2) {
  172. return true;
  173. } else {
  174. return false;
  175. }
  176. }
  177. public function orderList($data){
  178. list($status,$msg) = $this->orderListRule($data);
  179. if(! $status) return [false, $msg];
  180. $model = OrdersProduct::where('del_time',0)
  181. ->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','pre_shipment_time','process_id')
  182. ->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]])
  183. ->whereIn('id',$msg)
  184. ->orderBy('id','desc');
  185. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  186. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  187. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  188. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  189. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  190. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  191. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  192. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  193. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  194. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  195. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  196. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  197. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  198. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  199. 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]]);
  200. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  201. if(! empty($data['pre_shipment_time'][0]) && ! empty($data['pre_shipment_time'][1])) $model->whereBetween('pre_shipment_time',[$data['pre_shipment_time'][0],$data['pre_shipment_time'][1]]);
  202. if(isset($data['status'])) $model->where('status',$data['status']);
  203. if(isset($data['is_create'])) {
  204. if($data['is_create']){
  205. $model->whereColumn('dispatch_complete_quantity', '>=', 'production_quantity');
  206. }else{
  207. $model->whereColumn('production_quantity', '>', 'dispatch_complete_quantity');
  208. }
  209. }
  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. $time = date("Ymd",$data['out_order_no_time'][0]);
  220. $process_model = new OrdersProductProcess(['channel' => $time]);
  221. if(! $process_model->is_table_isset()) return [true, []];//不存在process子表 返回空数据
  222. $process_array = $process_model->where('del_time',0)
  223. // ->where('process_id',$data['process_id'])
  224. ->distinct()
  225. ->select('order_product_id')
  226. ->get()->toArray();
  227. $order_product_id = array_column($process_array,'order_product_id');
  228. if(empty($order_product_id)) return [true,[]];//process子表无数据 返回空数据
  229. return [true, $order_product_id];
  230. }
  231. public function orderRule($data){
  232. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单时间不能为空!'];
  233. if(! isset($data['is_split'])) return [false,'是否拆分标识不能为空!'];
  234. if(empty($data['detail'])) return [false, '派工明细数据不能为空!'];
  235. $id = [];
  236. $detail_map = $detail_2 = [];
  237. foreach ($data['detail'] as $value){
  238. if(empty($value['id'])) return [false,'请选择派工数据!'];
  239. if(empty($value['process_id'])) return [false,'工序不能为空!'];
  240. if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'数量不能小于0!'];
  241. if(empty($value['dispatch_time'][0]) || empty($value['dispatch_time'][1])) return [false,'计划生产时间不能为空!'];
  242. if(! in_array($value['id'], $id)) $id[] = $value['id'];
  243. $key = $value['id'] . $value['process_id'];
  244. if(isset($detail_map[$key])){
  245. $detail_map[$key] += $value['quantity'];
  246. }else{
  247. $detail_map[$key] = $value['quantity'];
  248. }
  249. if(isset($detail_2[$value['id']][$value['process_id']])){
  250. $detail_2[$value['id']][$value['process_id']] += $value['quantity'];
  251. }else{
  252. $detail_2[$value['id']][$value['process_id']] = $value['quantity'];
  253. }
  254. }
  255. $result = OrdersProduct::whereIn('id',$id)
  256. ->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','pre_shipment_time','process_id','production_no')
  257. ->orderBy('id','desc')
  258. ->get()->toArray();
  259. $result_map = array_column($result,null,'order_product_id');
  260. //每个生产订单所有的工序
  261. $process_map = $this->getProcess($result);
  262. //已派工数据
  263. $map = $this->getDispatchQuantity($id);
  264. //校验
  265. foreach ($result as $value){
  266. //总数量校验
  267. $detail2 = $detail_2[$value['order_product_id']] ?? [];
  268. $uniqueValuesCount = count(array_unique($detail2));
  269. if($uniqueValuesCount != 1) return [false , "生产订单号:" . $value['production_no'] . "所有工序派工数量必须相等"];
  270. //本次提交的工序
  271. // $submit_process = array_keys($detail2);
  272. //工序
  273. $tmp = $process_map[$value['order_product_id']] ?? [];
  274. foreach ($tmp as $v) {
  275. // if(! in_array($v['process_id'], $submit_process)) continue;
  276. //键值 生成订单id + 工序id
  277. $key = $value['order_product_id'] . $v['process_id'];
  278. //本次提交数量
  279. $quantity_tmp = $detail_map[$key] ?? 0;
  280. //工序已派工
  281. $q = $map[$key] ?? 0;
  282. if($q + $quantity_tmp > $value['production_quantity']) return [false, "生产订单号:" . $value['production_no'] . "的工序:" . $v['process_title'] . "的派单数量不能超过生产订单数量"];
  283. }
  284. }
  285. $return = [];
  286. foreach ($data['detail'] as $value){
  287. $tmp = $result_map[$value['id']] ?? [];
  288. unset($tmp['process_id']);unset($tmp['production_no']);
  289. $tmp['process_id'] = $value['process_id'];
  290. $tmp['team_id'] = $value['team_id'];
  291. $tmp['device_id'] = $value['device_id'];
  292. $tmp['dispatch_time_start'] = $value['dispatch_time'][0];
  293. $tmp['dispatch_time_end'] = $value['dispatch_time'][1];
  294. $tmp['dispatch_quantity'] = $value['quantity'];
  295. $return[] = $tmp;
  296. }
  297. return [true, $return];
  298. }
  299. public function fillData($data){
  300. if(empty($data['data'])) return $data;
  301. $map = $this->getDispatchQuantity(array_column($data['data'],'id'));
  302. $emp_map = Employee::whereIn('id',array_column($data['data'],'crt_id'))
  303. ->pluck('emp_name','id')
  304. ->toArray();
  305. $process_map = $this->getProcess($data['data']);
  306. foreach ($data['data'] as $key => $value){
  307. $tmp = $process_map[$value['id']] ?? [];
  308. foreach ($tmp as $t => $v) {
  309. $q = $map[$value['id'] . $v['process_id']] ?? 0;
  310. $tmp[$t]['dispatch_quantity'] = $q;
  311. $tmp[$t]['not_dispatch_quantity'] = $value['production_quantity'] - $q;
  312. }
  313. $data['data'][$key]['process'] = $tmp;
  314. $last_process = end($tmp);
  315. $dispatch_quantity = $last_process['dispatch_quantity'] ?? 0;
  316. $data['data'][$key]['dispatch_quantity'] = $dispatch_quantity;
  317. $data['data'][$key]['not_dispatch_quantity'] = $value['production_quantity'] - $dispatch_quantity;
  318. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  319. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  320. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  321. $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
  322. $data['data'][$key]['order_product_man'] = $emp_map[$value['crt_id']] ?? '';
  323. if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
  324. $data['data'][$key]['is_create'] = 1;
  325. }else{
  326. $data['data'][$key]['is_create'] = 0;
  327. }
  328. }
  329. $data['production_quantity'] = array_sum(array_column($data['data'], 'production_quantity'));
  330. $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
  331. $data['not_dispatch_quantity'] = array_sum(array_column($data['data'], 'not_dispatch_quantity'));
  332. return $data;
  333. }
  334. //返回已派工数量
  335. public function getDispatchQuantity($order_product_id = []){
  336. if(empty($order_product_id)) return [];
  337. $result = DispatchSub::where('del_time',0)
  338. ->whereIn("order_product_id",$order_product_id)
  339. ->select('dispatch_quantity','order_product_id','process_id')
  340. ->get()
  341. ->toArray();
  342. $return = [];
  343. foreach ($result as $value){
  344. $key = $value['order_product_id'] . $value['process_id'];
  345. if(isset($return[$key])){
  346. $return[$key] += $value['dispatch_quantity'];
  347. }else{
  348. $return[$key] = $value['dispatch_quantity'];
  349. }
  350. }
  351. return $return;
  352. }
  353. public function getProcess($data){
  354. if(empty($data)) return [];
  355. $process_id_array = [];
  356. $process_id = array_column($data,'process_id');
  357. foreach ($process_id as $value){
  358. $tmp = explode(',',$value);
  359. foreach ($tmp as $v){
  360. if(! in_array($v,$process_id_array)) $process_id_array[] = $v;
  361. }
  362. }
  363. $process_array = Process::whereIn('id', $process_id_array)->get()->toArray();
  364. $process_map = array_column($process_array,null,'id');
  365. $return = [];
  366. foreach ($data as $value){
  367. $tmp = explode(',',$value['process_id']);
  368. foreach ($tmp as $v){
  369. $process_tmp = $process_map[$v] ?? [];
  370. if(isset($value['id'])){
  371. $return[$value['id']][] = [
  372. 'process_id' => $v,
  373. 'process_title' => $process_tmp['title'] ?? "",
  374. 'team_id' => $process_tmp['team_id'] ?? 0,
  375. 'device_id' => $process_tmp['device_id'] ?? 0,
  376. ];
  377. }else{
  378. $return[$value['order_product_id']][] = [
  379. 'process_id' => $v,
  380. 'process_title' => $process_tmp['title'] ?? "",
  381. 'team_id' => $process_tmp['team_id'] ?? 0,
  382. 'device_id' => $process_tmp['device_id'] ?? 0,
  383. ];
  384. }
  385. }
  386. }
  387. return $return;
  388. }
  389. public function dispatchOrderList($data){
  390. $model = DispatchSub::where('del_time',0)
  391. ->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','team_id','device_id')
  392. ->orderBy('id','desc');
  393. if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
  394. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  395. if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  396. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  397. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  398. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  399. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  400. $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
  401. $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
  402. }
  403. if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
  404. if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);
  405. if(isset($data['is_finished'])) {
  406. if($data['is_finished']){
  407. $model->wherecolumn('dispatch_quantity','=','finished_num');
  408. }else{
  409. $model->wherecolumn('dispatch_quantity','>','finished_num');
  410. }
  411. }
  412. $list = $this->limit($model,'',$data);
  413. $list = $this->fillDispatchOrderListData($list);
  414. return [true,$list];
  415. }
  416. public function fillDispatchOrderListData($data){
  417. if(empty($data['data'])) return $data;
  418. $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
  419. ->pluck('title','id')
  420. ->toArray();
  421. $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
  422. ->pluck('title','id')
  423. ->toArray();
  424. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  425. ->pluck('title','id')
  426. ->toArray();
  427. foreach ($data['data'] as $key => $value){
  428. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  429. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  430. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  431. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  432. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  433. $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";
  434. $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";
  435. $data['data'][$key]['equipment_id'] = $value['device_id'];
  436. $data['data'][$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'];
  437. }
  438. $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
  439. return $data;
  440. }
  441. //反写写派工数量(增加)
  442. public function writeDispatchQuantity($order_product_id){
  443. if(empty($order_product_id)) return;
  444. //最后一道工序
  445. $last_process = [];
  446. $process_id = OrdersProduct::whereIn('id',$order_product_id)
  447. ->select('id','process_id')
  448. ->get()->toArray();
  449. foreach ($process_id as $value){
  450. $tmp_process = explode(',', $value['process_id']);
  451. $last_process[$value['id']] = end($tmp_process);
  452. }
  453. $result = DispatchSub::where('del_time',0)
  454. ->whereIn('order_product_id',$order_product_id)
  455. ->select('dispatch_quantity','order_product_id','process_id')
  456. ->get()
  457. ->toArray();
  458. if(empty($result)) return;
  459. $new_result = [];
  460. foreach ($result as $value){
  461. //统计最后一道工序的派工数量
  462. $tmp_last_process = $last_process[$value['order_product_id']];
  463. if($tmp_last_process == $value['process_id']){
  464. if(isset($new_result[$value['order_product_id']])){
  465. $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
  466. }else{
  467. $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
  468. }
  469. }
  470. }
  471. foreach ($new_result as $order_product_id => $num){
  472. OrdersProduct::where('id',$order_product_id)->update([
  473. 'dispatch_complete_quantity' => $num
  474. ]);
  475. }
  476. }
  477. //反写写派工数量(删除)
  478. public function writeDispatchQuantityDEL($order_product_id){
  479. if(empty($order_product_id)) return;
  480. //最后一道工序
  481. $last_process = [];
  482. $process_id = OrdersProduct::whereIn('id',$order_product_id)
  483. ->select('id','process_id')
  484. ->get()->toArray();
  485. foreach ($process_id as $value){
  486. $tmp_process = explode(',', $value['process_id']);
  487. $last_process[$value['id']] = end($tmp_process);
  488. }
  489. $result = DispatchSub::where('del_time',0)
  490. ->whereIn('order_product_id',$order_product_id)
  491. ->select('dispatch_quantity','order_product_id','process_id')
  492. ->get()
  493. ->toArray();
  494. $new_result = [];
  495. foreach ($result as $value){
  496. //统计最后一道工序的派工数量
  497. $tmp_last_process = $last_process[$value['order_product_id']];
  498. if($tmp_last_process == $value['process_id']){
  499. if(isset($new_result[$value['order_product_id']])){
  500. $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
  501. }else{
  502. $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
  503. }
  504. }
  505. }
  506. foreach ($order_product_id as $value){
  507. $quantity = $new_result[$value] ?? 0;
  508. OrdersProduct::where('id',$value)->update([
  509. 'dispatch_complete_quantity' => $quantity
  510. ]);
  511. }
  512. }
  513. //设备上的去完工列表
  514. public function dispatchMobileOrderList($data){
  515. $model = DispatchSub::where('del_time',0)
  516. ->select('id','product_title','product_no','dispatch_quantity','finished_num','dispatch_no','waste_num')
  517. ->whereRaw('dispatch_quantity > finished_num')
  518. ->orderBy('id','desc');
  519. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  520. if(! empty($data['order_number'])) $model->where('dispatch_no',$data['dispatch_no']);
  521. $list = $model->get()->toArray();
  522. $list = $this->fillDispatchMobileOrderList($list);
  523. return [true,$list];
  524. }
  525. public function fillDispatchMobileOrderList($data){
  526. if(empty($data)) return $data;
  527. foreach ($data as $key => $value){
  528. $data[$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'] - $value['waste_num'];
  529. }
  530. $return['product_num'] = count($data);
  531. $return['finished_num'] = array_sum(array_column($data, 'finished_num'));
  532. $return['un_finished_quantity'] = array_sum(array_column($data, 'un_finished_quantity'));
  533. $return['data'] = $data;
  534. unset($data);
  535. return $return;
  536. }
  537. //设备上完工填写数据的页面
  538. public function dispatchMobileOrderDetailsList($data){
  539. if(empty($data['id'])) return [false,'派工单ID不能为空!'];
  540. $dispatch = DispatchSub::whereIn('id',$data['id'])
  541. ->where('del_time',0)
  542. ->select('id','product_title','product_no','dispatch_no',DB::raw('(dispatch_quantity - finished_num) as quantity'))
  543. ->get()->toArray();
  544. $sub = DispatchEmpSub::where('del_time',0)
  545. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  546. ->select('dispatch_no','equipment_id','team_id','employee_id')
  547. ->get()->toArray();
  548. $sub_map = [];
  549. foreach ($sub as $s){
  550. $array = [
  551. 'equipment_id' => $s['equipment_id'],
  552. 'team_id' => $s['team_id'],
  553. 'employee_id' => $s['employee_id'],
  554. ];
  555. if(empty($sub_map[$s['dispatch_no']])){
  556. $sub_map[$s['dispatch_no']][] = $array;
  557. }else{
  558. if(! in_array($array,$sub_map[$s['dispatch_no']])) {
  559. $sub_map[$s['dispatch_no']][] = $array;
  560. }
  561. }
  562. }
  563. $return_list = $return_details = [];
  564. foreach ($dispatch as $value){
  565. $dispatch_tmp = $sub_map[$value['dispatch_no']];
  566. $counts = count($dispatch_tmp) ?? 1;
  567. // 计算每个人应该得到的数量
  568. $per_person = floor($value['quantity'] / $counts);
  569. // 计算最后一个人拿到的数量
  570. $last_person = $value['quantity'] - ($per_person * ($counts - 1));
  571. foreach ($dispatch_tmp as $k => $t){
  572. $dispatch_tmp[$k]['id'] = $value['id'];
  573. $dispatch_tmp[$k]['product_title'] = $value['product_title'];
  574. $dispatch_tmp[$k]['product_no'] = $value['product_no'];
  575. $dispatch_tmp[$k]['dispatch_no'] = $value['dispatch_no'];
  576. if ($k == $counts - 1) {
  577. $dispatch_tmp[$k]['quantity'] = (int)$last_person;
  578. }else{
  579. $dispatch_tmp[$k]['quantity'] = (int)$per_person;
  580. }
  581. }
  582. //列数据
  583. $return_list = array_merge_recursive($return_list,$dispatch_tmp);
  584. //总数量
  585. $return_details[$value['id']] = $value['quantity'];
  586. }
  587. $return['list'] = $return_list;
  588. $return['list_details'] = $return_details;
  589. return [true, $return];
  590. }
  591. }