DispatchService.php 43 KB

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