BoxService.php 36 KB

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