BoxService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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\Service;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * 包装相关
  13. * @package App\Models
  14. */
  15. class BoxService extends Service
  16. {
  17. protected static $instance;
  18. protected static $box_header;
  19. protected static $box_detail_header;
  20. protected static $box_hook;
  21. public $lock_key = 'box';
  22. public function __construct()
  23. {
  24. self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
  25. self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
  26. self::$box_hook = BoxHookService::getInstance();
  27. }
  28. /**
  29. * 包装
  30. * @param $data
  31. * @return array
  32. */
  33. public function boxIn($data)
  34. {
  35. // if(!isset($data['params'])) return [false,'not found params'];
  36. // $param = $data['params'];
  37. // $param = [
  38. // [
  39. // 'id' => 716,
  40. // 'order_no' => 716,
  41. // 'params' => [
  42. // '1',
  43. // '1',
  44. //
  45. // ],
  46. // ], [
  47. // 'id' => 723,
  48. // 'order_no' => 716,
  49. // 'params' => [
  50. // '40'
  51. // ],
  52. // ],
  53. // ];
  54. $ids = [];
  55. $sale_ids = [];
  56. // $key_list = [];
  57. // var_dump($data);die;
  58. $top_order_no = $data['order_no'];
  59. $box_no = isset($data['box_no']) ? $data['box_no'] : '';
  60. $transport_no = isset($data['transport_no']) ? $data['transport_no'] : '';
  61. $data = $data['param'];
  62. $key_list = [];
  63. foreach ($data as $v) {
  64. if($v['id'] < 0) $sale_ids[] = -$v['id'];
  65. else $ids[] = $v['id'];
  66. if($v['id'] < 0) $v['id'] = -$v['id'];
  67. $total = 0;
  68. foreach ($v['params'] as $vv) {
  69. $total += $vv;
  70. }
  71. $key_list[$v['id']] = [
  72. 'detail' => $v['params'],
  73. 'total' => $total,
  74. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  75. ];
  76. }
  77. $insert = [];
  78. // var_dump($ids);
  79. // var_dump($sale_ids);die;
  80. try{
  81. DB::beginTransaction();
  82. $key = $this->lock_key.'_'.$v['id'];
  83. $lock_status = $this->isLock($key);
  84. if(!$lock_status) return [false,'操作过于频繁'];
  85. $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
  86. $sale_product_list = SaleOrdersProduct::wherein('id', $sale_ids)->get()->toArray();
  87. $box_insert = [];
  88. foreach ($product_list as $v) {
  89. $num_list = $key_list[$v['id']];
  90. $total = $num_list['total'];
  91. $detail = $num_list['detail'];
  92. $team_id = $num_list['team_id'];
  93. $un_box_num = $v['dispatch_complete_quantity'] - $v['box_num'];
  94. if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
  95. $ext_1 = $v['product_no'];//产品编号
  96. $ext_2 = $v['technology_material']; //工艺材质
  97. $ext_3 = $v['technology_name'];//工艺名称
  98. $ext_4 = $v['wood_name'];//木皮
  99. $ext_5 = $v['process_mark'];//工艺备注
  100. $out_order_no = $insert['out_order_no'] = $v['out_order_no'];
  101. $top_id = $v['sale_orders_product_id'];
  102. foreach ($detail as $vv){
  103. $box_insert[] = [
  104. 'out_order_no' => $out_order_no,
  105. 'top_id' => $top_id,
  106. 'ext_1' => $ext_1,
  107. 'ext_2' => $ext_2,
  108. 'ext_3' => $ext_3,
  109. 'ext_4' => $ext_4,
  110. 'ext_5' => $ext_5,
  111. 'num' => $vv,
  112. 'box_type' => 1,
  113. 'team_id' => $team_id,
  114. 'shipment_order_no' => $transport_no,
  115. ];
  116. }
  117. SaleOrdersProduct::where('id',$v['sale_orders_product_id'])->update([
  118. 'box_num' => DB::raw('box_num + '.$total),
  119. ]);
  120. OrdersProduct::where('id',$v['id'])->update([
  121. 'box_num' => DB::raw('box_num + '.$total),
  122. ]);
  123. }
  124. foreach ($sale_product_list as $v) {
  125. $box_type = 0;
  126. if($v['id'] < 0) {
  127. $v['id'] = -$v['id'];
  128. $box_type = 1;
  129. }
  130. $num_list = $key_list[$v['id']];
  131. $total = $num_list['total'];
  132. $detail = $num_list['detail'];
  133. $team_id = $num_list['team_id'];
  134. $box_detail = new BoxDetail(['channel'=>$top_order_no]);
  135. $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');
  136. if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
  137. $ext_1 = $v['product_no'];//产品编号
  138. $ext_2 = $v['technology_material']; //工艺材质
  139. $ext_3 = $v['technology_name'];//工艺名称
  140. $ext_4 = $v['wood_name'];//木皮
  141. $ext_5 = $v['process_mark'];//工艺备注
  142. $out_order_no = $insert['out_order_no'] = $v['out_order_no'];
  143. $top_id = $v['id'];
  144. foreach ($detail as $vv){
  145. $box_insert[] = [
  146. 'out_order_no' => $out_order_no,
  147. 'top_id' => $top_id,
  148. 'ext_1' => $ext_1,
  149. 'ext_2' => $ext_2,
  150. 'ext_3' => $ext_3,
  151. 'ext_4' => $ext_4,
  152. 'ext_5' => $ext_5,
  153. 'num' => $vv,
  154. 'box_type' => $box_type,
  155. 'team_id' => $team_id,
  156. 'shipment_order_no' => $transport_no,
  157. ];
  158. }
  159. SaleOrdersProduct::where('id',$v['id'])->update([
  160. 'box_num' => DB::raw('box_num + '.$total),
  161. ]);
  162. }
  163. $insert['detail'] = $box_insert;
  164. $insert['top_order_no'] = $top_order_no;
  165. $insert['order_no'] = $box_no;
  166. $insert['shipment_order_no'] = $transport_no;
  167. list($status,$msg) = self::$box_hook->boxInsert($insert);
  168. if(!$status) {
  169. $this->delLock($key);
  170. DB::rollBack();
  171. return [false,$msg];
  172. }
  173. $this->delLock($key);
  174. DB::commit();
  175. return [true,''];
  176. }catch (\Exception $e){
  177. DB::rollBack();
  178. return [false,$e->getLine().':'.$e->getMessage()];
  179. }
  180. }
  181. /**
  182. * 根据发货单包装
  183. * @param $data
  184. * @return array
  185. */
  186. public function transportBoxIn($data)
  187. {
  188. // $param = [
  189. // [
  190. // 'order_no' => 'XD202309000155',
  191. // 'product_no' => '0301011816',
  192. // 'technology_material' => '多层板,单贴,新木纹钢板',
  193. // 'technology_name' => '',
  194. // 'wood_name' => '',
  195. // 'process_mark' => '',
  196. // 'num' => '5',
  197. // 'team_id' => '1',
  198. // ],[
  199. // 'order_no' => 'XD202309000155',
  200. // 'product_no' => '0104010753',
  201. // 'technology_material' => '多层线条',
  202. // 'technology_name' => '',
  203. // 'wood_name' => '',
  204. // 'process_mark' => '',
  205. // 'num' => '4',
  206. // 'team_id' => '1',
  207. // ],[
  208. // 'order_no' => 'XD202309000156',
  209. // 'product_no' => '0301011712',
  210. // 'technology_material' => '多层板,双贴,雨丝钢板',
  211. // 'technology_name' => '',
  212. // 'wood_name' => '',
  213. // 'process_mark' => '',
  214. // 'num' => '3',
  215. // ],
  216. // ];
  217. // dd(json_encode($param));
  218. // $params = [
  219. // 'params' => $param,
  220. // 'transport_no' => '123443212',
  221. // ];
  222. $param = $data['params'];
  223. $shipment_order_no = $data['transport_no'];
  224. $sale_order_nos = [];
  225. $key_list = [];
  226. foreach ($param as $v){
  227. if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no'];
  228. $key = $this->getKey($v);
  229. $key_list[$key] = [
  230. 'num' => $v['num'],
  231. 'order_no' => $v['order_no'],
  232. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  233. ];
  234. }
  235. $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray();
  236. $sale_order_product_key_list = [];
  237. foreach ($sale_order_product_list as $v){
  238. $key = $this->getKey($v);
  239. $sale_order_product_key_list[$key] = $v['id'];
  240. }
  241. $param = [];
  242. // var_dump($key_list);
  243. // var_dump($sale_order_product_key_list);die;
  244. foreach ($key_list as $k=>$v){
  245. if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!'];
  246. $param[$v['order_no']][] = [
  247. 'id' => -(intval($sale_order_product_key_list[$k])),
  248. 'team_id' => $v['team_id'],
  249. 'params' => [$v['num']]
  250. ];
  251. }
  252. $box_no = self::$box_hook->setOrderNo();
  253. foreach ($param as $k=>$v){
  254. $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no');
  255. $postParam = [
  256. 'order_no' => $top_order_no,
  257. 'param' => $v,
  258. 'box_no' => $box_no,
  259. 'transport_no' => $shipment_order_no,
  260. ];
  261. $this->boxIn($postParam);
  262. // var_dump($postParam);die;
  263. }
  264. return [true,'保存成功'];
  265. }
  266. protected function getKey($data){
  267. return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark'];
  268. }
  269. /**
  270. * 包装详情1用于包装前
  271. * @param $data
  272. * @return array
  273. */
  274. public function boxDetail($data)
  275. {
  276. list($status, $data) = self::$box_hook->boxDetail($data);
  277. if (!$status) return [false, $data];
  278. $sale_orders_product_ids = [];
  279. foreach ($data as $v){
  280. $sale_orders_product_ids[] = $v['top_id'];
  281. }
  282. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  283. $key_list = [];
  284. foreach ($list as $v){
  285. $key_list[$v['id']] = $v;
  286. }
  287. foreach ($data as &$v){
  288. $detail = $key_list[$v['top_id']];
  289. $v['customer_no'] = $detail['customer_no'];
  290. $v['customer_name'] = $detail['customer_name'];
  291. }
  292. return [true, $data];
  293. }
  294. public function boxProductList($data){
  295. if(!isset($data['id'])) return [false,'id not found'];
  296. $sale_order_ids = $data['id'];
  297. $model = SaleOrdersProduct::where('del_time',0)->wherein('id',$sale_order_ids)
  298. ->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')
  299. ->orderBy('id','desc')->get()->toArray();
  300. $product_list = ordersProduct::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','dispatch_complete_quantity','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')->get()->toArray();
  301. $model_key_list = [];
  302. foreach ($model as $v){
  303. $model_key_list[$v['id']] = $v;
  304. }
  305. $product_key_num_list = [];
  306. foreach ($product_list as $v){
  307. if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']]))$product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
  308. 'product_num' => 0 ,
  309. 'box_num' => 0 ,
  310. ];
  311. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
  312. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
  313. $detail = $model_key_list[$v['sale_orders_product_id']];
  314. $detail['box_type'] = 1;
  315. $detail['id'] = $v['id'];
  316. $detail['box_num'] = $v['box_num'];
  317. $detail['un_box_num'] = $v['dispatch_complete_quantity'] - $v['box_num'];
  318. $detail['type'] = 1;
  319. $model[] = $detail;
  320. }
  321. $return = [];
  322. $product_key_list = [];
  323. foreach ($model as $v){
  324. if(!isset($v['box_type'])) {
  325. $product_num = 0;
  326. $box_num = 0;
  327. if(isset($product_key_num_list[$v['id']])){
  328. foreach ($product_key_num_list[$v['id']] as $vv){
  329. $product_num += $vv['product_num'];
  330. $box_num += $vv['box_num'];
  331. }
  332. }
  333. // $product_num = isset($product_key_num_list[$v['id']]) ? $product_key_num_list[$v['id']]['product_num'] : 0;
  334. // $box_num = isset($product_key_num_list[$v['id']]) ? $product_key_num_list[$v['id']]['box_num'] : 0;
  335. if(($v['order_quantity'] - $product_num) === 0) continue;
  336. // var_dump($v['order_quantity']);
  337. // var_dump($v['order_quantity']);
  338. $product_key_list[$v['sale_orders_product_id']] = [
  339. 'out_order_no' => $v['out_order_no'],
  340. 'order_no' => $v['order_no'],
  341. 'production_time' => '未下生产',
  342. 'customer_no' => $v['customer_no'],
  343. 'customer_name' => $v['customer_name'],
  344. 'product_no' => $v['product_no'],
  345. 'product_title' => $v['product_title'],
  346. 'product_size' => $v['product_size'],
  347. 'id' => -$v['sale_orders_product_id'],
  348. 'technology_material' => $v['technology_material'],
  349. 'technology_name' => $v['technology_name'],
  350. 'wood_name' => $v['wood_name'],
  351. 'process_mark' => $v['process_mark'],
  352. 'type' => '2',
  353. 'is_box_num' => $v['box_num'] - $box_num,
  354. 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
  355. 'sale_num' => $v['order_quantity'],
  356. ];
  357. }else{
  358. $return[] = [
  359. 'id' => $v['id'],
  360. 'technology_material' => $v['technology_material'],
  361. 'technology_name' => $v['technology_name'],
  362. 'wood_name' => $v['wood_name'],
  363. 'order_no' => $v['order_no'],
  364. 'process_mark' => $v['process_mark'],
  365. 'out_order_no' => $v['out_order_no'],
  366. 'production_time' => date('Y-m-d',$v['crt_time']),
  367. 'customer_no' => $v['customer_no'],
  368. 'customer_name' => $v['customer_name'],
  369. 'product_no' => $v['product_no'],
  370. 'product_title' => $v['product_title'],
  371. 'product_size' => $v['product_size'],
  372. 'type' => '1',
  373. 'is_box_num' => $v['box_num'],
  374. 'un_box_num' => $v['un_box_num'],
  375. 'sale_num' => $v['order_quantity'],
  376. ];
  377. }
  378. }
  379. foreach ($product_key_list as $v){
  380. $return[] = $v;
  381. }
  382. // $data = [
  383. // [
  384. // 'id' => 1,
  385. // 'out_order_no' => '销售订单号',
  386. // 'production_time' => '下生产时间',
  387. // 'customer_no' => '客户编码',
  388. // 'customer_name' => '客户名称',
  389. // 'product_no' => '产品编码',
  390. // 'product_title' => '产品名称',
  391. // 'product_size' => '产品规格',
  392. // 'type' => '1生产包装2备用包装',
  393. // 'is_box_num' => '已包装数量',
  394. // 'un_box_num' => '未包装',
  395. // 'sale_num' => '销售数量',
  396. // ]
  397. // ];
  398. return [true,$return];
  399. }
  400. /**
  401. * 包装详情2用于包装后
  402. * @param $data
  403. * @return array
  404. */
  405. public function boxOrderDetail($data)
  406. {
  407. list($status, $data) = self::$box_hook->boxDetail($data);
  408. // var_dump($data);die;
  409. $sale_orders_product_ids = [];
  410. foreach ($data as $v){
  411. $sale_orders_product_ids[] = $v['top_id'];
  412. }
  413. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  414. $key_list = [];
  415. foreach ($list as $v){
  416. $key_list[$v['id']] = $v;
  417. }
  418. foreach ($data as &$v){
  419. $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
  420. $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
  421. $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
  422. $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
  423. $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
  424. }
  425. if (!$status) return [false, $data];
  426. return [true, $data];
  427. }
  428. /**
  429. * 通过销售单号获取包装单
  430. * @param $data
  431. * @return array
  432. */
  433. public function boxOrderDetailByTop($data){
  434. $list = new Box();
  435. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  436. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  437. // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  438. // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  439. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
  440. return [true,$list];
  441. }
  442. /**
  443. * 通过包装单获取包装详情
  444. * @param $data
  445. * @return array
  446. */
  447. public function boxOrderDetailByOrderNo($data){
  448. $order_nos = $data['order_nos'];
  449. $order_nos = array_unique($order_nos);
  450. $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();
  451. $detail_list = [];
  452. foreach ($list as $v){
  453. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  454. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  455. }
  456. $list = $detail_list;
  457. $ids = [];
  458. foreach ($list as $v){
  459. $ids[] = $v['top_id'];
  460. }
  461. $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
  462. $sale_orders_key_product = [];
  463. foreach ($sale_orders_product as $v){
  464. $sale_orders_key_product[$v['id']] = $v;
  465. }
  466. $team_key_list = Team::pluck('title','id')->toArray();
  467. foreach ($list as &$v){
  468. $detail = $sale_orders_key_product[$v['top_id']];
  469. // var_dump($detail);die;
  470. $v['customer_name'] = $detail['customer_name'];
  471. $v['table_body_mark'] = $detail['table_body_mark'];
  472. $v['ext_4'] = $detail['product_title'];
  473. $v['ext_5'] = $detail['product_size'];
  474. $v['customer_name'] = $detail['customer_name'];
  475. $v['technology_material'] = $detail['technology_material'];
  476. $v['transport_no'] = $v['shipment_order_no'];
  477. $v['wood_name'] = $detail['wood_name'];
  478. $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
  479. }
  480. return [true,$list];
  481. }
  482. public function boxOrderGroup($data){
  483. $list = new Box();
  484. if(!isset($data['top_order_no'])&&!isset($data['transport_no'])) return [false,'销售单或发货单必选一个'];
  485. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  486. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  487. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->get()->toArray();
  488. return [true,$list];
  489. }
  490. public function transportNo(){
  491. $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
  492. return [true,$list];
  493. }
  494. public function delBoxDetail($data){
  495. $order_nos = $data['order_nos'];
  496. foreach ($order_nos as $v){
  497. $list = self::$box_hook->delBox($v);
  498. foreach ($list as $vv){
  499. SaleOrdersProduct::where('id',$vv['top_id'])->update([
  500. 'box_num' => DB::raw('box_num - '.$vv['num'])
  501. ]);
  502. if($vv['box_type'] == 1){
  503. // $ext_1 = $v['product_no'];//产品编号
  504. // $ext_2 = $v['technology_material']; //工艺材质
  505. // $ext_3 = $v['technology_name'];//工艺名称
  506. // $ext_4 = $v['wood_name'];//木皮
  507. // $ext_5 = $v['process_mark'];//工艺备注
  508. OrdersProduct::where('product_no',$vv['ext_1'])->where('technology_material',$vv['ext_2'])->where('technology_name',$vv['ext_3'])->where('wood_name',$vv['ext_4'])->where('process_mark',$vv['ext_5'])->where('sale_orders_product_id',$vv['top_id'])
  509. ->update([
  510. 'box_num' => DB::raw('box_num - '.$vv['num'])
  511. ]);
  512. }
  513. }
  514. }
  515. return [true,'删除成功'];
  516. }
  517. public function boxAdd($data)
  518. {
  519. list($status,$msg) = $this->boxAddRule($data);
  520. if(! $status) return [false,$msg];
  521. try{
  522. DB::beginTransaction();
  523. $box = new Box();
  524. $box->order_no = $msg['order_no'];
  525. $box->save();
  526. DB::table('box_detail')->insert($msg['detail']);
  527. DB::commit();
  528. return [true,''];
  529. }catch (\Exception $e){
  530. DB::rollBack();
  531. return [false,$e->getLine().':'.$e->getMessage()];
  532. }
  533. }
  534. public function boxAddRule($data)
  535. {
  536. if(empty($data['param'])) return [false,'请选择包装数据'];
  537. $order_no = (new BoxHookService())->setOrderNo();
  538. $detail = [];
  539. foreach ($data['param'] as $value) {
  540. $key = $this->lock_key.'_'.$value['idlsid'];
  541. list($status,$msg) = $this->limitingSendRequestBackg($key);
  542. if(! $status) return [false,$msg];
  543. if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
  544. if(empty($value['team_id'])) return [false,'班组必须选择'];
  545. $detail[] = [
  546. 'order_no' => $order_no,
  547. 'out_order_no' => $value['csocode'],
  548. 'crt_time' => time(),
  549. 'upd_time' => time(),
  550. 'type' => 1,
  551. 'ext_1' => $value['cinvcode'],
  552. 'ext_2' => $value['technology_material'],
  553. 'ext_3' => $value['cfree1'] ?? "",
  554. 'ext_4' => $value['cfree2'] ?? "",
  555. 'ext_5' => $value['process_mark'],
  556. 'ext_6' => $value['customer_name'] ?? "",
  557. 'ext_7' => $value['cuscode'] ?? "",
  558. 'ext_8' => $value['product_title'],
  559. 'ext_9' => $value['product_size'],
  560. 'top_id' => $value['idlsid'],
  561. 'state' => 0,
  562. 'num' => $value['submit_quantity'],
  563. 'box_type' => 2,
  564. 'team_id' => $value['team_id'],
  565. 'shipment_order_no' => $value['cdlcode'],
  566. ];
  567. }
  568. return [true,['order_no' => $order_no, 'detail' => $detail]];
  569. }
  570. public function boxFhDetail($data){
  571. if(empty($data['idlsid'])) return [false,'请选择数据'];
  572. $result = DB::table('box_detail')->where('del_time',0)
  573. ->where('top_id',$data['idlsid'])
  574. ->select('order_no','ext_6','ext_7','ext_1','ext_8','ext_9','num','ext_2','ext_3','ext_4','ext_5')
  575. ->get()->toArray();
  576. if(! empty($result)){
  577. foreach ($result as $key => $value){
  578. $result[$key] = (array)$value;
  579. }
  580. }
  581. return [true,$result];
  582. }
  583. public function boxFhDel($data){
  584. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  585. $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
  586. DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
  587. 'del_time' => time()
  588. ]);
  589. return [true,''];
  590. }
  591. public function transportDetail($data){
  592. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  593. $order_nos = $data['order_nos'];
  594. $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')->get()->toArray();
  595. foreach ($list as &$v){
  596. $v['product_no'] = $v['ext_1'];
  597. $v['technology_material'] = $v['ext_2'];
  598. $v['technology_name'] = $v['ext_3'];
  599. $v['wood_name'] = $v['ext_4'];
  600. $v['process_mark'] = $v['ext_5'];
  601. $v['customer_no'] = $v['ext_6'];
  602. $v['customer_name'] = $v['ext_7'];
  603. $v['product_title'] = $v['ext_8'];
  604. $v['product_size'] = $v['ext_9'];
  605. }
  606. return [200,$list];
  607. }
  608. public function boxFhBzDetail($data){
  609. if(empty($data['order_no'])) return [false,'请选择包装单数据'];
  610. $result = DB::table('box_detail')->where('del_time',0)
  611. ->whereIn('order_no',$data['order_no'])
  612. ->select('order_no','ext_6','ext_7','ext_1','ext_8','ext_9','num','ext_2','ext_3','ext_4','ext_5')
  613. ->get()->toArray();
  614. if(! empty($result)){
  615. foreach ($result as $key => $value){
  616. $result[$key] = (array)$value;
  617. }
  618. }
  619. return [true,$result];
  620. }
  621. }