DispatchService.php 43 KB

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