DispatchService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. $dispatch_quantity = $value['dispatch_quantity'] * 1000;
  147. $process_model->where('order_product_id',$value['order_product_id'])
  148. ->where('process_id',$value['process_id'])
  149. ->where('dispatch_no','')
  150. ->take($dispatch_quantity)
  151. ->update([
  152. 'dispatch_no' => $dispatch_no,
  153. 'status' => 1
  154. ]);
  155. }
  156. DispatchSub::insert($result);
  157. return [true,''];
  158. }
  159. public function del($data){
  160. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  161. return [true,'删除成功'];
  162. }
  163. public function orderDetail($data){
  164. return [200,''];
  165. }
  166. public function is_same_month($timestamp1,$timestamp2){
  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','dispatch_complete_quantity','pre_shipment_time','process_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(! 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]]);
  203. if(isset($data['status'])) $model->where('status',$data['status']);
  204. if(isset($data['is_create'])) {
  205. if($data['is_create']){
  206. $model->whereColumn('dispatch_complete_quantity', '>=', 'production_quantity');
  207. }else{
  208. $model->whereColumn('production_quantity', '>', 'dispatch_complete_quantity');
  209. }
  210. }
  211. $list = $this->limit($model,'',$data);
  212. $list = $this->fillData($list);
  213. return [true,$list];
  214. }
  215. public function orderListRule($data){
  216. // if(empty($data['process_id'])) return [false, '工序必须选择!'];
  217. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期必须选择'];
  218. $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
  219. if(! $bool) return [false,'制单日期必须同月!'];
  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(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单时间不能为空!'];
  234. if(! isset($data['is_split'])) return [false,'是否拆分标识不能为空!'];
  235. if(empty($data['detail'])) return [false, '派工明细数据不能为空!'];
  236. $id = [];
  237. $detail_map = $detail_2 = [];
  238. $device_map = Equipment::where('del_time',0)
  239. ->pluck('title','id')
  240. ->toArray();
  241. foreach ($data['detail'] as $value){
  242. if(empty($value['id'])) return [false,'请选择派工数据!'];
  243. if(empty($value['process_id'])) return [false,'工序不能为空!'];
  244. if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'数量不能小于0!'];
  245. if(empty($value['dispatch_time'][0]) || empty($value['dispatch_time'][1])) return [false,'计划生产时间不能为空!'];
  246. if(! in_array($value['id'], $id)) $id[] = $value['id'];
  247. $key = $value['id'] . $value['process_id'];
  248. if(isset($detail_map[$key])){
  249. $detail_map[$key] += $value['quantity'];
  250. }else{
  251. $detail_map[$key] = $value['quantity'];
  252. }
  253. if(isset($detail_2[$value['id']][$value['process_id']])){
  254. $detail_2[$value['id']][$value['process_id']] += $value['quantity'];
  255. }else{
  256. $detail_2[$value['id']][$value['process_id']] = $value['quantity'];
  257. }
  258. if(! empty($value['device_id']) && ! isset($device_map[$value['device_id']])) return [false, '设备不存在或已被删除'];
  259. }
  260. $result = OrdersProduct::whereIn('id',$id)
  261. ->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')
  262. ->orderBy('id','desc')
  263. ->get()->toArray();
  264. $result_map = array_column($result,null,'order_product_id');
  265. //每个生产订单所有的工序
  266. $process_map = $this->getProcess($result);
  267. //已派工数据
  268. $map = $this->getDispatchQuantity($id);
  269. //校验
  270. foreach ($result as $value){
  271. //总数量校验
  272. $detail2 = $detail_2[$value['order_product_id']] ?? [];
  273. $uniqueValuesCount = count(array_unique($detail2));
  274. if($uniqueValuesCount != 1) return [false , "生产订单号:" . $value['production_no'] . "所有工序派工数量必须相等"];
  275. //本次提交的工序
  276. // $submit_process = array_keys($detail2);
  277. //工序
  278. $tmp = $process_map[$value['order_product_id']] ?? [];
  279. foreach ($tmp as $v) {
  280. // if(! in_array($v['process_id'], $submit_process)) continue;
  281. //键值 生成订单id + 工序id
  282. $key = $value['order_product_id'] . $v['process_id'];
  283. //本次提交数量
  284. $quantity_tmp = $detail_map[$key] ?? 0;
  285. //工序已派工
  286. $q = $map[$key] ?? 0;
  287. if($q + $quantity_tmp > $value['production_quantity']) return [false, "生产订单号:" . $value['production_no'] . "的工序:" . $v['process_title'] . "的派单数量不能超过生产订单数量"];
  288. }
  289. }
  290. $return = [];
  291. foreach ($data['detail'] as $value){
  292. $tmp = $result_map[$value['id']] ?? [];
  293. unset($tmp['process_id']);unset($tmp['production_no']);
  294. $tmp['process_id'] = $value['process_id'];
  295. $tmp['team_id'] = $value['team_id'];
  296. $tmp['device_id'] = $value['device_id'];
  297. $tmp['dispatch_time_start'] = $value['dispatch_time'][0];
  298. $tmp['dispatch_time_end'] = $value['dispatch_time'][1];
  299. $tmp['dispatch_quantity'] = $value['quantity'];
  300. $return[] = $tmp;
  301. }
  302. return [true, $return];
  303. }
  304. public function fillData($data){
  305. if(empty($data['data'])) return $data;
  306. $map = $this->getDispatchQuantity(array_column($data['data'],'id'));
  307. $emp_map = Employee::whereIn('id',array_column($data['data'],'crt_id'))
  308. ->pluck('emp_name','id')
  309. ->toArray();
  310. $process_map = $this->getProcess($data['data']);
  311. foreach ($data['data'] as $key => $value){
  312. $tmp = $process_map[$value['id']] ?? [];
  313. foreach ($tmp as $t => $v) {
  314. $q = $map[$value['id'] . $v['process_id']] ?? 0;
  315. $tmp[$t]['dispatch_quantity'] = $q;
  316. $tmp[$t]['not_dispatch_quantity'] = $value['production_quantity'] - $q;
  317. }
  318. $data['data'][$key]['process'] = $tmp;
  319. $last_process = end($tmp);
  320. $dispatch_quantity = $last_process['dispatch_quantity'] ?? 0;
  321. $data['data'][$key]['dispatch_quantity'] = $dispatch_quantity;
  322. $data['data'][$key]['not_dispatch_quantity'] = $value['production_quantity'] - $dispatch_quantity;
  323. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  324. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  325. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  326. $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
  327. $data['data'][$key]['order_product_man'] = $emp_map[$value['crt_id']] ?? '';
  328. if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
  329. $data['data'][$key]['is_create'] = 1;
  330. }else{
  331. $data['data'][$key]['is_create'] = 0;
  332. }
  333. }
  334. $data['production_quantity'] = $this->getTotal($data['data'], 'production_quantity');
  335. $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');
  336. $data['not_dispatch_quantity'] = $this->getTotal($data['data'], 'not_dispatch_quantity');
  337. return $data;
  338. }
  339. //返回已派工数量
  340. public function getDispatchQuantity($order_product_id = []){
  341. if(empty($order_product_id)) return [];
  342. $result = DispatchSub::where('del_time',0)
  343. ->whereIn("order_product_id",$order_product_id)
  344. ->select('dispatch_quantity','order_product_id','process_id')
  345. ->get()
  346. ->toArray();
  347. $return = [];
  348. foreach ($result as $value){
  349. $key = $value['order_product_id'] . $value['process_id'];
  350. if(isset($return[$key])){
  351. $return[$key] += $value['dispatch_quantity'];
  352. }else{
  353. $return[$key] = $value['dispatch_quantity'];
  354. }
  355. }
  356. return $return;
  357. }
  358. public function getProcess($data){
  359. if(empty($data)) return [];
  360. $process_id_array = [];
  361. $process_id = array_column($data,'process_id');
  362. foreach ($process_id as $value){
  363. $tmp = explode(',',$value);
  364. foreach ($tmp as $v){
  365. if(! in_array($v,$process_id_array)) $process_id_array[] = $v;
  366. }
  367. }
  368. $process_array = Process::whereIn('id', $process_id_array)->get()->toArray();
  369. $process_map = array_column($process_array,null,'id');
  370. $return = [];
  371. foreach ($data as $value){
  372. $tmp = explode(',',$value['process_id']);
  373. foreach ($tmp as $v){
  374. $process_tmp = $process_map[$v] ?? [];
  375. if(isset($value['id'])){
  376. $return[$value['id']][] = [
  377. 'process_id' => $v,
  378. 'process_title' => $process_tmp['title'] ?? "",
  379. 'team_id' => $process_tmp['team_id'] ?? 0,
  380. 'device_id' => $process_tmp['device_id'] ?? 0,
  381. ];
  382. }else{
  383. $return[$value['order_product_id']][] = [
  384. 'process_id' => $v,
  385. 'process_title' => $process_tmp['title'] ?? "",
  386. 'team_id' => $process_tmp['team_id'] ?? 0,
  387. 'device_id' => $process_tmp['device_id'] ?? 0,
  388. ];
  389. }
  390. }
  391. }
  392. return $return;
  393. }
  394. public function dispatchOrderList($data){
  395. $model = DispatchSub::where('del_time',0)
  396. ->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')
  397. ->orderBy('id','desc');
  398. if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
  399. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  400. if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  401. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  402. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  403. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  404. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  405. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  406. $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
  407. $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
  408. }
  409. if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
  410. if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);
  411. if(isset($data['is_finished'])) {
  412. if($data['is_finished']){
  413. $model->wherecolumn('dispatch_quantity','=','finished_num');
  414. }else{
  415. $model->wherecolumn('dispatch_quantity','>','finished_num');
  416. }
  417. }
  418. $list = $this->limit($model,'',$data);
  419. $list = $this->fillDispatchOrderListData($list);
  420. return [true,$list];
  421. }
  422. public function fillDispatchOrderListData($data){
  423. if(empty($data['data'])) return $data;
  424. $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
  425. ->pluck('title','id')
  426. ->toArray();
  427. $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
  428. ->pluck('title','id')
  429. ->toArray();
  430. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  431. ->pluck('title','id')
  432. ->toArray();
  433. foreach ($data['data'] as $key => $value){
  434. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  435. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  436. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  437. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  438. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  439. $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";
  440. $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";
  441. $data['data'][$key]['equipment_id'] = $value['device_id'];
  442. $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);
  443. }
  444. $total = $this->getTotal($data['data'], 'dispatch_quantity');
  445. $data['dispatch_quantity'] = $total;
  446. return $data;
  447. }
  448. //反写写派工数量(增加)
  449. public function writeDispatchQuantity($order_product_id){
  450. if(empty($order_product_id)) return;
  451. //最后一道工序
  452. $last_process = [];
  453. $process_id = OrdersProduct::whereIn('id',$order_product_id)
  454. ->select('id','process_id')
  455. ->get()->toArray();
  456. foreach ($process_id as $value){
  457. $tmp_process = explode(',', $value['process_id']);
  458. $last_process[$value['id']] = end($tmp_process);
  459. }
  460. $result = DispatchSub::where('del_time',0)
  461. ->whereIn('order_product_id',$order_product_id)
  462. ->select('dispatch_quantity','order_product_id','process_id')
  463. ->get()
  464. ->toArray();
  465. if(empty($result)) return;
  466. $new_result = [];
  467. foreach ($result as $value){
  468. //统计最后一道工序的派工数量
  469. $tmp_last_process = $last_process[$value['order_product_id']];
  470. if($tmp_last_process == $value['process_id']){
  471. if(isset($new_result[$value['order_product_id']])){
  472. $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
  473. }else{
  474. $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
  475. }
  476. }
  477. }
  478. foreach ($new_result as $order_product_id => $num){
  479. OrdersProduct::where('id',$order_product_id)->update([
  480. 'dispatch_complete_quantity' => $num
  481. ]);
  482. }
  483. }
  484. //反写写派工数量(删除)
  485. public function writeDispatchQuantityDEL($order_product_id){
  486. if(empty($order_product_id)) return;
  487. //最后一道工序
  488. $last_process = [];
  489. $process_id = OrdersProduct::whereIn('id',$order_product_id)
  490. ->select('id','process_id')
  491. ->get()->toArray();
  492. foreach ($process_id as $value){
  493. $tmp_process = explode(',', $value['process_id']);
  494. $last_process[$value['id']] = end($tmp_process);
  495. }
  496. $result = DispatchSub::where('del_time',0)
  497. ->whereIn('order_product_id',$order_product_id)
  498. ->select('dispatch_quantity','order_product_id','process_id')
  499. ->get()
  500. ->toArray();
  501. $new_result = [];
  502. foreach ($result as $value){
  503. //统计最后一道工序的派工数量
  504. $tmp_last_process = $last_process[$value['order_product_id']];
  505. if($tmp_last_process == $value['process_id']){
  506. if(isset($new_result[$value['order_product_id']])){
  507. $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
  508. }else{
  509. $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
  510. }
  511. }
  512. }
  513. foreach ($order_product_id as $value){
  514. $quantity = $new_result[$value] ?? 0;
  515. OrdersProduct::where('id',$value)->update([
  516. 'dispatch_complete_quantity' => $quantity
  517. ]);
  518. }
  519. }
  520. //设备上的去完工列表
  521. public function dispatchMobileOrderList($data){
  522. $model = DispatchSub::where('del_time',0)
  523. ->select('id','product_title','product_no','dispatch_quantity','finished_num','dispatch_no','waste_num')
  524. ->whereRaw('dispatch_quantity > finished_num')
  525. ->orderBy('id','desc');
  526. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  527. if(! empty($data['order_number'])) $model->where('dispatch_no',$data['dispatch_no']);
  528. $list = $model->get()->toArray();
  529. $list = $this->fillDispatchMobileOrderList($list);
  530. return [true,$list];
  531. }
  532. public function fillDispatchMobileOrderList($data){
  533. if(empty($data)) return $data;
  534. foreach ($data as $key => $value){
  535. $data[$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'] - $value['waste_num'];
  536. }
  537. $return['product_num'] = count($data);
  538. $return['finished_num'] = $this->getTotal($data,'finished_num');
  539. $return['un_finished_quantity'] = $this->getTotal($data,'un_finished_quantity');
  540. $return['data'] = $data;
  541. unset($data);
  542. return $return;
  543. }
  544. //设备上完工填写数据的页面
  545. public function dispatchMobileOrderDetailsList($data){
  546. if(empty($data['id'])) return [false,'派工单ID不能为空!'];
  547. $dispatch = DispatchSub::whereIn('id',$data['id'])
  548. ->where('del_time',0)
  549. ->select('id','product_title','product_no','dispatch_no',DB::raw('(dispatch_quantity - finished_num) as quantity'))
  550. ->get()->toArray();
  551. $sub = DispatchEmpSub::where('del_time',0)
  552. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  553. ->select('dispatch_no','equipment_id','team_id','employee_id')
  554. ->get()->toArray();
  555. $sub_map = [];
  556. foreach ($sub as $s){
  557. $array = [
  558. 'equipment_id' => $s['equipment_id'],
  559. 'team_id' => $s['team_id'],
  560. 'employee_id' => $s['employee_id'],
  561. ];
  562. if(empty($sub_map[$s['dispatch_no']])){
  563. $sub_map[$s['dispatch_no']][] = $array;
  564. }else{
  565. if(! in_array($array,$sub_map[$s['dispatch_no']])) {
  566. $sub_map[$s['dispatch_no']][] = $array;
  567. }
  568. }
  569. }
  570. $return_list = $return_details = [];
  571. foreach ($dispatch as $value){
  572. $dispatch_tmp = $sub_map[$value['dispatch_no']];
  573. $counts = count($dispatch_tmp) ?? 1;
  574. // 计算每个人应该得到的数量
  575. $per_person = floor($value['quantity'] / $counts);
  576. // 计算最后一个人拿到的数量
  577. $last_person = $value['quantity'] - ($per_person * ($counts - 1));
  578. foreach ($dispatch_tmp as $k => $t){
  579. $dispatch_tmp[$k]['id'] = $value['id'];
  580. $dispatch_tmp[$k]['product_title'] = $value['product_title'];
  581. $dispatch_tmp[$k]['product_no'] = $value['product_no'];
  582. $dispatch_tmp[$k]['dispatch_no'] = $value['dispatch_no'];
  583. if ($k == $counts - 1) {
  584. $dispatch_tmp[$k]['quantity'] = (int)$last_person;
  585. }else{
  586. $dispatch_tmp[$k]['quantity'] = (int)$per_person;
  587. }
  588. }
  589. //列数据
  590. $return_list = array_merge_recursive($return_list,$dispatch_tmp);
  591. //总数量
  592. $return_details[$value['id']] = $value['quantity'];
  593. }
  594. $return['list'] = $return_list;
  595. $return['list_details'] = $return_details;
  596. return [true, $return];
  597. }
  598. }