BoxService.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. <?php
  2. namespace App\Service\Box;
  3. use App\Model\Box;
  4. use App\Model\BoxDetail;
  5. use App\Model\DispatchSub;
  6. use App\Model\Header_ext;
  7. use App\Model\OrdersProduct;
  8. use App\Model\OrdersProductBom;
  9. use App\Model\OrdersProductProcess;
  10. use App\Model\SaleOrdersProduct;
  11. use App\Model\Team;
  12. use App\Service\DispatchService;
  13. use App\Service\FinishedOrderService;
  14. use App\Service\FyySqlServerService;
  15. use App\Service\Service;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 包装相关
  19. * @package App\Models
  20. */
  21. class BoxService extends Service
  22. {
  23. protected static $instance;
  24. protected static $box_header;
  25. protected static $box_detail_header;
  26. protected static $box_hook;
  27. public $lock_key = 'hc_box_add';
  28. public function __construct()
  29. {
  30. self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
  31. self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
  32. self::$box_hook = BoxHookService::getInstance();
  33. }
  34. /**
  35. * 包装
  36. * @param $data
  37. * @return array
  38. */
  39. public function boxIn($data,$user)
  40. {
  41. $ids = [];
  42. $sale_ids = [];
  43. $top_order_no = $data['order_no'];
  44. $box_no = isset($data['box_no']) ? $data['box_no'] : '';
  45. $transport_no = isset($data['transport_no']) ? $data['transport_no'] : '';
  46. $data = $data['param'];
  47. $key_list = [];
  48. foreach ($data as $v) {
  49. if($v['id'] < 0) $sale_ids[] = -$v['id'];
  50. else $ids[] = $v['id'];
  51. if($v['id'] < 0) $v['id'] = -$v['id'];
  52. $total = 0;
  53. foreach ($v['params'] as $vv) {
  54. $total += $vv;
  55. }
  56. $key_list[$v['id']] = [
  57. 'detail' => $v['params'],
  58. 'total' => $total,
  59. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  60. ];
  61. }
  62. $insert = [];
  63. //限制频率
  64. $limit_key = $this->lock_key;
  65. list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
  66. if(! $status) return [false, '正在生成包装数据,以及用友数据,请稍后操作!'];
  67. try{
  68. DB::beginTransaction();
  69. //数据获取
  70. $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
  71. $sale_product_list = SaleOrdersProduct::wherein('id', $sale_ids)->get()->toArray();
  72. //生产订单包装----------------
  73. $box_insert = [];
  74. foreach ($product_list as $v) {
  75. $num_list = $key_list[$v['id']];
  76. $total = $num_list['total'];
  77. $detail = $num_list['detail'];
  78. $team_id = $num_list['team_id'];
  79. $un_box_num = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
  80. if ($total > $un_box_num) {
  81. $this->dellimitingSendRequestBackgNeed($limit_key);
  82. return [false, $v['product_title'] . ' ' . $v['technology_name'] . ' 可包装的数量不足'];
  83. }
  84. $ext_1 = $v['product_no'];//产品编号
  85. $ext_2 = $v['technology_material']; //工艺材质 废弃
  86. $ext_3 = $v['technology_name'];//工艺名称 改为颜色
  87. $ext_4 = $v['wood_name'];//木皮 废弃
  88. $ext_5 = $v['process_mark'];//工艺备注 废弃
  89. $ext_8 = $v['product_title'];//产品名称
  90. $out_order_no = $insert['out_order_no'] = $v['out_order_no'];
  91. $top_id = $v['sale_orders_product_id'];
  92. foreach ($detail as $vv){
  93. $box_insert[] = [
  94. 'out_order_no' => $out_order_no,
  95. 'top_id' => $top_id,
  96. 'orders_product_id' => $v['id'],
  97. 'ext_1' => $ext_1,//产品编号
  98. 'ext_2' => $ext_2,
  99. 'ext_3' => $ext_3,//颜色
  100. 'ext_4' => $ext_4,
  101. 'ext_5' => $ext_5,
  102. 'ext_8' => $ext_8,//产品名称
  103. 'num' => $vv,
  104. 'price' => $v['price'],
  105. 'box_type' => 1,
  106. 'team_id' => $team_id,
  107. 'shipment_order_no' => $transport_no,
  108. ];
  109. }
  110. //修改销售订单 生产订单 包装数量
  111. SaleOrdersProduct::where('id',$v['sale_orders_product_id'])->update([
  112. 'box_num' => DB::raw('box_num + '.$total),
  113. ]);
  114. OrdersProduct::where('id',$v['id'])->update([
  115. 'box_num' => DB::raw('box_num + '.$total),
  116. ]);
  117. }
  118. //销售订单包装----------------todo 目前只能按找生产包所以continue
  119. foreach ($sale_product_list as $v) {continue;
  120. $box_type = 0;
  121. if($v['id'] < 0) {
  122. $v['id'] = -$v['id'];
  123. $box_type = 1;
  124. }
  125. $num_list = $key_list[$v['id']];
  126. $total = $num_list['total'];
  127. $detail = $num_list['detail'];
  128. $team_id = $num_list['team_id'];
  129. $box_detail = new BoxDetail(['channel'=>$top_order_no]);
  130. $un_box_num = $v['order_quantity'] - $v['box_num'] - $box_detail->where('top_id',$v['id'])->where('del_time',0)->where('box_type',1)->sum('num');
  131. if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
  132. $ext_1 = $v['product_no'];//产品编号
  133. $ext_2 = $v['technology_material']; //工艺材质 废弃
  134. $ext_3 = $v['technology_name'];//工艺名称 改为颜色
  135. $ext_4 = $v['wood_name'];//木皮 废弃
  136. $ext_5 = $v['process_mark'];//工艺备注 废弃
  137. $ext_8 = $v['product_title'];//产品名称
  138. $out_order_no = $insert['out_order_no'] = $v['out_order_no'];
  139. $top_id = $v['id'];
  140. foreach ($detail as $vv){
  141. $box_insert[] = [
  142. 'out_order_no' => $out_order_no,
  143. 'top_id' => $top_id,
  144. 'orders_product_id' => 0,
  145. 'ext_1' => $ext_1,
  146. 'ext_2' => $ext_2,
  147. 'ext_3' => $ext_3,
  148. 'ext_4' => $ext_4,
  149. 'ext_5' => $ext_5,
  150. 'ext_8' => $ext_8,
  151. 'num' => $vv,
  152. 'price' => $v['price'],
  153. 'box_type' => $box_type,
  154. 'team_id' => $team_id,
  155. 'shipment_order_no' => $transport_no,
  156. ];
  157. }
  158. //修改销售订单 包装数量
  159. SaleOrdersProduct::where('id',$v['id'])->update([
  160. 'box_num' => DB::raw('box_num + '.$total),
  161. ]);
  162. }
  163. //客户名称
  164. $ext_1 = "";
  165. if(empty($ext_1) && isset($sale_product_list[0]['customer_name'])) $ext_1 = $sale_product_list[0]['customer_name'];
  166. if(empty($ext_1) && isset($product_list[0]['customer_name'])) $ext_1 = $product_list[0]['customer_name'];
  167. $insert['ext_1'] = $ext_1;
  168. $insert['detail'] = $box_insert;
  169. $insert['top_order_no'] = $top_order_no;
  170. $insert['order_no'] = $box_no;
  171. $insert['shipment_order_no'] = $transport_no;
  172. list($status,$msg) = self::$box_hook->boxInsert($insert);
  173. if(! $status) {
  174. $this->dellimitingSendRequestBackgNeed($limit_key);
  175. DB::rollBack();
  176. return [false,$msg];
  177. }
  178. //更新完工数量
  179. list($status, $msg1) = $this->updateFinish($product_list,$key_list);
  180. if(! $status) {
  181. $this->dellimitingSendRequestBackgNeed($limit_key);
  182. DB::rollBack();
  183. return [false,$msg1];
  184. }
  185. //用友 ------产成品入库
  186. $package_data = $msg->toArray();
  187. if(! empty($package_data)) {
  188. $service = new FinishedOrderService();
  189. list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
  190. if(! $status) {
  191. $this->dellimitingSendRequestBackgNeed($limit_key);
  192. DB::rollBack();
  193. return [false, $msg];
  194. }
  195. }
  196. DB::commit();
  197. $this->dellimitingSendRequestBackgNeed($limit_key);
  198. return [true, ['package_data' => $package_data]];
  199. }catch (\Throwable $e){
  200. DB::rollBack();
  201. $this->dellimitingSendRequestBackgNeed($limit_key);
  202. return [false,$e->getLine().':'.$e->getMessage()];
  203. }
  204. }
  205. public function delBoxLock(){
  206. $this->dellimitingSendRequestBackgNeed($this->lock_key);
  207. }
  208. public function updateFinish($production_list = [], $map){
  209. if(empty($production_list)) return [true,''];
  210. //组织派工数据
  211. $dispatch = DispatchSub::where('del_time',0)
  212. ->where('order_product_id', array_column($production_list,'id'))
  213. ->get()->toArray();
  214. if(empty($dispatch)) return [true,''];
  215. $dispatch_map = [];
  216. foreach ($dispatch as $value){
  217. $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
  218. $dispatch_map[$key][] = $value;
  219. }
  220. $result = [];
  221. foreach ($dispatch_map as $value){
  222. $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
  223. $finished_num = array_sum(array_column($value, 'finished_num'));
  224. if($dispatch_quantity == $finished_num) continue; //同一批派工 且 全部完工的 跳过
  225. foreach ($value as $val){
  226. if($val['dispatch_quantity'] <= $val['finished_num']) continue; //某一个工序完工
  227. //包装数量
  228. $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
  229. if($tmp_num < 0) continue;
  230. $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
  231. if($tmp_num2 >= $tmp_num){
  232. $val['quantity'] = $tmp_num;
  233. }else{
  234. $val['quantity'] = $tmp_num2;
  235. }
  236. $result[] = $val;
  237. }
  238. }
  239. if(empty($result)) return [true,''];
  240. $time = time();
  241. try {
  242. DB::beginTransaction();
  243. //根据派工数据回写
  244. foreach ($result as $value){
  245. $finished_num = $value['quantity'] + $value['finished_num'];
  246. DispatchSub::where('id',$value['id'])->update([
  247. 'finished_num' => $finished_num
  248. ]);
  249. $quantity = $value['quantity'] * 1000;
  250. //工序表
  251. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  252. $process_model->where('order_product_id',$value['order_product_id'])
  253. ->where('process_id',$value['process_id'])
  254. ->where('dispatch_no',$value['dispatch_no'])
  255. ->where('status', 1)
  256. ->take($quantity)
  257. ->update([
  258. 'finished_time' => $time,
  259. 'status' => 2,
  260. 'finished_id' => 1,
  261. 'team_id' => 72,
  262. 'equipment_id' => 13,
  263. ]);
  264. }
  265. $service = new FinishedOrderService();
  266. //反写数量
  267. //生产订单
  268. $id = array_column($result,'order_product_id');
  269. $service->writeFinishedQuantityByOrdersProductId($id);
  270. //销售订单
  271. $service->writeFinishedQuantity($id);
  272. DB::commit();
  273. }catch (\Throwable $exception){
  274. DB::rollBack();
  275. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  276. }
  277. return [true, ''];
  278. }
  279. public function saveOutOrder($data,$user){
  280. list($status,$msg) = $this->saveOutOrderRule($data,$user);
  281. if(! $status) return [false, $msg];
  282. $limit_key = "saveOutOrder" . $user['id'];
  283. list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
  284. if(! $status) return [false, '正在生成销售出库订单数据,请稍后尝试!'];
  285. try {
  286. DB::beginTransaction();
  287. //组织数据
  288. $yongyou_data = $update = [];
  289. foreach ($data['out_data'] as $value){
  290. $pro_tmp = [];
  291. foreach ($value['product'] as $p){
  292. $t_k = $p['cinvcode'] . $p['cfree1'];
  293. $pro_tmp[$t_k] = [
  294. 'cinvcode' => $p['cinvcode'],
  295. 'cfree1' => $p['cfree1'],
  296. 'iquantity' => $p['iquantity'],
  297. 'line' => $p['line'],
  298. ];
  299. }
  300. if(isset($yongyou_data[$value['customer_code']])){
  301. $yongyou_data[$value['customer_code']]['cdlcode_string'] .= ',' . $value['cdlcode'];
  302. foreach ($pro_tmp as $k => $v){
  303. if(isset($yongyou_data[$value['customer_code']]['product'][$k])){
  304. $yongyou_data[$value['customer_code']]['product'][$k]['iquantity'] += $v['iquantity'];
  305. }else{
  306. $yongyou_data[$value['customer_code']]['product'][$k] = $v;
  307. }
  308. }
  309. }else{
  310. $yongyou_data[$value['customer_code']] = [
  311. 'customer_code' => $value['customer_code'],
  312. 'cdlcode_string' => $value['cdlcode'],
  313. 'product' => $pro_tmp
  314. ];
  315. }
  316. foreach ($value['box_no'] as $box_no){
  317. $update[$box_no] = $value['cdlcode'];
  318. }
  319. }
  320. if(empty($yongyou_data)) return [false, '暂无写入用友发货出库数据!'];
  321. //用友 ------发货出库
  322. $service = new FinishedOrderService();
  323. list($status,$msg) = $service->U8Rdrecord32Save($yongyou_data,$user);
  324. if(! $status) return [false, $msg];
  325. //本地数据更新
  326. if(! empty($update)){
  327. foreach ($update as $key => $value){
  328. Box::where('order_no', $key)->update([
  329. 'shipment_order_no' => $value
  330. ]);
  331. }
  332. }
  333. DB::commit();
  334. $this->dellimitingSendRequestBackgNeed($limit_key);
  335. }catch (\Throwable $exception){
  336. $this->dellimitingSendRequestBackgNeed($limit_key);
  337. DB::rollBack();
  338. return [false, $exception->getFile() . $exception->getFile() . $exception->getMessage()];
  339. }
  340. return [true, ''];
  341. }
  342. public function saveOutOrderRule($data,$user){
  343. if(empty($data['out_data'])) return [false, '未获取到发货数据,操作失败!'];
  344. //客户校验
  345. $customer_code = array_column($data['out_data'],'customer_code');
  346. $isAllSame = count(array_unique($customer_code)) === 1;
  347. if(! $isAllSame) return [false, '发货出库,请选择同一个客户的订单!'];
  348. $box_no = array_column($data['out_data'],'box_no');
  349. if(empty($box_no)) return [false, '包装单信息不能为空'];
  350. $box_arr = [];
  351. foreach ($box_no as $value){
  352. foreach ($value as $val){
  353. $box_arr[] = $val;
  354. }
  355. }
  356. $box_map = Box::where('del_time',0)
  357. ->whereIn('order_no',$box_arr)
  358. ->pluck('shipment_order_no', "order_no")
  359. ->toArray();
  360. foreach ($box_arr as $value){
  361. $tmp = $box_map[$value] ?? "";
  362. if($tmp != "") return [false, "包装单:" . $value . "已在发货单(" . $tmp .")中发出!"];
  363. }
  364. //用友发货数据查询
  365. $cdlcode = array_column($data['out_data'],'cdlcode');
  366. $service = new FyySqlServerService($user);
  367. if($service->error) return [false, $service->error];
  368. $out_data_map = $service->getDataFromDispatchListForCheck(['cdlcode' => $cdlcode]);
  369. if(empty($out_data_map)) return [false, '未在用友发货数据中查找到相关数据!'];
  370. //数据校验
  371. foreach ($data['out_data'] as $value){
  372. if(empty($value['cdlcode'])) return [false, '发货单号不能为空!'];
  373. if(! isset($out_data_map[$value['cdlcode']])) return [false, '发货单号:' . $value['cdlcode'] . '不存在或已被删除!'];
  374. $tmp = $out_data_map[$value['cdlcode']];
  375. if(empty($value['customer_code'])) return [false, '客户编码不能为空!'];
  376. if($tmp['customer_code'] != $value['customer_code']) return [false, '客户编码与用友数据中不一致!'];
  377. if(empty($tmp['product'])) return [false, '发货单号:' . $value['cdlcode'] . '未查找到用友的发货产品数据!'];
  378. $tmp_pro = [];
  379. foreach ($tmp['product'] as $t){
  380. $t_k = $t['cinvcode'] . $t['cfree1'];
  381. $tmp_pro[$t_k] = bcsub($t['iquantity'] , $t['out_quantity'],3);//未发货数量
  382. }
  383. if(empty($value['product'])) return [false, '发货产品不能为空!'];
  384. foreach ($value['product'] as $p){
  385. if(empty($p['line'])) return [false, '存货行号不能为空!'];
  386. if(empty($p['cinvcode'])) return [false, '存货编码不能为空!'];
  387. if(empty($p['cfree1'])) return [false, '存货颜色不能为空!'];
  388. $p_k = $p['cinvcode'] . $p['cfree1'];
  389. if(! isset($tmp_pro[$p_k])) return [false, '发货单号:' . $value['cdlcode'] . '下无该发货产品:编码为' . $p['cinvcode'] . ',颜色为:' . $p['cfree1']];
  390. if(empty($p['iquantity']) || $p['iquantity'] <= 0) return [false, '发货数量不能为空!'];
  391. $last = $tmp_pro[$p_k];
  392. if($p['iquantity'] > $last) return [false, '发货单号:' . $value['cdlcode'] . '下的发货产品:编码为' . $p['cinvcode'] . ',颜色为' . $p['cfree1'] . '的未发货数量为' . $last];
  393. }
  394. }
  395. return [true, ''];
  396. }
  397. /**
  398. * 根据发货单包装(恒成塑业不需要 如果有调用 直接返回)
  399. * @param $data
  400. * @return array
  401. */
  402. public function transportBoxIn($data)
  403. {
  404. return [true,''];
  405. // $param = [
  406. // [
  407. // 'order_no' => 'XD202309000155',
  408. // 'product_no' => '0301011816',
  409. // 'technology_material' => '多层板,单贴,新木纹钢板',
  410. // 'technology_name' => '',
  411. // 'wood_name' => '',
  412. // 'process_mark' => '',
  413. // 'num' => '5',
  414. // 'team_id' => '1',
  415. // ],[
  416. // 'order_no' => 'XD202309000155',
  417. // 'product_no' => '0104010753',
  418. // 'technology_material' => '多层线条',
  419. // 'technology_name' => '',
  420. // 'wood_name' => '',
  421. // 'process_mark' => '',
  422. // 'num' => '4',
  423. // 'team_id' => '1',
  424. // ],[
  425. // 'order_no' => 'XD202309000156',
  426. // 'product_no' => '0301011712',
  427. // 'technology_material' => '多层板,双贴,雨丝钢板',
  428. // 'technology_name' => '',
  429. // 'wood_name' => '',
  430. // 'process_mark' => '',
  431. // 'num' => '3',
  432. // ],
  433. // ];
  434. // dd(json_encode($param));
  435. // $params = [
  436. // 'params' => $param,
  437. // 'transport_no' => '123443212',
  438. // ];
  439. $param = $data['params'];
  440. $shipment_order_no = $data['transport_no'];
  441. $sale_order_nos = [];
  442. $key_list = [];
  443. foreach ($param as $v){
  444. if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no'];
  445. $key = $this->getKey($v);
  446. $key_list[$key] = [
  447. 'num' => $v['num'],
  448. 'order_no' => $v['order_no'],
  449. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  450. ];
  451. }
  452. $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray();
  453. $sale_order_product_key_list = [];
  454. foreach ($sale_order_product_list as $v){
  455. $key = $this->getKey($v);
  456. $sale_order_product_key_list[$key] = $v['id'];
  457. }
  458. $param = [];
  459. // var_dump($key_list);
  460. // var_dump($sale_order_product_key_list);die;
  461. foreach ($key_list as $k=>$v){
  462. if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!'];
  463. $param[$v['order_no']][] = [
  464. 'id' => -(intval($sale_order_product_key_list[$k])),
  465. 'team_id' => $v['team_id'],
  466. 'params' => [$v['num']]
  467. ];
  468. }
  469. $box_no = self::$box_hook->setOrderNo();
  470. foreach ($param as $k=>$v){
  471. $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no');
  472. $postParam = [
  473. 'order_no' => $top_order_no,
  474. 'param' => $v,
  475. 'box_no' => $box_no,
  476. 'transport_no' => $shipment_order_no,
  477. ];
  478. $this->boxIn($postParam);
  479. // var_dump($postParam);die;
  480. }
  481. return [true,'保存成功'];
  482. }
  483. protected function getKey($data){
  484. return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark'];
  485. }
  486. /**
  487. * 包装详情1用于包装前
  488. * @param $data
  489. * @return array
  490. */
  491. public function boxDetail($data)
  492. {
  493. list($status, $data) = self::$box_hook->boxDetail($data);
  494. if (!$status) return [false, $data];
  495. $return = [];
  496. foreach ($data as $v){
  497. $key = $v['ext_1'] . $v['ext_3'];
  498. if(! isset($return[$key])){
  499. $return[$key] = $v;
  500. }else{
  501. $return[$key]['num'] += $v['num'];
  502. }
  503. }
  504. $sale_orders_product_ids = array_unique(array_column($data,'top_id'));
  505. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  506. $key_list = [];
  507. foreach ($list as $v){
  508. $key_list[$v['id']] = $v;
  509. }
  510. foreach ($return as &$v){
  511. $detail = $key_list[$v['top_id']];
  512. $v['customer_no'] = $detail['customer_no'];
  513. $v['customer_name'] = $detail['customer_name'];
  514. }
  515. return [true, array_values($return)];
  516. }
  517. public function boxProductList($data){
  518. if(empty($data['id']) && empty($data['out_order_no'])) return [false,'请选择数据!'];
  519. if(! empty($data['id'])){
  520. $sale_order_ids = $data['id'];
  521. }else{
  522. $ids = SaleOrdersProduct::where('del_time',0)
  523. ->where('out_order_no','Like','%'. $data['out_order_no'] . '%')
  524. ->select('id')
  525. ->get()->toArray();
  526. $sale_order_ids = array_column($ids,'id');
  527. if(empty($sale_order_ids)) return [false, '暂无数据!'];
  528. }
  529. $model = SaleOrdersProduct::where('del_time',0)
  530. ->whereIn('id',$sale_order_ids)
  531. ->select('id','out_order_no','order_no','customer_no','customer_name','product_no','product_title','product_size','crt_time as production_time','id as sale_orders_product_id','finished_num as dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time','order_quantity','technology_material','technology_name','wood_name','process_mark')
  532. ->orderBy('id','desc')
  533. ->get()->toArray();
  534. //原先按照完工数量 未包装 = 完工数量 - 已包数量
  535. $product_list = ordersProduct::where('del_time',0)
  536. ->whereIn('sale_orders_product_id',$sale_order_ids)->select('id','out_order_no','customer_no','customer_name','product_no','sale_orders_product_id','product_title','product_size','box_num','technology_name','wood_name','crt_time','production_quantity','dispatch_complete_quantity','technology_material','technology_name','wood_name','process_mark',"production_no")
  537. ->get()->toArray();
  538. $model_key_list = [];
  539. foreach ($model as $v){
  540. $model_key_list[$v['id']] = $v;
  541. }
  542. $model = [];
  543. //存在生产订单,组织数据
  544. if(! empty($product_list)){
  545. $product_key_num_list = [];
  546. foreach ($product_list as $v){
  547. if($v['production_quantity'] == $v['box_num']) continue;
  548. if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']])) {
  549. $product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
  550. 'product_num' => 0 ,
  551. 'box_num' => 0 ,
  552. ];
  553. }
  554. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
  555. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
  556. $detail = $model_key_list[$v['sale_orders_product_id']];
  557. $detail['box_type'] = 1;
  558. $detail['id'] = $v['id'];
  559. $detail['box_num'] = $v['box_num'];
  560. $detail['dispatch_complete_quantity'] = $v['dispatch_complete_quantity'];
  561. $detail['un_box_num'] = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
  562. $detail['type'] = 1;
  563. $detail['production_no'] = $v['production_no'];
  564. $detail['production_quantity'] = $v['production_quantity'];
  565. $model[] = $detail;
  566. }
  567. }
  568. $return = [];
  569. foreach ($model as $v){
  570. if(!isset($v['box_type'])) {continue; //没有销售订单直接包了。
  571. $product_num = 0;
  572. $box_num = 0;
  573. if(isset($product_key_num_list[$v['id']])){
  574. foreach ($product_key_num_list[$v['id']] as $vv){
  575. $product_num += $vv['product_num'];
  576. $box_num += $vv['box_num'];
  577. }
  578. }
  579. if(($v['order_quantity'] - $product_num) === 0) continue;
  580. $product_key_list[$v['sale_orders_product_id']] = [
  581. 'out_order_no' => $v['out_order_no'],
  582. 'order_no' => $v['order_no'],
  583. 'production_no' => "",
  584. 'production_time' => '未下生产',
  585. 'customer_no' => $v['customer_no'],
  586. 'customer_name' => $v['customer_name'],
  587. 'product_no' => $v['product_no'],
  588. 'product_title' => $v['product_title'],
  589. 'product_size' => $v['product_size'],
  590. 'id' => -$v['sale_orders_product_id'],
  591. 'technology_material' => $v['technology_material'],
  592. 'technology_name' => $v['technology_name'],
  593. 'wood_name' => $v['wood_name'],
  594. 'process_mark' => $v['process_mark'],
  595. 'type' => '2',
  596. 'is_box_num' => $v['box_num'] - $box_num,
  597. 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
  598. 'sale_num' => $v['order_quantity'],
  599. ];
  600. }else{
  601. $return[] = [
  602. 'id' => $v['id'],
  603. 'technology_material' => $v['technology_material'],
  604. 'technology_name' => $v['technology_name'],
  605. 'wood_name' => $v['wood_name'],
  606. 'order_no' => $v['order_no'],
  607. 'production_no' => $v['production_no'],
  608. 'process_mark' => $v['process_mark'],
  609. 'out_order_no' => $v['out_order_no'],
  610. 'production_time' => date('Y-m-d',$v['crt_time']),
  611. 'customer_no' => $v['customer_no'],
  612. 'customer_name' => $v['customer_name'],
  613. 'product_no' => $v['product_no'],
  614. 'product_title' => $v['product_title'],
  615. 'product_size' => $v['product_size'],
  616. 'type' => '1',
  617. 'is_box_num' => $v['box_num'],
  618. 'un_box_num' => $v['un_box_num'],
  619. 'sale_num' => $v['order_quantity'],
  620. 'production_num' => $v['production_quantity'],
  621. 'dispatch_complete_quantity' => $v['dispatch_complete_quantity']
  622. ];
  623. }
  624. }
  625. return [true,$return];
  626. }
  627. /**
  628. * 包装详情2用于包装后
  629. * @param $data
  630. * @return array
  631. */
  632. public function boxOrderDetail($data)
  633. {
  634. list($status, $data) = self::$box_hook->boxDetail($data);
  635. // var_dump($data);die;
  636. $sale_orders_product_ids = [];
  637. foreach ($data as $v){
  638. $sale_orders_product_ids[] = $v['top_id'];
  639. }
  640. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  641. $key_list = [];
  642. foreach ($list as $v){
  643. $key_list[$v['id']] = $v;
  644. }
  645. foreach ($data as &$v){
  646. $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
  647. $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
  648. $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
  649. $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
  650. $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
  651. }
  652. if (!$status) return [false, $data];
  653. return [true, $data];
  654. }
  655. /**
  656. * 通过销售单号获取包装单
  657. * @param $data
  658. * @return array
  659. */
  660. public function boxOrderDetailByTop($data){
  661. $list = new Box();
  662. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  663. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  664. // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  665. // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  666. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
  667. return [true,$list];
  668. }
  669. /**
  670. * 通过包装单获取包装详情
  671. * @param $data
  672. * @return array
  673. */
  674. public function boxOrderDetailByOrderNo($data){
  675. $order_nos = $data['order_nos'];
  676. $order_nos = array_unique($order_nos);
  677. $list = Box::where('del_time',0)->wherein('order_no',$order_nos)->select('order_no','out_order_no','top_order_no','shipment_order_no as transport_no')->get()->toArray();
  678. $detail_list = [];
  679. foreach ($list as $v){
  680. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  681. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  682. }
  683. $list = $detail_list;
  684. $ids = [];
  685. foreach ($list as $v){
  686. $ids[] = $v['top_id'];
  687. }
  688. $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
  689. $sale_orders_key_product = [];
  690. foreach ($sale_orders_product as $v){
  691. $sale_orders_key_product[$v['id']] = $v;
  692. }
  693. $team_key_list = Team::pluck('title','id')->toArray();
  694. foreach ($list as &$v){
  695. $detail = $sale_orders_key_product[$v['top_id']];
  696. // var_dump($detail);die;
  697. $v['customer_name'] = $detail['customer_name'];
  698. $v['table_body_mark'] = $detail['table_body_mark'];
  699. $v['ext_4'] = $detail['product_title'];
  700. $v['ext_5'] = $detail['product_size'];
  701. $v['customer_name'] = $detail['customer_name'];
  702. $v['technology_material'] = $detail['technology_material'];
  703. $v['transport_no'] = $v['shipment_order_no'];
  704. $v['wood_name'] = $detail['wood_name'];
  705. $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
  706. }
  707. return [true,$list];
  708. }
  709. public function boxOrderGroup($data){
  710. $list = new Box();
  711. if(empty($data['top_order_no']) && empty($data['transport_no'])) return [false,'请输入单号!'];
  712. if(! empty($data['top_order_no'])) $list = $list->whereIn('top_order_no',$data['top_order_no']);
  713. if(! empty($data['transport_no'])) {
  714. if(is_array($data['transport_no'])){
  715. $list = $list->whereIn('shipment_order_no',$data['transport_no']);
  716. }else{
  717. $list = $list->where('shipment_order_no','Like','%'. $data['transport_no'] . '%');
  718. }
  719. }
  720. $list = $list->select('top_order_no','order_no as box_no','crt_time','shipment_order_no')->where('del_time',0)
  721. ->get()->toArray();
  722. foreach ($list as $key => $value){
  723. $list[$key]['crt_time'] = date('Y-m-d H:i:s',$value['crt_time']);
  724. }
  725. return [true,$list];
  726. }
  727. public function boxOrderGroupGwp($data){
  728. $out_order_no = $data['out_order_no'] ?? "";
  729. $model = Box::where('del_time',0)
  730. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  731. return $query->where('out_order_no','Like','%'. $out_order_no . '%');
  732. })
  733. ->select('out_order_no','ext_1','top_order_no','order_no','crt_time','shipment_order_no',DB::raw("count(id) as package_num"))
  734. ->groupBy('out_order_no');
  735. $list = $this->limit($model,'',$data);
  736. $list = $this->fillData($list);
  737. return [true, $list];
  738. }
  739. public function fillData($data){
  740. if(empty($data['data'])) return $data;
  741. foreach ($data['data'] as $key => $value){
  742. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  743. $tmp = $model->where('del_time',0)
  744. ->where('out_order_no',$value['out_order_no'])
  745. ->select('num')
  746. ->get()->toArray();
  747. $data['data'][$key]['num'] = array_sum(array_column($tmp,'num'));
  748. }
  749. return $data;
  750. }
  751. public function boxOrderGroupGwpDetail($data){
  752. if(empty($data['top_order_no']) || empty($data['out_order_no'])) return [false, '请选择数据!'];
  753. $top_order_no = $data['top_order_no'];
  754. $model = new BoxDetail(['channel'=> $top_order_no]);
  755. $detail_list = $model->where('del_time',0)
  756. ->where('out_order_no',$data['out_order_no'])
  757. ->get()->toArray();
  758. $map = Box::whereIn('order_no',array_unique(array_column($detail_list,'order_no')))
  759. ->pluck('ext_1','order_no')
  760. ->toArray();
  761. $return = [];
  762. foreach ($detail_list as $value){
  763. //销售订单号 包装单号 产品编码 颜色
  764. $key = $value['out_order_no'] . $value['order_no'] . $value['ext_1'] . $value['ext_3'];
  765. if(isset($return[$key])){
  766. $return[$key]['num'] += $value['num'];
  767. }else{
  768. $return[$key] = [
  769. 'box_no' => $value['order_no'],
  770. 'out_order_no' => $value['out_order_no'],
  771. 'ext_8' => $value['ext_8'],
  772. 'ext_3' => $value['ext_3'],
  773. 'num' => $value['num'],
  774. 'crt_time' => date("Y-m-d",$value['crt_time']),
  775. 'top_order_no' => $value['top_order_no'],
  776. 'customer_name' => $map[$value['order_no']] ?? "",
  777. ];
  778. }
  779. }
  780. return [true, array_values($return)];
  781. }
  782. public function transportNo(){
  783. $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
  784. return [true,$list];
  785. }
  786. public function delBoxDetail($data){
  787. if(empty($data['order_nos'])) return [false, '请选择包装单!'];
  788. $order_nos = $data['order_nos'];
  789. try {
  790. DB::beginTransaction();
  791. foreach ($order_nos as $v){
  792. $list = self::$box_hook->delBox($v);
  793. if(empty($list)) continue;
  794. foreach ($list as $vv){
  795. SaleOrdersProduct::where('id',$vv['top_id'])->update([
  796. 'box_num' => DB::raw('box_num - '.$vv['num'])
  797. ]);
  798. if($vv['box_type'] == 1){
  799. OrdersProduct::where('id',$vv['orders_product_id'])->update([
  800. 'box_num' => DB::raw('box_num - '.$vv['num'])
  801. ]);
  802. }
  803. }
  804. }
  805. DB::commit();
  806. }catch (\Throwable $exception){
  807. DB::rollBack();
  808. return [false, $exception->getMessage()];
  809. }
  810. return [true, ''];
  811. }
  812. public function boxAdd($data)
  813. {
  814. list($status,$msg) = $this->boxAddRule($data);
  815. if(! $status) return [false,$msg];
  816. try{
  817. DB::beginTransaction();
  818. $box = new Box();
  819. $box->order_no = $msg['order_no'];
  820. $box->save();
  821. DB::table('box_detail')->insert($msg['detail']);
  822. DB::commit();
  823. return [true,''];
  824. }catch (\Exception $e){
  825. DB::rollBack();
  826. return [false,$e->getLine().':'.$e->getMessage()];
  827. }
  828. }
  829. public function boxAddRule($data)
  830. {
  831. if(empty($data['param'])) return [false,'请选择包装数据'];
  832. $order_no = (new BoxHookService())->setOrderNo();
  833. $detail = [];
  834. foreach ($data['param'] as $value) {
  835. $key = $this->lock_key.'_'.$value['idlsid'];
  836. list($status,$msg) = $this->limitingSendRequestBackg($key);
  837. if(! $status) return [false,$msg];
  838. // if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
  839. if(empty($value['team_id'])) return [false,'班组必须选择'];
  840. $tmp = $value;
  841. $tmp['iquantity'] = $value['submit_quantity'];
  842. $detail[] = [
  843. 'order_no' => $order_no,
  844. 'out_order_no' => $value['csocode']??'',
  845. 'crt_time' => time(),
  846. 'upd_time' => time(),
  847. 'type' => 1,
  848. 'ext_1' => $value['cinvcode'],
  849. 'ext_2' => $value['technology_material'],
  850. 'ext_3' => $value['cfree1'] ?? "",
  851. 'ext_4' => $value['cfree2'] ?? "",
  852. 'ext_5' => $value['process_mark'],
  853. 'ext_6' => $value['customer_name'] ?? "",
  854. 'ext_7' => $value['cuscode'] ?? "",
  855. 'ext_8' => $value['product_title'],
  856. 'ext_9' => $value['product_size'],
  857. 'ext_10' => $value['table_header_mark'],
  858. 'ext_11' => $value['table_body_mark'],
  859. 'top_id' => $value['idlsid'],
  860. 'state' => 0,
  861. 'num' => $value['submit_quantity'],
  862. 'box_type' => 2,
  863. 'team_id' => $value['team_id'],
  864. 'shipment_order_no' => $value['cdlcode'],
  865. 'post' => json_encode($tmp),
  866. ];
  867. }
  868. return [true,['order_no' => $order_no, 'detail' => $detail]];
  869. }
  870. public function boxFhDetail($data){
  871. if(empty($data['idlsid'])) return [false,'请选择数据'];
  872. $result = DB::table('box_detail')->where('del_time',0)
  873. ->where('top_id',$data['idlsid'])
  874. ->select('order_no','ext_6','ext_7','ext_1','ext_8','ext_9','num','ext_2','ext_3','ext_4','ext_5','ext_10','ext_11')
  875. ->get()->toArray();
  876. if(! empty($result)){
  877. foreach ($result as $key => $value){
  878. $result[$key] = (array)$value;
  879. }
  880. }
  881. return [true,$result];
  882. }
  883. public function boxFhDel($data){
  884. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  885. $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
  886. DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
  887. 'del_time' => time()
  888. ]);
  889. return [true,''];
  890. }
  891. public function transportDetail($data){
  892. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  893. $order_nos = $data['order_nos'];
  894. $list = BoxDetail::wherein('order_no',$order_nos)->select('ext_1','ext_2','ext_3','ext_4','ext_5','ext_6','ext_7','ext_8','ext_9','num','order_no','shipment_order_no','post')->get()->toArray();
  895. foreach ($list as &$v){
  896. $v['product_no'] = $v['ext_1'];
  897. $v['technology_material'] = $v['ext_2'];
  898. $v['technology_name'] = $v['ext_3'];
  899. $v['wood_name'] = $v['ext_4'];
  900. $v['process_mark'] = $v['ext_5'];
  901. $v['customer_no'] = $v['ext_6'];
  902. $v['customer_name'] = $v['ext_7'];
  903. $v['product_title'] = $v['ext_8'];
  904. $v['product_size'] = $v['ext_9'];
  905. $v['post'] = json_decode($v['post'],true);
  906. }
  907. return [200,$list];
  908. }
  909. public function boxFhBzDetail($data){
  910. if(empty($data['order_no'])) return [false,'请选择包装单数据'];
  911. $result = DB::table('box_detail')->where('del_time',0)
  912. ->whereIn('order_no',$data['order_no'])
  913. ->select('order_no','ext_6','ext_7','ext_1','ext_8','ext_9','num','ext_2','ext_3','ext_4','ext_5','shipment_order_no','crt_time','team_id','ext_10','ext_11')
  914. ->get()->toArray();
  915. if(! empty($result)){
  916. foreach ($result as $key => $value){
  917. $result[$key] = (array)$value;
  918. }
  919. $map = Team::where('id',array_unique(array_column($result,'team_id')))->pluck('title','id')->toArray();
  920. foreach ($result as $key => $value){
  921. $result[$key]['team_name'] = $map[$value['team_id']] ?? '';
  922. }
  923. }
  924. return [true,$result];
  925. }
  926. public function boxFhBzList($data){
  927. if(empty($data['shipment_order_no'])) return [false, '请输入发货单号'];
  928. $model = DB::table('box_detail')->where('del_time',0)
  929. ->where('shipment_order_no','LIKE', '%'.$data['shipment_order_no'].'%')
  930. ->select('order_no','crt_time','shipment_order_no');
  931. $return = [];
  932. $result = $model->get()->toArray();
  933. if(! empty($result)){
  934. foreach ($result as $value){
  935. $return[$value->order_no] = [
  936. 'order_no' => $value->order_no,
  937. 'shipment_order_no' => $value->shipment_order_no,
  938. 'crt_time' => date("Y-m-d H:i:s",$value->crt_time)
  939. ];
  940. }
  941. }
  942. return [true,array_values($return)];
  943. }
  944. public function boxOrderDetailByOrderNoNew($data){
  945. if(empty($data['order_nos'])) return [false, '包装单号不能为空'];
  946. $order_nos = $data['order_nos'];
  947. $order_nos = array_unique($order_nos);
  948. $list = Box::where('del_time',0)
  949. ->whereIn('order_no',$order_nos)
  950. ->select('order_no','out_order_no','top_order_no','shipment_order_no as transport_no')
  951. ->get()->toArray();
  952. $detail_list = [];
  953. foreach ($list as $v){
  954. if(! empty($v['transport_no'])) return [false, "包装单:" . $v['transport_no'] . "已在发货单(" . $v['transport_no'] .")中发出!"];
  955. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  956. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  957. }
  958. $return = [];
  959. foreach ($detail_list as $value){
  960. //销售订单号 产品编码 颜色
  961. $return[] = [
  962. 'ext_1' => $value['ext_1'],
  963. 'ext_3' => $value['ext_3'],
  964. 'ext_8' => $value['ext_8'],
  965. 'num' => $value['num'],
  966. 'out_order_no' => $value['out_order_no'],
  967. 'box_no' => $value['order_no']
  968. ];
  969. }
  970. return [true, array_values($return)];
  971. }
  972. }