BoxService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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['process'] = $detail['technology_material'];
  290. $v['process_title'] = $detail['technology_name'];
  291. $v['material_name'] = $detail['wood_name'];
  292. $v['process_mark'] = $detail['process_mark'];
  293. }
  294. return [true, $data];
  295. }
  296. public function boxProductList($data){
  297. if(!isset($data['id'])) return [false,'id not found'];
  298. $sale_order_ids = $data['id'];
  299. $model = SaleOrdersProduct::where('del_time',0)->wherein('id',$sale_order_ids)
  300. ->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')
  301. ->orderBy('id','desc')->get()->toArray();
  302. $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();
  303. $model_key_list = [];
  304. foreach ($model as $v){
  305. $model_key_list[$v['id']] = $v;
  306. }
  307. $product_key_num_list = [];
  308. foreach ($product_list as $v){
  309. if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']]))$product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
  310. 'product_num' => 0 ,
  311. 'box_num' => 0 ,
  312. ];
  313. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
  314. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
  315. $detail = $model_key_list[$v['sale_orders_product_id']];
  316. $detail['box_type'] = 1;
  317. $detail['id'] = $v['id'];
  318. $detail['box_num'] = $v['box_num'];
  319. $detail['un_box_num'] = $v['dispatch_complete_quantity'] - $v['box_num'];
  320. $detail['type'] = 1;
  321. $model[] = $detail;
  322. }
  323. $return = [];
  324. $product_key_list = [];
  325. foreach ($model as $v){
  326. if(!isset($v['box_type'])) {
  327. $product_num = 0;
  328. $box_num = 0;
  329. if(isset($product_key_num_list[$v['id']])){
  330. foreach ($product_key_num_list[$v['id']] as $vv){
  331. $product_num += $vv['product_num'];
  332. $box_num += $vv['box_num'];
  333. }
  334. }
  335. // $product_num = isset($product_key_num_list[$v['id']]) ? $product_key_num_list[$v['id']]['product_num'] : 0;
  336. // $box_num = isset($product_key_num_list[$v['id']]) ? $product_key_num_list[$v['id']]['box_num'] : 0;
  337. if(($v['order_quantity'] - $product_num) === 0) continue;
  338. // var_dump($v['order_quantity']);
  339. // var_dump($v['order_quantity']);
  340. $product_key_list[$v['sale_orders_product_id']] = [
  341. 'out_order_no' => $v['out_order_no'],
  342. 'order_no' => $v['order_no'],
  343. 'production_time' => '未下生产',
  344. 'customer_no' => $v['customer_no'],
  345. 'customer_name' => $v['customer_name'],
  346. 'product_no' => $v['product_no'],
  347. 'product_title' => $v['product_title'],
  348. 'product_size' => $v['product_size'],
  349. 'id' => -$v['sale_orders_product_id'],
  350. 'technology_material' => $v['technology_material'],
  351. 'technology_name' => $v['technology_name'],
  352. 'wood_name' => $v['wood_name'],
  353. 'process_mark' => $v['process_mark'],
  354. 'type' => '2',
  355. 'is_box_num' => $v['box_num'] - $box_num,
  356. 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
  357. 'sale_num' => $v['order_quantity'],
  358. ];
  359. }else{
  360. $return[] = [
  361. 'id' => $v['id'],
  362. 'technology_material' => $v['technology_material'],
  363. 'technology_name' => $v['technology_name'],
  364. 'wood_name' => $v['wood_name'],
  365. 'order_no' => $v['order_no'],
  366. 'process_mark' => $v['process_mark'],
  367. 'out_order_no' => $v['out_order_no'],
  368. 'production_time' => date('Y-m-d',$v['crt_time']),
  369. 'customer_no' => $v['customer_no'],
  370. 'customer_name' => $v['customer_name'],
  371. 'product_no' => $v['product_no'],
  372. 'product_title' => $v['product_title'],
  373. 'product_size' => $v['product_size'],
  374. 'type' => '1',
  375. 'is_box_num' => $v['box_num'],
  376. 'un_box_num' => $v['un_box_num'],
  377. 'sale_num' => $v['order_quantity'],
  378. ];
  379. }
  380. }
  381. foreach ($product_key_list as $v){
  382. $return[] = $v;
  383. }
  384. // $data = [
  385. // [
  386. // 'id' => 1,
  387. // 'out_order_no' => '销售订单号',
  388. // 'production_time' => '下生产时间',
  389. // 'customer_no' => '客户编码',
  390. // 'customer_name' => '客户名称',
  391. // 'product_no' => '产品编码',
  392. // 'product_title' => '产品名称',
  393. // 'product_size' => '产品规格',
  394. // 'type' => '1生产包装2备用包装',
  395. // 'is_box_num' => '已包装数量',
  396. // 'un_box_num' => '未包装',
  397. // 'sale_num' => '销售数量',
  398. // ]
  399. // ];
  400. return [true,$return];
  401. }
  402. /**
  403. * 包装详情2用于包装后
  404. * @param $data
  405. * @return array
  406. */
  407. public function boxOrderDetail($data)
  408. {
  409. list($status, $data) = self::$box_hook->boxDetail($data);
  410. // var_dump($data);die;
  411. $sale_orders_product_ids = [];
  412. foreach ($data as $v){
  413. $sale_orders_product_ids[] = $v['top_id'];
  414. }
  415. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  416. $key_list = [];
  417. foreach ($list as $v){
  418. $key_list[$v['id']] = $v;
  419. }
  420. foreach ($data as &$v){
  421. $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
  422. $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
  423. $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
  424. $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
  425. $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
  426. }
  427. if (!$status) return [false, $data];
  428. return [true, $data];
  429. }
  430. /**
  431. * 通过销售单号获取包装单
  432. * @param $data
  433. * @return array
  434. */
  435. public function boxOrderDetailByTop($data){
  436. $list = new Box();
  437. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  438. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  439. // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  440. // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  441. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
  442. return [true,$list];
  443. }
  444. /**
  445. * 通过包装单获取包装详情
  446. * @param $data
  447. * @return array
  448. */
  449. public function boxOrderDetailByOrderNo($data){
  450. $order_nos = $data['order_nos'];
  451. $order_nos = array_unique($order_nos);
  452. $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();
  453. $detail_list = [];
  454. foreach ($list as $v){
  455. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  456. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  457. }
  458. $list = $detail_list;
  459. $ids = [];
  460. foreach ($list as $v){
  461. $ids[] = $v['top_id'];
  462. }
  463. $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
  464. $sale_orders_key_product = [];
  465. foreach ($sale_orders_product as $v){
  466. $sale_orders_key_product[$v['id']] = $v;
  467. }
  468. $team_key_list = Team::pluck('title','id')->toArray();
  469. foreach ($list as &$v){
  470. $detail = $sale_orders_key_product[$v['top_id']];
  471. // var_dump($detail);die;
  472. $v['customer_name'] = $detail['customer_name'];
  473. $v['table_body_mark'] = $detail['table_body_mark'];
  474. $v['ext_4'] = $detail['product_title'];
  475. $v['ext_5'] = $detail['product_size'];
  476. $v['customer_name'] = $detail['customer_name'];
  477. $v['technology_material'] = $detail['technology_material'];
  478. $v['transport_no'] = $v['shipment_order_no'];
  479. $v['wood_name'] = $detail['wood_name'];
  480. $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
  481. }
  482. return [true,$list];
  483. }
  484. public function boxOrderGroup($data){
  485. $list = new Box();
  486. if(!isset($data['top_order_no'])&&!isset($data['transport_no'])) return [false,'销售单或发货单必选一个'];
  487. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  488. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  489. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->get()->toArray();
  490. return [true,$list];
  491. }
  492. public function transportNo(){
  493. $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
  494. return [true,$list];
  495. }
  496. public function delBoxDetail($data){
  497. $order_nos = $data['order_nos'];
  498. foreach ($order_nos as $v){
  499. $list = self::$box_hook->delBox($v);
  500. foreach ($list as $vv){
  501. SaleOrdersProduct::where('id',$vv['top_id'])->update([
  502. 'box_num' => DB::raw('box_num - '.$vv['num'])
  503. ]);
  504. if($vv['box_type'] == 1){
  505. // $ext_1 = $v['product_no'];//产品编号
  506. // $ext_2 = $v['technology_material']; //工艺材质
  507. // $ext_3 = $v['technology_name'];//工艺名称
  508. // $ext_4 = $v['wood_name'];//木皮
  509. // $ext_5 = $v['process_mark'];//工艺备注
  510. 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'])
  511. ->update([
  512. 'box_num' => DB::raw('box_num - '.$vv['num'])
  513. ]);
  514. }
  515. }
  516. }
  517. return [true,'删除成功'];
  518. }
  519. public function boxAdd($data)
  520. {
  521. list($status,$msg) = $this->boxAddRule($data);
  522. if(! $status) return [false,$msg];
  523. try{
  524. DB::beginTransaction();
  525. $box = new Box();
  526. $box->order_no = $msg['order_no'];
  527. $box->save();
  528. DB::table('box_detail')->insert($msg['detail']);
  529. DB::commit();
  530. return [true,''];
  531. }catch (\Exception $e){
  532. DB::rollBack();
  533. return [false,$e->getLine().':'.$e->getMessage()];
  534. }
  535. }
  536. public function boxAddRule($data)
  537. {
  538. if(empty($data['param'])) return [false,'请选择包装数据'];
  539. $order_no = (new BoxHookService())->setOrderNo();
  540. $detail = [];
  541. foreach ($data['param'] as $value) {
  542. $key = $this->lock_key.'_'.$value['idlsid'];
  543. list($status,$msg) = $this->limitingSendRequestBackg($key);
  544. if(! $status) return [false,$msg];
  545. if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
  546. if(empty($value['team_id'])) return [false,'班组必须选择'];
  547. $detail[] = [
  548. 'order_no' => $order_no,
  549. 'out_order_no' => $value['csocode'],
  550. 'crt_time' => time(),
  551. 'upd_time' => time(),
  552. 'type' => 1,
  553. 'ext_1' => $value['cinvcode'],
  554. 'ext_2' => $value['technology_material'],
  555. 'ext_3' => $value['cfree1'] ?? "",
  556. 'ext_4' => $value['cfree2'] ?? "",
  557. 'ext_5' => $value['process_mark'],
  558. 'ext_6' => $value['customer_name'] ?? "",
  559. 'ext_7' => $value['cuscode'] ?? "",
  560. 'ext_8' => $value['product_title'],
  561. 'ext_9' => $value['product_size'],
  562. 'top_id' => $value['idlsid'],
  563. 'state' => 0,
  564. 'num' => $value['submit_quantity'],
  565. 'box_type' => 2,
  566. 'team_id' => $value['team_id'],
  567. 'shipment_order_no' => $value['cdlcode'],
  568. ];
  569. }
  570. return [true,['order_no' => $order_no, 'detail' => $detail]];
  571. }
  572. public function boxFhDetail($data){
  573. if(empty($data['idlsid'])) return [false,'请选择数据'];
  574. $result = DB::table('box_detail')->where('del_time',0)
  575. ->select('order_no','ext_6','ext_7','ext_1','ext_8','ext_9','num','ext_2','ext_3','ext_4','ext_5')
  576. ->get()->toArray();
  577. if(! empty($result)){
  578. foreach ($result as $key => $value){
  579. $result[$key] = (array)$value;
  580. }
  581. }
  582. return [true,$result];
  583. }
  584. public function boxFhDel($data){
  585. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  586. $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
  587. DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
  588. 'del_time' => time()
  589. ]);
  590. return [true,''];
  591. }
  592. }