BoxService.php 29 KB

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