BoxService.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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,$user);
  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. $tmp['team_id'] = 0;
  434. $last_update2[] = $tmp;
  435. }else{
  436. $t_m = $team_man[$team_tmp] ?? []; //班组下的人
  437. if(empty($t_m)) {
  438. $tmp['finished_id'] = 0;
  439. $tmp['team_id'] = $team_tmp;
  440. $last_update2[] = $tmp;
  441. }else{
  442. foreach ($t_m as $m_v){
  443. $tmp['finished_id'] = $m_v;
  444. $tmp['team_id'] = $team_tmp;
  445. $last_update2[] = $tmp;
  446. }
  447. }
  448. }
  449. $quantity = $value['quantity'] * 1000;
  450. //工序表
  451. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  452. $process_model->where('order_product_id',$value['order_product_id'])
  453. ->where('process_id',$value['process_id'])
  454. ->where('dispatch_no',$value['dispatch_no'])
  455. ->where('status', 1)
  456. ->take($quantity)
  457. ->update([
  458. 'finished_time' => $time,
  459. 'status' => 2,
  460. 'finished_id' => 1,
  461. 'team_id' => $team_tmp,
  462. 'equipment_id' => 13,
  463. ]);
  464. }
  465. $service = new FinishedOrderService();
  466. //反写数量
  467. //生产订单
  468. $id = array_column($result,'order_product_id');
  469. $service->writeFinishedQuantityByOrdersProductId($id);
  470. //销售订单
  471. $service->writeFinishedQuantity($id);
  472. //生成完工入库申请单
  473. list($status, $msg) = $this->createWGSQ($last_update, $user);
  474. if(! $status) return [false, $msg];
  475. //生成报工单
  476. $service = new FinishedOrderService();
  477. list($status, $msg) = $service->createbg($last_update2, $user);
  478. if(! $status) return [false, $msg];
  479. DB::commit();
  480. }catch (\Throwable $exception){
  481. DB::rollBack();
  482. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  483. }
  484. return [true, ''];
  485. }
  486. public function createWGSQ($insert_id, $user){
  487. //完工入库申请单自动审核并同步用友
  488. $em = new EmployeeService();
  489. $auto = $em->is_auto($user, "wgsq_auto");
  490. try {
  491. DB::beginTransaction();
  492. //生成申请单
  493. $service = new ApplyOrderService();
  494. list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_two, $auto);
  495. if(! $status) {
  496. DB::rollBack();
  497. return [false, $msg];
  498. }
  499. if($auto) {
  500. //生成流水
  501. list($status, $msg) = $service->createRecord($msg);
  502. if(! $status) {
  503. DB::rollBack();
  504. return [false, $msg];
  505. }
  506. }
  507. DB::commit();
  508. }catch (\Throwable $exception){
  509. DB::rollBack();
  510. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  511. }
  512. return [true, ''];
  513. }
  514. public function saveOutOrder($data,$user){
  515. list($status,$msg) = $this->saveOutOrderRule($data,$user);
  516. if(! $status) return [false, $msg];
  517. $limit_key = "saveOutOrder" . $user['id'];
  518. list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
  519. if(! $status) return [false, '正在生成销售出库订单数据,请稍后尝试!'];
  520. //不超时
  521. ini_set('max_execution_time', 0);
  522. try {
  523. DB::beginTransaction();
  524. //组织数据
  525. $yongyou_data = $update = [];
  526. foreach ($data['out_data'] as $value){
  527. $pro_tmp = [];
  528. foreach ($value['product'] as $p){
  529. $t_k = $p['cinvcode'] . $p['cfree1'];
  530. $pro_tmp[$t_k] = [
  531. 'cinvcode' => $p['cinvcode'],
  532. 'cfree1' => $p['cfree1'],
  533. 'iquantity' => $p['iquantity'],
  534. 'line' => $p['line'],
  535. ];
  536. }
  537. if(isset($yongyou_data[$value['customer_code']])){
  538. $yongyou_data[$value['customer_code']]['cdlcode_string'] .= ',' . $value['cdlcode'];
  539. foreach ($pro_tmp as $k => $v){
  540. if(isset($yongyou_data[$value['customer_code']]['product'][$k])){
  541. $yongyou_data[$value['customer_code']]['product'][$k]['iquantity'] += $v['iquantity'];
  542. }else{
  543. $yongyou_data[$value['customer_code']]['product'][$k] = $v;
  544. }
  545. }
  546. }else{
  547. $yongyou_data[$value['customer_code']] = [
  548. 'customer_code' => $value['customer_code'],
  549. 'cdlcode_string' => $value['cdlcode'],
  550. 'product' => $pro_tmp
  551. ];
  552. }
  553. foreach ($value['box_no'] as $box_no){
  554. $update[$box_no] = $value['cdlcode'];
  555. }
  556. }
  557. if(empty($yongyou_data)) {
  558. $this->dellimitingSendRequestBackgNeed($limit_key);
  559. return [false, '暂无写入用友发货出库数据!'];
  560. }
  561. //用友 ------发货出库
  562. $service = new FinishedOrderService();
  563. list($status,$msg) = $service->U8Rdrecord32Save($yongyou_data,$user);
  564. if(! $status) {
  565. $this->dellimitingSendRequestBackgNeed($limit_key);
  566. return [false, $msg];
  567. }
  568. //本地数据更新
  569. if(! empty($update)){
  570. foreach ($update as $key => $value){
  571. Box::where('order_no', $key)->update([
  572. 'shipment_order_no' => $value
  573. ]);
  574. }
  575. }
  576. DB::commit();
  577. $this->dellimitingSendRequestBackgNeed($limit_key);
  578. }catch (\Throwable $exception){
  579. $this->dellimitingSendRequestBackgNeed($limit_key);
  580. DB::rollBack();
  581. return [false, $exception->getFile() . $exception->getFile() . $exception->getMessage()];
  582. }
  583. return [true, ''];
  584. }
  585. public function saveOutOrderRule($data,$user){
  586. if(empty($data['out_data'])) return [false, '未获取到发货数据,操作失败!'];
  587. Log::channel('sendData')->info('发货:提交包装单数据', ["param" => $data]);
  588. //客户校验
  589. $customer_code = array_column($data['out_data'],'customer_code');
  590. $isAllSame = count(array_unique($customer_code)) === 1;
  591. if(! $isAllSame) return [false, '发货出库,请选择同一个客户的订单!'];
  592. $box_no = array_column($data['out_data'],'box_no');
  593. if(empty($box_no)) return [false, '包装单信息不能为空'];
  594. $box_arr = [];
  595. foreach ($box_no as $value){
  596. foreach ($value as $val){
  597. $box_arr[] = $val;
  598. }
  599. }
  600. $box_map = Box::where('del_time',0)
  601. ->whereIn('order_no',$box_arr)
  602. ->pluck('shipment_order_no', "order_no")
  603. ->toArray();
  604. foreach ($box_arr as $value){
  605. $tmp = $box_map[$value] ?? "";
  606. if($tmp != "") return [false, "包装单:" . $value . "已在发货单(" . $tmp .")中发出!"];
  607. }
  608. //用友发货数据查询
  609. $cdlcode = array_column($data['out_data'],'cdlcode');
  610. $service = new FyySqlServerService($user);
  611. if($service->error) return [false, $service->error];
  612. $out_data_map = $service->getDataFromDispatchListForCheck(['cdlcode' => $cdlcode]);
  613. if(empty($out_data_map)) return [false, '未在用友发货数据中查找到相关数据!'];
  614. //数据校验
  615. foreach ($data['out_data'] as $value){
  616. if(empty($value['cdlcode'])) return [false, '发货单号不能为空!'];
  617. if(! isset($out_data_map[$value['cdlcode']])) return [false, '发货单号:' . $value['cdlcode'] . '不存在或已被删除!'];
  618. $tmp = $out_data_map[$value['cdlcode']];
  619. if(empty($value['customer_code'])) return [false, '客户编码不能为空!'];
  620. if($tmp['customer_code'] != $value['customer_code']) return [false, '客户编码与用友数据中不一致!'];
  621. if(empty($tmp['product'])) return [false, '发货单号:' . $value['cdlcode'] . '未查找到用友的发货产品数据!'];
  622. $tmp_pro = [];
  623. foreach ($tmp['product'] as $t){
  624. $t_k = $t['cinvcode'] . $t['cfree1'];
  625. $tmp_pro[$t_k] = bcsub($t['iquantity'] , $t['out_quantity'],3);//未发货数量
  626. }
  627. if(empty($value['product'])) return [false, '发货产品不能为空!'];
  628. foreach ($value['product'] as $p){
  629. if(empty($p['line'])) return [false, '存货行号不能为空!'];
  630. if(empty($p['cinvcode'])) return [false, '存货编码不能为空!'];
  631. if(empty($p['cfree1'])) return [false, '存货颜色不能为空!'];
  632. $p_k = $p['cinvcode'] . $p['cfree1'];
  633. if(! isset($tmp_pro[$p_k])) return [false, '发货单号:' . $value['cdlcode'] . '下无该发货产品:编码为' . $p['cinvcode'] . ',颜色为:' . $p['cfree1']];
  634. if(empty($p['iquantity']) || $p['iquantity'] <= 0) return [false, '发货数量不能为空!'];
  635. $last = $tmp_pro[$p_k];
  636. if($p['iquantity'] > $last) return [false, '发货单号:' . $value['cdlcode'] . '下的发货产品:编码为' . $p['cinvcode'] . ',颜色为' . $p['cfree1'] . '的未发货数量为' . $last];
  637. }
  638. }
  639. return [true, ''];
  640. }
  641. /**
  642. * 根据发货单包装(恒成塑业不需要 如果有调用 直接返回)
  643. * @param $data
  644. * @return array
  645. */
  646. public function transportBoxIn($data)
  647. {
  648. return [true,''];
  649. $param = $data['params'];
  650. $shipment_order_no = $data['transport_no'];
  651. $sale_order_nos = [];
  652. $key_list = [];
  653. foreach ($param as $v){
  654. if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no'];
  655. $key = $this->getKey($v);
  656. $key_list[$key] = [
  657. 'num' => $v['num'],
  658. 'order_no' => $v['order_no'],
  659. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  660. ];
  661. }
  662. $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray();
  663. $sale_order_product_key_list = [];
  664. foreach ($sale_order_product_list as $v){
  665. $key = $this->getKey($v);
  666. $sale_order_product_key_list[$key] = $v['id'];
  667. }
  668. $param = [];
  669. foreach ($key_list as $k=>$v){
  670. if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!'];
  671. $param[$v['order_no']][] = [
  672. 'id' => -(intval($sale_order_product_key_list[$k])),
  673. 'team_id' => $v['team_id'],
  674. 'params' => [$v['num']]
  675. ];
  676. }
  677. // $box_no = self::$box_hook->setOrderNo(); todo
  678. foreach ($param as $k=>$v){
  679. $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no');
  680. $postParam = [
  681. 'order_no' => $top_order_no,
  682. 'param' => $v,
  683. 'box_no' => $box_no,
  684. 'transport_no' => $shipment_order_no,
  685. ];
  686. $this->boxIn($postParam);
  687. }
  688. return [true,''];
  689. }
  690. protected function getKey($data){
  691. return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark'];
  692. }
  693. /**
  694. * 包装详情1用于包装前
  695. * @param $data
  696. * @return array
  697. */
  698. public function boxDetail($data)
  699. {
  700. list($status, $data) = self::$box_hook->boxDetail($data);
  701. if (!$status) return [false, $data];
  702. $return = [];
  703. foreach ($data as $v){
  704. $key = $v['ext_1'] . $v['ext_3'];
  705. if(! isset($return[$key])){
  706. $return[$key] = $v;
  707. }else{
  708. $return[$key]['num'] += $v['num'];
  709. }
  710. }
  711. $sale_orders_product_ids = array_unique(array_column($data,'top_id'));
  712. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  713. $key_list = [];
  714. foreach ($list as $v){
  715. $key_list[$v['id']] = $v;
  716. }
  717. foreach ($return as &$v){
  718. $detail = $key_list[$v['top_id']];
  719. $v['customer_no'] = $detail['customer_no'];
  720. $v['customer_name'] = $detail['customer_name'];
  721. }
  722. return [true, array_values($return)];
  723. }
  724. public function boxProductList($data){
  725. if(empty($data['id']) && empty($data['out_order_no'])) return [false,'请选择数据!'];
  726. if(! empty($data['id'])){
  727. $sale_order_ids = $data['id'];
  728. }else{
  729. $ids = SaleOrdersProduct::where('del_time',0)
  730. ->where('out_order_no','Like','%'. $data['out_order_no'] . '%')
  731. ->select('id')
  732. ->get()->toArray();
  733. $sale_order_ids = array_column($ids,'id');
  734. if(empty($sale_order_ids)) return [false, '暂无数据!'];
  735. }
  736. $model = SaleOrdersProduct::where('del_time',0)
  737. ->whereIn('id',$sale_order_ids)
  738. ->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')
  739. ->orderBy('id','desc')
  740. ->get()->toArray();
  741. //原先按照完工数量 未包装 = 完工数量 - 已包数量
  742. $product_list = ordersProduct::where('del_time',0)
  743. ->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")
  744. ->get()->toArray();
  745. $model_key_list = [];
  746. foreach ($model as $v){
  747. $model_key_list[$v['id']] = $v;
  748. }
  749. $model = [];
  750. //存在生产订单,组织数据
  751. if(! empty($product_list)){
  752. $product_key_num_list = [];
  753. foreach ($product_list as $v){
  754. //下派工数量 就是 能包的数量 相等以后 踢掉数据
  755. if($v['dispatch_complete_quantity'] == $v['box_num']) continue;
  756. if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']])) {
  757. $product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
  758. 'product_num' => 0 ,
  759. 'box_num' => 0 ,
  760. ];
  761. }
  762. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
  763. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
  764. $detail = $model_key_list[$v['sale_orders_product_id']];
  765. $detail['box_type'] = 1;
  766. $detail['id'] = $v['id'];
  767. $detail['box_num'] = $v['box_num'];
  768. $detail['dispatch_complete_quantity'] = $v['dispatch_complete_quantity'];
  769. $detail['un_box_num'] = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
  770. $detail['type'] = 1;
  771. $detail['production_no'] = $v['production_no'];
  772. $detail['production_quantity'] = $v['production_quantity'];
  773. $model[] = $detail;
  774. }
  775. }
  776. $return = [];
  777. foreach ($model as $v){
  778. if(!isset($v['box_type'])) {continue; //没有销售订单直接包了。
  779. $product_num = 0;
  780. $box_num = 0;
  781. if(isset($product_key_num_list[$v['id']])){
  782. foreach ($product_key_num_list[$v['id']] as $vv){
  783. $product_num += $vv['product_num'];
  784. $box_num += $vv['box_num'];
  785. }
  786. }
  787. if(($v['order_quantity'] - $product_num) === 0) continue;
  788. $product_key_list[$v['sale_orders_product_id']] = [
  789. 'out_order_no' => $v['out_order_no'],
  790. 'order_no' => $v['order_no'],
  791. 'production_no' => "",
  792. 'production_time' => '未下生产',
  793. 'customer_no' => $v['customer_no'],
  794. 'customer_name' => $v['customer_name'],
  795. 'product_no' => $v['product_no'],
  796. 'product_title' => $v['product_title'],
  797. 'product_size' => $v['product_size'],
  798. 'id' => -$v['sale_orders_product_id'],
  799. 'technology_material' => $v['technology_material'],
  800. 'technology_name' => $v['technology_name'],
  801. 'wood_name' => $v['wood_name'],
  802. 'process_mark' => $v['process_mark'],
  803. 'type' => '2',
  804. 'is_box_num' => $v['box_num'] - $box_num,
  805. 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
  806. 'sale_num' => $v['order_quantity'],
  807. ];
  808. }else{
  809. $return[] = [
  810. 'id' => $v['id'],
  811. 'technology_material' => $v['technology_material'],
  812. 'technology_name' => $v['technology_name'],
  813. 'wood_name' => $v['wood_name'],
  814. 'order_no' => $v['order_no'],
  815. 'production_no' => $v['production_no'],
  816. 'process_mark' => $v['process_mark'],
  817. 'out_order_no' => $v['out_order_no'],
  818. 'production_time' => date('Y-m-d',$v['crt_time']),
  819. 'customer_no' => $v['customer_no'],
  820. 'customer_name' => $v['customer_name'],
  821. 'product_no' => $v['product_no'],
  822. 'product_title' => $v['product_title'],
  823. 'product_size' => $v['product_size'],
  824. 'type' => '1',
  825. 'is_box_num' => $v['box_num'],
  826. 'un_box_num' => $v['un_box_num'],
  827. 'sale_num' => $v['order_quantity'],
  828. 'production_num' => $v['production_quantity'],
  829. 'dispatch_complete_quantity' => $v['dispatch_complete_quantity']
  830. ];
  831. }
  832. }
  833. return [true,$return];
  834. }
  835. /**
  836. * 包装详情2用于包装后
  837. * @param $data
  838. * @return array
  839. */
  840. public function boxOrderDetail($data)
  841. {
  842. list($status, $data) = self::$box_hook->boxDetail($data);
  843. $sale_orders_product_ids = [];
  844. foreach ($data as $v){
  845. $sale_orders_product_ids[] = $v['top_id'];
  846. }
  847. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  848. $key_list = [];
  849. foreach ($list as $v){
  850. $key_list[$v['id']] = $v;
  851. }
  852. foreach ($data as &$v){
  853. $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
  854. $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
  855. $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
  856. $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
  857. $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
  858. }
  859. if (!$status) return [false, $data];
  860. return [true, $data];
  861. }
  862. /**
  863. * 通过销售单号获取包装单
  864. * @param $data
  865. * @return array
  866. */
  867. public function boxOrderDetailByTop($data){
  868. $list = new Box();
  869. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  870. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  871. // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  872. // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  873. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
  874. return [true,$list];
  875. }
  876. /**
  877. * 通过包装单获取包装详情
  878. * @param $data
  879. * @return array
  880. */
  881. public function boxOrderDetailByOrderNo($data){
  882. $order_nos = $data['order_nos'];
  883. $order_nos = array_unique($order_nos);
  884. $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();
  885. $detail_list = [];
  886. foreach ($list as $v){
  887. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  888. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  889. }
  890. $list = $detail_list;
  891. $ids = [];
  892. foreach ($list as $v){
  893. $ids[] = $v['top_id'];
  894. }
  895. $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
  896. $sale_orders_key_product = [];
  897. foreach ($sale_orders_product as $v){
  898. $sale_orders_key_product[$v['id']] = $v;
  899. }
  900. $team_key_list = Team::pluck('title','id')->toArray();
  901. foreach ($list as &$v){
  902. $detail = $sale_orders_key_product[$v['top_id']];
  903. $v['customer_name'] = $detail['customer_name'];
  904. $v['table_body_mark'] = $detail['table_body_mark'];
  905. $v['ext_4'] = $detail['product_title'];
  906. $v['ext_5'] = $detail['product_size'];
  907. $v['customer_name'] = $detail['customer_name'];
  908. $v['technology_material'] = $detail['technology_material'];
  909. $v['transport_no'] = $v['shipment_order_no'];
  910. $v['wood_name'] = $detail['wood_name'];
  911. $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
  912. }
  913. return [true,$list];
  914. }
  915. public function boxOrderGroup($data){
  916. $list = new Box();
  917. if(empty($data['top_order_no']) && empty($data['transport_no'])) return [false,'请输入单号!'];
  918. if(! empty($data['top_order_no'])) $list = $list->whereIn('top_order_no',$data['top_order_no']);
  919. if(! empty($data['transport_no'])) {
  920. if(is_array($data['transport_no'])){
  921. $list = $list->whereIn('shipment_order_no',$data['transport_no']);
  922. }else{
  923. $list = $list->where('shipment_order_no','Like','%'. $data['transport_no'] . '%');
  924. }
  925. }
  926. $list = $list->select('top_order_no','order_no as box_no','crt_time','shipment_order_no','out_order_no')->where('del_time',0)
  927. ->get()->toArray();
  928. foreach ($list as $key => $value){
  929. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  930. $tmp = $model->where('del_time',0)
  931. ->where('order_no',$value['box_no'])
  932. ->where('out_order_no',$value['out_order_no'])
  933. ->select('order_no','out_order_no','orders_product_id')
  934. ->first();
  935. $production_no = "";
  936. if(! empty($tmp)){
  937. $production_no = OrdersProduct::where('id',$tmp->orders_product_id)->select('production_no')->pluck('production_no')->toArray();
  938. $production_no = $production_no[0];
  939. }
  940. $list[$key]['production_no'] = $production_no;
  941. $list[$key]['crt_time'] = date('Y-m-d H:i:s',$value['crt_time']);
  942. }
  943. return [true,$list];
  944. }
  945. public function boxOrderGroupGwp($data){
  946. $out_order_no = $data['out_order_no'] ?? "";
  947. $model = Box::where('del_time',0)
  948. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  949. return $query->where('out_order_no','Like','%'. $out_order_no . '%');
  950. })
  951. ->select('out_order_no','ext_1','top_order_no','order_no','crt_time','shipment_order_no',DB::raw("count(id) as package_num"))
  952. ->groupBy('out_order_no');
  953. $list = $this->limit($model,'',$data);
  954. $list = $this->fillData($list);
  955. return [true, $list];
  956. }
  957. public function fillData($data){
  958. if(empty($data['data'])) return $data;
  959. foreach ($data['data'] as $key => $value){
  960. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  961. $tmp = $model->where('del_time',0)
  962. ->where('out_order_no',$value['out_order_no'])
  963. ->select('num')
  964. ->get()->toArray();
  965. $data['data'][$key]['num'] = $this->getTotal($tmp, 'num');
  966. }
  967. return $data;
  968. }
  969. public function boxOrderGroupGwpDetail($data){
  970. if(empty($data['top_order_no']) || empty($data['out_order_no'])) return [false, '请选择数据!'];
  971. $top_order_no = $data['top_order_no'];
  972. $model = new BoxDetail(['channel'=> $top_order_no]);
  973. $detail_list = $model->where('del_time',0)
  974. ->where('out_order_no',$data['out_order_no'])
  975. ->get()->toArray();
  976. $map = Box::whereIn('order_no',array_unique(array_column($detail_list,'order_no')))
  977. ->pluck('ext_1','order_no')
  978. ->toArray();
  979. $return = [];
  980. foreach ($detail_list as $value){
  981. //销售订单号 包装单号 产品编码 颜色
  982. $key = $value['out_order_no'] . $value['order_no'] . $value['ext_1'] . $value['ext_3'];
  983. if(isset($return[$key])){
  984. $return[$key]['num'] += $value['num'];
  985. }else{
  986. $return[$key] = [
  987. 'box_no' => $value['order_no'],
  988. 'out_order_no' => $value['out_order_no'],
  989. 'ext_8' => $value['ext_8'],
  990. 'ext_3' => $value['ext_3'],
  991. 'num' => $value['num'],
  992. 'crt_time' => date("Y-m-d",$value['crt_time']),
  993. 'top_order_no' => $value['top_order_no'],
  994. 'customer_name' => $map[$value['order_no']] ?? "",
  995. ];
  996. }
  997. }
  998. return [true, array_values($return)];
  999. }
  1000. public function transportNo(){
  1001. $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
  1002. return [true,$list];
  1003. }
  1004. public function delBoxDetail($data){
  1005. if(empty($data['order_nos'])) return [false, '请选择包装单!'];
  1006. $order_nos = $data['order_nos'];
  1007. try {
  1008. DB::beginTransaction();
  1009. //更新销售订单 生产订单数据
  1010. $orders_product_id_map = [];
  1011. foreach ($order_nos as $v){
  1012. $list = self::$box_hook->delBox($v);
  1013. if(empty($list)) continue;
  1014. foreach ($list as $vv){
  1015. SaleOrdersProduct::where('id',$vv['top_id'])->update([
  1016. 'box_num' => DB::raw('box_num - '.$vv['num']),
  1017. 'finished_num' => DB::raw('finished_num - '.$vv['num'])
  1018. ]);
  1019. OrdersProduct::where('id',$vv['orders_product_id'])->update([
  1020. 'box_num' => DB::raw('box_num - '.$vv['num']),
  1021. 'finished_num' => DB::raw('finished_num - '.$vv['num'])
  1022. ]);
  1023. if(isset($orders_product_id_map[$vv['orders_product_id']])){
  1024. $num = bcadd($orders_product_id_map[$vv['orders_product_id']], $vv['num'],3);
  1025. $orders_product_id_map[$vv['orders_product_id']] = $num;
  1026. }else{
  1027. $orders_product_id_map[$vv['orders_product_id']] = $vv['num'];
  1028. }
  1029. }
  1030. }
  1031. //更新完工数据 和 底表数据
  1032. list($status,$msg) = $this->updateFinishDel($orders_product_id_map);
  1033. if(! $status) return [false, $msg];
  1034. DB::commit();
  1035. }catch (\Throwable $exception){
  1036. DB::rollBack();
  1037. return [false, $exception->getMessage()];
  1038. }
  1039. return [true, ''];
  1040. }
  1041. public function updateFinishDel($orders_product_id_map){
  1042. if(empty($orders_product_id_map)) return [true,''];
  1043. $production_list = array_keys($orders_product_id_map);
  1044. $dispatch = $this->getDispatch($production_list, $orders_product_id_map);
  1045. if(empty($dispatch)) return [true,''];
  1046. try {
  1047. DB::beginTransaction();
  1048. //根据派工数据回写
  1049. foreach ($dispatch as $value){
  1050. $finished_num = bcsub($value['finished_num'], $value['out_number'],3);
  1051. DispatchSub::where('id',$value['id'])->update([
  1052. 'finished_num' => $finished_num
  1053. ]);
  1054. $quantity = $value['out_number'] * 1000;
  1055. //工序表
  1056. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  1057. $process_model->where('order_product_id',$value['order_product_id'])
  1058. ->where('process_id',$value['process_id'])
  1059. ->where('dispatch_no',$value['dispatch_no'])
  1060. ->where('status', 2)
  1061. ->take($quantity)
  1062. ->update([
  1063. 'finished_time' => 0,
  1064. 'status' => 1,
  1065. 'finished_id' => 0,
  1066. 'team_id' => 0,
  1067. 'equipment_id' => 0,
  1068. ]);
  1069. }
  1070. DB::commit();
  1071. }catch (\Throwable $exception){
  1072. DB::rollBack();
  1073. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  1074. }
  1075. return [true, ''];
  1076. }
  1077. public function getDispatch($order_product_id,$map){
  1078. $order = OrdersProduct::where('del_time',0)
  1079. ->where('id', $order_product_id)
  1080. ->select('id', 'process_id')
  1081. ->get()->toArray();
  1082. $order_map = [];
  1083. foreach ($order as $value){
  1084. $order_map[$value['id']] = explode(',', $value['process_id']);
  1085. }
  1086. //组织派工数据
  1087. $dispatch = DispatchSub::where('del_time',0)
  1088. ->whereIn('order_product_id', $order_product_id)
  1089. ->where('finished_num','>',0)
  1090. ->get()->toArray();
  1091. if(empty($dispatch)) return [];
  1092. $dispatch_map = [];
  1093. foreach ($dispatch as $value){
  1094. $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
  1095. $dispatch_map[$key][] = $value;
  1096. }
  1097. $result = []; $tmp = [];
  1098. foreach ($dispatch_map as $key => $value){
  1099. foreach ($value as $v){
  1100. $process = $order_map[$v['order_product_id']] ?? [];
  1101. $num = $map[$v['order_product_id']];
  1102. if(in_array($v['process_id'], $process)){
  1103. $str = $v['order_product_id'] . $v['process_id'];
  1104. $tmp_v = $tmp[$str] ?? 0;
  1105. $num = bcsub($num, $tmp_v,3);
  1106. if($num <= 0) continue;
  1107. if($v['finished_num'] >= $num){
  1108. $v['out_number'] = $num;
  1109. $result[] = $v;
  1110. }else{
  1111. $v['out_number'] = $v['finished_num'];
  1112. $result[] = $v;
  1113. }
  1114. if(isset($tmp[$str])){
  1115. $num = bcadd($tmp[$str], $v['out_number'],3);
  1116. $tmp[$str] = $num;
  1117. }else{
  1118. $tmp[$str] = $v['out_number'];
  1119. }
  1120. }
  1121. }
  1122. }unset($tmp);
  1123. return $result;
  1124. }
  1125. public function boxAdd($data)
  1126. {
  1127. list($status,$msg) = $this->boxAddRule($data);
  1128. if(! $status) return [false,$msg];
  1129. try{
  1130. DB::beginTransaction();
  1131. $box = new Box();
  1132. $box->order_no = $msg['order_no'];
  1133. $box->save();
  1134. DB::table('box_detail')->insert($msg['detail']);
  1135. DB::commit();
  1136. return [true,''];
  1137. }catch (\Exception $e){
  1138. DB::rollBack();
  1139. return [false,$e->getLine().':'.$e->getMessage()];
  1140. }
  1141. }
  1142. public function boxAddRule($data)
  1143. {
  1144. if(empty($data['param'])) return [false,'请选择包装数据'];
  1145. // $order_no = (new BoxHookService())->setOrderNo();
  1146. $order_no = "";
  1147. if(empty($order_no)) return [false,'包装单号不能为空'];
  1148. $detail = [];
  1149. foreach ($data['param'] as $value) {
  1150. $key = $this->lock_key.'_'.$value['idlsid'];
  1151. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1152. if(! $status) return [false,$msg];
  1153. // if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
  1154. if(empty($value['team_id'])) return [false,'班组必须选择'];
  1155. $tmp = $value;
  1156. $tmp['iquantity'] = $value['submit_quantity'];
  1157. $detail[] = [
  1158. 'order_no' => $order_no,
  1159. 'out_order_no' => $value['csocode']??'',
  1160. 'crt_time' => time(),
  1161. 'upd_time' => time(),
  1162. 'type' => 1,
  1163. 'ext_1' => $value['cinvcode'],
  1164. 'ext_2' => $value['technology_material'],
  1165. 'ext_3' => $value['cfree1'] ?? "",
  1166. 'ext_4' => $value['cfree2'] ?? "",
  1167. 'ext_5' => $value['process_mark'],
  1168. 'ext_6' => $value['customer_name'] ?? "",
  1169. 'ext_7' => $value['cuscode'] ?? "",
  1170. 'ext_8' => $value['product_title'],
  1171. 'ext_9' => $value['product_size'],
  1172. 'ext_10' => $value['table_header_mark'],
  1173. 'ext_11' => $value['table_body_mark'],
  1174. 'top_id' => $value['idlsid'],
  1175. 'state' => 0,
  1176. 'num' => $value['submit_quantity'],
  1177. 'box_type' => 2,
  1178. 'team_id' => $value['team_id'],
  1179. 'shipment_order_no' => $value['cdlcode'],
  1180. 'post' => json_encode($tmp),
  1181. ];
  1182. }
  1183. return [true,['order_no' => $order_no, 'detail' => $detail]];
  1184. }
  1185. public function boxFhDetail($data){
  1186. if(empty($data['idlsid'])) return [false,'请选择数据'];
  1187. $result = DB::table('box_detail')->where('del_time',0)
  1188. ->where('top_id',$data['idlsid'])
  1189. ->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')
  1190. ->get()->toArray();
  1191. if(! empty($result)){
  1192. foreach ($result as $key => $value){
  1193. $result[$key] = (array)$value;
  1194. }
  1195. }
  1196. return [true,$result];
  1197. }
  1198. public function boxFhDel($data){
  1199. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  1200. $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
  1201. DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
  1202. 'del_time' => time()
  1203. ]);
  1204. return [true,''];
  1205. }
  1206. public function transportDetail($data){
  1207. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  1208. $order_nos = $data['order_nos'];
  1209. $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();
  1210. foreach ($list as &$v){
  1211. $v['product_no'] = $v['ext_1'];
  1212. $v['technology_material'] = $v['ext_2'];
  1213. $v['technology_name'] = $v['ext_3'];
  1214. $v['wood_name'] = $v['ext_4'];
  1215. $v['process_mark'] = $v['ext_5'];
  1216. $v['customer_no'] = $v['ext_6'];
  1217. $v['customer_name'] = $v['ext_7'];
  1218. $v['product_title'] = $v['ext_8'];
  1219. $v['product_size'] = $v['ext_9'];
  1220. $v['post'] = json_decode($v['post'],true);
  1221. }
  1222. return [200,$list];
  1223. }
  1224. public function boxFhBzDetail($data){
  1225. if(empty($data['order_no'])) return [false,'请选择包装单数据'];
  1226. $result = DB::table('box_detail')->where('del_time',0)
  1227. ->whereIn('order_no',$data['order_no'])
  1228. ->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')
  1229. ->get()->toArray();
  1230. if(! empty($result)){
  1231. foreach ($result as $key => $value){
  1232. $result[$key] = (array)$value;
  1233. }
  1234. $map = Team::where('id',array_unique(array_column($result,'team_id')))->pluck('title','id')->toArray();
  1235. foreach ($result as $key => $value){
  1236. $result[$key]['team_name'] = $map[$value['team_id']] ?? '';
  1237. }
  1238. }
  1239. return [true,$result];
  1240. }
  1241. public function boxFhBzList($data){
  1242. if(empty($data['shipment_order_no'])) return [false, '请输入发货单号'];
  1243. $model = DB::table('box_detail')->where('del_time',0)
  1244. ->where('shipment_order_no','LIKE', '%'.$data['shipment_order_no'].'%')
  1245. ->select('order_no','crt_time','shipment_order_no');
  1246. $return = [];
  1247. $result = $model->get()->toArray();
  1248. if(! empty($result)){
  1249. foreach ($result as $value){
  1250. $return[$value->order_no] = [
  1251. 'order_no' => $value->order_no,
  1252. 'shipment_order_no' => $value->shipment_order_no,
  1253. 'crt_time' => date("Y-m-d H:i:s",$value->crt_time)
  1254. ];
  1255. }
  1256. }
  1257. return [true,array_values($return)];
  1258. }
  1259. public function boxOrderDetailByOrderNoNew($data){
  1260. if(empty($data['order_nos'])) return [false, '包装单号不能为空'];
  1261. $order_nos = $data['order_nos'];
  1262. $order_nos = array_unique($order_nos);
  1263. Log::channel('sendData')->info('发货:扫码包装单数据', ["param" => $order_nos]);
  1264. $list = Box::where('del_time',0)
  1265. ->whereIn('order_no',$order_nos)
  1266. ->where('shipment_order_no','')
  1267. ->select('order_no','out_order_no','top_order_no','shipment_order_no as transport_no','upd_time')
  1268. ->get()->toArray();
  1269. $detail_list = [];
  1270. foreach ($list as $v){
  1271. // if(! empty($v['transport_no'])) return [false, "包装单号:" . $v['order_no'] . "已在发货单(" . $v['transport_no'] .")中发出!"];
  1272. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1273. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  1274. }
  1275. $return = [];
  1276. foreach ($detail_list as $value){
  1277. //销售订单号 产品编码 颜色
  1278. $return[] = [
  1279. 'ext_1' => $value['ext_1'],
  1280. 'ext_3' => $value['ext_3'],
  1281. 'ext_8' => $value['ext_8'],
  1282. 'num' => $value['num'],
  1283. 'out_order_no' => $value['out_order_no'],
  1284. 'box_no' => $value['order_no']
  1285. ];
  1286. }
  1287. return [true, array_values($return)];
  1288. }
  1289. public function boxList($data,$user){
  1290. $model = Box::where('del_time',0)
  1291. ->select('id','order_no','out_order_no','top_order_no','crt_time','status','shipment_order_no')
  1292. ->orderBy('id','desc');
  1293. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  1294. if(! empty($data['shipment_order_no'])) $model->where('shipment_order_no', 'LIKE', '%'.$data['shipment_order_no'].'%');
  1295. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  1296. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  1297. if(isset($data['status'])) $model->where('status',$data['status']);
  1298. $list = $this->limit($model,'',$data);
  1299. $list = $this->fillBoxListData($list, $data,$user);
  1300. return [true, $list];
  1301. }
  1302. public function fillBoxListData($data, $erg,$user){
  1303. if(empty($data['data'])) return $data;
  1304. $product = $material = [];
  1305. if(! empty($erg['box'])){
  1306. //获取包装单里的产品
  1307. $id = array_column($data['data'], 'id');
  1308. list($product,$product_no) = $this->getBoxDetail($id);
  1309. if(! empty($erg['box_material'])){
  1310. //产品的包装材料
  1311. $service = new FyyOrderService();
  1312. list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
  1313. if($status) $material = $msg;
  1314. }
  1315. }
  1316. foreach ($data['data'] as $key => $value){
  1317. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1318. $data['data'][$key]['status_title'] = Box::$status_name[$value['status']] ?? "";
  1319. $p = $product[$value['id']] ?? [];
  1320. if(! empty($p)){
  1321. foreach ($p as $k => $v){
  1322. $p[$k]['box_material'] = $material[$v['product_no']] ?? [];
  1323. }
  1324. }
  1325. $data['data'][$key]['product'] = $p;
  1326. }
  1327. return $data;
  1328. }
  1329. public function getBoxDetail($id = []){
  1330. $return = $product_no= [];
  1331. if(empty($id)) return [$return,$product_no];
  1332. $box = Box::where('del_time',0)
  1333. ->whereIn('id', $id)
  1334. ->select('id', 'order_no','top_order_no')
  1335. ->get()->toArray();
  1336. if (empty($box)) return [$return,$product_no];
  1337. $map = array_column($box,'id','order_no');
  1338. $result = [];
  1339. foreach ($box as $v){
  1340. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1341. $result = array_merge($result,$model->where('del_time',0)
  1342. ->where('order_no',$v['order_no'])
  1343. ->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());
  1344. }
  1345. foreach ($result as $value){
  1346. $value['id'] = $map[$value['order_no']] ?? 0;
  1347. $value['product_unit'] = "吨";
  1348. $return[$value['id']][] = $value;
  1349. if(! in_array($value['product_no'], $product_no)) $product_no[] = $value['product_no'];
  1350. }
  1351. return [$return, $product_no];
  1352. }
  1353. public function searchDetail($data){
  1354. $out_order_no = $data['out_order_no'] ?? '';
  1355. $box = Box::where('del_time',0)
  1356. ->where('out_order_no', 'LIKE', '%'. $out_order_no .'%')
  1357. ->select('top_order_no')
  1358. ->get()->toArray();
  1359. if(empty($box)) return [];
  1360. $order_no = [];
  1361. foreach ($box as $value){
  1362. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  1363. $result = $model->where('del_time',0)
  1364. ->where('order_no',$box['order_no'])
  1365. ->select('order_no')
  1366. ->get()->toArray();
  1367. $detail_list = array_merge($order_no, array_column($result, 'order_no'));
  1368. }
  1369. $result = BoxDetail::where('del_time',0)
  1370. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  1371. return $query->where('out_order_no', 'LIKE', '%'. $out_order_no .'%');
  1372. })
  1373. ->when(! empty($shipment_order_no), function ($query) use ($shipment_order_no) {
  1374. return $query->where('shipment_order_no', 'LIKE', '%'. $shipment_order_no .'%');
  1375. })
  1376. ->select('order_no')
  1377. ->get()->toArray();
  1378. return array_unique(array_column($result,'order_no'));
  1379. }
  1380. public function boxFhDetailNew($data){
  1381. if(empty($data['id'])) return [false,'请选择数据'];
  1382. $box = Box::where('id', $data['id'])->where('del_time',0)->first();
  1383. if(empty($box)) return [false, '包装单不存在或已被删除'];
  1384. $box = $box->toArray();
  1385. $model = new BoxDetail(['channel'=>$box['top_order_no']]);
  1386. $result = $model->where('del_time',0)
  1387. ->where('order_no',$box['order_no'])
  1388. ->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();
  1389. foreach ($result as $key => $value){
  1390. $result[$key]['product_unit'] = "吨";
  1391. }
  1392. return [true,$result];
  1393. }
  1394. }