FinishedOrderService.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\Box;
  5. use App\Model\BoxDetail;
  6. use App\Model\DispatchSub;
  7. use App\Model\Employee;
  8. use App\Model\EmployeeTeamPermission;
  9. use App\Model\Equipment;
  10. use App\Model\FinishedOrder;
  11. use App\Model\FinishedOrderScrapp;
  12. use App\Model\FinishedOrderSub;
  13. use App\Model\OrdersProduct;
  14. use App\Model\OrdersProductProcess;
  15. use App\Model\Process;
  16. use App\Model\ReportWorking;
  17. use App\Model\ReportWorkingDetail;
  18. use App\Model\SaleOrdersProduct;
  19. use App\Model\Scrapp;
  20. use App\Model\ScrappCount;
  21. use App\Model\Team;
  22. use App\Service\Box\BoxService;
  23. use Illuminate\Support\Facades\DB;
  24. use Illuminate\Support\Facades\Redis;
  25. class FinishedOrderService extends Service
  26. {
  27. public function edit($data){}
  28. public function setOrderNO(){
  29. $str = date('Ymd',time());
  30. $order_number = FinishedOrder::where('finished_no','Like','%'. $str . '%')
  31. ->max('finished_no');
  32. if(empty($order_number)){
  33. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  34. $number = $str . $number;
  35. }else{
  36. $tmp = substr($order_number, -3);
  37. $tmp = $tmp + 1;
  38. //超过99999
  39. if(strlen($tmp) > 3) return '';
  40. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  41. $number = $str . $number;
  42. }
  43. return $number;
  44. }
  45. public function add($data,$user){
  46. //数据校验以及填充
  47. list($status,$msg) = $this->orderRule($data);
  48. if(!$status) return [$status,$msg];
  49. //数据源
  50. $result = $msg;
  51. $time = time();
  52. try{
  53. DB::beginTransaction();
  54. $em = new EmployeeService();
  55. $auto = $em->is_auto($user, "wg_auto");
  56. //班组下的人
  57. $team_man = [];
  58. $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray();
  59. foreach ($team_array as $v){
  60. $team_man[$v['team_id']][] = $v['employee_id'];
  61. }
  62. $last_update = $last_update2 = $str_tmp = [];
  63. foreach ($result as $value){
  64. $finished_num = $value['quantity'] + $value['finished_num'];
  65. $waste_num = $value['waste_quantity'] + $value['waste_num'];
  66. DispatchSub::where('id',$value['id'])->update([
  67. 'finished_num' => $finished_num,
  68. 'waste_num' => $waste_num,
  69. 'wg_status' => $auto,
  70. ]);
  71. $tmp = [
  72. 'id' => $value['id'],
  73. 'quantity' => $value['quantity'],
  74. 'product_no' => $value['product_no'] ?? "",
  75. 'product_title' => $value['product_title'] ?? "",
  76. 'product_size' => $value['product_size'] ?? "",
  77. 'product_unit' => $value['product_unit'] ?? "",
  78. ];
  79. $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
  80. if(! in_array($str, $str_tmp)){
  81. $last_update[] = $tmp;
  82. $str_tmp[] = $str;
  83. }
  84. //班组下的人
  85. $man = $team_man[$value['team_id']] ?? [];
  86. $f_m = 0;
  87. if(! in_array($value['finish_id'], $man) && $value['finish_id'] > 0) {
  88. $man[] = $value['finish_id'];
  89. $f_m = 1;
  90. }
  91. $tmp['process_id'] = $value['process_id'];
  92. foreach ($man as $m_v){
  93. if($f_m && $m_v == $value['finish_id']){
  94. $tmp['team_id'] = 0;
  95. }else{
  96. $tmp['team_id'] = $value['team_id'];
  97. }
  98. $tmp['finished_id'] = $m_v;
  99. $last_update2[] = $tmp;
  100. }
  101. $insert_waste = $scrapp = [];
  102. if(! empty($value['waste_array'])){
  103. foreach ($value['waste_array'] as $v){
  104. $num = $v['num'] * 1000;
  105. for($i = 0 ;$i < $num; $i++){
  106. $insert_waste[] = [
  107. 'order_product_id' => $value['order_product_id'],
  108. 'order_no' => $value['order_no'],
  109. 'product_no' => $value['product_no'],
  110. 'product_title' => $value['product_title'],
  111. 'process_id' => $value['process_id'],
  112. 'crt_time' => $time,
  113. 'dispatch_no' => $value['dispatch_no'],
  114. 'status' => 4,//不良品
  115. 'team_id' => $value['team_id'],
  116. 'finished_id' => $value['finish_id'],
  117. 'equipment_id' => $value['device_id'],
  118. 'scrapp_id' => $v['scrapp_id']
  119. ];
  120. }
  121. $scrapp[] = [
  122. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  123. 'order_product_id' => $value['order_product_id'],
  124. 'out_order_no' => $value['out_order_no'],
  125. 'order_no' => $value['order_no'],
  126. 'customer_no' => $value['customer_no'],
  127. 'customer_name' => $value['customer_name'],
  128. 'product_no' => $value['product_no'],
  129. 'product_title' => $value['product_title'],
  130. 'product_size' => $value['product_size'],
  131. 'product_unit' => $value['product_unit'],
  132. 'technology_material' => $value['technology_material'],
  133. 'technology_name' => $value['technology_name'],
  134. 'wood_name' => $value['wood_name'],
  135. 'price' => $value['price'],
  136. 'process_mark' => $value['process_mark'],
  137. 'table_body_mark' => $value['table_body_mark'],
  138. 'table_header_mark' => $value['table_header_mark'],
  139. 'crt_time' => $time,
  140. 'scrapp_num' => $v['num'],
  141. 'scrapp_id' => $v['scrapp_id'],
  142. 'team_id' => $value['team_id'],
  143. 'finished_id' => $value['finish_id'],
  144. 'equipment_id' => $value['device_id'],
  145. 'order_number' => $value['id'] . $time . $value['process_id'],
  146. 'process_id' => $value['process_id'],
  147. 'quantity' => $value['quantity'],
  148. ];
  149. }
  150. }
  151. $quantity = $value['quantity'] * 1000;
  152. //工序表
  153. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  154. $process_model->where('order_product_id',$value['order_product_id'])
  155. ->where('process_id',$value['process_id'])
  156. ->where('dispatch_no',$value['dispatch_no'])
  157. ->where('status', 1)
  158. ->take($quantity)
  159. ->update([
  160. 'finished_time' => $time,
  161. 'status' => 2,
  162. 'finished_id' => $value['finish_id'],
  163. 'team_id' => $value['team_id'],
  164. 'equipment_id' => $value['device_id'],
  165. ]);
  166. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  167. if(! empty($scrapp)) {
  168. $scrapp_model = new ScrappCount();
  169. $scrapp_model->insert($scrapp);
  170. }
  171. //
  172. // if(! empty($value['waste_quantity'])){
  173. // $num = $value['waste_quantity'];
  174. // OrdersProduct::where('id',$value['order_product_id'])->update([
  175. // 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  176. // ]);
  177. // }
  178. }
  179. //反写数量
  180. //生产订单
  181. $id = array_column($result,'order_product_id');
  182. $this->writeFinishedQuantityByOrdersProductId($id);
  183. //销售订单
  184. $this->writeFinishedQuantity($id);
  185. //生成完工入库申请单
  186. $service = new BoxService();
  187. list($status, $msg) = $service->createWGSQ($last_update, $user);
  188. if(! $status) return [false, $msg];
  189. //生成报工单
  190. list($status, $msg) = $this->createbg($last_update2, $user);
  191. if(! $status) return [false, $msg];
  192. DB::commit();
  193. }catch (\Exception $e){
  194. DB::rollBack();
  195. return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
  196. }
  197. return [true, ''];
  198. }
  199. public function createbg($insert, $user){
  200. //报工单
  201. $em = new EmployeeService();
  202. $auto = $em->is_auto($user, "wgbg_auto");
  203. $order_number = (new ApplyOrderService())->setOrderNO2();
  204. if(empty($order_number)) return [false, '报工单编号生成失败'];
  205. try {
  206. DB::beginTransaction();
  207. $time = time();
  208. $model = new ReportWorking();
  209. $model->order_number = $order_number;
  210. $model->report_time = $time;
  211. $model->mark = $data['mark']?? "";
  212. $model->crt_id = $user['id'];
  213. $model->status = $auto;
  214. $model->save();
  215. $id = $model->id;
  216. $detail_insert = [];
  217. foreach ($insert as $v){
  218. $detail_insert[] = [
  219. 'report_working_id' => $id,
  220. 'data_id' => $v['id'],
  221. 'quantity' => $v['quantity'] ?? 0,
  222. 'finished_id' => $v['finished_id'] ?? 0,
  223. 'process_id' => $v['process_id'] ?? 0,
  224. 'team_id' => $v['team_id'] ?? 0,
  225. 'crt_time' => $time,
  226. ];
  227. }
  228. ReportWorkingDetail::insert($detail_insert);
  229. DB::commit();
  230. }catch (\Throwable $exception){
  231. DB::rollBack();
  232. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  233. }
  234. return [true, ''];
  235. }
  236. //产成品入库
  237. public function U8Rdrecord10Save($package_data, $user){
  238. if(empty($package_data)) return [false, '产成品入库为空,请确认!'];
  239. try{
  240. //获取包装单信息
  241. $box = $package_data;
  242. $boxDetail = new BoxDetail(['channel'=>$box['top_order_no']]);
  243. $boxDetail = $boxDetail->where('del_time',0)
  244. ->where('order_no',$box['order_no'])
  245. ->select('id','top_id','num','ext_1','ext_2','ext_3','ext_4','ext_5','out_order_no','box_type','price')
  246. ->get()->toArray();
  247. //用友数据插入------------
  248. if(! empty($boxDetail)){
  249. $sqlServerModel = new FyySqlServerService($user);
  250. if($sqlServerModel->error) return [false, $sqlServerModel->error];
  251. $username = $sqlServerModel->getYongyouName();
  252. $box['create_name'] = $username;
  253. list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($box,$boxDetail);
  254. if(! $status) return [false, $msg];
  255. }
  256. //用友数据插入------------
  257. }catch (\Exception $e){
  258. return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];
  259. }
  260. return [true,''];
  261. }
  262. //销售发货出库
  263. public function U8Rdrecord32Save($send_data, $user){
  264. if(empty($send_data)) return [false, '销售发货出库为空,请确认!'];
  265. try{
  266. //用友数据插入------------
  267. $sqlServerModel = new FyySqlServerService($user);
  268. if($sqlServerModel->error) return [false, $sqlServerModel->error];
  269. $create_name = $sqlServerModel->getYongyouName();
  270. list($status,$msg) = $sqlServerModel->U8Rdrecord32Save($send_data,$create_name);
  271. if(! $status) return [false, $msg];
  272. //用友数据插入------------
  273. }catch (\Exception $e){
  274. return [false,$e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine()];
  275. }
  276. return [true,''];
  277. }
  278. public function addInJob($result,$data,$user){
  279. try{
  280. //获取数据库连接
  281. $database = $this->getConnectionName($user['zt']);
  282. //用友数据插入------------
  283. $insert_sql_server = [];
  284. foreach ($result as $key => $value){
  285. $quantity_tmp = $data['quantity'][$key];
  286. $result[$key]['quantity'] = $quantity_tmp;
  287. //工序表
  288. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  289. $process_model->setConnection($database);
  290. $process_id = $process_model->select('process_id')
  291. ->where('sort',$process_model->where('del_time',0)
  292. ->where('order_product_id',$value['order_product_id'])
  293. ->max('sort'))
  294. ->first();
  295. if(empty($process_id)) return [false,"未找到最后一道工序"];
  296. $process_id = $process_id->process_id;
  297. if($process_id == $value['process_id']){
  298. $insert_sql_server[] = $result[$key];
  299. }
  300. }
  301. if(! empty($insert_sql_server)){
  302. $sqlServerModel = new FyySqlServerService($user);
  303. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  304. foreach ($insert_sql_server as $value){
  305. // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
  306. // if(! $status) return [false,$msg];
  307. }
  308. }
  309. //用友数据插入结束----------
  310. //本地数据更新
  311. DB::beginTransaction();
  312. $waste = [];
  313. foreach ($data['waste'] as $key => $value){
  314. $waste[$key] = array_sum(array_column($value,'num'));
  315. }
  316. $time = time();
  317. foreach ($result as $key => $value){
  318. $finished_id_tmp = $data['finish_id'][$key];
  319. $team_tmp = $data['team_id'][$key];
  320. $equipment_id_tmp = $data['equipment_id'][$key];
  321. $finished_num = $value['quantity'] + $value['finished_num'];
  322. $model = new DispatchSub();
  323. $model->setConnection($database);
  324. $model->where('id',$value['id'])->update([
  325. 'finished_num' => $finished_num,
  326. 'waste_num' => $waste[$key],
  327. 'job_status' => 0,
  328. 'dispatch_quantity' => DB::raw("dispatch_quantity + {$waste[$key]}"),//派工数量增加 派工单可以继续派送
  329. ]);
  330. $insert_waste = $insert_dispatch = $scrapp = [];
  331. if(! empty($data['waste'][$key])){
  332. foreach ($data['waste'][$key] as $v){
  333. for($i = 0 ;$i < $v['num']; $i++){
  334. $insert_waste[] = [
  335. 'order_product_id' => $value['order_product_id'],
  336. 'order_no' => $value['order_no'],
  337. 'product_no' => $value['product_no'],
  338. 'product_title' => $value['product_title'],
  339. 'process_id' => $value['process_id'],
  340. 'crt_time' => $time,
  341. 'dispatch_no' => $value['dispatch_no'],
  342. 'status' => 4,//不良品
  343. 'team_id' => $team_tmp,
  344. 'finished_id' => $finished_id_tmp,
  345. 'equipment_id' => $equipment_id_tmp,
  346. 'scrapp_id' => $v['scrapp_id']
  347. ];
  348. $insert_dispatch[] = [
  349. 'order_product_id' => $value['order_product_id'],
  350. 'out_order_no' => $value['out_order_no'],
  351. 'order_no' => $value['order_no'],
  352. 'product_no' => $value['product_no'],
  353. 'product_title' => $value['product_title'],
  354. 'process_id' => $value['process_id'],
  355. 'crt_time' => $time,
  356. 'dispatch_no' => $value['dispatch_no'],
  357. 'status' => 1, //已派工
  358. 'team_id' => 0,
  359. 'finished_id' => 0,
  360. 'equipment_id' => 0,
  361. 'scrapp_id' => 0
  362. ];
  363. }
  364. $scrapp[] = [
  365. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  366. 'order_product_id' => $value['order_product_id'],
  367. 'out_order_no' => $value['out_order_no'],
  368. 'order_no' => $value['order_no'],
  369. 'customer_no' => $value['customer_no'],
  370. 'customer_name' => $value['customer_name'],
  371. 'product_no' => $value['product_no'],
  372. 'product_title' => $value['product_title'],
  373. 'product_size' => $value['product_size'],
  374. 'product_unit' => $value['product_unit'],
  375. 'technology_material' => $value['technology_material'],
  376. 'technology_name' => $value['technology_name'],
  377. 'wood_name' => $value['wood_name'],
  378. 'price' => $value['price'],
  379. 'process_mark' => $value['process_mark'],
  380. 'table_body_mark' => $value['table_body_mark'],
  381. 'table_header_mark' => $value['table_header_mark'],
  382. 'crt_time' => $time,
  383. 'scrapp_num' => $v['num'],
  384. 'scrapp_id' => $v['scrapp_id']
  385. ];
  386. }
  387. }
  388. //工序表
  389. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  390. $process_model->setConnection($database);
  391. $process_model->where('order_product_id',$value['order_product_id'])
  392. ->where('process_id',$value['process_id'])
  393. ->where('dispatch_no',$value['dispatch_no'])
  394. ->take($value['quantity'])
  395. ->update([
  396. 'finished_time' => $time,
  397. 'status' => 2,
  398. 'finished_id' => $finished_id_tmp,
  399. 'team_id' => $team_tmp,
  400. 'equipment_id' => $equipment_id_tmp
  401. ]);
  402. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  403. if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
  404. if(! empty($scrapp)) {
  405. $scrapp_model = new ScrappCount();
  406. $scrapp_model->setConnection($database);
  407. $scrapp_model->insert($scrapp);
  408. }
  409. //生产订单数量
  410. if(! empty($waste[$key])){
  411. $num = $waste[$key];
  412. $model2 = new OrdersProduct();
  413. $model2->setConnection($database);
  414. $model2->where('id',$value['order_product_id'])->update([
  415. 'production_quantity' => DB::raw("production_quantity + {$num}"),
  416. 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  417. 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
  418. ]);
  419. }
  420. }
  421. //反写数量
  422. // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
  423. // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
  424. DB::commit();
  425. }catch (\Exception $e){
  426. DB::rollBack();
  427. return [false,$e->getFile() . $e->getLine(). $e->getMessage()];
  428. }
  429. return [true,''];
  430. }
  431. public function del($data){
  432. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  433. return [true,'删除成功'];
  434. }
  435. public function orderDetail($data){
  436. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  437. $first = DispatchSub::where('id',$data['id'])->first();
  438. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$first->out_order_no_time)]);
  439. $result = $process_model->where('del_time',0)
  440. ->where('dispatch_no',$first->dispatch_no)
  441. ->where('order_product_id',$first->order_product_id)
  442. ->where('status',4)
  443. ->select(DB::raw('count(id) as num'),'scrapp_id')
  444. ->groupBy('scrapp_id')
  445. ->get()->toArray();
  446. $map = Scrapp::whereIn('id',array_column($result,'scrapp_id'))
  447. ->pluck('title','id')
  448. ->toArray();
  449. foreach ($result as $key => $value){
  450. $result[$key]['scrapp_name'] = $map[$value['scrapp_id']] ?? '';
  451. }
  452. return [true,$result];
  453. }
  454. public function is_same_month($timestamp1,$timestamp2){
  455. // 格式化时间戳为年份和月份
  456. $year1 = date('Y', $timestamp1);
  457. $month1 = date('m', $timestamp1);
  458. $year2 = date('Y', $timestamp2);
  459. $month2 = date('m', $timestamp2);
  460. if ($year1 === $year2 && $month1 === $month2) {
  461. return true;
  462. } else {
  463. return false;
  464. }
  465. }
  466. public function orderRule($data){
  467. if(! isset($data['is_finish_all'])) return [false, '完工类型不能为空!'];
  468. if(empty($data['detail'])) return [false, '完工明细数据不能为空!'];
  469. $detail_map = $id = $detail_map2 = $waste_map = [];
  470. //班组下的人
  471. $team_man = [];
  472. $team_array = EmployeeTeamPermission::select('employee_id', 'team_id')->get()->toArray();
  473. foreach ($team_array as $v){
  474. $team_man[$v['team_id']][] = $v['employee_id'];
  475. }
  476. foreach ($data['detail'] as $value){
  477. if(empty($value['id'])) return [false,'请选择完工数据!'];
  478. $id[] = $value['id'];
  479. if(empty($value['device_id'])) return [false, '请选择设备!'];
  480. if(empty($value['team_id'])) return [false, '班组必须选择'];
  481. if(empty($team_man[$value['team_id']])) return [false, '班组下的人不能为空,请设置'];
  482. // if(empty($value['team_id']) && empty($value['finish_id'])) return [false,'班组和人员必须选择一个!'];
  483. if(! is_numeric($value['quantity']) || $value['quantity'] < 0) return [false,'完工数量不能小于0!'];
  484. if(! empty($value['waste'])){
  485. foreach ($value['waste'] as $waste){
  486. if(empty($waste['scrapp_id'])) return [false,'请选择损耗原因!'];
  487. if(! is_numeric($value['num']) || $value['num'] < 0) return [false,'损耗数量不能小于0!'];
  488. if(isset($waste_map[$value['id']])){
  489. $waste_map[$value['id']] += $value['num'];
  490. }else{
  491. $waste_map[$value['id']] = $value['num'];
  492. }
  493. }
  494. }
  495. //完工数量
  496. if(isset($detail_map[$value['id']])){
  497. $detail_map[$value['id']] += $value['quantity'];
  498. }else{
  499. $detail_map[$value['id']] = $value['quantity'];
  500. }
  501. if(! isset($detail_map2[$value['id']])){
  502. $detail_map2[$value['id']] = $value;
  503. }
  504. }
  505. //校验
  506. $result = DispatchSub::where('del_time',0)
  507. ->whereIn('id',$id)
  508. ->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','crt_time')
  509. ->get()->toArray();
  510. if(empty($result)) return [false, '派工单不存在或已被删除!'];
  511. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))->pluck('title','id')->toArray();
  512. $search = "";$args = [];
  513. foreach ($result as $key => $value){
  514. $detail2 = $detail_map2[$value['id']] ?? [];
  515. $result[$key]['device_id'] = $detail2['device_id'] ?? 0;
  516. $result[$key]['team_id'] = $detail2['team_id'] ?? 0;
  517. $result[$key]['finish_id'] = $detail2['finish_id'] ?? 0;
  518. $result[$key]['waste_quantity'] = $waste_map[$value['id']] ?? 0;
  519. $result[$key]['waste_array'] = $detail2['waste'] ?? [];
  520. $q = $detail_map[$value['id']] ?? 0;
  521. $quantity_tmp = bcadd($q,$value['finished_num'],3);
  522. if($quantity_tmp > $value['dispatch_quantity']) {
  523. $process_tmp = $process_map[$value['process_id']] ?? "";
  524. return [false,'派工单:' . $value['dispatch_no']. '的工序' . $process_tmp .'完工数量不能大于派工数量'];
  525. }
  526. $result[$key]['quantity'] = $q;
  527. $search_key = $value['order_product_id'] . $value['crt_time'];
  528. if($data['is_finish_all'] && ! in_array($search_key , $args)){
  529. $search .= "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  530. $args[] = $search_key;
  531. }
  532. }
  533. $search = rtrim($search,'OR ');
  534. $search = "($search)";
  535. if($data['is_finish_all']){
  536. //系统帮助完工的数据
  537. $result2 = DispatchSub::where('del_time',0)
  538. ->whereNotIn('id',$id)
  539. ->whereColumn('dispatch_quantity', '>', 'finished_num')
  540. ->whereRaw($search)
  541. ->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','crt_time')
  542. ->get()->toArray();
  543. if(! empty($result2)){
  544. foreach ($result2 as $key => $value){
  545. $not_finished_num = $value['dispatch_quantity'] - $value['finished_num'];
  546. $result2[$key]['quantity'] = $not_finished_num;
  547. $result2[$key]['waste_array'] = [];
  548. $result2[$key]['waste_quantity'] = 0;
  549. $result2[$key]['device_id'] = 0;
  550. $result2[$key]['team_id'] = 0;
  551. $result2[$key]['finish_id'] = 0;
  552. }
  553. }
  554. $result = array_merge_recursive($result, $result2);
  555. }
  556. return [true, $result];
  557. }
  558. public function orderList($data){
  559. $model = FinishedOrderSub::where('del_time',0)
  560. ->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')
  561. ->orderBy('upd_time','desc')
  562. ->orderBy('id','desc');
  563. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  564. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  565. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  566. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  567. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  568. if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
  569. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  570. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  571. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  572. if(! empty($data['dispatch_time'][0]) && ! empty($data['dispatch_time'][1])) $model->whereBetween('dispatch_time',[$data['dispatch_time'][0],$data['dispatch_time'][1]]);
  573. if(! empty($data['employee_id'])) {
  574. $team_id = Employee::from('employee as a')
  575. ->leftJoin('employee_team_permission as b','b.employee_id','a.id')
  576. ->where('a.id', $data['employee_id'])
  577. ->select('b.team_id')
  578. ->get()->toArray();
  579. $team_id = array_column($team_id,'team_id');
  580. $model->where('team_id',$team_id ?? []);
  581. }
  582. if(isset($data['status'])) $model->where('status',$data['status']);
  583. $list = $this->limit($model,'',$data);
  584. $list = $this->fillData($list);
  585. return [true,$list];
  586. }
  587. public function fillData($data){
  588. if(empty($data['data'])) return $data;
  589. $waste_map = $waste_map_2 = [];
  590. $waste = FinishedOrderScrapp::where('del_time',0)
  591. ->whereIn('finished_order_id',array_column($data['data'],'id'))
  592. ->select('finished_order_id','num','scrapp_id')
  593. ->get()->toArray();
  594. if(! empty($waste)){
  595. foreach ($waste as $value){
  596. $waste_map[$value['finished_order_id']][] = [
  597. 'num' => $value['num'],
  598. 'scrapp_id' => $value['scrapp_id']
  599. ];
  600. if(isset($waste_map_2[$value['finished_order_id']])){
  601. $waste_map_2[$value['finished_order_id']] += $value['num'];
  602. }else{
  603. $waste_map_2[$value['finished_order_id']] = $value['num'];
  604. }
  605. }
  606. }
  607. $team = EmployeeTeamPermission::from('employee_team_permission as a')
  608. ->leftJoin('employee as b','b.id','a.employee_id')
  609. ->whereIn('a.team_id',array_column($data['data'],'team_id'))
  610. ->select('b.emp_name','a.team_id')
  611. ->get()
  612. ->toArray();
  613. $team_map = [];
  614. if(! empty($team)){
  615. foreach ($team as $value){
  616. if(isset($team_map[$value['team_id']])){
  617. $team_map[$value['team_id']] .= ','. $value['emp_name'];
  618. }else{
  619. $team_map[$value['team_id']] = $value['emp_name'];
  620. }
  621. }
  622. }
  623. $process_map = Process::whereIn('id',array_column($data['data'],'process_id'))
  624. ->pluck('title','id')
  625. ->toArray();
  626. $team_maps = Team::whereIn('id',array_column($data['data'],'team_id'))
  627. ->pluck('title','id')
  628. ->toArray();
  629. $equipment_map = Equipment::whereIn('id',array_column($data['data'],'equipment_id'))
  630. ->pluck('title','id')
  631. ->toArray();
  632. foreach ($data['data'] as $key => $value){
  633. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  634. $time1 = $value['dispatch_time_start'] ? date('Y-m-d',$value['dispatch_time_start']) : '';
  635. $time2 = $value['dispatch_time_end'] ? date('Y-m-d',$value['dispatch_time_end']) : '';
  636. $data['data'][$key]['dispatch_plan_time'] = $time1 . ' ' . $time2;
  637. $data['data'][$key]['dispatch_time'] = $value['dispatch_time'] ? date('Y-m-d',$value['dispatch_time']) : '';
  638. $data['data'][$key]['team_man'] = $team_map[$value['team_id']] ?? '';
  639. $data['data'][$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  640. $data['data'][$key]['team_name'] = $team_maps[$value['team_id']] ?? '';
  641. $data['data'][$key]['equipment_name'] = $equipment_map[$value['equipment_id']] ?? '';
  642. $data['data'][$key]['waste'] = $waste_map[$value['id']] ?? [];
  643. $data['data'][$key]['waste_quantity'] = $waste_map_2[$value['id']] ?? 0;
  644. $data['data'][$key]['not_finished_num'] = $value['dispatch_quantity'] - $value['finished_num'];
  645. }
  646. $data['finished_num'] = $this->getTotal($data['data'], 'finished_num');
  647. $data['dispatch_quantity'] = $this->getTotal($data['data'], 'dispatch_quantity');
  648. $data['waste_quantity'] = $this->getTotal($data['data'], 'waste_quantity');
  649. $data['not_finished_num'] = $this->getTotal($data['data'], 'not_finished_num');
  650. return $data;
  651. }
  652. //反写销售订单完工数量
  653. public function writeFinishedQuantity($orders_product_id){
  654. if(empty($orders_product_id)) return;
  655. $sale_orders_product_id = OrdersProduct::where('del_time',0)
  656. ->whereIn('id', $orders_product_id)
  657. ->select('sale_orders_product_id')
  658. ->get()->toArray();
  659. $sale_orders_product_id = array_unique(array_column($sale_orders_product_id,'sale_orders_product_id'));
  660. $production_order = OrdersProduct::where('del_time',0)
  661. ->whereIn('sale_orders_product_id', $sale_orders_product_id)
  662. ->select(DB::raw("sum(finished_num) as finished_num"),'sale_orders_product_id')
  663. ->groupby('sale_orders_product_id')
  664. ->pluck('finished_num','sale_orders_product_id')
  665. ->toArray();
  666. foreach ($production_order as $sale_orders_product_id => $finished_num){
  667. SaleOrdersProduct::where('id',$sale_orders_product_id)->update([
  668. 'finished_num' => $finished_num,
  669. ]);
  670. }
  671. }
  672. //反写生产订单完工数量
  673. public function writeFinishedQuantityByOrdersProductId($order_product_id){
  674. if(empty($order_product_id)) return;
  675. $result = DispatchSub::where('del_time',0)
  676. ->whereIn('order_product_id',$order_product_id)
  677. ->select('finished_num','order_product_id','crt_time','process_id')
  678. ->get()
  679. ->toArray();
  680. if(empty($result)) return;
  681. //生产订单
  682. $production_order = OrdersProduct::where('del_time',0)
  683. ->whereIn('id',array_unique(array_column($result,'order_product_id')))
  684. ->select('process_id','id','production_quantity')
  685. ->get()
  686. ->toArray();
  687. $judge = [];
  688. foreach ($result as $value){
  689. if(isset($judge[$value['order_product_id']][$value['process_id']])){
  690. $judge[$value['order_product_id']][$value['process_id']] += $value['finished_num'];
  691. }else{
  692. $judge[$value['order_product_id']][$value['process_id']] = $value['finished_num'];
  693. }
  694. }
  695. foreach ($production_order as $value){
  696. //生产订单下的所有工序派工单
  697. $tmp = $judge[$value['id']] ?? [];
  698. if(empty($tmp)) continue;
  699. //生产订单下的工序种类
  700. $ori_process = explode(',',$value['process_id']);
  701. $ori_process_count = count($ori_process);
  702. //生产订单下的派工单的工序种类
  703. $process = array_keys($tmp);
  704. $process_count = count($process);
  705. if($ori_process_count != $process_count) continue;
  706. //获取工序里最小的数值
  707. $min_finished_num = min($tmp);
  708. //更新完工数量
  709. OrdersProduct::where('id',$value['id'])->update([
  710. 'finished_num' => $min_finished_num
  711. ]);
  712. }
  713. }
  714. public function mobileAdd($data,$user){
  715. //数据校验以及填充
  716. list($status,$msg,$count_arr) = $this->orderMobileRule($data);
  717. if(!$status) return [$status,$msg];
  718. $user = [
  719. 'id' => $user['id'],
  720. 'operate_time' => time(),
  721. 'zt' => Request()->header('zt'),
  722. ];
  723. $redis = [
  724. 'result' => $msg,
  725. 'data' => $data,
  726. 'quantity_count' => $count_arr,
  727. ];
  728. $job = ProcessDataJob::dispatch($redis,$user,3)->onQueue(ProcessDataJob::job_one);
  729. if(! $job) return [false,'任务没有进入队列!'];
  730. //错误计数
  731. Redis::hSet('order_failures', md5(json_encode($redis)), 0);
  732. //标记进入队列的数据
  733. DispatchSub::whereIn('id',array_column($msg,'id'))->update([
  734. 'job_status' => 1,
  735. ]);
  736. return [true,'任务已进入队列!'];
  737. }
  738. public function addMobileInJob($result, $data, $count_arr,$user){
  739. try{
  740. //获取数据库连接
  741. $database = $this->getConnectionName($user['zt']);
  742. //用友数据写入------------
  743. $insert_sql_server = [];
  744. foreach ($result as $key => $value){
  745. $quantity_tmp = $count_arr[$value['id']];
  746. $result[$key]['quantity'] = $quantity_tmp;
  747. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  748. $process_model->setConnection($database);
  749. $process_id = $process_model->select('process_id')
  750. ->where('sort',$process_model->where('del_time',0)
  751. ->where('order_product_id',$value['order_product_id'])
  752. ->max('sort'))
  753. ->first();
  754. if(empty($process_id)) return [false,"未找到最后一道工序"];
  755. $process_id = $process_id->process_id;
  756. if($process_id == $value['process_id']){
  757. $insert_sql_server[] = $result[$key];
  758. }
  759. }
  760. if(! empty($insert_sql_server)){
  761. $sqlServerModel = new FyySqlServerService($user);
  762. if($sqlServerModel->error) return [false,$sqlServerModel->error];
  763. foreach ($insert_sql_server as $value){
  764. // list($status,$msg) = $sqlServerModel->U8Rdrecord10Save($value);
  765. // if(! $status) return [false,$msg];
  766. }
  767. }
  768. //用友数据写入结束------------
  769. //本地数据写入-----------
  770. DB::beginTransaction();
  771. $waste = $waste2 = [];
  772. foreach ($data as $value){
  773. if(! empty($value['waste'])){
  774. foreach ($value['waste'] as $v){
  775. if(isset($waste[$value['id']])){
  776. $waste[$value['id']] += $v['num'];
  777. $waste2[$value['order_product_id']] += $v['num'];
  778. }else{
  779. $waste[$value['id']] = $v['num'];
  780. $waste2[$value['order_product_id']] = $v['num'];
  781. }
  782. }
  783. }
  784. }
  785. $time = time();
  786. foreach ($result as $value){
  787. $finished_num = $value['quantity'] + $value['finished_num'];
  788. $watste_num = $waste[$value['id']] ?? 0;
  789. $model = new DispatchSub();
  790. $model->setConnection($database);
  791. $model->where('id',$value['id'])->update([
  792. 'finished_num' => $finished_num,
  793. 'waste_num' => $watste_num,
  794. 'job_status' => 0,
  795. 'dispatch_quantity' => DB::raw("dispatch_quantity + {$watste_num}"),//派工数量增加 派工单可以继续派送
  796. ]);
  797. //生产订单数量
  798. if(! empty($waste2[$value['order_product_id']])){
  799. $num = $waste2[$value['order_product_id']];
  800. $model2 = new OrdersProduct();
  801. $model2->setConnection($database);
  802. $model2->where('id',$value['order_product_id'])->update([
  803. 'production_quantity' => DB::raw("production_quantity + {$num}"),
  804. 'scrapp_num' => DB::raw("scrapp_num + {$num}"),
  805. 'dispatch_complete_quantity' => DB::raw("dispatch_complete_quantity + {$num}"),//已派工数量增加
  806. ]);
  807. }
  808. //损耗和派工 损耗统计
  809. $insert_waste = $insert_dispatch = $scrapp = [];
  810. //工序
  811. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  812. $process_model->setConnection($database);
  813. foreach ($data as $d){
  814. if($d['id'] == $value['id']){
  815. $process_model->where('order_product_id',$value['order_product_id'])
  816. ->where('process_id',$value['process_id'])
  817. ->where('dispatch_no',$value['dispatch_no'])
  818. ->take($d['quantity'])
  819. ->update([
  820. 'finished_time' => $time,
  821. 'status' => 2,
  822. 'finished_id' => $d['finished_id'],
  823. 'team_id' => $d['team_id'],
  824. 'equipment_id' => $d['equipment_id']
  825. ]);
  826. if(! empty($d['waste'])){
  827. foreach ($d['waste'] as $v){
  828. for($i = 0 ;$i < $v['num']; $i++){
  829. $insert_waste[] = [
  830. 'order_product_id' => $value['order_product_id'],
  831. 'out_order_no' => $value['out_order_no'],
  832. 'order_no' => $value['order_no'],
  833. 'product_no' => $value['product_no'],
  834. 'product_title' => $value['product_title'],
  835. 'process_id' => $value['process_id'],
  836. 'crt_time' => $time,
  837. 'dispatch_no' => $value['dispatch_no'],
  838. 'status' => 4,
  839. 'team_id' => $d['team_id'],
  840. 'finished_id' => $d['finished_id'],
  841. 'equipment_id' => $d['equipment_id'],
  842. 'scrapp_id' => $v['scrapp_id']
  843. ];
  844. $insert_dispatch[] = [
  845. 'order_product_id' => $value['order_product_id'],
  846. 'out_order_no' => $value['out_order_no'],
  847. 'order_no' => $value['order_no'],
  848. 'product_no' => $value['product_no'],
  849. 'product_title' => $value['product_title'],
  850. 'process_id' => $value['process_id'],
  851. 'crt_time' => $time,
  852. 'dispatch_no' => $value['dispatch_no'],
  853. 'status' => 1, //已派工
  854. 'team_id' => 0,
  855. 'finished_id' => 0,
  856. 'equipment_id' => 0,
  857. 'scrapp_id' => 0
  858. ];
  859. }
  860. $scrapp[] = [
  861. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  862. 'order_product_id' => $value['order_product_id'],
  863. 'out_order_no' => $value['out_order_no'],
  864. 'order_no' => $value['order_no'],
  865. 'customer_no' => $value['customer_no'],
  866. 'customer_name' => $value['customer_name'],
  867. 'product_no' => $value['product_no'],
  868. 'product_title' => $value['product_title'],
  869. 'product_size' => $value['product_size'],
  870. 'product_unit' => $value['product_unit'],
  871. 'technology_material' => $value['technology_material'],
  872. 'technology_name' => $value['technology_name'],
  873. 'wood_name' => $value['wood_name'],
  874. 'price' => $value['price'],
  875. 'process_mark' => $value['process_mark'],
  876. 'table_body_mark' => $value['table_body_mark'],
  877. 'table_header_mark' => $value['table_header_mark'],
  878. 'crt_time' => $time,
  879. 'scrapp_num' => $v['num'],
  880. 'scrapp_id' => $v['scrapp_id']
  881. ];
  882. }
  883. }
  884. }
  885. }
  886. if(! empty($insert_waste)) $process_model->insert($insert_waste);
  887. if(! empty($insert_dispatch)) $process_model->insert($insert_dispatch);
  888. if(! empty($scrapp)) {
  889. $scrapp_model = new ScrappCount();
  890. $scrapp_model->setConnection($database);
  891. $scrapp_model->insert($scrapp);
  892. }
  893. }
  894. //反写数量
  895. // $this->writeFinishedQuantity(array_column($result,'sale_orders_product_id'),$database);
  896. // $this->writeFinishedQuantityByOrdersProductId(array_column($result,'order_product_id'),$database);
  897. DB::commit();
  898. //本地数据写入结束-----------
  899. }catch (\Exception $e){
  900. DB::rollBack();
  901. return [false,$e->getFile() . $e->getLine() . $e->getMessage()];
  902. }
  903. return [true,''];
  904. }
  905. public function orderMobileRule($data){
  906. if(empty($data)) return [false,'数据不能为空!',''];
  907. $dispatch_id = array_unique(array_column($data,'id'));
  908. $bool = DispatchSub::whereIn('id',$dispatch_id)->where('job_status',1)->exists();
  909. if($bool) return [false,'正在队列中,请不要重复操作!',''];
  910. $post = $waste = [];
  911. foreach ($data as $value){
  912. if(! is_numeric($value['quantity']) || $value['quantity'] <= 0) return [false,'请填写正确的完工数量!',''];
  913. if(empty($value['finished_id']) && empty($value['team_id'])) return [false,'人员和班组必须填写一项!',''];
  914. if(isset($post[$value['id']])){
  915. $post[$value['id']] += $value['quantity'];
  916. }else{
  917. $post[$value['id']] = $value['quantity'];
  918. }
  919. if(! empty($value['waste'])){
  920. foreach ($value['waste'] as $v){
  921. if(isset($waste[$value['id']])){
  922. $waste[$value['id']] += $v['num'];
  923. }else{
  924. $waste[$value['id']] = $v['num'];
  925. }
  926. }
  927. }
  928. }
  929. $result = DispatchSub::whereIn('id',$dispatch_id)
  930. ->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')
  931. ->orderBy('id','desc')
  932. ->get()->toArray();
  933. foreach ($result as $key => $value){
  934. $tmp = $waste[$value['id']] ?? 0;
  935. $quantity_tmp = $post[$value['id']] + $value['finished_num'] + $value['waste_num'] + $tmp;
  936. if($quantity_tmp > $value['dispatch_quantity']) return [false,"完工数量加上损耗数量不能大于派工数量",''];
  937. }
  938. return [true, $result, $post];
  939. }
  940. public function getConnectionName($name)
  941. {
  942. $mysql = "mysql";
  943. if ($name === '001') {
  944. $mysql = "mysql_001";
  945. } elseif ($name === '002') {
  946. $mysql = "mysql_002";
  947. }
  948. return $mysql;
  949. }
  950. public function insertYy($order, $user){
  951. //包装单
  952. $package_data = json_decode($order['post'], true);
  953. if(! empty($package_data)) {
  954. //用友 ------产成品入库
  955. $service = new FinishedOrderService();
  956. list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
  957. if(! $status) return [false, $msg];
  958. }
  959. return [true, ''];
  960. }
  961. }