FinishedOrderService.php 49 KB

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