BoxService.php 44 KB

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