BoxService.php 29 KB

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