FinishedOrderService.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\DispatchSub;
  5. use App\Model\Employee;
  6. use App\Model\EmployeeTeamPermission;
  7. use App\Model\Equipment;
  8. use App\Model\FinishedOrder;
  9. use App\Model\FinishedOrderScrapp;
  10. use App\Model\FinishedOrderSub;
  11. use App\Model\OrdersProduct;
  12. use App\Model\OrdersProductProcess;
  13. use App\Model\Process;
  14. use App\Model\SaleOrdersProduct;
  15. use App\Model\Scrapp;
  16. use App\Model\ScrappCount;
  17. use App\Model\Team;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Redis;
  20. class FinishedOrderService extends Service
  21. {
  22. public function edit($data){}
  23. public function setOrderNO(){
  24. $str = date('Ymd',time());
  25. $order_number = FinishedOrder::where('finished_no','Like','%'. $str . '%')
  26. ->max('finished_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. $user = [
  45. 'id' => $user['id'],
  46. 'operate_time' => time(),
  47. 'zt' => Request()->header('zt'),
  48. ];
  49. $redis = [
  50. 'result' => $msg,
  51. 'data' => $data,
  52. ];
  53. $job = ProcessDataJob::dispatch($redis,$user,1)->onQueue('finished_operation');
  54. if(! $job){
  55. return [false,'任务没有进入队列!'];
  56. }
  57. //错误计数
  58. Redis::hSet('order_failures', md5(json_encode($redis)), 0);
  59. //标记进入队列的数据
  60. DispatchSub::whereIn('id',$data['id'])->update([
  61. 'job_status' => 1,
  62. ]);
  63. return [true,'任务已进入队列!'];
  64. }
  65. public function addInJob($result,$data,$user){
  66. try{
  67. //用友数据插入------------
  68. $insert_sql_server = [];
  69. foreach ($result as $key => $value){
  70. $quantity_tmp = $data['quantity'][$key];
  71. $result[$key]['quantity'] = $quantity_tmp;
  72. //工序表
  73. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  74. $process_id = $process_model->select('process_id')
  75. ->where('sort',$process_model->where('del_time',0)
  76. ->where('order_product_id',$value['order_product_id'])
  77. ->max('sort'))
  78. ->first();
  79. if(empty($process_id)) return [false,"未找到最后一道工序"];
  80. $process_id = $process_id->process_id;
  81. if($process_id == $value['process_id']){
  82. $insert_sql_server[] = $result[$key];
  83. }
  84. }
  85. if(! empty($insert_sql_server)){
  86. $sqlServerModel = new FyySqlServerService($user);
  87. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  88. foreach ($insert_sql_server as $value){
  89. list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
  90. if(! $status) return [false,$msg];
  91. }
  92. }
  93. //用友数据插入结束----------
  94. //本地数据更新
  95. DB::beginTransaction();
  96. $waste = [];
  97. foreach ($data['waste'] as $key => $value){
  98. $waste[$key] = array_sum(array_column($value,'num'));
  99. }
  100. $time = time();
  101. foreach ($result as $key => $value){
  102. $finished_id_tmp = $data['finish_id'][$key];
  103. $team_tmp = $data['team_id'][$key];
  104. $equipment_id_tmp = $data['equipment_id'][$key];
  105. $finished_num = $value['quantity'] + $value['finished_num'];
  106. DispatchSub::where('id',$value['id'])->update([
  107. 'finished_num' => $finished_num,
  108. 'waste_num' => $waste[$key],
  109. 'job_status' => 0,
  110. 'dispatch_quantity' => DB::raw("dispatch_quantity + {$waste[$key]}"),//派工数量增加 派工单可以继续派送
  111. ]);
  112. $insert_waste = $insert_dispatch = $scrapp = [];
  113. if(! empty($data['waste'][$key])){
  114. foreach ($data['waste'][$key] as $v){
  115. for($i = 0 ;$i < $v['num']; $i++){
  116. $insert_waste[] = [
  117. 'order_product_id' => $value['order_product_id'],
  118. 'order_no' => $value['order_no'],
  119. 'product_no' => $value['product_no'],
  120. 'product_title' => $value['product_title'],
  121. 'process_id' => $value['process_id'],
  122. 'crt_time' => $time,
  123. 'dispatch_no' => $value['dispatch_no'],
  124. 'status' => 4,//不良品
  125. 'team_id' => $team_tmp,
  126. 'finished_id' => $finished_id_tmp,
  127. 'equipment_id' => $equipment_id_tmp,
  128. 'scrapp_id' => $v['scrapp_id']
  129. ];
  130. $insert_dispatch[] = [
  131. 'order_product_id' => $value['order_product_id'],
  132. 'out_order_no' => $value['out_order_no'],
  133. 'order_no' => $value['order_no'],
  134. 'product_no' => $value['product_no'],
  135. 'product_title' => $value['product_title'],
  136. 'process_id' => $value['process_id'],
  137. 'crt_time' => $time,
  138. 'dispatch_no' => $value['dispatch_no'],
  139. 'status' => 1, //已派工
  140. 'team_id' => 0,
  141. 'finished_id' => 0,
  142. 'equipment_id' => 0,
  143. 'scrapp_id' => 0
  144. ];
  145. }
  146. $scrapp[] = [
  147. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  148. 'order_product_id' => $value['order_product_id'],
  149. 'out_order_no' => $value['out_order_no'],
  150. 'order_no' => $value['order_no'],
  151. 'customer_no' => $value['customer_no'],
  152. 'customer_name' => $value['customer_name'],
  153. 'product_no' => $value['product_no'],
  154. 'product_title' => $value['product_title'],
  155. 'product_size' => $value['product_size'],
  156. 'product_unit' => $value['product_unit'],
  157. 'technology_material' => $value['technology_material'],
  158. 'technology_name' => $value['technology_name'],
  159. 'wood_name' => $value['wood_name'],
  160. 'price' => $value['price'],
  161. 'process_mark' => $value['process_mark'],
  162. 'table_body_mark' => $value['table_body_mark'],
  163. 'table_header_mark' => $value['table_header_mark'],
  164. 'crt_time' => $time,
  165. 'scrapp_num' => $v['num'],
  166. 'scrapp_id' => $v['scrapp_id']
  167. ];
  168. }
  169. }
  170. //工序表
  171. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  172. $process_model->where('order_product_id',$value['order_product_id'])
  173. ->where('process_id',$value['process_id'])
  174. ->where('dispatch_no',$value['dispatch_no'])
  175. ->take($value['quantity'])
  176. ->update([
  177. 'finished_time' => $time,
  178. 'status' => 2,
  179. 'finished_id' => $finished_id_tmp,
  180. 'team_id' => $team_tmp,
  181. 'equipment_id' => $equipment_id_tmp
  182. ]);
  183. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  184. if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
  185. if(! empty($scrapp)) ScrappCount::insert($scrapp);
  186. //生产订单数量
  187. if(! empty($waste[$key])){
  188. $num = $waste[$key];
  189. OrdersProduct::where('id',$value['order_product_id'])->update([
  190. 'production_quantity' => DB::raw("production_quantity + {$num}"),
  191. 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  192. 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
  193. ]);
  194. }
  195. }
  196. //反写数量
  197. $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'));
  198. $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'));
  199. DB::commit();
  200. }catch (\Exception $e){
  201. DB::rollBack();
  202. return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
  203. }
  204. return [true,''];
  205. }
  206. public function del($data){
  207. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  208. return [true,'删除成功'];
  209. }
  210. public function orderDetail($data){
  211. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  212. $first = DispatchSub::where('id',$data['id'])->first();
  213. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$first->out_order_no_time)]);
  214. $result = $process_model->where('del_time',0)
  215. ->where('dispatch_no',$first->dispatch_no)
  216. ->where('order_product_id',$first->order_product_id)
  217. ->where('status',4)
  218. ->select(DB::raw('count(id) as num'),'scrapp_id')
  219. ->groupBy('scrapp_id')
  220. ->get()->toArray();
  221. $map = Scrapp::whereIn('id',array_column($result,'scrapp_id'))
  222. ->pluck('title','id')
  223. ->toArray();
  224. foreach ($result as $key => $value){
  225. $result[$key]['scrapp_name'] = $map[$value['scrapp_id']] ?? '';
  226. }
  227. return [true,$result];
  228. }
  229. public function is_same_month($timestamp1,$timestamp2){
  230. // 格式化时间戳为年份和月份
  231. $year1 = date('Y', $timestamp1);
  232. $month1 = date('m', $timestamp1);
  233. $year2 = date('Y', $timestamp2);
  234. $month2 = date('m', $timestamp2);
  235. if ($year1 === $year2 && $month1 === $month2) {
  236. return true;
  237. } else {
  238. return false;
  239. }
  240. }
  241. public function orderRule($data){
  242. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  243. if($this->isEmpty($data,'quantity')) return [false,'请填写完工数量!'];
  244. foreach ($data['quantity'] as $value){
  245. if(! is_numeric($value) && $value < 0) return [false,'请填写正确的完工数量!'];
  246. }
  247. if($this->isEmpty($data,'finish_id') && $this->isEmpty($data,'team_id')) return [false,'人员和班组不能都为空!'];
  248. $bool = DispatchSub::whereIn('id',$data['id'])->where('job_status',1)->exists();
  249. if($bool) return [false,'正在队列中,请不要重复操作!'];
  250. $result = DispatchSub::whereIn('id',$data['id'])
  251. ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id')
  252. ->orderBy('id','desc')
  253. ->get()->toArray();
  254. $waste = [];
  255. foreach ($data['waste'] as $key => $value){
  256. $waste[$key] = array_sum(array_column($value,'num'));
  257. }
  258. foreach ($result as $key => $value){
  259. $tmp = $waste[$key] ?? 0;
  260. $quantity_tmp = $data['quantity'][$key] + $value['finished_num'] + $value['waste_num'] + $tmp;
  261. if($quantity_tmp > $value['dispatch_quantity']) return [false,'完工数量加上损耗数量不能大于派工数量'];
  262. }
  263. return [true, $result];
  264. }
  265. public function orderList($data){
  266. $model = FinishedOrderSub::where('del_time',0)
  267. ->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','dispatch_quantity','finished_num','status','crt_id','process_id','equipment_id','team_id','dispatch_time_start','dispatch_time_end','dispatch_time','crt_time','dispatch_no')
  268. ->orderBy('upd_time','desc')
  269. ->orderBy('id','desc');
  270. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  271. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  272. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  273. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  274. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  275. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  276. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  277. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  278. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  279. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])) $model->whereBetween('dispatch_time',[$data['dispatch_time'][0],$data['dispatch_time'][1]]);
  280. if(! empty($data['employee_id'])) {
  281. $team_id = Employee::from('employee as a')
  282. ->leftJoin('employee_team_permission as b','b.employee_id','a.id')
  283. ->where('a.id', $data['employee_id'])
  284. ->select('b.team_id')
  285. ->get()->toArray();
  286. $team_id = array_column($team_id,'team_id');
  287. $model->where('team_id',$team_id ?? []);
  288. }
  289. if(isset($data['status'])) $model->where('status',$data['status']);
  290. $list = $this->limit($model,'',$data);
  291. $list = $this->fillData($list);
  292. return [true,$list];
  293. }
  294. public function fillData($data){
  295. if(empty($data['data'])) return $data;
  296. $waste_map = $waste_map_2 = [];
  297. $waste = FinishedOrderScrapp::where('del_time',0)
  298. ->whereIn('finished_order_id',array_column($data['data'],'id'))
  299. ->select('finished_order_id','num','scrapp_id')
  300. ->get()->toArray();
  301. if(! empty($waste)){
  302. foreach ($waste as $value){
  303. $waste_map[$value['finished_order_id']][] = [
  304. 'num' => $value['num'],
  305. 'scrapp_id' => $value['scrapp_id']
  306. ];
  307. if(isset($waste_map_2[$value['finished_order_id']])){
  308. $waste_map_2[$value['finished_order_id']] += $value['num'];
  309. }else{
  310. $waste_map_2[$value['finished_order_id']] = $value['num'];
  311. }
  312. }
  313. }
  314. $team = EmployeeTeamPermission::from('employee_team_permission as a')
  315. ->leftJoin('employee as b','b.id','a.employee_id')
  316. ->whereIn('a.team_id',array_column($data['data'],'team_id'))
  317. ->select('b.emp_name','a.team_id')
  318. ->get()
  319. ->toArray();
  320. $team_map = [];
  321. if(! empty($team)){
  322. foreach ($team as $value){
  323. if(isset($team_map[$value['team_id']])){
  324. $team_map[$value['team_id']] .= ','. $value['emp_name'];
  325. }else{
  326. $team_map[$value['team_id']] = $value['emp_name'];
  327. }
  328. }
  329. }
  330. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  331. ->pluck('title','id')
  332. ->toArray();
  333. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  334. ->pluck('title','id')
  335. ->toArray();
  336. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  337. ->pluck('title','id')
  338. ->toArray();
  339. foreach ($data['data'] as $key => $value){
  340. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  341. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  342. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  343. $data['data'][$key]['dispatch_plan_time'] = $time1 . ' ' . $time2;
  344. $data['data'][$key]['dispatch_time'] = $value['dispatch_time'] ? date('Y-m-d',$value['dispatch_time']) : '';
  345. $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? '';
  346. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  347. $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? '';
  348. $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? '';
  349. $data['data'][$key]['waste'] = $waste_map[$value['id']] ?? [];
  350. $data['data'][$key]['waste_quantity'] = $waste_map_2[$value['id']] ?? 0;
  351. $data['data'][$key]['not_finished_num'] = $value['dispatch_quantity'] - $value['finished_num'];
  352. }
  353. $data['finished_num'] = array_sum(array_column($data['data'], 'finished_num'));
  354. $data['dispatch_quantity'] = array_sum(array_column($data['data'], 'dispatch_quantity'));
  355. $data['waste_quantity'] = array_sum(array_column($data['data'], 'waste_quantity'));
  356. $data['not_finished_num'] = array_sum(array_column($data['data'], 'not_finished_num'));
  357. return $data;
  358. }
  359. //反写写完工数量(根据销售订单id)
  360. public function writeFinishedQuantity($sale_orders_product_id){
  361. if(empty($sale_orders_product_id)) return;
  362. $result = DispatchSub::where('del_time',0)
  363. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  364. ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id')
  365. ->groupby('sale_orders_product_id')
  366. ->pluck('finished_num','sale_orders_product_id')
  367. ->toArray();
  368. if(empty($result)) return;
  369. foreach ($result as $key => $value){
  370. SaleOrdersProduct::where('id',$key)->update([
  371. 'finished_num' => $value
  372. ]);
  373. }
  374. }
  375. //反写写完工数量(根据生产订单id)
  376. public function writeFinishedQuantityByOrdersProductId($order_product_id){
  377. if(empty($order_product_id)) return;
  378. $result = DispatchSub::where('del_time',0)
  379. ->whereIn('order_product_id',$order_product_id)
  380. ->select(DB::raw("sum(finished_num) as finished_num"),'order_product_id')
  381. ->groupby('order_product_id')
  382. ->pluck('finished_num','order_product_id')
  383. ->toArray();
  384. if(empty($result)) return;
  385. foreach ($result as $key => $value){
  386. OrdersProduct::where('id',$key)->update([
  387. 'finished_num' => $value
  388. ]);
  389. }
  390. }
  391. public function mobileAdd($data,$user){
  392. //数据校验以及填充
  393. list($status,$msg,$count_arr) = $this->orderMobileRule($data);
  394. if(!$status) return [$status,$msg];
  395. $user = [
  396. 'id' => $user['id'],
  397. 'operate_time' => time(),
  398. 'zt' => Request()->header('zt'),
  399. ];
  400. $redis = [
  401. 'result' => $msg,
  402. 'data' => $data,
  403. 'quantity_count' => $count_arr,
  404. ];
  405. $job = ProcessDataJob::dispatch($redis,$user,3)->onQueue(ProcessDataJob::job_one);
  406. if(! $job) return [false,'任务没有进入队列!'];
  407. //错误计数
  408. Redis::hSet('order_failures', md5(json_encode($redis)), 0);
  409. //标记进入队列的数据
  410. DispatchSub::whereIn('id',array_column($msg,'id'))->update([
  411. 'job_status' => 1,
  412. ]);
  413. return [true,'任务已进入队列!'];
  414. }
  415. public function addMobileInJob($result, $data, $count_arr,$user){
  416. try{
  417. //用友数据写入------------
  418. $insert_sql_server = [];
  419. foreach ($result as $key => $value){
  420. $quantity_tmp = $count_arr[$value['id']];
  421. $result[$key]['quantity'] = $quantity_tmp;
  422. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  423. $process_id = $process_model->select('process_id')
  424. ->where('sort',$process_model->where('del_time',0)
  425. ->where('order_product_id',$value['order_product_id'])
  426. ->max('sort'))
  427. ->first();
  428. if(empty($process_id)) return [false,"未找到最后一道工序"];
  429. $process_id = $process_id->process_id;
  430. if($process_id == $value['process_id']){
  431. $insert_sql_server[] = $result[$key];
  432. }
  433. }
  434. if(! empty($insert_sql_server)){
  435. $sqlServerModel = new FyySqlServerService($user);
  436. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  437. foreach ($insert_sql_server as $value){
  438. list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
  439. if(! $status) return [false,$msg];
  440. }
  441. }
  442. //用友数据写入结束------------
  443. //本地数据写入-----------
  444. DB::beginTransaction();
  445. $waste = $waste2 = [];
  446. foreach ($data as $value){
  447. if(! empty($value['waste'])){
  448. foreach ($value['waste'] as $v){
  449. if(isset($waste[$value['id']])){
  450. $waste[$value['id']] += $v['num'];
  451. $waste2[$value['order_product_id']] += $v['num'];
  452. }else{
  453. $waste[$value['id']] = $v['num'];
  454. $waste2[$value['order_product_id']] = $v['num'];
  455. }
  456. }
  457. }
  458. }
  459. $time = time();
  460. foreach ($result as $value){
  461. $finished_num = $value['quantity'] + $value['finished_num'];
  462. $watste_num = $waste[$value['id']] ?? 0;
  463. DispatchSub::where('id',$value['id'])
  464. ->update([
  465. 'finished_num' => $finished_num,
  466. 'waste_num' => $watste_num,
  467. 'job_status' => 0,
  468. 'dispatch_quantity' => DB::raw("dispatch_quantity + {$watste_num}"),//派工数量增加 派工单可以继续派送
  469. ]);
  470. //生产订单数量
  471. if(! empty($waste2[$value['order_product_id']])){
  472. $num = $waste2[$value['order_product_id']];
  473. OrdersProduct::where('id',$value['order_product_id'])->update([
  474. 'production_quantity' => DB::raw("production_quantity + {$num}"),
  475. 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  476. 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
  477. ]);
  478. }
  479. //损耗和派工 损耗统计
  480. $insert_waste = $insert_dispatch = $scrapp = [];
  481. //工序
  482. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  483. foreach ($data as $d){
  484. if($d['id'] == $value['id']){
  485. $process_model->where('order_product_id',$value['order_product_id'])
  486. ->where('process_id',$value['process_id'])
  487. ->where('dispatch_no',$value['dispatch_no'])
  488. ->take($d['quantity'])
  489. ->update([
  490. 'finished_time' => $time,
  491. 'status' => 2,
  492. 'finished_id' => $d['finished_id'],
  493. 'team_id' => $d['team_id'],
  494. 'equipment_id' => $d['equipment_id']
  495. ]);
  496. if(! empty($d['waste'])){
  497. foreach ($d['waste'] as $v){
  498. for($i = 0 ;$i < $v['num']; $i++){
  499. $insert_waste[] = [
  500. 'order_product_id' => $value['order_product_id'],
  501. 'out_order_no' => $value['out_order_no'],
  502. 'order_no' => $value['order_no'],
  503. 'product_no' => $value['product_no'],
  504. 'product_title' => $value['product_title'],
  505. 'process_id' => $value['process_id'],
  506. 'crt_time' => $time,
  507. 'dispatch_no' => $value['dispatch_no'],
  508. 'status' => 4,
  509. 'team_id' => $d['team_id'],
  510. 'finished_id' => $d['finished_id'],
  511. 'equipment_id' => $d['equipment_id'],
  512. 'scrapp_id' => $v['scrapp_id']
  513. ];
  514. $insert_dispatch[] = [
  515. 'order_product_id' => $value['order_product_id'],
  516. 'out_order_no' => $value['out_order_no'],
  517. 'order_no' => $value['order_no'],
  518. 'product_no' => $value['product_no'],
  519. 'product_title' => $value['product_title'],
  520. 'process_id' => $value['process_id'],
  521. 'crt_time' => $time,
  522. 'dispatch_no' => $value['dispatch_no'],
  523. 'status' => 1, //已派工
  524. 'team_id' => 0,
  525. 'finished_id' => 0,
  526. 'equipment_id' => 0,
  527. 'scrapp_id' => 0
  528. ];
  529. }
  530. $scrapp[] = [
  531. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  532. 'order_product_id' => $value['order_product_id'],
  533. 'out_order_no' => $value['out_order_no'],
  534. 'order_no' => $value['order_no'],
  535. 'customer_no' => $value['customer_no'],
  536. 'customer_name' => $value['customer_name'],
  537. 'product_no' => $value['product_no'],
  538. 'product_title' => $value['product_title'],
  539. 'product_size' => $value['product_size'],
  540. 'product_unit' => $value['product_unit'],
  541. 'technology_material' => $value['technology_material'],
  542. 'technology_name' => $value['technology_name'],
  543. 'wood_name' => $value['wood_name'],
  544. 'price' => $value['price'],
  545. 'process_mark' => $value['process_mark'],
  546. 'table_body_mark' => $value['table_body_mark'],
  547. 'table_header_mark' => $value['table_header_mark'],
  548. 'crt_time' => $time,
  549. 'scrapp_num' => $v['num'],
  550. 'scrapp_id' => $v['scrapp_id']
  551. ];
  552. }
  553. }
  554. }
  555. }
  556. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  557. if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
  558. if(! empty($scrapp)) ScrappCount::insert($scrapp);
  559. }
  560. //反写数量
  561. $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'));
  562. $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'));
  563. DB::commit();
  564. //本地数据写入结束-----------
  565. }catch (\Exception $e){
  566. DB::rollBack();
  567. return [false,$e->getFile() . $e->getLine() . $e->getMessage()];
  568. }
  569. return [true,''];
  570. }
  571. public function orderMobileRule($data){
  572. if(empty($data)) return [false,'数据不能为空!',''];
  573. $dispatch_id = array_unique(array_column($data,'id'));
  574. $bool = DispatchSub::whereIn('id',$dispatch_id)->where('job_status',1)->exists();
  575. if($bool) return [false,'正在队列中,请不要重复操作!',''];
  576. $post = $waste = [];
  577. foreach ($data as $value){
  578. if(! is_numeric($value['quantity']) && $value['quantity'] < 0) return [false,'请填写正确的完工数量!'];
  579. if(empty($value['finished_id']) && empty($value['team_id'])) return [false,'人员和班组必须填写一项!',''];
  580. if(isset($post[$value['id']])){
  581. $post[$value['id']] += $value['quantity'];
  582. }else{
  583. $post[$value['id']] = $value['quantity'];
  584. }
  585. if(! empty($value['waste'])){
  586. foreach ($value['waste'] as $v){
  587. if(isset($waste[$value['id']])){
  588. $waste[$value['id']] += $v['num'];
  589. }else{
  590. $waste[$value['id']] = $v['num'];
  591. }
  592. }
  593. }
  594. }
  595. $result = DispatchSub::whereIn('id',$dispatch_id)
  596. ->select('id','finished_num','dispatch_quantity','out_order_no_time','process_id','dispatch_no','order_product_id','sale_orders_product_id','order_no','product_no','product_title','price','customer_name','technology_material','technology_name','wood_name','customer_no','out_order_no','waste_num','product_size','product_unit','process_mark','table_body_mark','table_header_mark','sale_orders_product_id')
  597. ->orderBy('id','desc')
  598. ->get()->toArray();
  599. foreach ($result as $key => $value){
  600. $tmp = $waste[$value['id']] ?? 0;
  601. $quantity_tmp = $post[$value['id']] + $value['finished_num'] + $value['waste_num'] + $tmp;
  602. if($quantity_tmp > $value['dispatch_quantity']) return [false,"完工数量加上损耗数量不能大于派工数量",''];
  603. }
  604. return [true, $result, $post];
  605. }
  606. }