FinishedOrderService.php 34 KB

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