DispatchService.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. if(empty($tmp['zj_plan_id']) && in_array($value['process_id'],[12,14])){
  401. if($value['process_id'] == 12){
  402. $tmp['zj_plan_id'] = 23;
  403. }else{
  404. $tmp['zj_plan_id'] = 24;
  405. }
  406. }
  407. $tmp['team_id'] = $value['team_id'];
  408. $tmp['device_id'] = $value['device_id'];
  409. $tmp['dispatch_time_start'] = $value['dispatch_time'][0];
  410. $tmp['dispatch_time_end'] = $value['dispatch_time'][1];
  411. $tmp['dispatch_quantity'] = $value['quantity'];
  412. $return[] = $tmp;
  413. }
  414. return [true, $return];
  415. }
  416. public function fillData($data){
  417. if(empty($data['data'])) return $data;
  418. $map = $this->getDispatchQuantity(array_column($data['data'],'id'));
  419. $emp_map = Employee::whereIn('id',array_column($data['data'],'crt_id'))
  420. ->pluck('emp_name','id')
  421. ->toArray();
  422. $tech = Technology::where('id',9)->first();
  423. $process_id_str = "";
  424. if(! empty($tech)) $process_id_str = $tech->process_id;
  425. $process_map = $this->getProcess($data['data']);
  426. foreach ($data['data'] as $key => $value){
  427. $tmp = $process_map[$value['id']] ?? [];
  428. if(! empty($process_id_str)) $tmp = $this->forSort($process_id_str, $tmp);
  429. foreach ($tmp as $t => $v) {
  430. $q = $map[$value['id'] . $v['process_id']] ?? 0;
  431. $tmp[$t]['dispatch_quantity'] = $q;
  432. $q_s = bcsub($value['production_quantity'], $q,3);
  433. $tmp[$t]['not_dispatch_quantity'] = $q_s >= 0 ? $q_s : 0;
  434. }
  435. $data['data'][$key]['process'] = $tmp;
  436. $last_process = end($tmp);
  437. $dispatch_quantity = $last_process['dispatch_quantity'] ?? 0;
  438. $data['data'][$key]['dispatch_quantity'] = $dispatch_quantity;
  439. $not_dispatch_quantity = bcsub($value['production_quantity'], $dispatch_quantity,3);
  440. $data['data'][$key]['not_dispatch_quantity'] = $not_dispatch_quantity >= 0 ? $not_dispatch_quantity : 0;
  441. $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  442. $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
  443. $data['data'][$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  444. $data['data'][$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
  445. $data['data'][$key]['order_product_man'] = $emp_map[$value['crt_id']] ?? '';
  446. if($value['dispatch_complete_quantity'] >= $value['production_quantity']){
  447. $data['data'][$key]['is_create'] = 1;
  448. }else{
  449. $data['data'][$key]['is_create'] = 0;
  450. }
  451. $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
  452. }
  453. $data['production_quantity'] = $this->getTotal($data['data'], 'production_quantity');
  454. $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');
  455. $data['not_dispatch_quantity'] = $this->getTotal($data['data'], 'not_dispatch_quantity');
  456. return $data;
  457. }
  458. private function forSort($order, $array){
  459. $order = explode(',',$order);
  460. // 构建一个值到索引的映射,用于比较函数
  461. $priority = array_flip($order);
  462. // 使用 usort 自定义排序
  463. usort($array, function($a, $b) use ($priority) {
  464. $aKey = $a['process_id'];
  465. $bKey = $b['process_id'];
  466. // 如果某个 process_id 不在优先级列表中,可以放在最后
  467. $aPos = isset($priority[$aKey]) ? $priority[$aKey] : count($priority);
  468. $bPos = isset($priority[$bKey]) ? $priority[$bKey] : count($priority);
  469. return $aPos <=> $bPos;
  470. });
  471. return $array;
  472. }
  473. //返回已派工数量
  474. public function getDispatchQuantity($order_product_id = []){
  475. if(empty($order_product_id)) return [];
  476. $result = DispatchSub::where('del_time',0)
  477. ->whereIn("order_product_id",$order_product_id)
  478. ->select('dispatch_quantity','order_product_id','process_id')
  479. ->get()
  480. ->toArray();
  481. $return = [];
  482. foreach ($result as $value){
  483. $key = $value['order_product_id'] . $value['process_id'];
  484. if(isset($return[$key])){
  485. $tmp = bcadd($return[$key],$value['dispatch_quantity'],3);
  486. $return[$key] = $tmp;
  487. }else{
  488. $return[$key] = $value['dispatch_quantity'];
  489. }
  490. }
  491. return $return;
  492. }
  493. public function getProcess($data){
  494. if(empty($data)) return [];
  495. $process_id_array = [];
  496. $process_id = array_column($data,'process_id');
  497. foreach ($process_id as $value){
  498. $tmp = explode(',',$value);
  499. foreach ($tmp as $v){
  500. if(! in_array($v,$process_id_array)) $process_id_array[] = $v;
  501. }
  502. }
  503. $process_array = Process::whereIn('id', $process_id_array)->get()->toArray();
  504. $process_map = array_column($process_array,null,'id');
  505. $return = [];
  506. foreach ($data as $value){
  507. $tmp = explode(',',$value['process_id']);
  508. foreach ($tmp as $v){
  509. $process_tmp = $process_map[$v] ?? [];
  510. if(isset($value['id'])){
  511. $return[$value['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. }else{
  518. $return[$value['order_product_id']][] = [
  519. 'process_id' => $v,
  520. 'process_title' => $process_tmp['title'] ?? "",
  521. 'team_id' => $process_tmp['team_id'] ?? 0,
  522. 'device_id' => $process_tmp['device_id'] ?? 0,
  523. ];
  524. }
  525. }
  526. }
  527. return $return;
  528. }
  529. public function dispatchOrderList($data){
  530. $model = DispatchSub::where('del_time',0)
  531. ->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')
  532. ->orderBy('id','desc');
  533. if(isset($data['type'])) $model->where('type', $data['type']);
  534. if(isset($data['status'])) $model->where('status', $data['status']);
  535. if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);
  536. if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
  537. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  538. if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  539. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  540. if(! empty($data['production_no'])) {
  541. $id = OrdersProduct::where('del_time', 0)
  542. ->where('production_no', 'LIKE', '%'.$data['production_no'].'%')
  543. ->select('id')
  544. ->get()->toArray();
  545. $model->whereIn('order_product_id', array_column($id,'id'));
  546. }
  547. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  548. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  549. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  550. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  551. $model->where('dispatch_time_start','>=',$data['dispatch_time'][0]);
  552. $model->where('dispatch_time_end','<=',$data['dispatch_time'][1]);
  553. }
  554. if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
  555. if(! empty($data['device_id'])) $model->where('device_id',$data['device_id']);
  556. if(! empty($data['equipment_id'])) $model->where('device_id',$data['equipment_id']);
  557. if(isset($data['is_finished'])) {
  558. if($data['is_finished']){
  559. $model->wherecolumn('dispatch_quantity','=','finished_num');
  560. }else{
  561. $model->wherecolumn('dispatch_quantity','>','finished_num');
  562. }
  563. }
  564. if(! empty($data['employee_id'])) {
  565. $team = EmployeeTeamPermission::where('employee_id',$data['employee_id'])
  566. ->select('team_id')
  567. ->get()->toArray();
  568. $model->whereIn('team_id',array_unique(array_column($team,'team_id')));
  569. }
  570. if(! empty($data['dispatch_id'])){
  571. $id = explode(',',$data['dispatch_id']);
  572. $model->whereIn('id', $id);
  573. }
  574. $list = $this->limit($model,'',$data);
  575. $list = $this->fillDispatchOrderListData($list);
  576. return [true,$list];
  577. }
  578. public function fillDispatchOrderListData($data){
  579. if(empty($data['data'])) return $data;
  580. $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
  581. ->pluck('title','id')
  582. ->toArray();
  583. $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
  584. ->pluck('title','id')
  585. ->toArray();
  586. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  587. ->pluck('title','id')
  588. ->toArray();
  589. $orders = OrdersProduct::whereIn('id', array_column($data['data'],'order_product_id'))
  590. ->pluck('production_no','id')
  591. ->toArray();
  592. $map = ZjPlan::where('del_time',0)->pluck('title','id')->toArray();
  593. foreach ($data['data'] as $key => $value){
  594. $data['data'][$key]['zj_plan_title'] = $map[$value['zj_plan_id']] ?? "";
  595. $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";
  596. $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
  597. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  598. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  599. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  600. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  601. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  602. $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";
  603. $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";
  604. $data['data'][$key]['equipment_id'] = $value['device_id'];
  605. $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);
  606. $data['data'][$key]['production_no'] = $orders[$value['order_product_id']] ?? '';
  607. }
  608. $total = $this->getTotal($data['data'], 'dispatch_quantity');
  609. $data['dispatch_quantity'] = $total;
  610. return $data;
  611. }
  612. //反写写派工数量(增加)
  613. public function writeDispatchQuantity($order_product_id){
  614. if(empty($order_product_id)) return;
  615. //最后一道工序
  616. $last_process = [];
  617. $process_id = OrdersProduct::whereIn('id',$order_product_id)
  618. ->select('id','process_id')
  619. ->get()->toArray();
  620. foreach ($process_id as $value){
  621. $tmp_process = explode(',', $value['process_id']);
  622. $last_process[$value['id']] = end($tmp_process);
  623. }
  624. $result = DispatchSub::where('del_time',0)
  625. ->whereIn('order_product_id',$order_product_id)
  626. ->select('dispatch_quantity','order_product_id','process_id')
  627. ->get()
  628. ->toArray();
  629. if(empty($result)) return;
  630. $new_result = [];
  631. foreach ($result as $value){
  632. //统计最后一道工序的派工数量
  633. $tmp_last_process = $last_process[$value['order_product_id']];
  634. if($tmp_last_process == $value['process_id']){
  635. if(isset($new_result[$value['order_product_id']])){
  636. $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
  637. }else{
  638. $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
  639. }
  640. }
  641. }
  642. foreach ($new_result as $order_product_id => $num){
  643. OrdersProduct::where('id',$order_product_id)->update([
  644. 'dispatch_complete_quantity' => $num
  645. ]);
  646. }
  647. }
  648. //反写写派工数量(删除)
  649. public function writeDispatchQuantityDEL($order_product_id){
  650. if(empty($order_product_id)) return;
  651. //最后一道工序
  652. $last_process = [];
  653. $process_id = OrdersProduct::whereIn('id',$order_product_id)
  654. ->select('id','process_id')
  655. ->get()->toArray();
  656. foreach ($process_id as $value){
  657. $tmp_process = explode(',', $value['process_id']);
  658. $last_process[$value['id']] = end($tmp_process);
  659. }
  660. $result = DispatchSub::where('del_time',0)
  661. ->whereIn('order_product_id',$order_product_id)
  662. ->select('dispatch_quantity','order_product_id','process_id')
  663. ->get()
  664. ->toArray();
  665. $new_result = [];
  666. foreach ($result as $value){
  667. //统计最后一道工序的派工数量
  668. $tmp_last_process = $last_process[$value['order_product_id']];
  669. if($tmp_last_process == $value['process_id']){
  670. if(isset($new_result[$value['order_product_id']])){
  671. $new_result[$value['order_product_id']] += $value['dispatch_quantity'];
  672. }else{
  673. $new_result[$value['order_product_id']] = $value['dispatch_quantity'];
  674. }
  675. }
  676. }
  677. foreach ($order_product_id as $value){
  678. $quantity = $new_result[$value] ?? 0;
  679. OrdersProduct::where('id',$value)->update([
  680. 'dispatch_complete_quantity' => $quantity
  681. ]);
  682. }
  683. }
  684. //设备上的去完工列表
  685. public function dispatchMobileOrderList($data){
  686. $model = DispatchSub::where('del_time',0)
  687. ->select('id','product_title','product_no','dispatch_quantity','finished_num','dispatch_no','waste_num')
  688. ->whereRaw('dispatch_quantity > finished_num')
  689. ->orderBy('id','desc');
  690. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  691. if(! empty($data['order_number'])) $model->where('dispatch_no',$data['dispatch_no']);
  692. $list = $model->get()->toArray();
  693. $list = $this->fillDispatchMobileOrderList($list);
  694. return [true,$list];
  695. }
  696. public function fillDispatchMobileOrderList($data){
  697. if(empty($data)) return $data;
  698. foreach ($data as $key => $value){
  699. $data[$key]['un_finished_quantity'] = $value['dispatch_quantity'] - $value['finished_num'] - $value['waste_num'];
  700. }
  701. $return['product_num'] = count($data);
  702. $return['finished_num'] = $this->getTotal($data,'finished_num');
  703. $return['un_finished_quantity'] = $this->getTotal($data,'un_finished_quantity');
  704. $return['data'] = $data;
  705. unset($data);
  706. return $return;
  707. }
  708. //设备上完工填写数据的页面
  709. public function dispatchMobileOrderDetailsList($data){
  710. if(empty($data['id'])) return [false,'派工单ID不能为空!'];
  711. $dispatch = DispatchSub::whereIn('id',$data['id'])
  712. ->where('del_time',0)
  713. ->select('id','product_title','product_no','dispatch_no',DB::raw('(dispatch_quantity - finished_num) as quantity'))
  714. ->get()->toArray();
  715. $sub = DispatchEmpSub::where('del_time',0)
  716. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  717. ->select('dispatch_no','equipment_id','team_id','employee_id')
  718. ->get()->toArray();
  719. $sub_map = [];
  720. foreach ($sub as $s){
  721. $array = [
  722. 'equipment_id' => $s['equipment_id'],
  723. 'team_id' => $s['team_id'],
  724. 'employee_id' => $s['employee_id'],
  725. ];
  726. if(empty($sub_map[$s['dispatch_no']])){
  727. $sub_map[$s['dispatch_no']][] = $array;
  728. }else{
  729. if(! in_array($array,$sub_map[$s['dispatch_no']])) {
  730. $sub_map[$s['dispatch_no']][] = $array;
  731. }
  732. }
  733. }
  734. $return_list = $return_details = [];
  735. foreach ($dispatch as $value){
  736. $dispatch_tmp = $sub_map[$value['dispatch_no']];
  737. $counts = count($dispatch_tmp) ?? 1;
  738. // 计算每个人应该得到的数量
  739. $per_person = floor($value['quantity'] / $counts);
  740. // 计算最后一个人拿到的数量
  741. $last_person = $value['quantity'] - ($per_person * ($counts - 1));
  742. foreach ($dispatch_tmp as $k => $t){
  743. $dispatch_tmp[$k]['id'] = $value['id'];
  744. $dispatch_tmp[$k]['product_title'] = $value['product_title'];
  745. $dispatch_tmp[$k]['product_no'] = $value['product_no'];
  746. $dispatch_tmp[$k]['dispatch_no'] = $value['dispatch_no'];
  747. if ($k == $counts - 1) {
  748. $dispatch_tmp[$k]['quantity'] = (int)$last_person;
  749. }else{
  750. $dispatch_tmp[$k]['quantity'] = (int)$per_person;
  751. }
  752. }
  753. //列数据
  754. $return_list = array_merge_recursive($return_list,$dispatch_tmp);
  755. //总数量
  756. $return_details[$value['id']] = $value['quantity'];
  757. }
  758. $return['list'] = $return_list;
  759. $return['list_details'] = $return_details;
  760. return [true, $return];
  761. }
  762. public function dispatchOrderForSqList($data,$user){
  763. $model = DispatchSub::where('del_time',0)
  764. ->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'))
  765. ->orderBy('id','desc')
  766. ->groupBy('order_product_id','crt_time');
  767. if(isset($data['status'])) $model->where('status', $data['status']);
  768. if(isset($data['wg_status'])) $model->where('wg_status', $data['wg_status']);
  769. if(isset($data['finished_num'])) $model->where('finished_num', '>',0);
  770. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  771. if(! empty($data['dispatch_no'])) $model->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%');
  772. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  773. if(! empty($data['production_no'])) {
  774. $id = OrdersProduct::where('del_time', 0)
  775. ->where('production_no', 'LIKE', '%'.$data['production_no'].'%')
  776. ->select('id')
  777. ->get()->toArray();
  778. $model->whereIn('order_product_id', array_column($id,'id'));
  779. }
  780. if(! empty($data['process_id'])) $model->where('process_id',$data['process_id']);
  781. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  782. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  783. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])){
  784. $model->where('dispatch_time_start','<=',$data['dispatch_time'][0]);
  785. $model->where('dispatch_time_end','>=',$data['dispatch_time'][1]);
  786. }
  787. if(! empty($data['team_id'])) $model->where('team_id',$data['team_id']);
  788. if(! empty($data['device_id'])) $model->whereIn('device_id',$data['device_id']);
  789. if(isset($data['is_finished'])) {
  790. if($data['is_finished']){
  791. $model->wherecolumn('dispatch_quantity','=','finished_num');
  792. }else{
  793. $model->wherecolumn('dispatch_quantity','>','finished_num');
  794. }
  795. }
  796. $list = $this->limit($model,'',$data);
  797. $list = $this->fillDispatchOrderForSqListData($list,$data,$user);
  798. return [true,$list];
  799. }
  800. public function fillDispatchOrderForSqListData($data,$erg,$user){
  801. if(empty($data['data'])) return $data;
  802. $team_map = Team::whereIn('id',array_unique(array_column($data['data'],'team_id')))
  803. ->pluck('title','id')
  804. ->toArray();
  805. $equipment_map = Equipment::whereIn('id',array_unique(array_column($data['data'],'device_id')))
  806. ->pluck('title','id')
  807. ->toArray();
  808. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  809. ->pluck('title','id')
  810. ->toArray();
  811. $orders = OrdersProduct::whereIn('id', array_column($data['data'],'order_product_id'))
  812. ->pluck('production_no','id')
  813. ->toArray();
  814. $return = [];
  815. if(! empty($erg['material'])){
  816. $product_no = array_unique(array_column($data['data'],'product_no'));
  817. $service = new FyyOrderService();
  818. list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no, 'type' => 1], $user);
  819. if($status) $return = $msg;
  820. }
  821. foreach ($data['data'] as $key => $value){
  822. $data['data'][$key]['material'] = $return[$value['product_no']] ?? [];
  823. $data['data'][$key]['wg_status_title'] = DispatchSub::$status_name[$value['wg_status']] ?? "";
  824. $data['data'][$key]['status_title'] = DispatchSub::$status_name[$value['status']] ?? "";
  825. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  826. $data['data'][$key]['production_no'] = $orders[$value['order_product_id']] ?? '';
  827. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  828. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  829. $data['data'][$key]['dispatch_time'] = $time1 . ' ' . $time2;
  830. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  831. $data['data'][$key]['team_name'] = $team_map[$value['team_id']] ?? "";
  832. $data['data'][$key]['equipment_name'] = $equipment_map[$value['device_id']] ?? "";
  833. $data['data'][$key]['equipment_id'] = $value['device_id'];
  834. $data['data'][$key]['un_finished_quantity'] = bcsub($value['dispatch_quantity'] , $value['finished_num'],3);
  835. }
  836. $total = $this->getTotal($data['data'], 'dispatch_quantity');
  837. $data['dispatch_quantity'] = $total;
  838. return $data;
  839. }
  840. }