DispatchService.php 41 KB

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