BoxService.php 44 KB

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