DispatchService.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. <?php
  2. namespace App\Service;
  3. use App\Model\ApplyOrder;
  4. use App\Model\Dispatch;
  5. use App\Model\DispatchEmpSub;
  6. use App\Model\DispatchSub;
  7. use App\Model\Employee;
  8. use App\Model\Equipment;
  9. use App\Model\OrdersProduct;
  10. use App\Model\OrdersProductProcess;
  11. use App\Model\Process;
  12. use App\Model\Team;
  13. use Illuminate\Support\Facades\DB;
  14. class DispatchService extends Service
  15. {
  16. public function edit($data){}
  17. public function setOrderNO(){
  18. $str = date('Ymd',time());
  19. $order_number = Dispatch::where('dispatch_no','Like','%'. $str . '%')
  20. ->max('dispatch_no');
  21. if(empty($order_number)){
  22. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  23. $number = $str . $number;
  24. }else{
  25. $tmp = substr($order_number, -3);
  26. $tmp = $tmp + 1;
  27. //超过99999
  28. if(strlen($tmp) > 3) return '';
  29. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  30. $number = $str . $number;
  31. }
  32. return $number;
  33. }
  34. public function add($data,$user){
  35. //数据校验以及填充
  36. list($status,$msg) = $this->orderRule($data);
  37. if(! $status) return [$status,$msg];
  38. try{
  39. DB::beginTransaction();
  40. $time = time();
  41. $insert_data = [];
  42. if($data['is_split']){
  43. foreach ($msg as $value){
  44. list($s,$m) = $this->insertDispatch([$value],$data,$user,$time);
  45. if(! $s) return [false,$m];
  46. foreach ($m as $v_m){
  47. $insert_data[] = $v_m;
  48. }
  49. }
  50. }else{
  51. list($s,$m) = $this->insertDispatch($msg,$data,$user,$time);
  52. if(! $s) return [false, $m];
  53. $insert_data = $m;
  54. }
  55. $insert = [];$tmp = [];
  56. if(! empty($insert_data)){
  57. foreach ($insert_data as $value){
  58. $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
  59. if(! in_array($str, $tmp)) {
  60. $insert[] = [
  61. 'id' => $value['id'],
  62. 'quantity' => $value['quantity'],
  63. 'product_no' => $value['product_no'],
  64. ];
  65. $tmp[] = $str;
  66. }
  67. }
  68. }
  69. //反写已派工数量
  70. $this->writeDispatchQuantity(array_column($msg,'order_product_id'));
  71. //是否自动审核 领料申请单
  72. list($status, $msg) = $this->createSQ($insert, $user);
  73. if(! $status) return [false, $msg];
  74. DB::commit();
  75. }catch (\Exception $e){
  76. DB::rollBack();
  77. return [false,$e->getFile() . ':' .$e->getLine().':'.$e->getMessage()];
  78. }
  79. return [true,''];
  80. }
  81. public function createSQ($insert_data, $user){
  82. try {
  83. DB::beginTransaction();
  84. //是否自动审核领料申请单
  85. $em = new EmployeeService();
  86. $auto = $em->is_auto($user, "llsq_auto");
  87. $return = [];
  88. $product_no = array_unique(array_column($insert_data,'product_no'));
  89. //获取原料
  90. $service = new FyyOrderService();
  91. list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
  92. if($status) $return = $msg;
  93. //组织原材料写入数据
  94. $insert = [];
  95. foreach ($insert_data as $value){
  96. $t = $return[$value['product_no']] ?? [];
  97. $tmp = [
  98. 'id' => $value['id'],
  99. 'quantity' => $value['quantity'] ?? 0,
  100. 'product_no' => "",
  101. 'product_title' => "",
  102. 'product_size' => "",
  103. 'product_unit' => "",
  104. ];
  105. if(! empty($t)){
  106. foreach ($t as $v){
  107. $tmp['product_no'] = $v['product_no'];
  108. $tmp['product_title'] = $v['product_title'];
  109. $tmp['product_size'] = $v['product_size'];
  110. $tmp['product_unit'] = $v['product_unit'];
  111. $insert[] = $tmp;
  112. }
  113. }else{
  114. $tmp['quantity'] = 0;
  115. $insert[] = $tmp;
  116. }
  117. }
  118. //生成申请单
  119. $service = new ApplyOrderService();
  120. list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_one, $auto);
  121. if(! $status) {
  122. DB::rollBack();
  123. return [false, $msg];
  124. }
  125. if($auto) {
  126. //生成流水
  127. list($status, $msg) = $service->createRecord($msg);
  128. if(! $status) {
  129. DB::rollBack();
  130. return [false, $msg];
  131. }
  132. }
  133. DB::commit();
  134. }catch (\Throwable $exception){
  135. DB::rollBack();
  136. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  137. }
  138. return [true, ''];
  139. }
  140. public function makeData($equipment_id, $team_id,$employee_id,$message){
  141. $arr = [];
  142. if(! empty($equipment_id)){
  143. foreach ($equipment_id as $v_e){
  144. if(! empty($team_id)){
  145. foreach ($team_id as $t){
  146. if(! empty($employee_id)){
  147. foreach ($employee_id as $e){
  148. $arr[] = [
  149. 'equipment_id' => $v_e,
  150. 'team_id' => $t,
  151. 'employee_id' => $e,
  152. 'order_product_id' => $message['order_product_id'],
  153. 'dispatch_no' => $message['dispatch_no'],
  154. ];
  155. }
  156. }else{
  157. $arr[] = [
  158. 'equipment_id' => $v_e,
  159. 'team_id' => $t,
  160. 'order_product_id' => $message['order_product_id'],
  161. 'dispatch_no' => $message['dispatch_no'],
  162. ];
  163. }
  164. }
  165. }elseif(! empty($employee_id)){
  166. foreach ($employee_id as $e){
  167. $arr[] = [
  168. 'equipment_id' => $v_e,
  169. 'employee_id' => $e,
  170. 'order_product_id' => $message['order_product_id'],
  171. 'dispatch_no' => $message['dispatch_no'],
  172. ];
  173. }
  174. }else{
  175. $arr[] = [
  176. 'equipment_id' => $v_e,
  177. 'order_product_id' => $message['order_product_id'],
  178. 'dispatch_no' => $message['dispatch_no'],
  179. ];
  180. }
  181. }
  182. }elseif (! empty($team_id)){
  183. foreach ($team_id as $t){
  184. if(! empty($employee_id)){
  185. foreach ($employee_id as $e){
  186. $arr[] = [
  187. 'team_id' => $t,
  188. 'employee_id' => $e,
  189. 'order_product_id' => $message['order_product_id'],
  190. 'dispatch_no' => $message['dispatch_no'],
  191. ];
  192. }
  193. }else{
  194. $arr[] = [
  195. 'team_id' => $t,
  196. 'order_product_id' => $message['order_product_id'],
  197. 'dispatch_no' => $message['dispatch_no'],
  198. ];
  199. }
  200. }
  201. }elseif(! empty($employee_id)){
  202. foreach ($employee_id as $e){
  203. $arr[] = [
  204. 'employee_id' => $e,
  205. 'order_product_id' => $message['order_product_id'],
  206. 'dispatch_no' => $message['dispatch_no'],
  207. ];
  208. }
  209. }
  210. return $arr;
  211. }
  212. public function insertDispatch($msg, $data, $user, $time){
  213. //生产数据的源数据
  214. $result = $msg;
  215. $dispatch_no = $this->setOrderNO();
  216. if(! $dispatch_no) return [false,'单号生成失败!'];
  217. //主表
  218. Dispatch::insert(['dispatch_no' => $dispatch_no,'crt_time' => $time]);
  219. $time_tmp = date("Ymd", $data['out_order_no_time'][0]);
  220. $process_model = new OrdersProductProcess(['channel' => $time_tmp]);
  221. //是否自动审核
  222. $em = new EmployeeService();
  223. $auto = $em->is_auto($user, "pg_auto");
  224. foreach ($result as $key => $value){
  225. $result[$key]['dispatch_no'] = $dispatch_no;
  226. $result[$key]['crt_time'] = $time;
  227. $result[$key]['crt_id'] = $user['id'];
  228. $result[$key]['status'] = $auto;
  229. $dispatch_quantity = $value['dispatch_quantity'] * 1000;
  230. $process_model->where('order_product_id',$value['order_product_id'])
  231. ->where('process_id',$value['process_id'])
  232. ->where('dispatch_no','')
  233. ->take($dispatch_quantity)
  234. ->update([
  235. 'dispatch_no' => $dispatch_no,
  236. 'status' => 1
  237. ]);
  238. }
  239. DispatchSub::insert($result);
  240. //获取上一次插入订单的所有id
  241. $last_insert_id = DispatchSub::where('dispatch_no',$dispatch_no)
  242. ->where('crt_time',$time)
  243. ->where('crt_id',$user['id'])
  244. ->select('id','dispatch_quantity as quantity',"product_no","order_product_id","product_no",'technology_name','crt_time')
  245. ->orderBy('order_product_id')
  246. ->get()->toArray();
  247. return [true, $last_insert_id];
  248. }
  249. public function del($data){
  250. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  251. return [true,'删除成功'];
  252. }
  253. public function orderDetail($data){
  254. return [200,''];
  255. }
  256. public function is_same_month($timestamp1,$timestamp2){
  257. // 格式化时间戳为年份和月份
  258. $year1 = date('Y', $timestamp1);
  259. $month1 = date('m', $timestamp1);
  260. $year2 = date('Y', $timestamp2);
  261. $month2 = date('m', $timestamp2);
  262. if ($year1 === $year2 && $month1 === $month2) {
  263. return true;
  264. } else {
  265. return false;
  266. }
  267. }
  268. public function orderList($data){
  269. list($status,$msg) = $this->orderListRule($data);
  270. if(! $status) return [false, $msg];
  271. $model = OrdersProduct::where('del_time',0)
  272. ->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')
  273. ->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]])
  274. ->whereIn('id',$msg)
  275. ->orderBy('id','desc');
  276. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  277. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  278. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  279. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  280. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  281. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  282. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  283. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  284. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  285. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  286. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  287. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  288. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  289. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  290. 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]]);
  291. if(! empty($data['production_time'][0]) && ! empty($data['production_time'][1])) $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  292. 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]]);
  293. if(isset($data['status'])) $model->where('status',$data['status']);
  294. if(isset($data['is_create'])) {
  295. if($data['is_create']){
  296. $model->whereColumn('dispatch_complete_quantity', '>=', 'production_quantity');
  297. }else{
  298. $model->whereColumn('production_quantity', '>', 'dispatch_complete_quantity');
  299. }
  300. }
  301. $list = $this->limit($model,'',$data);
  302. $list = $this->fillData($list);
  303. return [true,$list];
  304. }
  305. public function orderListRule($data){
  306. // if(empty($data['process_id'])) return [false, '工序必须选择!'];
  307. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期必须选择'];
  308. $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
  309. if(! $bool) return [false,'制单日期必须同月!'];
  310. $time = date("Ymd",$data['out_order_no_time'][0]);
  311. $process_model = new OrdersProductProcess(['channel' => $time]);
  312. if(! $process_model->is_table_isset()) return [true, []];//不存在process子表 返回空数据
  313. $process_array = $process_model->where('del_time',0)
  314. // ->where('process_id',$data['process_id'])
  315. ->distinct()
  316. ->select('order_product_id')
  317. ->get()->toArray();
  318. $order_product_id = array_column($process_array,'order_product_id');
  319. if(empty($order_product_id)) return [true,[]];//process子表无数据 返回空数据
  320. return [true, $order_product_id];
  321. }
  322. public function orderRule($data){
  323. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单时间不能为空!'];
  324. if(! isset($data['is_split'])) return [false,'是否拆分标识不能为空!'];
  325. if(empty($data['detail'])) return [false, '派工明细数据不能为空!'];
  326. $id = [];
  327. $detail_map = $detail_2 = [];
  328. $device_map = Equipment::where('del_time',0)
  329. ->pluck('title','id')
  330. ->toArray();
  331. foreach ($data['detail'] as $value){
  332. if(empty($value['id'])) return [false,'请选择派工数据!'];
  333. if(empty($value['process_id'])) return [false,'工序不能为空!'];
  334. if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'数量不能小于0!'];
  335. if(empty($value['dispatch_time'][0]) || empty($value['dispatch_time'][1])) return [false,'计划生产时间不能为空!'];
  336. if(! in_array($value['id'], $id)) $id[] = $value['id'];
  337. $key = $value['id'] . $value['process_id'];
  338. if(isset($detail_map[$key])){
  339. $detail_map[$key] += $value['quantity'];
  340. }else{
  341. $detail_map[$key] = $value['quantity'];
  342. }
  343. if(isset($detail_2[$value['id']][$value['process_id']])){
  344. $detail_2[$value['id']][$value['process_id']] += $value['quantity'];
  345. }else{
  346. $detail_2[$value['id']][$value['process_id']] = $value['quantity'];
  347. }
  348. if(! empty($value['device_id']) && ! isset($device_map[$value['device_id']])) return [false, '设备不存在或已被删除'];
  349. }
  350. $result = OrdersProduct::whereIn('id',$id)
  351. ->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')
  352. ->orderBy('id','desc')
  353. ->get()->toArray();
  354. $result_map = array_column($result,null,'order_product_id');
  355. //每个生产订单所有的工序
  356. $process_map = $this->getProcess($result);
  357. //已派工数据
  358. $map = $this->getDispatchQuantity($id);
  359. //校验
  360. foreach ($result as $value){
  361. //总数量校验
  362. $detail2 = $detail_2[$value['order_product_id']] ?? [];
  363. $uniqueValuesCount = count(array_unique($detail2));
  364. if($uniqueValuesCount != 1) return [false , "生产订单号:" . $value['production_no'] . "所有工序派工数量必须相等"];
  365. //本次提交的工序
  366. // $submit_process = array_keys($detail2);
  367. //工序
  368. $tmp = $process_map[$value['order_product_id']] ?? [];
  369. foreach ($tmp as $v) {
  370. // if(! in_array($v['process_id'], $submit_process)) continue;
  371. //键值 生成订单id + 工序id
  372. $key = $value['order_product_id'] . $v['process_id'];
  373. //本次提交数量
  374. $quantity_tmp = $detail_map[$key] ?? 0;
  375. //工序已派工
  376. $q = $map[$key] ?? 0;
  377. if($q + $quantity_tmp > $value['production_quantity']) return [false, "生产订单号:" . $value['production_no'] . "的工序:" . $v['process_title'] . "的派单数量不能超过生产订单数量"];
  378. }
  379. }
  380. $return = [];
  381. foreach ($data['detail'] as $value){
  382. $tmp = $result_map[$value['id']] ?? [];
  383. unset($tmp['process_id']);unset($tmp['production_no']);
  384. $tmp['process_id'] = $value['process_id'];
  385. $tmp['team_id'] = $value['team_id'];
  386. $tmp['device_id'] = $value['device_id'];
  387. $tmp['dispatch_time_start'] = $value['dispatch_time'][0];
  388. $tmp['dispatch_time_end'] = $value['dispatch_time'][1];
  389. $tmp['dispatch_quantity'] = $value['quantity'];
  390. $return[] = $tmp;
  391. }
  392. return [true, $return];
  393. }
  394. public function fillData($data){
  395. if(empty($data['data'])) return $data;
  396. $map = $this->getDispatchQuantity(array_column($data['data'],'id'));
  397. $emp_map = Employee::whereIn('id',array_column($data['data'],'crt_id'))
  398. ->pluck('emp_name','id')
  399. ->toArray();
  400. $process_map = $this->getProcess($data['data']);
  401. foreach ($data['data'] as $key => $value){
  402. $tmp = $process_map[$value['id']] ?? [];
  403. foreach ($tmp as $t => $v) {
  404. $q = $map[$value['id'] . $v['process_id']] ?? 0;
  405. $tmp[$t]['dispatch_quantity'] = $q;
  406. $tmp[$t]['not_dispatch_quantity'] = $value['production_quantity'] - $q;
  407. }
  408. $data['data'][$key]['process'] = $tmp;
  409. $last_process = end($tmp);
  410. $dispatch_quantity = $last_process['dispatch_quantity'] ?? 0;
  411. $data['data'][$key]['dispatch_quantity'] = $dispatch_quantity;
  412. $data['data'][$key]['not_dispatch_quantity'] = $value['production_quantity'] - $dispatch_quantity;
  413. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  414. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  415. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  416. $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
  417. $data['data'][$key]['order_product_man'] = $emp_map[$value['crt_id']] ?? '';
  418. if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
  419. $data['data'][$key]['is_create'] = 1;
  420. }else{
  421. $data['data'][$key]['is_create'] = 0;
  422. }
  423. $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
  424. }
  425. $data['production_quantity'] = $this->getTotal($data['data'], 'production_quantity');
  426. $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');
  427. $data['not_dispatch_quantity'] = $this->getTotal($data['data'], 'not_dispatch_quantity');
  428. return $data;
  429. }
  430. //返回已派工数量
  431. public function getDispatchQuantity($order_product_id = []){
  432. if(empty($order_product_id)) return [];
  433. $result = DispatchSub::where('del_time',0)
  434. ->whereIn("order_product_id",$order_product_id)
  435. ->select('dispatch_quantity','order_product_id','process_id')
  436. ->get()
  437. ->toArray();
  438. $return = [];
  439. foreach ($result as $value){
  440. $key = $value['order_product_id'] . $value['process_id'];
  441. if(isset($return[$key])){
  442. $return[$key] += $value['dispatch_quantity'];
  443. }else{
  444. $return[$key] = $value['dispatch_quantity'];
  445. }
  446. }
  447. return $return;
  448. }
  449. public function getProcess($data){
  450. if(empty($data)) return [];
  451. $process_id_array = [];
  452. $process_id = array_column($data,'process_id');
  453. foreach ($process_id as $value){
  454. $tmp = explode(',',$value);
  455. foreach ($tmp as $v){
  456. if(! in_array($v,$process_id_array)) $process_id_array[] = $v;
  457. }
  458. }
  459. $process_array = Process::whereIn('id', $process_id_array)->get()->toArray();
  460. $process_map = array_column($process_array,null,'id');
  461. $return = [];
  462. foreach ($data as $value){
  463. $tmp = explode(',',$value['process_id']);
  464. foreach ($tmp as $v){
  465. $process_tmp = $process_map[$v] ?? [];
  466. if(isset($value['id'])){
  467. $return[$value['id']][] = [
  468. 'process_id' => $v,
  469. 'process_title' => $process_tmp['title'] ?? "",
  470. 'team_id' => $process_tmp['team_id'] ?? 0,
  471. 'device_id' => $process_tmp['device_id'] ?? 0,
  472. ];
  473. }else{
  474. $return[$value['order_product_id']][] = [
  475. 'process_id' => $v,
  476. 'process_title' => $process_tmp['title'] ?? "",
  477. 'team_id' => $process_tmp['team_id'] ?? 0,
  478. 'device_id' => $process_tmp['device_id'] ?? 0,
  479. ];
  480. }
  481. }
  482. }
  483. return $return;
  484. }
  485. public function dispatchOrderList($data){
  486. $model = DispatchSub::where('del_time',0)
  487. ->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','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','wg_status','status')
  488. ->orderBy('id','desc');
  489. if(isset($data['status'])) $model->where('status', $data['status']);
  490. if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);
  491. if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
  492. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  493. if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  494. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  495. if(! empty($data['production_no'])) {
  496. $id = OrdersProduct::where('del_time', 0)
  497. ->where('production_no', 'LIKE', '%'.$data['production_no'].'%')
  498. ->select('id')
  499. ->get()->toArray();
  500. $model->whereIn('order_product_id', array_column($id,'id'));
  501. }
  502. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  503. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  504. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  505. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  506. $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
  507. $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
  508. }
  509. if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
  510. if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);
  511. if(isset($data['is_finished'])) {
  512. if($data['is_finished']){
  513. $model->wherecolumn('dispatch_quantity','=','finished_num');
  514. }else{
  515. $model->wherecolumn('dispatch_quantity','>','finished_num');
  516. }
  517. }
  518. $list = $this->limit($model,'',$data);
  519. $list = $this->fillDispatchOrderListData($list);
  520. return [true,$list];
  521. }
  522. public function fillDispatchOrderListData($data){
  523. if(empty($data['data'])) return $data;
  524. $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
  525. ->pluck('title','id')
  526. ->toArray();
  527. $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
  528. ->pluck('title','id')
  529. ->toArray();
  530. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  531. ->pluck('title','id')
  532. ->toArray();
  533. $orders = OrdersProduct::whereIn('id', array_column($data['data'],'order_product_id'))
  534. ->pluck('production_no','id')
  535. ->toArray();
  536. foreach ($data['data'] as $key => $value){
  537. $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";
  538. $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
  539. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  540. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  541. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  542. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  543. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  544. $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";
  545. $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";
  546. $data['data'][$key]['equipment_id'] = $value['device_id'];
  547. $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);
  548. $data['data'][$key]['production_no'] = $orders[$value['order_product_id']] ?? '';
  549. }
  550. $total = $this->getTotal($data['data'], 'dispatch_quantity');
  551. $data['dispatch_quantity'] = $total;
  552. return $data;
  553. }
  554. //反写写派工数量(增加)
  555. public function writeDispatchQuantity($order_product_id){
  556. if(empty($order_product_id)) return;
  557. //最后一道工序
  558. $last_process = [];
  559. $process_id = OrdersProduct::whereIn('id',$order_product_id)
  560. ->select('id','process_id')
  561. ->get()->toArray();
  562. foreach ($process_id as $value){
  563. $tmp_process = explode(',', $value['process_id']);
  564. $last_process[$value['id']] = end($tmp_process);
  565. }
  566. $result = DispatchSub::where('del_time',0)
  567. ->whereIn('order_product_id',$order_product_id)
  568. ->select('dispatch_quantity','order_product_id','process_id')
  569. ->get()
  570. ->toArray();
  571. if(empty($result)) return;
  572. $new_result = [];
  573. foreach ($result as $value){
  574. //统计最后一道工序的派工数量
  575. $tmp_last_process = $last_process[$value['order_product_id']];
  576. if($tmp_last_process == $value['process_id']){
  577. if(isset($new_result[$value['order_product_id']])){
  578. $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
  579. }else{
  580. $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
  581. }
  582. }
  583. }
  584. foreach ($new_result as $order_product_id => $num){
  585. OrdersProduct::where('id',$order_product_id)->update([
  586. 'dispatch_complete_quantity' => $num
  587. ]);
  588. }
  589. }
  590. //反写写派工数量(删除)
  591. public function writeDispatchQuantityDEL($order_product_id){
  592. if(empty($order_product_id)) return;
  593. //最后一道工序
  594. $last_process = [];
  595. $process_id = OrdersProduct::whereIn('id',$order_product_id)
  596. ->select('id','process_id')
  597. ->get()->toArray();
  598. foreach ($process_id as $value){
  599. $tmp_process = explode(',', $value['process_id']);
  600. $last_process[$value['id']] = end($tmp_process);
  601. }
  602. $result = DispatchSub::where('del_time',0)
  603. ->whereIn('order_product_id',$order_product_id)
  604. ->select('dispatch_quantity','order_product_id','process_id')
  605. ->get()
  606. ->toArray();
  607. $new_result = [];
  608. foreach ($result as $value){
  609. //统计最后一道工序的派工数量
  610. $tmp_last_process = $last_process[$value['order_product_id']];
  611. if($tmp_last_process == $value['process_id']){
  612. if(isset($new_result[$value['order_product_id']])){
  613. $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
  614. }else{
  615. $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
  616. }
  617. }
  618. }
  619. foreach ($order_product_id as $value){
  620. $quantity = $new_result[$value] ?? 0;
  621. OrdersProduct::where('id',$value)->update([
  622. 'dispatch_complete_quantity' => $quantity
  623. ]);
  624. }
  625. }
  626. //设备上的去完工列表
  627. public function dispatchMobileOrderList($data){
  628. $model = DispatchSub::where('del_time',0)
  629. ->select('id','product_title','product_no','dispatch_quantity','finished_num','dispatch_no','waste_num')
  630. ->whereRaw('dispatch_quantity > finished_num')
  631. ->orderBy('id','desc');
  632. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  633. if(! empty($data['order_number'])) $model->where('dispatch_no',$data['dispatch_no']);
  634. $list = $model->get()->toArray();
  635. $list = $this->fillDispatchMobileOrderList($list);
  636. return [true,$list];
  637. }
  638. public function fillDispatchMobileOrderList($data){
  639. if(empty($data)) return $data;
  640. foreach ($data as $key => $value){
  641. $data[$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'] - $value['waste_num'];
  642. }
  643. $return['product_num'] = count($data);
  644. $return['finished_num'] = $this->getTotal($data,'finished_num');
  645. $return['un_finished_quantity'] = $this->getTotal($data,'un_finished_quantity');
  646. $return['data'] = $data;
  647. unset($data);
  648. return $return;
  649. }
  650. //设备上完工填写数据的页面
  651. public function dispatchMobileOrderDetailsList($data){
  652. if(empty($data['id'])) return [false,'派工单ID不能为空!'];
  653. $dispatch = DispatchSub::whereIn('id',$data['id'])
  654. ->where('del_time',0)
  655. ->select('id','product_title','product_no','dispatch_no',DB::raw('(dispatch_quantity - finished_num) as quantity'))
  656. ->get()->toArray();
  657. $sub = DispatchEmpSub::where('del_time',0)
  658. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  659. ->select('dispatch_no','equipment_id','team_id','employee_id')
  660. ->get()->toArray();
  661. $sub_map = [];
  662. foreach ($sub as $s){
  663. $array = [
  664. 'equipment_id' => $s['equipment_id'],
  665. 'team_id' => $s['team_id'],
  666. 'employee_id' => $s['employee_id'],
  667. ];
  668. if(empty($sub_map[$s['dispatch_no']])){
  669. $sub_map[$s['dispatch_no']][] = $array;
  670. }else{
  671. if(! in_array($array,$sub_map[$s['dispatch_no']])) {
  672. $sub_map[$s['dispatch_no']][] = $array;
  673. }
  674. }
  675. }
  676. $return_list = $return_details = [];
  677. foreach ($dispatch as $value){
  678. $dispatch_tmp = $sub_map[$value['dispatch_no']];
  679. $counts = count($dispatch_tmp) ?? 1;
  680. // 计算每个人应该得到的数量
  681. $per_person = floor($value['quantity'] / $counts);
  682. // 计算最后一个人拿到的数量
  683. $last_person = $value['quantity'] - ($per_person * ($counts - 1));
  684. foreach ($dispatch_tmp as $k => $t){
  685. $dispatch_tmp[$k]['id'] = $value['id'];
  686. $dispatch_tmp[$k]['product_title'] = $value['product_title'];
  687. $dispatch_tmp[$k]['product_no'] = $value['product_no'];
  688. $dispatch_tmp[$k]['dispatch_no'] = $value['dispatch_no'];
  689. if ($k == $counts - 1) {
  690. $dispatch_tmp[$k]['quantity'] = (int)$last_person;
  691. }else{
  692. $dispatch_tmp[$k]['quantity'] = (int)$per_person;
  693. }
  694. }
  695. //列数据
  696. $return_list = array_merge_recursive($return_list,$dispatch_tmp);
  697. //总数量
  698. $return_details[$value['id']] = $value['quantity'];
  699. }
  700. $return['list'] = $return_list;
  701. $return['list_details'] = $return_details;
  702. return [true, $return];
  703. }
  704. public function dispatchOrderForSqList($data,$user){
  705. $model = DispatchSub::where('del_time',0)
  706. ->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','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','wg_status','status',DB::raw('GROUP_CONCAT(DISTINCT dispatch_no ORDER BY dispatch_no SEPARATOR ",") as dispatch_no'))
  707. ->orderBy('id','desc')
  708. ->groupBy('order_product_id','crt_time');
  709. if(isset($data['status'])) $model->where('status', $data['status']);
  710. if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);
  711. if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
  712. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  713. if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  714. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  715. if(! empty($data['production_no'])) {
  716. $id = OrdersProduct::where('del_time', 0)
  717. ->where('production_no', 'LIKE', '%'.$data['production_no'].'%')
  718. ->select('id')
  719. ->get()->toArray();
  720. $model->whereIn('order_product_id', array_column($id,'id'));
  721. }
  722. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  723. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  724. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  725. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  726. $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
  727. $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
  728. }
  729. if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
  730. if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);
  731. if(isset($data['is_finished'])) {
  732. if($data['is_finished']){
  733. $model->wherecolumn('dispatch_quantity','=','finished_num');
  734. }else{
  735. $model->wherecolumn('dispatch_quantity','>','finished_num');
  736. }
  737. }
  738. $list = $this->limit($model,'',$data);
  739. $list = $this->fillDispatchOrderForSqListData($list,$data,$user);
  740. return [true,$list];
  741. }
  742. public function fillDispatchOrderForSqListData($data,$erg,$user){
  743. if(empty($data['data'])) return $data;
  744. $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
  745. ->pluck('title','id')
  746. ->toArray();
  747. $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
  748. ->pluck('title','id')
  749. ->toArray();
  750. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  751. ->pluck('title','id')
  752. ->toArray();
  753. $orders = OrdersProduct::whereIn('id', array_column($data['data'],'order_product_id'))
  754. ->pluck('production_no','id')
  755. ->toArray();
  756. $return = [];
  757. if(! empty($erg['material'])){
  758. $product_no = array_unique(array_column($data['data'],'product_no'));
  759. $service = new FyyOrderService();
  760. list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
  761. if($status) $return = $msg;
  762. }
  763. foreach ($data['data'] as $key => $value){
  764. $data['data'][$key]['material'] = $return[$value['product_no']] ?? [];
  765. $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";
  766. $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
  767. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  768. $data['data'][$key]['production_no'] = $orders[$value['order_product_id']] ?? '';
  769. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  770. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  771. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  772. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  773. $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";
  774. $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";
  775. $data['data'][$key]['equipment_id'] = $value['device_id'];
  776. $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);
  777. }
  778. $total = $this->getTotal($data['data'], 'dispatch_quantity');
  779. $data['dispatch_quantity'] = $total;
  780. return $data;
  781. }
  782. }