FinishedOrderService.php 33 KB

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