BoxService.php 61 KB

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