DispatchService.php 30 KB

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