BoxService.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. <?php
  2. namespace App\Service\Box;
  3. use App\Jobs\BoxAddJob;
  4. use App\Model\ApplyOrder;
  5. use App\Model\ApplyOrderForYy;
  6. use App\Model\Box;
  7. use App\Model\BoxDetail;
  8. use App\Model\DispatchSub;
  9. use App\Model\Header_ext;
  10. use App\Model\OrdersProduct;
  11. use App\Model\OrdersProductProcess;
  12. use App\Model\SaleOrdersProduct;
  13. use App\Model\Setting;
  14. use App\Model\Team;
  15. use App\Service\ApplyOrderService;
  16. use App\Service\EmployeeService;
  17. use App\Service\FinishedOrderService;
  18. use App\Service\FyyOrderService;
  19. use App\Service\FyySqlServerService;
  20. use App\Service\Service;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Log;
  23. /**
  24. * 包装相关
  25. * @package App\Models
  26. */
  27. class BoxService extends Service
  28. {
  29. protected static $instance;
  30. protected static $box_header;
  31. protected static $box_detail_header;
  32. protected static $box_hook;
  33. public $lock_key = 'hc_box_new_add';
  34. public function __construct()
  35. {
  36. self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
  37. self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
  38. self::$box_hook = BoxHookService::getInstance();
  39. }
  40. public function boxIn($data,$user)
  41. {
  42. list($status, $msg) = $this->boxInRule($data, $user);
  43. if(! $status) return [false, $msg];
  44. //限制频率
  45. $limit_key = $this->lock_key;
  46. list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
  47. if(! $status) return [false, '请稍后操作'];
  48. $job = BoxAddJob::dispatch($data,$user)->onQueue(BoxAddJob::job);
  49. if(! $job) {
  50. $this->dellimitingSendRequestBackgNeed($limit_key);
  51. return [false,'任务未进入后台队列,请联系管理员'];
  52. }
  53. return [true, ''];
  54. }
  55. public function boxInRule($data,$user)
  56. {
  57. try{
  58. $ids = [];
  59. $sale_ids = [];
  60. $data = $data['param'];
  61. $key_list = [];
  62. foreach ($data as $v) {
  63. if($v['id'] < 0) $sale_ids[] = -$v['id'];
  64. else $ids[] = $v['id'];
  65. if($v['id'] < 0) $v['id'] = -$v['id'];
  66. $total = 0;
  67. foreach ($v['params'] as $vv) {
  68. $total += $vv;
  69. }
  70. $key_list[$v['id']] = [
  71. 'detail' => $v['params'],
  72. 'total' => $total,
  73. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  74. ];
  75. }
  76. //数据获取
  77. $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
  78. //校验
  79. $flag = "";
  80. foreach ($product_list as $value){
  81. $key = $value['out_order_no'] . $value['product_no'] . $value['technology_name'];
  82. if(! empty($flag)){
  83. if($flag != $key) return [false , "一次包装只允许包一种产品的一种颜色!"];
  84. }else{
  85. $flag = $key;
  86. }
  87. $num_list = $key_list[$value['id']];
  88. $total = $num_list['total'];
  89. $un_box_num = bcsub($value['dispatch_complete_quantity'] , $value['box_num'],3);
  90. if ($total > $un_box_num) return [false, $value['product_title'] . ' ' . $value['technology_name'] . ' 可包装的数量不足'];
  91. }
  92. return [true, ''];
  93. }catch (\Throwable $e){
  94. return [false,$e->getFile() . '|' . $e->getLine(). '|' .$e->getMessage()];
  95. }
  96. }
  97. public function boxInSettle($data, $user){
  98. $ids = [];
  99. $sale_ids = [];
  100. $top_order_no = $data['order_no'];
  101. $box_no = isset($data['box_no']) ? $data['box_no'] : '';
  102. $transport_no = isset($data['transport_no']) ? $data['transport_no'] : '';
  103. $data = $data['param'];
  104. $key_list = [];
  105. foreach ($data as $v) {
  106. if($v['id'] < 0) $sale_ids[] = -$v['id'];
  107. else $ids[] = $v['id'];
  108. if($v['id'] < 0) $v['id'] = -$v['id'];
  109. $total = 0;
  110. foreach ($v['params'] as $vv) {
  111. $total += $vv;
  112. }
  113. $key_list[$v['id']] = [
  114. 'detail' => $v['params'],
  115. 'total' => $total,
  116. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  117. ];
  118. }
  119. $insert = [];
  120. //锁
  121. $limit_key = $this->lock_key;
  122. try{
  123. DB::beginTransaction();
  124. //数据获取
  125. $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
  126. $sale_product_list = SaleOrdersProduct::wherein('id', $sale_ids)->get()->toArray();
  127. //生产订单包装----------------
  128. $box_insert = [];
  129. foreach ($product_list as $v) {
  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. $ext_1 = $v['product_no'];//产品编号
  135. $ext_2 = $v['technology_material']; //工艺材质 废弃
  136. $ext_3 = $v['technology_name'];//工艺名称 改为颜色
  137. $ext_4 = $v['wood_name'];//木皮 废弃
  138. $ext_5 = $v['process_mark'];//工艺备注 废弃
  139. $ext_8 = $v['product_title'];//产品名称
  140. $out_order_no = $insert['out_order_no'] = $v['out_order_no'];
  141. $top_id = $v['sale_orders_product_id'];
  142. foreach ($detail as $vv){
  143. $box_insert[] = [
  144. 'out_order_no' => $out_order_no,
  145. 'top_id' => $top_id,
  146. 'orders_product_id' => $v['id'],
  147. 'ext_1' => $ext_1,//产品编号
  148. 'ext_2' => $ext_2,
  149. 'ext_3' => $ext_3,//颜色
  150. 'ext_4' => $ext_4,
  151. 'ext_5' => $ext_5,
  152. 'ext_8' => $ext_8,//产品名称
  153. 'num' => $vv,
  154. 'price' => $v['price'],
  155. 'box_type' => 1,
  156. 'team_id' => $team_id,
  157. 'shipment_order_no' => $transport_no,
  158. ];
  159. }
  160. //修改销售订单 生产订单 包装数量
  161. SaleOrdersProduct::where('id',$v['sale_orders_product_id'])->update([
  162. 'box_num' => DB::raw('box_num + '.$total),
  163. ]);
  164. OrdersProduct::where('id',$v['id'])->update([
  165. 'box_num' => DB::raw('box_num + '.$total),
  166. ]);
  167. }
  168. //客户名称
  169. $ext_1 = "";
  170. if(empty($ext_1) && isset($sale_product_list[0]['customer_name'])) $ext_1 = $sale_product_list[0]['customer_name'];
  171. if(empty($ext_1) && isset($product_list[0]['customer_name'])) $ext_1 = $product_list[0]['customer_name'];
  172. $insert['ext_1'] = $ext_1;
  173. $insert['detail'] = $box_insert;
  174. $insert['top_order_no'] = $top_order_no;
  175. $insert['order_no'] = $box_no;
  176. $insert['shipment_order_no'] = $transport_no;
  177. //生成包装单
  178. list($status,$msg) = self::$box_hook->boxInsert($insert);
  179. if(! $status) {
  180. $this->dellimitingSendRequestBackgNeed($limit_key);
  181. DB::rollBack();
  182. return [false, $msg];
  183. }
  184. //自动完工 更新完工数量
  185. $em = new EmployeeService();
  186. $auto = $em->is_auto($user, "bzwg_auto");
  187. if($auto){
  188. list($status, $msg1) = $this->updateFinish($product_list,$key_list,$user);
  189. if(! $status) {
  190. $this->dellimitingSendRequestBackgNeed($limit_key);
  191. DB::rollBack();
  192. return [false, $msg1];
  193. }
  194. }
  195. //包装单
  196. $package_data = $msg->toArray();
  197. //包装单产品
  198. $boxDetail = new BoxDetail(['channel'=>$package_data['top_order_no']]);
  199. $boxDetail = $boxDetail->where('del_time',0)
  200. ->where('order_no',$package_data['order_no'])
  201. ->select('ext_1 as product_no', 'ext_3 as technology_name', 'ext_8 as product_title', 'ext_9 as product_size', 'num as quantity')
  202. ->get()->toArray();
  203. //生成包装领料申请单
  204. list($status, $msg1) = $this->createllSQ($package_data,$boxDetail, $user);
  205. if(! $status) {
  206. $this->dellimitingSendRequestBackgNeed($limit_key);
  207. DB::rollBack();
  208. return [false, $msg1];
  209. }
  210. //生成包装入库申请单
  211. list($status, $msg) = $this->createBZSQ($package_data,$boxDetail, $user);
  212. if(! $status) {
  213. $this->dellimitingSendRequestBackgNeed($limit_key);
  214. DB::rollBack();
  215. return [false, $msg];
  216. }
  217. //是否自动审核 包装入库申请单
  218. $auto = $em->is_auto($user,"bzsq_auto");
  219. if(! empty($package_data) && $auto) {
  220. //用友 ------产成品入库
  221. $service = new FinishedOrderService();
  222. list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
  223. if(! $status) {
  224. $this->dellimitingSendRequestBackgNeed($limit_key);
  225. DB::rollBack();
  226. return [false, $msg];
  227. }
  228. }
  229. DB::commit();
  230. $this->dellimitingSendRequestBackgNeed($limit_key);
  231. return [true, ['package_data' => $package_data]];
  232. }catch (\Throwable $e){
  233. DB::rollBack();
  234. $this->dellimitingSendRequestBackgNeed($limit_key);
  235. if (str_contains($e->getMessage(), '1062') || str_contains($e->getMessage(), 'Duplicate entry')) {
  236. return [false, '网络波动,请重新操作!'];
  237. }
  238. return [false, $e->getFile(). '|' .$e->getLine().'|'.$e->getMessage()];
  239. }
  240. }
  241. public function createllSQ($package_data, $insert_id, $user){
  242. //是否自动审核 包装领料申请单
  243. $em = new EmployeeService();
  244. $auto = $em->is_auto($user,"bzllsq_auto");
  245. $return = [];
  246. $product_no = array_unique(array_column($insert_id,'product_no'));
  247. //获取包装原料
  248. $service = new FyyOrderService();
  249. list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
  250. if($status) $return = $msg;
  251. //组织包装原材料写入数据
  252. $insert = [];
  253. $box_id = $package_data['id'] ?? 0;
  254. foreach ($insert_id as $value){
  255. $t = $return[$value['product_no']] ?? [];
  256. $tmp = [
  257. 'id' => $box_id,
  258. 'quantity' => $value['quantity'] ?? 0,
  259. 'product_no' => "",
  260. 'product_title' => "",
  261. 'product_size' => "",
  262. 'product_unit' => "",
  263. 'top_product_no' => $value['product_no'],
  264. 'top_product_title' => $value['product_title'],
  265. 'technology_name' => $value['technology_name'],
  266. ];
  267. if(! empty($t)){
  268. foreach ($t as $v){
  269. $tmp['product_no'] = $v['product_no'];
  270. $tmp['product_title'] = $v['product_title'];
  271. $tmp['product_size'] = $v['product_size'];
  272. $tmp['product_unit'] = $v['product_unit'];
  273. $insert[] = $tmp;
  274. }
  275. }else{
  276. $tmp['quantity'] = 0;
  277. $insert[] = $tmp;
  278. }
  279. }
  280. try {
  281. DB::beginTransaction();
  282. //生成申请单
  283. $service = new ApplyOrderService();
  284. list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_four, $auto);
  285. if(! $status) {
  286. DB::rollBack();
  287. return [false, $msg];
  288. }
  289. if($auto) {
  290. //生成流水
  291. list($status, $msg) = $service->createRecord($msg);
  292. if(! $status) {
  293. DB::rollBack();
  294. return [false, $msg];
  295. }
  296. }
  297. DB::commit();
  298. }catch (\Throwable $exception){
  299. DB::rollBack();
  300. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  301. }
  302. return [true, ''];
  303. }
  304. public function createBZSQ($package_data, $insert_id, $user){
  305. //是否自动审核 包装入库申请单
  306. $em = new EmployeeService();
  307. $auto = $em->is_auto($user,"bzsq_auto");
  308. try {
  309. DB::beginTransaction();
  310. $box_id = $package_data['id'] ?? 0;
  311. foreach ($insert_id as $key => $value){
  312. $insert_id[$key]['id'] = $box_id;
  313. }
  314. //生成申请单
  315. $service = new ApplyOrderService();
  316. list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_three, $auto);
  317. if(! $status) {
  318. DB::rollBack();
  319. return [false, $msg];
  320. }
  321. //用友写入数据信息
  322. $apply_order_id = $msg;
  323. ApplyOrderForYy::insert([
  324. 'apply_order_id' => $apply_order_id,
  325. 'crt_time' => time(),
  326. 'post' => json_encode($package_data),
  327. ]);
  328. if($auto) {
  329. //生成流水
  330. list($status, $msg) = $service->createRecord($msg);
  331. if(! $status) {
  332. DB::rollBack();
  333. return [false, $msg];
  334. }
  335. }
  336. DB::commit();
  337. }catch (\Throwable $exception){
  338. DB::rollBack();
  339. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  340. }
  341. return [true, ''];
  342. }
  343. public function delBoxLock(){
  344. $this->dellimitingSendRequestBackgNeed($this->lock_key);
  345. }
  346. public function updateFinish($production_list = [], $map,$user){
  347. if(empty($production_list)) return [true,''];
  348. //组织派工数据
  349. $dispatch = DispatchSub::where('del_time',0)
  350. ->whereIn('order_product_id', array_column($production_list,'id'))
  351. ->get()->toArray();
  352. if(empty($dispatch)) return [true,''];
  353. $dispatch_map = [];
  354. foreach ($dispatch as $value){
  355. $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
  356. $dispatch_map[$key][] = $value;
  357. }
  358. $result = [];
  359. foreach ($dispatch_map as $value){
  360. $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
  361. $finished_num = array_sum(array_column($value, 'finished_num'));
  362. if($dispatch_quantity == $finished_num) continue; //同一批派工 且 全部完工的 跳过
  363. foreach ($value as $val){
  364. if($val['dispatch_quantity'] <= $val['finished_num']) continue; //某一个工序完工
  365. //包装数量
  366. $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
  367. if($tmp_num <= 0) continue;
  368. $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
  369. if($tmp_num2 >= $tmp_num){
  370. $val['quantity'] = $tmp_num;
  371. }else{
  372. $val['quantity'] = $tmp_num2;
  373. }
  374. $result[] = $val;
  375. }
  376. }
  377. if(empty($result)) return [true,''];
  378. $time = time();
  379. try {
  380. DB::beginTransaction();
  381. //根据派工数据回写
  382. $last_update = $tmp = [];
  383. foreach ($result as $value){
  384. $finished_num = $value['quantity'] + $value['finished_num'];
  385. DispatchSub::where('id',$value['id'])->update([
  386. 'finished_num' => $finished_num,
  387. 'wg_status' => 1,
  388. ]);
  389. $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
  390. if(! in_array($str, $tmp)){
  391. $last_update[] = [
  392. 'id' => $value['id'],
  393. 'quantity' => $value['quantity'],
  394. 'product_no' => $value['product_no'] ?? "",
  395. 'product_title' => $value['product_title'] ?? "",
  396. 'product_size' => $value['product_size'] ?? "",
  397. 'product_unit' => $value['product_unit'] ?? "",
  398. ];
  399. $tmp[] = $str;
  400. }
  401. $quantity = $value['quantity'] * 1000;
  402. //工序表
  403. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  404. $process_model->where('order_product_id',$value['order_product_id'])
  405. ->where('process_id',$value['process_id'])
  406. ->where('dispatch_no',$value['dispatch_no'])
  407. ->where('status', 1)
  408. ->take($quantity)
  409. ->update([
  410. 'finished_time' => $time,
  411. 'status' => 2,
  412. 'finished_id' => 1,
  413. 'team_id' => 72,
  414. 'equipment_id' => 13,
  415. ]);
  416. }
  417. $service = new FinishedOrderService();
  418. //反写数量
  419. //生产订单
  420. $id = array_column($result,'order_product_id');
  421. $service->writeFinishedQuantityByOrdersProductId($id);
  422. //销售订单
  423. $service->writeFinishedQuantity($id);
  424. //生成完工入库申请单
  425. list($status, $msg) = $this->createWGSQ($last_update, $user);
  426. if(! $status) return [false, $msg];
  427. DB::commit();
  428. }catch (\Throwable $exception){
  429. DB::rollBack();
  430. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  431. }
  432. return [true, ''];
  433. }
  434. public function createWGSQ($insert_id, $user){
  435. //完工入库申请单自动审核并同步用友
  436. $em = new EmployeeService();
  437. $auto = $em->is_auto($user, "wgsq_auto");
  438. try {
  439. DB::beginTransaction();
  440. //生成申请单
  441. $service = new ApplyOrderService();
  442. list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_two, $auto);
  443. if(! $status) {
  444. DB::rollBack();
  445. return [false, $msg];
  446. }
  447. if($auto) {
  448. //生成流水
  449. list($status, $msg) = $service->createRecord($msg);
  450. if(! $status) {
  451. DB::rollBack();
  452. return [false, $msg];
  453. }
  454. }
  455. DB::commit();
  456. }catch (\Throwable $exception){
  457. DB::rollBack();
  458. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  459. }
  460. return [true, ''];
  461. }
  462. public function saveOutOrder($data,$user){
  463. list($status,$msg) = $this->saveOutOrderRule($data,$user);
  464. if(! $status) return [false, $msg];
  465. $limit_key = "saveOutOrder" . $user['id'];
  466. list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
  467. if(! $status) return [false, '正在生成销售出库订单数据,请稍后尝试!'];
  468. //不超时
  469. ini_set('max_execution_time', 0);
  470. try {
  471. DB::beginTransaction();
  472. //组织数据
  473. $yongyou_data = $update = [];
  474. foreach ($data['out_data'] as $value){
  475. $pro_tmp = [];
  476. foreach ($value['product'] as $p){
  477. $t_k = $p['cinvcode'] . $p['cfree1'];
  478. $pro_tmp[$t_k] = [
  479. 'cinvcode' => $p['cinvcode'],
  480. 'cfree1' => $p['cfree1'],
  481. 'iquantity' => $p['iquantity'],
  482. 'line' => $p['line'],
  483. ];
  484. }
  485. if(isset($yongyou_data[$value['customer_code']])){
  486. $yongyou_data[$value['customer_code']]['cdlcode_string'] .= ',' . $value['cdlcode'];
  487. foreach ($pro_tmp as $k => $v){
  488. if(isset($yongyou_data[$value['customer_code']]['product'][$k])){
  489. $yongyou_data[$value['customer_code']]['product'][$k]['iquantity'] += $v['iquantity'];
  490. }else{
  491. $yongyou_data[$value['customer_code']]['product'][$k] = $v;
  492. }
  493. }
  494. }else{
  495. $yongyou_data[$value['customer_code']] = [
  496. 'customer_code' => $value['customer_code'],
  497. 'cdlcode_string' => $value['cdlcode'],
  498. 'product' => $pro_tmp
  499. ];
  500. }
  501. foreach ($value['box_no'] as $box_no){
  502. $update[$box_no] = $value['cdlcode'];
  503. }
  504. }
  505. if(empty($yongyou_data)) {
  506. $this->dellimitingSendRequestBackgNeed($limit_key);
  507. return [false, '暂无写入用友发货出库数据!'];
  508. }
  509. //用友 ------发货出库
  510. $service = new FinishedOrderService();
  511. list($status,$msg) = $service->U8Rdrecord32Save($yongyou_data,$user);
  512. if(! $status) {
  513. $this->dellimitingSendRequestBackgNeed($limit_key);
  514. return [false, $msg];
  515. }
  516. //本地数据更新
  517. if(! empty($update)){
  518. foreach ($update as $key => $value){
  519. Box::where('order_no', $key)->update([
  520. 'shipment_order_no' => $value
  521. ]);
  522. }
  523. }
  524. DB::commit();
  525. $this->dellimitingSendRequestBackgNeed($limit_key);
  526. }catch (\Throwable $exception){
  527. $this->dellimitingSendRequestBackgNeed($limit_key);
  528. DB::rollBack();
  529. return [false, $exception->getFile() . $exception->getFile() . $exception->getMessage()];
  530. }
  531. return [true, ''];
  532. }
  533. public function saveOutOrderRule($data,$user){
  534. if(empty($data['out_data'])) return [false, '未获取到发货数据,操作失败!'];
  535. Log::channel('sendData')->info('发货:提交包装单数据', ["param" => $data]);
  536. //客户校验
  537. $customer_code = array_column($data['out_data'],'customer_code');
  538. $isAllSame = count(array_unique($customer_code)) === 1;
  539. if(! $isAllSame) return [false, '发货出库,请选择同一个客户的订单!'];
  540. $box_no = array_column($data['out_data'],'box_no');
  541. if(empty($box_no)) return [false, '包装单信息不能为空'];
  542. $box_arr = [];
  543. foreach ($box_no as $value){
  544. foreach ($value as $val){
  545. $box_arr[] = $val;
  546. }
  547. }
  548. $box_map = Box::where('del_time',0)
  549. ->whereIn('order_no',$box_arr)
  550. ->pluck('shipment_order_no', "order_no")
  551. ->toArray();
  552. foreach ($box_arr as $value){
  553. $tmp = $box_map[$value] ?? "";
  554. if($tmp != "") return [false, "包装单:" . $value . "已在发货单(" . $tmp .")中发出!"];
  555. }
  556. //用友发货数据查询
  557. $cdlcode = array_column($data['out_data'],'cdlcode');
  558. $service = new FyySqlServerService($user);
  559. if($service->error) return [false, $service->error];
  560. $out_data_map = $service->getDataFromDispatchListForCheck(['cdlcode' => $cdlcode]);
  561. if(empty($out_data_map)) return [false, '未在用友发货数据中查找到相关数据!'];
  562. //数据校验
  563. foreach ($data['out_data'] as $value){
  564. if(empty($value['cdlcode'])) return [false, '发货单号不能为空!'];
  565. if(! isset($out_data_map[$value['cdlcode']])) return [false, '发货单号:' . $value['cdlcode'] . '不存在或已被删除!'];
  566. $tmp = $out_data_map[$value['cdlcode']];
  567. if(empty($value['customer_code'])) return [false, '客户编码不能为空!'];
  568. if($tmp['customer_code'] != $value['customer_code']) return [false, '客户编码与用友数据中不一致!'];
  569. if(empty($tmp['product'])) return [false, '发货单号:' . $value['cdlcode'] . '未查找到用友的发货产品数据!'];
  570. $tmp_pro = [];
  571. foreach ($tmp['product'] as $t){
  572. $t_k = $t['cinvcode'] . $t['cfree1'];
  573. $tmp_pro[$t_k] = bcsub($t['iquantity'] , $t['out_quantity'],3);//未发货数量
  574. }
  575. if(empty($value['product'])) return [false, '发货产品不能为空!'];
  576. foreach ($value['product'] as $p){
  577. if(empty($p['line'])) return [false, '存货行号不能为空!'];
  578. if(empty($p['cinvcode'])) return [false, '存货编码不能为空!'];
  579. if(empty($p['cfree1'])) return [false, '存货颜色不能为空!'];
  580. $p_k = $p['cinvcode'] . $p['cfree1'];
  581. if(! isset($tmp_pro[$p_k])) return [false, '发货单号:' . $value['cdlcode'] . '下无该发货产品:编码为' . $p['cinvcode'] . ',颜色为:' . $p['cfree1']];
  582. if(empty($p['iquantity']) || $p['iquantity'] <= 0) return [false, '发货数量不能为空!'];
  583. $last = $tmp_pro[$p_k];
  584. if($p['iquantity'] > $last) return [false, '发货单号:' . $value['cdlcode'] . '下的发货产品:编码为' . $p['cinvcode'] . ',颜色为' . $p['cfree1'] . '的未发货数量为' . $last];
  585. }
  586. }
  587. return [true, ''];
  588. }
  589. /**
  590. * 根据发货单包装(恒成塑业不需要 如果有调用 直接返回)
  591. * @param $data
  592. * @return array
  593. */
  594. public function transportBoxIn($data)
  595. {
  596. return [true,''];
  597. $param = $data['params'];
  598. $shipment_order_no = $data['transport_no'];
  599. $sale_order_nos = [];
  600. $key_list = [];
  601. foreach ($param as $v){
  602. if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no'];
  603. $key = $this->getKey($v);
  604. $key_list[$key] = [
  605. 'num' => $v['num'],
  606. 'order_no' => $v['order_no'],
  607. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  608. ];
  609. }
  610. $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray();
  611. $sale_order_product_key_list = [];
  612. foreach ($sale_order_product_list as $v){
  613. $key = $this->getKey($v);
  614. $sale_order_product_key_list[$key] = $v['id'];
  615. }
  616. $param = [];
  617. foreach ($key_list as $k=>$v){
  618. if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!'];
  619. $param[$v['order_no']][] = [
  620. 'id' => -(intval($sale_order_product_key_list[$k])),
  621. 'team_id' => $v['team_id'],
  622. 'params' => [$v['num']]
  623. ];
  624. }
  625. // $box_no = self::$box_hook->setOrderNo(); todo
  626. foreach ($param as $k=>$v){
  627. $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no');
  628. $postParam = [
  629. 'order_no' => $top_order_no,
  630. 'param' => $v,
  631. 'box_no' => $box_no,
  632. 'transport_no' => $shipment_order_no,
  633. ];
  634. $this->boxIn($postParam);
  635. }
  636. return [true,''];
  637. }
  638. protected function getKey($data){
  639. return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark'];
  640. }
  641. /**
  642. * 包装详情1用于包装前
  643. * @param $data
  644. * @return array
  645. */
  646. public function boxDetail($data)
  647. {
  648. list($status, $data) = self::$box_hook->boxDetail($data);
  649. if (!$status) return [false, $data];
  650. $return = [];
  651. foreach ($data as $v){
  652. $key = $v['ext_1'] . $v['ext_3'];
  653. if(! isset($return[$key])){
  654. $return[$key] = $v;
  655. }else{
  656. $return[$key]['num'] += $v['num'];
  657. }
  658. }
  659. $sale_orders_product_ids = array_unique(array_column($data,'top_id'));
  660. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  661. $key_list = [];
  662. foreach ($list as $v){
  663. $key_list[$v['id']] = $v;
  664. }
  665. foreach ($return as &$v){
  666. $detail = $key_list[$v['top_id']];
  667. $v['customer_no'] = $detail['customer_no'];
  668. $v['customer_name'] = $detail['customer_name'];
  669. }
  670. return [true, array_values($return)];
  671. }
  672. public function boxProductList($data){
  673. if(empty($data['id']) && empty($data['out_order_no'])) return [false,'请选择数据!'];
  674. if(! empty($data['id'])){
  675. $sale_order_ids = $data['id'];
  676. }else{
  677. $ids = SaleOrdersProduct::where('del_time',0)
  678. ->where('out_order_no','Like','%'. $data['out_order_no'] . '%')
  679. ->select('id')
  680. ->get()->toArray();
  681. $sale_order_ids = array_column($ids,'id');
  682. if(empty($sale_order_ids)) return [false, '暂无数据!'];
  683. }
  684. $model = SaleOrdersProduct::where('del_time',0)
  685. ->whereIn('id',$sale_order_ids)
  686. ->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')
  687. ->orderBy('id','desc')
  688. ->get()->toArray();
  689. //原先按照完工数量 未包装 = 完工数量 - 已包数量
  690. $product_list = ordersProduct::where('del_time',0)
  691. ->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','box_num','technology_name','wood_name','crt_time','production_quantity','dispatch_complete_quantity','technology_material','technology_name','wood_name','process_mark',"production_no")
  692. ->get()->toArray();
  693. $model_key_list = [];
  694. foreach ($model as $v){
  695. $model_key_list[$v['id']] = $v;
  696. }
  697. $model = [];
  698. //存在生产订单,组织数据
  699. if(! empty($product_list)){
  700. $product_key_num_list = [];
  701. foreach ($product_list as $v){
  702. //下派工数量 就是 能包的数量 相等以后 踢掉数据
  703. if($v['dispatch_complete_quantity'] == $v['box_num']) continue;
  704. if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']])) {
  705. $product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
  706. 'product_num' => 0 ,
  707. 'box_num' => 0 ,
  708. ];
  709. }
  710. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
  711. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
  712. $detail = $model_key_list[$v['sale_orders_product_id']];
  713. $detail['box_type'] = 1;
  714. $detail['id'] = $v['id'];
  715. $detail['box_num'] = $v['box_num'];
  716. $detail['dispatch_complete_quantity'] = $v['dispatch_complete_quantity'];
  717. $detail['un_box_num'] = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
  718. $detail['type'] = 1;
  719. $detail['production_no'] = $v['production_no'];
  720. $detail['production_quantity'] = $v['production_quantity'];
  721. $model[] = $detail;
  722. }
  723. }
  724. $return = [];
  725. foreach ($model as $v){
  726. if(!isset($v['box_type'])) {continue; //没有销售订单直接包了。
  727. $product_num = 0;
  728. $box_num = 0;
  729. if(isset($product_key_num_list[$v['id']])){
  730. foreach ($product_key_num_list[$v['id']] as $vv){
  731. $product_num += $vv['product_num'];
  732. $box_num += $vv['box_num'];
  733. }
  734. }
  735. if(($v['order_quantity'] - $product_num) === 0) continue;
  736. $product_key_list[$v['sale_orders_product_id']] = [
  737. 'out_order_no' => $v['out_order_no'],
  738. 'order_no' => $v['order_no'],
  739. 'production_no' => "",
  740. 'production_time' => '未下生产',
  741. 'customer_no' => $v['customer_no'],
  742. 'customer_name' => $v['customer_name'],
  743. 'product_no' => $v['product_no'],
  744. 'product_title' => $v['product_title'],
  745. 'product_size' => $v['product_size'],
  746. 'id' => -$v['sale_orders_product_id'],
  747. 'technology_material' => $v['technology_material'],
  748. 'technology_name' => $v['technology_name'],
  749. 'wood_name' => $v['wood_name'],
  750. 'process_mark' => $v['process_mark'],
  751. 'type' => '2',
  752. 'is_box_num' => $v['box_num'] - $box_num,
  753. 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
  754. 'sale_num' => $v['order_quantity'],
  755. ];
  756. }else{
  757. $return[] = [
  758. 'id' => $v['id'],
  759. 'technology_material' => $v['technology_material'],
  760. 'technology_name' => $v['technology_name'],
  761. 'wood_name' => $v['wood_name'],
  762. 'order_no' => $v['order_no'],
  763. 'production_no' => $v['production_no'],
  764. 'process_mark' => $v['process_mark'],
  765. 'out_order_no' => $v['out_order_no'],
  766. 'production_time' => date('Y-m-d',$v['crt_time']),
  767. 'customer_no' => $v['customer_no'],
  768. 'customer_name' => $v['customer_name'],
  769. 'product_no' => $v['product_no'],
  770. 'product_title' => $v['product_title'],
  771. 'product_size' => $v['product_size'],
  772. 'type' => '1',
  773. 'is_box_num' => $v['box_num'],
  774. 'un_box_num' => $v['un_box_num'],
  775. 'sale_num' => $v['order_quantity'],
  776. 'production_num' => $v['production_quantity'],
  777. 'dispatch_complete_quantity' => $v['dispatch_complete_quantity']
  778. ];
  779. }
  780. }
  781. return [true,$return];
  782. }
  783. /**
  784. * 包装详情2用于包装后
  785. * @param $data
  786. * @return array
  787. */
  788. public function boxOrderDetail($data)
  789. {
  790. list($status, $data) = self::$box_hook->boxDetail($data);
  791. $sale_orders_product_ids = [];
  792. foreach ($data as $v){
  793. $sale_orders_product_ids[] = $v['top_id'];
  794. }
  795. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  796. $key_list = [];
  797. foreach ($list as $v){
  798. $key_list[$v['id']] = $v;
  799. }
  800. foreach ($data as &$v){
  801. $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
  802. $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
  803. $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
  804. $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
  805. $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
  806. }
  807. if (!$status) return [false, $data];
  808. return [true, $data];
  809. }
  810. /**
  811. * 通过销售单号获取包装单
  812. * @param $data
  813. * @return array
  814. */
  815. public function boxOrderDetailByTop($data){
  816. $list = new Box();
  817. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  818. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  819. // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  820. // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  821. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
  822. return [true,$list];
  823. }
  824. /**
  825. * 通过包装单获取包装详情
  826. * @param $data
  827. * @return array
  828. */
  829. public function boxOrderDetailByOrderNo($data){
  830. $order_nos = $data['order_nos'];
  831. $order_nos = array_unique($order_nos);
  832. $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();
  833. $detail_list = [];
  834. foreach ($list as $v){
  835. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  836. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  837. }
  838. $list = $detail_list;
  839. $ids = [];
  840. foreach ($list as $v){
  841. $ids[] = $v['top_id'];
  842. }
  843. $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
  844. $sale_orders_key_product = [];
  845. foreach ($sale_orders_product as $v){
  846. $sale_orders_key_product[$v['id']] = $v;
  847. }
  848. $team_key_list = Team::pluck('title','id')->toArray();
  849. foreach ($list as &$v){
  850. $detail = $sale_orders_key_product[$v['top_id']];
  851. $v['customer_name'] = $detail['customer_name'];
  852. $v['table_body_mark'] = $detail['table_body_mark'];
  853. $v['ext_4'] = $detail['product_title'];
  854. $v['ext_5'] = $detail['product_size'];
  855. $v['customer_name'] = $detail['customer_name'];
  856. $v['technology_material'] = $detail['technology_material'];
  857. $v['transport_no'] = $v['shipment_order_no'];
  858. $v['wood_name'] = $detail['wood_name'];
  859. $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
  860. }
  861. return [true,$list];
  862. }
  863. public function boxOrderGroup($data){
  864. $list = new Box();
  865. if(empty($data['top_order_no']) && empty($data['transport_no'])) return [false,'请输入单号!'];
  866. if(! empty($data['top_order_no'])) $list = $list->whereIn('top_order_no',$data['top_order_no']);
  867. if(! empty($data['transport_no'])) {
  868. if(is_array($data['transport_no'])){
  869. $list = $list->whereIn('shipment_order_no',$data['transport_no']);
  870. }else{
  871. $list = $list->where('shipment_order_no','Like','%'. $data['transport_no'] . '%');
  872. }
  873. }
  874. $list = $list->select('top_order_no','order_no as box_no','crt_time','shipment_order_no','out_order_no')->where('del_time',0)
  875. ->get()->toArray();
  876. foreach ($list as $key => $value){
  877. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  878. $tmp = $model->where('del_time',0)
  879. ->where('order_no',$value['box_no'])
  880. ->where('out_order_no',$value['out_order_no'])
  881. ->select('order_no','out_order_no','orders_product_id')
  882. ->first();
  883. $production_no = "";
  884. if(! empty($tmp)){
  885. $production_no = OrdersProduct::where('id',$tmp->orders_product_id)->select('production_no')->pluck('production_no')->toArray();
  886. $production_no = $production_no[0];
  887. }
  888. $list[$key]['production_no'] = $production_no;
  889. $list[$key]['crt_time'] = date('Y-m-d H:i:s',$value['crt_time']);
  890. }
  891. return [true,$list];
  892. }
  893. public function boxOrderGroupGwp($data){
  894. $out_order_no = $data['out_order_no'] ?? "";
  895. $model = Box::where('del_time',0)
  896. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  897. return $query->where('out_order_no','Like','%'. $out_order_no . '%');
  898. })
  899. ->select('out_order_no','ext_1','top_order_no','order_no','crt_time','shipment_order_no',DB::raw("count(id) as package_num"))
  900. ->groupBy('out_order_no');
  901. $list = $this->limit($model,'',$data);
  902. $list = $this->fillData($list);
  903. return [true, $list];
  904. }
  905. public function fillData($data){
  906. if(empty($data['data'])) return $data;
  907. foreach ($data['data'] as $key => $value){
  908. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  909. $tmp = $model->where('del_time',0)
  910. ->where('out_order_no',$value['out_order_no'])
  911. ->select('num')
  912. ->get()->toArray();
  913. $data['data'][$key]['num'] = $this->getTotal($tmp, 'num');
  914. }
  915. return $data;
  916. }
  917. public function boxOrderGroupGwpDetail($data){
  918. if(empty($data['top_order_no']) || empty($data['out_order_no'])) return [false, '请选择数据!'];
  919. $top_order_no = $data['top_order_no'];
  920. $model = new BoxDetail(['channel'=> $top_order_no]);
  921. $detail_list = $model->where('del_time',0)
  922. ->where('out_order_no',$data['out_order_no'])
  923. ->get()->toArray();
  924. $map = Box::whereIn('order_no',array_unique(array_column($detail_list,'order_no')))
  925. ->pluck('ext_1','order_no')
  926. ->toArray();
  927. $return = [];
  928. foreach ($detail_list as $value){
  929. //销售订单号 包装单号 产品编码 颜色
  930. $key = $value['out_order_no'] . $value['order_no'] . $value['ext_1'] . $value['ext_3'];
  931. if(isset($return[$key])){
  932. $return[$key]['num'] += $value['num'];
  933. }else{
  934. $return[$key] = [
  935. 'box_no' => $value['order_no'],
  936. 'out_order_no' => $value['out_order_no'],
  937. 'ext_8' => $value['ext_8'],
  938. 'ext_3' => $value['ext_3'],
  939. 'num' => $value['num'],
  940. 'crt_time' => date("Y-m-d",$value['crt_time']),
  941. 'top_order_no' => $value['top_order_no'],
  942. 'customer_name' => $map[$value['order_no']] ?? "",
  943. ];
  944. }
  945. }
  946. return [true, array_values($return)];
  947. }
  948. public function transportNo(){
  949. $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
  950. return [true,$list];
  951. }
  952. public function delBoxDetail($data){
  953. if(empty($data['order_nos'])) return [false, '请选择包装单!'];
  954. $order_nos = $data['order_nos'];
  955. try {
  956. DB::beginTransaction();
  957. //更新销售订单 生产订单数据
  958. $orders_product_id_map = [];
  959. foreach ($order_nos as $v){
  960. $list = self::$box_hook->delBox($v);
  961. if(empty($list)) continue;
  962. foreach ($list as $vv){
  963. SaleOrdersProduct::where('id',$vv['top_id'])->update([
  964. 'box_num' => DB::raw('box_num - '.$vv['num']),
  965. 'finished_num' => DB::raw('finished_num - '.$vv['num'])
  966. ]);
  967. OrdersProduct::where('id',$vv['orders_product_id'])->update([
  968. 'box_num' => DB::raw('box_num - '.$vv['num']),
  969. 'finished_num' => DB::raw('finished_num - '.$vv['num'])
  970. ]);
  971. if(isset($orders_product_id_map[$vv['orders_product_id']])){
  972. $num = bcadd($orders_product_id_map[$vv['orders_product_id']], $vv['num'],3);
  973. $orders_product_id_map[$vv['orders_product_id']] = $num;
  974. }else{
  975. $orders_product_id_map[$vv['orders_product_id']] = $vv['num'];
  976. }
  977. }
  978. }
  979. //更新完工数据 和 底表数据
  980. list($status,$msg) = $this->updateFinishDel($orders_product_id_map);
  981. if(! $status) return [false, $msg];
  982. DB::commit();
  983. }catch (\Throwable $exception){
  984. DB::rollBack();
  985. return [false, $exception->getMessage()];
  986. }
  987. return [true, ''];
  988. }
  989. public function updateFinishDel($orders_product_id_map){
  990. if(empty($orders_product_id_map)) return [true,''];
  991. $production_list = array_keys($orders_product_id_map);
  992. //组织派工数据
  993. $dispatch = DispatchSub::where('del_time',0)
  994. ->whereIn('order_product_id', $production_list)
  995. ->get()->toArray();
  996. if(empty($dispatch)) return [true,''];
  997. foreach ($dispatch as $key => $value){
  998. $dispatch[$key]['out_number'] = $orders_product_id_map[$value['order_product_id']] ?? 0;
  999. }
  1000. try {
  1001. DB::beginTransaction();
  1002. //根据派工数据回写
  1003. foreach ($dispatch as $value){
  1004. $finished_num = bcsub($value['finished_num'], $value['out_number'],3);
  1005. DispatchSub::where('id',$value['id'])->update([
  1006. 'finished_num' => $finished_num
  1007. ]);
  1008. $quantity = $value['out_number'] * 1000;
  1009. //工序表
  1010. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  1011. $process_model->where('order_product_id',$value['order_product_id'])
  1012. ->where('process_id',$value['process_id'])
  1013. ->where('dispatch_no',$value['dispatch_no'])
  1014. ->where('status', 2)
  1015. ->take($quantity)
  1016. ->update([
  1017. 'finished_time' => 0,
  1018. 'status' => 1,
  1019. 'finished_id' => 0,
  1020. 'team_id' => 0,
  1021. 'equipment_id' => 0,
  1022. ]);
  1023. }
  1024. DB::commit();
  1025. }catch (\Throwable $exception){
  1026. DB::rollBack();
  1027. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  1028. }
  1029. return [true, ''];
  1030. }
  1031. public function boxAdd($data)
  1032. {
  1033. list($status,$msg) = $this->boxAddRule($data);
  1034. if(! $status) return [false,$msg];
  1035. try{
  1036. DB::beginTransaction();
  1037. $box = new Box();
  1038. $box->order_no = $msg['order_no'];
  1039. $box->save();
  1040. DB::table('box_detail')->insert($msg['detail']);
  1041. DB::commit();
  1042. return [true,''];
  1043. }catch (\Exception $e){
  1044. DB::rollBack();
  1045. return [false,$e->getLine().':'.$e->getMessage()];
  1046. }
  1047. }
  1048. public function boxAddRule($data)
  1049. {
  1050. if(empty($data['param'])) return [false,'请选择包装数据'];
  1051. // $order_no = (new BoxHookService())->setOrderNo();
  1052. $order_no = "";
  1053. if(empty($order_no)) return [false,'包装单号不能为空'];
  1054. $detail = [];
  1055. foreach ($data['param'] as $value) {
  1056. $key = $this->lock_key.'_'.$value['idlsid'];
  1057. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1058. if(! $status) return [false,$msg];
  1059. // if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
  1060. if(empty($value['team_id'])) return [false,'班组必须选择'];
  1061. $tmp = $value;
  1062. $tmp['iquantity'] = $value['submit_quantity'];
  1063. $detail[] = [
  1064. 'order_no' => $order_no,
  1065. 'out_order_no' => $value['csocode']??'',
  1066. 'crt_time' => time(),
  1067. 'upd_time' => time(),
  1068. 'type' => 1,
  1069. 'ext_1' => $value['cinvcode'],
  1070. 'ext_2' => $value['technology_material'],
  1071. 'ext_3' => $value['cfree1'] ?? "",
  1072. 'ext_4' => $value['cfree2'] ?? "",
  1073. 'ext_5' => $value['process_mark'],
  1074. 'ext_6' => $value['customer_name'] ?? "",
  1075. 'ext_7' => $value['cuscode'] ?? "",
  1076. 'ext_8' => $value['product_title'],
  1077. 'ext_9' => $value['product_size'],
  1078. 'ext_10' => $value['table_header_mark'],
  1079. 'ext_11' => $value['table_body_mark'],
  1080. 'top_id' => $value['idlsid'],
  1081. 'state' => 0,
  1082. 'num' => $value['submit_quantity'],
  1083. 'box_type' => 2,
  1084. 'team_id' => $value['team_id'],
  1085. 'shipment_order_no' => $value['cdlcode'],
  1086. 'post' => json_encode($tmp),
  1087. ];
  1088. }
  1089. return [true,['order_no' => $order_no, 'detail' => $detail]];
  1090. }
  1091. public function boxFhDetail($data){
  1092. if(empty($data['idlsid'])) return [false,'请选择数据'];
  1093. $result = DB::table('box_detail')->where('del_time',0)
  1094. ->where('top_id',$data['idlsid'])
  1095. ->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')
  1096. ->get()->toArray();
  1097. if(! empty($result)){
  1098. foreach ($result as $key => $value){
  1099. $result[$key] = (array)$value;
  1100. }
  1101. }
  1102. return [true,$result];
  1103. }
  1104. public function boxFhDel($data){
  1105. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  1106. $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
  1107. DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
  1108. 'del_time' => time()
  1109. ]);
  1110. return [true,''];
  1111. }
  1112. public function transportDetail($data){
  1113. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  1114. $order_nos = $data['order_nos'];
  1115. $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();
  1116. foreach ($list as &$v){
  1117. $v['product_no'] = $v['ext_1'];
  1118. $v['technology_material'] = $v['ext_2'];
  1119. $v['technology_name'] = $v['ext_3'];
  1120. $v['wood_name'] = $v['ext_4'];
  1121. $v['process_mark'] = $v['ext_5'];
  1122. $v['customer_no'] = $v['ext_6'];
  1123. $v['customer_name'] = $v['ext_7'];
  1124. $v['product_title'] = $v['ext_8'];
  1125. $v['product_size'] = $v['ext_9'];
  1126. $v['post'] = json_decode($v['post'],true);
  1127. }
  1128. return [200,$list];
  1129. }
  1130. public function boxFhBzDetail($data){
  1131. if(empty($data['order_no'])) return [false,'请选择包装单数据'];
  1132. $result = DB::table('box_detail')->where('del_time',0)
  1133. ->whereIn('order_no',$data['order_no'])
  1134. ->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')
  1135. ->get()->toArray();
  1136. if(! empty($result)){
  1137. foreach ($result as $key => $value){
  1138. $result[$key] = (array)$value;
  1139. }
  1140. $map = Team::where('id',array_unique(array_column($result,'team_id')))->pluck('title','id')->toArray();
  1141. foreach ($result as $key => $value){
  1142. $result[$key]['team_name'] = $map[$value['team_id']] ?? '';
  1143. }
  1144. }
  1145. return [true,$result];
  1146. }
  1147. public function boxFhBzList($data){
  1148. if(empty($data['shipment_order_no'])) return [false, '请输入发货单号'];
  1149. $model = DB::table('box_detail')->where('del_time',0)
  1150. ->where('shipment_order_no','LIKE', '%'.$data['shipment_order_no'].'%')
  1151. ->select('order_no','crt_time','shipment_order_no');
  1152. $return = [];
  1153. $result = $model->get()->toArray();
  1154. if(! empty($result)){
  1155. foreach ($result as $value){
  1156. $return[$value->order_no] = [
  1157. 'order_no' => $value->order_no,
  1158. 'shipment_order_no' => $value->shipment_order_no,
  1159. 'crt_time' => date("Y-m-d H:i:s",$value->crt_time)
  1160. ];
  1161. }
  1162. }
  1163. return [true,array_values($return)];
  1164. }
  1165. public function boxOrderDetailByOrderNoNew($data){
  1166. if(empty($data['order_nos'])) return [false, '包装单号不能为空'];
  1167. $order_nos = $data['order_nos'];
  1168. $order_nos = array_unique($order_nos);
  1169. Log::channel('sendData')->info('发货:扫码包装单数据', ["param" => $order_nos]);
  1170. $list = Box::where('del_time',0)
  1171. ->whereIn('order_no',$order_nos)
  1172. ->where('shipment_order_no','')
  1173. ->select('order_no','out_order_no','top_order_no','shipment_order_no as transport_no','upd_time')
  1174. ->get()->toArray();
  1175. $detail_list = [];
  1176. foreach ($list as $v){
  1177. // if(! empty($v['transport_no'])) return [false, "包装单号:" . $v['order_no'] . "已在发货单(" . $v['transport_no'] .")中发出!"];
  1178. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1179. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  1180. }
  1181. $return = [];
  1182. foreach ($detail_list as $value){
  1183. //销售订单号 产品编码 颜色
  1184. $return[] = [
  1185. 'ext_1' => $value['ext_1'],
  1186. 'ext_3' => $value['ext_3'],
  1187. 'ext_8' => $value['ext_8'],
  1188. 'num' => $value['num'],
  1189. 'out_order_no' => $value['out_order_no'],
  1190. 'box_no' => $value['order_no']
  1191. ];
  1192. }
  1193. return [true, array_values($return)];
  1194. }
  1195. public function boxList($data,$user){
  1196. $model = Box::where('del_time',0)
  1197. ->select('id','order_no','out_order_no','top_order_no','crt_time','status','shipment_order_no')
  1198. ->orderBy('id','desc');
  1199. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  1200. if(! empty($data['out_order_no']) || ! empty($data['shipment_order_no'])) {
  1201. $id = $this->searchDetail($data);
  1202. $model->whereIn('order_no', $id);
  1203. }
  1204. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  1205. if(isset($data['status'])) $model->where('status',$data['status']);
  1206. $list = $this->limit($model,'',$data);
  1207. $list = $this->fillBoxListData($list, $data,$user);
  1208. return [true, $list];
  1209. }
  1210. public function fillBoxListData($data, $erg,$user){
  1211. if(empty($data['data'])) return $data;
  1212. $product = $material = [];
  1213. if(! empty($erg['box'])){
  1214. //获取包装单里的产品
  1215. $id = array_column($data['data'], 'id');
  1216. list($product,$product_no) = $this->getBoxDetail($id);
  1217. if(! empty($erg['box_material'])){
  1218. //产品的包装材料
  1219. $service = new FyyOrderService();
  1220. list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
  1221. if($status) $material = $msg;
  1222. }
  1223. }
  1224. foreach ($data['data'] as $key => $value){
  1225. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1226. $data['data'][$key]['status_title'] = Box::$status_name[$value['status']] ?? "";
  1227. $p = $product[$value['id']] ?? [];
  1228. if(! empty($p)){
  1229. foreach ($p as $k => $v){
  1230. $p[$k]['box_material'] = $material[$v['product_no']] ?? [];
  1231. }
  1232. }
  1233. $data['data'][$key]['product'] = $p;
  1234. }
  1235. return $data;
  1236. }
  1237. public function getBoxDetail($id = []){
  1238. $return = $product_no= [];
  1239. if(empty($id)) return [$return,$product_no];
  1240. $box = Box::where('del_time',0)
  1241. ->whereIn('id', $id)
  1242. ->select('id', 'order_no','top_order_no')
  1243. ->get()->toArray();
  1244. if (empty($box)) return [$return,$product_no];
  1245. $map = array_column($box,'id','order_no');
  1246. $result = [];
  1247. foreach ($box as $v){
  1248. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1249. $result = array_merge($result,$model->where('del_time',0)
  1250. ->where('order_no',$v['order_no'])
  1251. ->select('order_no', 'ext_1 as product_no', 'ext_8 as product_title', 'ext_9 as product_size','num as quantity','ext_3 as technology_name')->get()->toArray());
  1252. }
  1253. foreach ($result as $value){
  1254. $value['id'] = $map[$value['order_no']] ?? 0;
  1255. $value['product_unit'] = "吨";
  1256. $return[$value['id']][] = $value;
  1257. if(! in_array($value['product_no'], $product_no)) $product_no[] = $value['product_no'];
  1258. }
  1259. return [$return, $product_no];
  1260. }
  1261. public function searchDetail($data){
  1262. $out_order_no = $data['out_order_no'] ?? '';
  1263. $shipment_order_no = $data['shipment_order_no'] ?? '';
  1264. $result = BoxDetail::where('del_time',0)
  1265. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  1266. return $query->where('out_order_no', 'LIKE', '%'. $out_order_no .'%');
  1267. })
  1268. ->when(! empty($shipment_order_no), function ($query) use ($shipment_order_no) {
  1269. return $query->where('shipment_order_no', 'LIKE', '%'. $shipment_order_no .'%');
  1270. })
  1271. ->select('order_no')
  1272. ->get()->toArray();
  1273. return array_unique(array_column($result,'order_no'));
  1274. }
  1275. public function boxFhDetailNew($data){
  1276. if(empty($data['id'])) return [false,'请选择数据'];
  1277. $box = Box::where('id', $data['id'])->where('del_time',0)->first();
  1278. if(empty($box)) return [false, '包装单不存在或已被删除'];
  1279. $box = $box->toArray();
  1280. $model = new BoxDetail(['channel'=>$box['top_order_no']]);
  1281. $result = $model->where('del_time',0)
  1282. ->where('order_no',$box['order_no'])
  1283. ->select('order_no', 'ext_1 as product_no', 'ext_8 as product_title', 'ext_9 as product_size','num as quantity','ext_3 as technology_name','out_order_no')->get()->toArray();
  1284. foreach ($result as $key => $value){
  1285. $result[$key]['product_unit'] = "吨";
  1286. }
  1287. return [true,$result];
  1288. }
  1289. }