BoxService.php 29 KB

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