FinishedOrderService.php 47 KB

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