BoxService.php 44 KB

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