DispatchService.php 42 KB

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