BoxService.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. //todo
  189. }
  190. /**
  191. * 根据发货单包装(恒诚塑业不需要)
  192. * @param $data
  193. * @return array
  194. */
  195. public function transportBoxIn($data)
  196. {return [true,''];
  197. // $param = [
  198. // [
  199. // 'order_no' => 'XD202309000155',
  200. // 'product_no' => '0301011816',
  201. // 'technology_material' => '多层板,单贴,新木纹钢板',
  202. // 'technology_name' => '',
  203. // 'wood_name' => '',
  204. // 'process_mark' => '',
  205. // 'num' => '5',
  206. // 'team_id' => '1',
  207. // ],[
  208. // 'order_no' => 'XD202309000155',
  209. // 'product_no' => '0104010753',
  210. // 'technology_material' => '多层线条',
  211. // 'technology_name' => '',
  212. // 'wood_name' => '',
  213. // 'process_mark' => '',
  214. // 'num' => '4',
  215. // 'team_id' => '1',
  216. // ],[
  217. // 'order_no' => 'XD202309000156',
  218. // 'product_no' => '0301011712',
  219. // 'technology_material' => '多层板,双贴,雨丝钢板',
  220. // 'technology_name' => '',
  221. // 'wood_name' => '',
  222. // 'process_mark' => '',
  223. // 'num' => '3',
  224. // ],
  225. // ];
  226. // dd(json_encode($param));
  227. // $params = [
  228. // 'params' => $param,
  229. // 'transport_no' => '123443212',
  230. // ];
  231. $param = $data['params'];
  232. $shipment_order_no = $data['transport_no'];
  233. $sale_order_nos = [];
  234. $key_list = [];
  235. foreach ($param as $v){
  236. if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no'];
  237. $key = $this->getKey($v);
  238. $key_list[$key] = [
  239. 'num' => $v['num'],
  240. 'order_no' => $v['order_no'],
  241. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  242. ];
  243. }
  244. $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray();
  245. $sale_order_product_key_list = [];
  246. foreach ($sale_order_product_list as $v){
  247. $key = $this->getKey($v);
  248. $sale_order_product_key_list[$key] = $v['id'];
  249. }
  250. $param = [];
  251. // var_dump($key_list);
  252. // var_dump($sale_order_product_key_list);die;
  253. foreach ($key_list as $k=>$v){
  254. if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!'];
  255. $param[$v['order_no']][] = [
  256. 'id' => -(intval($sale_order_product_key_list[$k])),
  257. 'team_id' => $v['team_id'],
  258. 'params' => [$v['num']]
  259. ];
  260. }
  261. $box_no = self::$box_hook->setOrderNo();
  262. foreach ($param as $k=>$v){
  263. $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no');
  264. $postParam = [
  265. 'order_no' => $top_order_no,
  266. 'param' => $v,
  267. 'box_no' => $box_no,
  268. 'transport_no' => $shipment_order_no,
  269. ];
  270. $this->boxIn($postParam);
  271. // var_dump($postParam);die;
  272. }
  273. return [true,'保存成功'];
  274. }
  275. protected function getKey($data){
  276. return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark'];
  277. }
  278. /**
  279. * 包装详情1用于包装前
  280. * @param $data
  281. * @return array
  282. */
  283. public function boxDetail($data)
  284. {
  285. list($status, $data) = self::$box_hook->boxDetail($data);
  286. if (!$status) return [false, $data];
  287. $sale_orders_product_ids = [];
  288. foreach ($data as $v){
  289. $sale_orders_product_ids[] = $v['top_id'];
  290. }
  291. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  292. $key_list = [];
  293. foreach ($list as $v){
  294. $key_list[$v['id']] = $v;
  295. }
  296. foreach ($data as &$v){
  297. $detail = $key_list[$v['top_id']];
  298. $v['customer_no'] = $detail['customer_no'];
  299. $v['customer_name'] = $detail['customer_name'];
  300. }
  301. return [true, $data];
  302. }
  303. public function boxProductList($data){
  304. if(empty($data['id']) && empty($data['out_order_no'])) return [false,'请选择数据!'];
  305. if(! empty($data['id'])){
  306. $sale_order_ids = $data['id'];
  307. }else{
  308. $ids = SaleOrdersProduct::where('del_time',0)
  309. ->where('out_order_no','Like','%'. $data['out_order_no'] . '%')
  310. ->select('id')
  311. ->get()->toArray();
  312. $sale_order_ids = array_column($ids,'id');
  313. if(empty($sale_order_ids)) return [false, '暂无数据!'];
  314. }
  315. $model = SaleOrdersProduct::where('del_time',0)
  316. ->whereIn('id',$sale_order_ids)
  317. ->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')
  318. ->orderBy('id','desc')
  319. ->get()->toArray();
  320. $product_list = ordersProduct::where('del_time',0)
  321. ->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();
  322. $model_key_list = [];
  323. foreach ($model as $v){
  324. $model_key_list[$v['id']] = $v;
  325. }
  326. //存在生产订单,组织数据
  327. if(! empty($product_list)){
  328. $product_key_num_list = [];
  329. foreach ($product_list as $v){
  330. if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']]))$product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
  331. 'product_num' => 0 ,
  332. 'box_num' => 0 ,
  333. ];
  334. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
  335. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
  336. $detail = $model_key_list[$v['sale_orders_product_id']];
  337. $detail['box_type'] = 1;
  338. $detail['id'] = $v['id'];
  339. $detail['box_num'] = $v['box_num'];
  340. $detail['un_box_num'] = $v['dispatch_complete_quantity'] - $v['box_num'];
  341. $detail['type'] = 1;
  342. $detail['production_no'] = $v['production_no'];
  343. $model[] = $detail;
  344. }
  345. }
  346. $return = [];
  347. $product_key_list = [];
  348. foreach ($model as $v){
  349. if(!isset($v['box_type'])) {
  350. $product_num = 0;
  351. $box_num = 0;
  352. if(isset($product_key_num_list[$v['id']])){
  353. foreach ($product_key_num_list[$v['id']] as $vv){
  354. $product_num += $vv['product_num'];
  355. $box_num += $vv['box_num'];
  356. }
  357. }
  358. if(($v['order_quantity'] - $product_num) === 0) continue;
  359. $product_key_list[$v['sale_orders_product_id']] = [
  360. 'out_order_no' => $v['out_order_no'],
  361. 'order_no' => $v['order_no'],
  362. 'production_no' => "",
  363. 'production_time' => '未下生产',
  364. 'customer_no' => $v['customer_no'],
  365. 'customer_name' => $v['customer_name'],
  366. 'product_no' => $v['product_no'],
  367. 'product_title' => $v['product_title'],
  368. 'product_size' => $v['product_size'],
  369. 'id' => -$v['sale_orders_product_id'],
  370. 'technology_material' => $v['technology_material'],
  371. 'technology_name' => $v['technology_name'],
  372. 'wood_name' => $v['wood_name'],
  373. 'process_mark' => $v['process_mark'],
  374. 'type' => '2',
  375. 'is_box_num' => $v['box_num'] - $box_num,
  376. 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
  377. 'sale_num' => $v['order_quantity'],
  378. ];
  379. }else{
  380. $return[] = [
  381. 'id' => $v['id'],
  382. 'technology_material' => $v['technology_material'],
  383. 'technology_name' => $v['technology_name'],
  384. 'wood_name' => $v['wood_name'],
  385. 'order_no' => $v['order_no'],
  386. 'production_no' => $v['production_no'],
  387. 'process_mark' => $v['process_mark'],
  388. 'out_order_no' => $v['out_order_no'],
  389. 'production_time' => date('Y-m-d',$v['crt_time']),
  390. 'customer_no' => $v['customer_no'],
  391. 'customer_name' => $v['customer_name'],
  392. 'product_no' => $v['product_no'],
  393. 'product_title' => $v['product_title'],
  394. 'product_size' => $v['product_size'],
  395. 'type' => '1',
  396. 'is_box_num' => $v['box_num'],
  397. 'un_box_num' => $v['un_box_num'],
  398. 'sale_num' => $v['order_quantity'],
  399. ];
  400. }
  401. }
  402. foreach ($product_key_list as $v){
  403. $return[] = $v;
  404. }
  405. return [true,$return];
  406. }
  407. /**
  408. * 包装详情2用于包装后
  409. * @param $data
  410. * @return array
  411. */
  412. public function boxOrderDetail($data)
  413. {
  414. list($status, $data) = self::$box_hook->boxDetail($data);
  415. // var_dump($data);die;
  416. $sale_orders_product_ids = [];
  417. foreach ($data as $v){
  418. $sale_orders_product_ids[] = $v['top_id'];
  419. }
  420. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  421. $key_list = [];
  422. foreach ($list as $v){
  423. $key_list[$v['id']] = $v;
  424. }
  425. foreach ($data as &$v){
  426. $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
  427. $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
  428. $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
  429. $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
  430. $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
  431. }
  432. if (!$status) return [false, $data];
  433. return [true, $data];
  434. }
  435. /**
  436. * 通过销售单号获取包装单
  437. * @param $data
  438. * @return array
  439. */
  440. public function boxOrderDetailByTop($data){
  441. $list = new Box();
  442. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  443. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  444. // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  445. // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  446. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
  447. return [true,$list];
  448. }
  449. /**
  450. * 通过包装单获取包装详情
  451. * @param $data
  452. * @return array
  453. */
  454. public function boxOrderDetailByOrderNo($data){
  455. $order_nos = $data['order_nos'];
  456. $order_nos = array_unique($order_nos);
  457. $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();
  458. $detail_list = [];
  459. foreach ($list as $v){
  460. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  461. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  462. }
  463. $list = $detail_list;
  464. $ids = [];
  465. foreach ($list as $v){
  466. $ids[] = $v['top_id'];
  467. }
  468. $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
  469. $sale_orders_key_product = [];
  470. foreach ($sale_orders_product as $v){
  471. $sale_orders_key_product[$v['id']] = $v;
  472. }
  473. $team_key_list = Team::pluck('title','id')->toArray();
  474. foreach ($list as &$v){
  475. $detail = $sale_orders_key_product[$v['top_id']];
  476. // var_dump($detail);die;
  477. $v['customer_name'] = $detail['customer_name'];
  478. $v['table_body_mark'] = $detail['table_body_mark'];
  479. $v['ext_4'] = $detail['product_title'];
  480. $v['ext_5'] = $detail['product_size'];
  481. $v['customer_name'] = $detail['customer_name'];
  482. $v['technology_material'] = $detail['technology_material'];
  483. $v['transport_no'] = $v['shipment_order_no'];
  484. $v['wood_name'] = $detail['wood_name'];
  485. $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
  486. }
  487. return [true,$list];
  488. }
  489. public function boxOrderGroup($data){
  490. $list = new Box();
  491. if(empty($data['top_order_no']) && empty($data['transport_no'])) return [false,'请输入单号!'];
  492. if(! empty($data['top_order_no'])) $list = $list->whereIn('top_order_no',$data['top_order_no']);
  493. if(! empty($data['transport_no'])) {
  494. if(is_array($data['transport_no'])){
  495. $list = $list->whereIn('shipment_order_no',$data['transport_no']);
  496. }else{
  497. $list = $list->where('shipment_order_no','Like','%'. $data['transport_no'] . '%');
  498. }
  499. }
  500. $list = $list->select('top_order_no','order_no','crt_time','shipment_order_no')->where('del_time',0)
  501. ->get()->toArray();
  502. return [true,$list];
  503. }
  504. public function boxOrderGroupGwp($data){
  505. $out_order_no = $data['out_order_no'] ?? "";
  506. $model = Box::where('del_time',0)
  507. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  508. return $query->where('out_order_no','Like','%'. $out_order_no . '%');
  509. })
  510. ->select('out_order_no','ext_1','top_order_no','order_no','crt_time','shipment_order_no',DB::raw("count(id) as package_num"))
  511. ->groupBy('out_order_no');
  512. $list = $this->limit($model,'',$data);
  513. $list = $this->fillData($list);
  514. return [true, $list];
  515. }
  516. public function fillData($data){
  517. if(empty($data['data'])) return $data;
  518. foreach ($data['data'] as $key => $value){
  519. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  520. $tmp = $model->where('del_time',0)
  521. ->where('out_order_no',$value['out_order_no'])
  522. ->select('num')
  523. ->get()->toArray();
  524. $data['data'][$key]['num'] = array_sum(array_column($tmp,'num'));
  525. }
  526. return $data;
  527. }
  528. public function boxOrderGroupGwpDetail($data){
  529. if(empty($data['top_order_no']) || empty($data['out_order_no'])) return [false, '请选择数据!'];
  530. $top_order_no = $data['top_order_no'];
  531. $model = new BoxDetail(['channel'=> $top_order_no]);
  532. $detail_list = $model->where('del_time',0)
  533. ->where('out_order_no',$data['out_order_no'])
  534. ->get()->toArray();
  535. $return = [];
  536. foreach ($detail_list as $value){
  537. //销售订单号 包装单号 产品编码 颜色
  538. $key = $value['out_order_no'] . $value['order_no'] . $value['ext_1'] . $value['ext_3'];
  539. if(isset($return[$key])){
  540. $return[$key]['num'] += $value['num'];
  541. }else{
  542. $return[$key] = [
  543. 'out_order_no' => $value['out_order_no'],
  544. 'ext_8' => $value['ext_8'],
  545. 'ext_3' => $value['ext_3'],
  546. 'num' => $value['num'],
  547. 'crt_time' => date("Y-m-d",$value['crt_time']),
  548. 'top_order_no' => $value['top_order_no'],
  549. ];
  550. }
  551. }
  552. return [true, array_values($return)];
  553. }
  554. public function transportNo(){
  555. $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
  556. return [true,$list];
  557. }
  558. public function delBoxDetail($data){
  559. if(empty($data['order_nos'])) return [false, '请选择包装单!'];
  560. $order_nos = $data['order_nos'];
  561. try {
  562. DB::beginTransaction();
  563. foreach ($order_nos as $v){
  564. $list = self::$box_hook->delBox($v);
  565. if(empty($list)) continue;
  566. foreach ($list as $vv){
  567. SaleOrdersProduct::where('id',$vv['top_id'])->update([
  568. 'box_num' => DB::raw('box_num - '.$vv['num'])
  569. ]);
  570. if($vv['box_type'] == 1){
  571. OrdersProduct::where('id',$vv['orders_product_id'])->update([
  572. 'box_num' => DB::raw('box_num - '.$vv['num'])
  573. ]);
  574. }
  575. }
  576. }
  577. DB::commit();
  578. }catch (\Throwable $exception){
  579. DB::rollBack();
  580. return [false, $exception->getMessage()];
  581. }
  582. return [true, ''];
  583. }
  584. public function boxAdd($data)
  585. {
  586. list($status,$msg) = $this->boxAddRule($data);
  587. if(! $status) return [false,$msg];
  588. try{
  589. DB::beginTransaction();
  590. $box = new Box();
  591. $box->order_no = $msg['order_no'];
  592. $box->save();
  593. DB::table('box_detail')->insert($msg['detail']);
  594. DB::commit();
  595. return [true,''];
  596. }catch (\Exception $e){
  597. DB::rollBack();
  598. return [false,$e->getLine().':'.$e->getMessage()];
  599. }
  600. }
  601. public function boxAddRule($data)
  602. {
  603. if(empty($data['param'])) return [false,'请选择包装数据'];
  604. $order_no = (new BoxHookService())->setOrderNo();
  605. $detail = [];
  606. foreach ($data['param'] as $value) {
  607. $key = $this->lock_key.'_'.$value['idlsid'];
  608. list($status,$msg) = $this->limitingSendRequestBackg($key);
  609. if(! $status) return [false,$msg];
  610. // if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
  611. if(empty($value['team_id'])) return [false,'班组必须选择'];
  612. $tmp = $value;
  613. $tmp['iquantity'] = $value['submit_quantity'];
  614. $detail[] = [
  615. 'order_no' => $order_no,
  616. 'out_order_no' => $value['csocode']??'',
  617. 'crt_time' => time(),
  618. 'upd_time' => time(),
  619. 'type' => 1,
  620. 'ext_1' => $value['cinvcode'],
  621. 'ext_2' => $value['technology_material'],
  622. 'ext_3' => $value['cfree1'] ?? "",
  623. 'ext_4' => $value['cfree2'] ?? "",
  624. 'ext_5' => $value['process_mark'],
  625. 'ext_6' => $value['customer_name'] ?? "",
  626. 'ext_7' => $value['cuscode'] ?? "",
  627. 'ext_8' => $value['product_title'],
  628. 'ext_9' => $value['product_size'],
  629. 'ext_10' => $value['table_header_mark'],
  630. 'ext_11' => $value['table_body_mark'],
  631. 'top_id' => $value['idlsid'],
  632. 'state' => 0,
  633. 'num' => $value['submit_quantity'],
  634. 'box_type' => 2,
  635. 'team_id' => $value['team_id'],
  636. 'shipment_order_no' => $value['cdlcode'],
  637. 'post' => json_encode($tmp),
  638. ];
  639. }
  640. return [true,['order_no' => $order_no, 'detail' => $detail]];
  641. }
  642. public function boxFhDetail($data){
  643. if(empty($data['idlsid'])) return [false,'请选择数据'];
  644. $result = DB::table('box_detail')->where('del_time',0)
  645. ->where('top_id',$data['idlsid'])
  646. ->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')
  647. ->get()->toArray();
  648. if(! empty($result)){
  649. foreach ($result as $key => $value){
  650. $result[$key] = (array)$value;
  651. }
  652. }
  653. return [true,$result];
  654. }
  655. public function boxFhDel($data){
  656. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  657. $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
  658. DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
  659. 'del_time' => time()
  660. ]);
  661. return [true,''];
  662. }
  663. public function transportDetail($data){
  664. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  665. $order_nos = $data['order_nos'];
  666. $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();
  667. foreach ($list as &$v){
  668. $v['product_no'] = $v['ext_1'];
  669. $v['technology_material'] = $v['ext_2'];
  670. $v['technology_name'] = $v['ext_3'];
  671. $v['wood_name'] = $v['ext_4'];
  672. $v['process_mark'] = $v['ext_5'];
  673. $v['customer_no'] = $v['ext_6'];
  674. $v['customer_name'] = $v['ext_7'];
  675. $v['product_title'] = $v['ext_8'];
  676. $v['product_size'] = $v['ext_9'];
  677. $v['post'] = json_decode($v['post'],true);
  678. }
  679. return [200,$list];
  680. }
  681. public function boxFhBzDetail($data){
  682. if(empty($data['order_no'])) return [false,'请选择包装单数据'];
  683. $result = DB::table('box_detail')->where('del_time',0)
  684. ->whereIn('order_no',$data['order_no'])
  685. ->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')
  686. ->get()->toArray();
  687. if(! empty($result)){
  688. foreach ($result as $key => $value){
  689. $result[$key] = (array)$value;
  690. }
  691. $map = Team::where('id',array_unique(array_column($result,'team_id')))->pluck('title','id')->toArray();
  692. foreach ($result as $key => $value){
  693. $result[$key]['team_name'] = $map[$value['team_id']] ?? '';
  694. }
  695. }
  696. return [true,$result];
  697. }
  698. public function boxFhBzList($data){
  699. if(empty($data['shipment_order_no'])) return [false, '请输入发货单号'];
  700. $model = DB::table('box_detail')->where('del_time',0)
  701. ->where('shipment_order_no','LIKE', '%'.$data['shipment_order_no'].'%')
  702. ->select('order_no','crt_time','shipment_order_no');
  703. $return = [];
  704. $result = $model->get()->toArray();
  705. if(! empty($result)){
  706. foreach ($result as $value){
  707. $return[$value->order_no] = [
  708. 'order_no' => $value->order_no,
  709. 'shipment_order_no' => $value->shipment_order_no,
  710. 'crt_time' => date("Y-m-d H:i:s",$value->crt_time)
  711. ];
  712. }
  713. }
  714. return [true,array_values($return)];
  715. }
  716. }