BoxService.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  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\Process;
  14. use App\Model\ReportMessage;
  15. use App\Model\ReportWorking;
  16. use App\Model\ReportWorkingDetail;
  17. use App\Model\SaleOrdersProduct;
  18. use App\Model\ScrappCount;
  19. use App\Model\Setting;
  20. use App\Model\Team;
  21. use App\Service\ApplyOrderService;
  22. use App\Service\EmployeeService;
  23. use App\Service\FinishedOrderService;
  24. use App\Service\FyyOrderService;
  25. use App\Service\FyySqlServerService;
  26. use App\Service\Service;
  27. use Illuminate\Support\Facades\DB;
  28. use Illuminate\Support\Facades\Log;
  29. /**
  30. * 包装相关
  31. * @package App\Models
  32. */
  33. class BoxService extends Service
  34. {
  35. protected static $instance;
  36. protected static $box_header;
  37. protected static $box_detail_header;
  38. protected static $box_hook;
  39. public $lock_key = 'hc_box_new_add';
  40. public function __construct()
  41. {
  42. self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
  43. self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
  44. self::$box_hook = BoxHookService::getInstance();
  45. }
  46. public function boxIn($data,$user)
  47. {
  48. list($status, $msg) = $this->boxInRule($data, $user);
  49. if(! $status) return [false, $msg];
  50. $data['zt'] = Request()->header('zt');
  51. //list($status, $msg) = $this->boxInSettle($data,$user);dd($status,$msg);
  52. //限制频率
  53. $limit_key = $this->lock_key;
  54. list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
  55. if(! $status) return [false, '请稍后操作'];
  56. $job = BoxAddJob::dispatch($data,$user)->onQueue(BoxAddJob::job);
  57. if(! $job) {
  58. $this->dellimitingSendRequestBackgNeed($limit_key);
  59. return [false,'任务未进入后台队列,请联系管理员'];
  60. }
  61. return [true, ''];
  62. }
  63. public function boxInRule($data,$user)
  64. {
  65. try{
  66. $ids = [];
  67. $sale_ids = [];
  68. $data = $data['param'];
  69. $key_list = [];
  70. foreach ($data as $v) {
  71. if($v['id'] < 0) $sale_ids[] = -$v['id'];
  72. else $ids[] = $v['id'];
  73. if($v['id'] < 0) $v['id'] = -$v['id'];
  74. $total = 0;
  75. foreach ($v['params'] as $vv) {
  76. $total += $vv;
  77. }
  78. $key_list[$v['id']] = [
  79. 'detail' => $v['params'],
  80. 'total' => $total,
  81. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  82. ];
  83. }
  84. //数据获取
  85. $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
  86. //校验
  87. $flag = "";
  88. foreach ($product_list as $value){
  89. $key = $value['out_order_no'] . $value['product_no'] . $value['technology_name'];
  90. if(! empty($flag)){
  91. if($flag != $key) return [false , "一次包装只允许包一种产品的一种颜色!"];
  92. }else{
  93. $flag = $key;
  94. }
  95. $num_list = $key_list[$value['id']];
  96. $total = $num_list['total'];
  97. $un_box_num = bcsub($value['dispatch_complete_quantity'] , $value['box_num'],3);
  98. if ($total > $un_box_num) return [false, $value['product_title'] . ' ' . $value['technology_name'] . ' 可包装的数量不足'];
  99. }
  100. return [true, ''];
  101. }catch (\Throwable $e){
  102. return [false,$e->getFile() . '|' . $e->getLine(). '|' .$e->getMessage()];
  103. }
  104. }
  105. public function boxInSettle($data, $user){
  106. //设置数据库连接
  107. $this->setConnect($data);
  108. $zt = $data['zt'] ?? "";
  109. $ids = [];
  110. $sale_ids = [];
  111. $top_order_no = $data['order_no'];
  112. $box_no = isset($data['box_no']) ? $data['box_no'] : '';
  113. $transport_no = isset($data['transport_no']) ? $data['transport_no'] : '';
  114. $data = $data['param'];
  115. $key_list = [];
  116. foreach ($data as $v) {
  117. if($v['id'] < 0) $sale_ids[] = -$v['id'];
  118. else $ids[] = $v['id'];
  119. if($v['id'] < 0) $v['id'] = -$v['id'];
  120. $total = 0;
  121. foreach ($v['params'] as $vv) {
  122. $total += $vv;
  123. }
  124. $key_list[$v['id']] = [
  125. 'detail' => $v['params'],
  126. 'total' => $total,
  127. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  128. ];
  129. }
  130. $insert = [];
  131. //锁
  132. $limit_key = $this->lock_key;
  133. try{
  134. DB::beginTransaction();
  135. //数据获取
  136. $product_list = OrdersProduct::whereIn('id', $ids)->get()->toArray();
  137. $sale_product_list = SaleOrdersProduct::whereIn('id', $sale_ids)->get()->toArray();
  138. //生产订单包装----------------
  139. $box_insert = [];
  140. foreach ($product_list as $v) {
  141. $num_list = $key_list[$v['id']];
  142. $total = $num_list['total'];
  143. $detail = $num_list['detail'];
  144. $team_id = $num_list['team_id'];
  145. $ext_1 = $v['product_no'];//产品编号
  146. $ext_2 = $v['technology_material']; //工艺材质 废弃
  147. $ext_3 = $v['technology_name'];//工艺名称 改为颜色
  148. $ext_4 = $v['wood_name'];//木皮 废弃
  149. $ext_5 = $v['process_mark'];//工艺备注 废弃
  150. $ext_8 = $v['product_title'];//产品名称
  151. $out_order_no = $insert['out_order_no'] = $v['out_order_no'];
  152. $top_id = $v['sale_orders_product_id'];
  153. foreach ($detail as $vv){
  154. $box_insert[] = [
  155. 'out_order_no' => $out_order_no,
  156. 'top_id' => $top_id,
  157. 'orders_product_id' => $v['id'],
  158. 'ext_1' => $ext_1,//产品编号
  159. 'ext_2' => $ext_2,
  160. 'ext_3' => $ext_3,//颜色
  161. 'ext_4' => $ext_4,
  162. 'ext_5' => $ext_5,
  163. 'ext_8' => $ext_8,//产品名称
  164. 'num' => $vv,
  165. 'price' => $v['price'],
  166. 'box_type' => 1,
  167. 'team_id' => $team_id,
  168. 'shipment_order_no' => $transport_no,
  169. ];
  170. }
  171. //修改销售订单 生产订单 包装数量
  172. SaleOrdersProduct::where('id',$v['sale_orders_product_id'])->update([
  173. 'box_num' => DB::raw('box_num + '.$total),
  174. ]);
  175. OrdersProduct::where('id',$v['id'])->update([
  176. 'box_num' => DB::raw('box_num + '.$total),
  177. ]);
  178. }
  179. //客户名称
  180. $ext_1 = "";
  181. if(empty($ext_1) && isset($sale_product_list[0]['customer_name'])) $ext_1 = $sale_product_list[0]['customer_name'];
  182. if(empty($ext_1) && isset($product_list[0]['customer_name'])) $ext_1 = $product_list[0]['customer_name'];
  183. $insert['ext_1'] = $ext_1;
  184. $insert['detail'] = $box_insert;
  185. $insert['top_order_no'] = $top_order_no;
  186. $insert['order_no'] = $box_no;
  187. $insert['shipment_order_no'] = $transport_no;
  188. //生成包装单
  189. list($status,$msg) = self::$box_hook->boxInsert($insert,$user);
  190. if(! $status) {
  191. $this->dellimitingSendRequestBackgNeed($limit_key);
  192. DB::rollBack();
  193. return [false, $msg];
  194. }
  195. //反写数量
  196. $em = new EmployeeService();
  197. $auto = $em->is_auto($user, "bzwg_auto");
  198. if($auto){
  199. list($status, $msg1) = $this->updateFinishNew($product_list,$key_list,$user);
  200. if(! $status) {
  201. $this->dellimitingSendRequestBackgNeed($limit_key);
  202. DB::rollBack();
  203. return [false, $msg1];
  204. }
  205. }
  206. //包装单
  207. $package_data = $msg->toArray();
  208. //包装单产品
  209. $boxDetail = new BoxDetail(['channel'=>$package_data['top_order_no']]);
  210. $boxDetail = $boxDetail->where('del_time',0)
  211. ->where('order_no',$package_data['order_no'])
  212. ->select('ext_1 as product_no', 'ext_3 as technology_name', 'ext_8 as product_title', 'ext_9 as product_size', 'num as quantity')
  213. ->get()->toArray();
  214. //生成包装领料申请单
  215. list($status, $msg1) = $this->createllSQ($package_data,$boxDetail, $user);
  216. if(! $status) {
  217. $this->dellimitingSendRequestBackgNeed($limit_key);
  218. DB::rollBack();
  219. return [false, $msg1];
  220. }
  221. //生成包装入库申请单
  222. list($status, $msg) = $this->createBZSQ($package_data,$boxDetail, $user);
  223. if(! $status) {
  224. $this->dellimitingSendRequestBackgNeed($limit_key);
  225. DB::rollBack();
  226. return [false, $msg];
  227. }
  228. //是否自动审核 包装入库申请单 如果是测试的不写用友
  229. $auto = $em->is_auto($user,"bzsq_auto");
  230. if(! empty($package_data) && $auto && $zt != 'cs') {
  231. //用友 ------产成品入库
  232. $service = new FinishedOrderService();
  233. list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
  234. if(! $status) {
  235. $this->dellimitingSendRequestBackgNeed($limit_key);
  236. DB::rollBack();
  237. return [false, $msg];
  238. }
  239. }
  240. DB::commit();
  241. $this->dellimitingSendRequestBackgNeed($limit_key);
  242. return [true, ['package_data' => $package_data]];
  243. }catch (\Throwable $e){
  244. DB::rollBack();
  245. $this->dellimitingSendRequestBackgNeed($limit_key);
  246. if (str_contains($e->getMessage(), '1062') || str_contains($e->getMessage(), 'Duplicate entry')) {
  247. return [false, '网络波动,请重新操作!'];
  248. }
  249. return [false, $e->getFile(). '|' .$e->getLine().'|'.$e->getMessage()];
  250. }
  251. }
  252. private function setConnect($data){
  253. $storeCode = $data['zt'] ?? '';
  254. $connectionName = "mysql";
  255. if($storeCode == 'cs') $connectionName = "mysql_" . $storeCode;
  256. if (in_array($connectionName, array_keys(config('database.connections')))) {
  257. // 切换默认数据库连接
  258. config(['database.default' => $connectionName]);
  259. // 重新解析 DB Facade 使用新的连接
  260. app()->forgetInstance('db');
  261. $db = app('db');
  262. // 如果用到了模型,还要重置模型连接
  263. \Illuminate\Database\Eloquent\Model::setConnectionResolver($db);
  264. }
  265. }
  266. public function createllSQ($package_data, $insert_id, $user){
  267. //是否自动审核 包装领料申请单
  268. $em = new EmployeeService();
  269. $auto = $em->is_auto($user,"bzllsq_auto");
  270. $return = [];
  271. $product_no = array_unique(array_column($insert_id,'product_no'));
  272. //获取包装原料
  273. $service = new FyyOrderService();
  274. list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
  275. if($status) $return = $msg;
  276. //组织包装原材料写入数据
  277. $insert = [];
  278. $box_id = $package_data['id'] ?? 0;
  279. foreach ($insert_id as $value){
  280. $t = $return[$value['product_no']] ?? [];
  281. $tmp = [
  282. 'id' => $box_id,
  283. 'quantity' => $value['quantity'] ?? 0,
  284. 'product_no' => "",
  285. 'product_title' => "",
  286. 'product_size' => "",
  287. 'product_unit' => "",
  288. 'top_product_no' => $value['product_no'],
  289. 'top_product_title' => $value['product_title'],
  290. 'technology_name' => $value['technology_name'],
  291. ];
  292. if(! empty($t)){
  293. foreach ($t as $v){
  294. $tmp['product_no'] = $v['product_no'];
  295. $tmp['product_title'] = $v['product_title'];
  296. $tmp['product_size'] = $v['product_size'];
  297. $tmp['product_unit'] = $v['product_unit'];
  298. $insert[] = $tmp;
  299. }
  300. }else{
  301. $tmp['quantity'] = 0;
  302. $insert[] = $tmp;
  303. }
  304. }
  305. try {
  306. DB::beginTransaction();
  307. //生成申请单
  308. $service = new ApplyOrderService();
  309. list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_four, $auto);
  310. if(! $status) {
  311. DB::rollBack();
  312. return [false, $msg];
  313. }
  314. if($auto) {
  315. //生成流水
  316. list($status, $msg) = $service->createRecord($msg);
  317. if(! $status) {
  318. DB::rollBack();
  319. return [false, $msg];
  320. }
  321. }
  322. DB::commit();
  323. }catch (\Throwable $exception){
  324. DB::rollBack();
  325. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  326. }
  327. return [true, ''];
  328. }
  329. public function createBZSQ($package_data, $insert_id, $user){
  330. //是否自动审核 包装入库申请单
  331. $em = new EmployeeService();
  332. $auto = $em->is_auto($user,"bzsq_auto");
  333. try {
  334. DB::beginTransaction();
  335. $box_id = $package_data['id'] ?? 0;
  336. foreach ($insert_id as $key => $value){
  337. $insert_id[$key]['id'] = $box_id;
  338. }
  339. //生成申请单
  340. $service = new ApplyOrderService();
  341. list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_three, $auto);
  342. if(! $status) {
  343. DB::rollBack();
  344. return [false, $msg];
  345. }
  346. //用友写入数据信息
  347. $apply_order_id = $msg;
  348. ApplyOrderForYy::insert([
  349. 'apply_order_id' => $apply_order_id,
  350. 'crt_time' => time(),
  351. 'post' => json_encode($package_data),
  352. ]);
  353. if($auto) {
  354. //生成流水
  355. list($status, $msg) = $service->createRecord($msg);
  356. if(! $status) {
  357. DB::rollBack();
  358. return [false, $msg];
  359. }
  360. }
  361. DB::commit();
  362. }catch (\Throwable $exception){
  363. DB::rollBack();
  364. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  365. }
  366. return [true, ''];
  367. }
  368. public function delBoxLock(){
  369. $this->dellimitingSendRequestBackgNeed($this->lock_key);
  370. }
  371. //新的完工
  372. public function updateFinishNew($production_list = [], $map, $user){
  373. if(empty($production_list)) return [true, ''];
  374. $order_product_id = array_column($production_list,'id');
  375. $order = OrdersProduct::where('del_time',0)
  376. ->where('id', $order_product_id)
  377. ->select('id', 'process_id')
  378. ->get()->toArray();
  379. $order_map = [];
  380. //生产订单 =》 工序
  381. foreach ($order as $value){
  382. $order_map[$value['id']] = explode(',', $value['process_id']);
  383. }
  384. //组织派工数据
  385. $dispatch = DispatchSub::where('del_time',0)
  386. ->whereIn('order_product_id', array_column($production_list,'id'))
  387. ->get()->toArray();
  388. if(empty($dispatch)) return [true,''];
  389. $sale_order_id = array_unique(array_column($dispatch,'sale_orders_product_id'));
  390. $dispatch_map = [];
  391. foreach ($dispatch as $value){
  392. $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
  393. $dispatch_map[$key][] = $value;
  394. }
  395. $result = []; $tmp = [];
  396. foreach ($dispatch_map as $key => $value){
  397. $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
  398. $finished_num = array_sum(array_column($value, 'finished_num'));
  399. //同一批派工 且 全部完工的 跳过
  400. if($dispatch_quantity == $finished_num) continue;
  401. $order_id = explode('|', $key)[0];
  402. // 生产订单id存在 跳过(原因 同一个生产订单可能下多次派工,那么这一次包装数量只需要一次派工的数据)
  403. if(in_array($order_id, $tmp)) continue;
  404. foreach ($value as $val){
  405. $num = bcadd($val['finished_num'], $val['waste_num'], 3);
  406. if($num >= $val['dispatch_quantity']) continue; //某一个工序完工
  407. //包装数量
  408. $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
  409. if($tmp_num <= 0) continue;
  410. $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
  411. if($tmp_num2 >= $tmp_num){
  412. $val['quantity'] = $tmp_num;
  413. }else{
  414. $val['quantity'] = $tmp_num2;
  415. }
  416. $result[] = $val;
  417. $tmp[] = $order_id;
  418. }
  419. }
  420. if(empty($result)) return [true,''];
  421. try {
  422. DB::beginTransaction();
  423. //班组下的人
  424. $team_man = [];
  425. $team_array = EmployeeTeamPermission::select('employee_id','team_id')->get()->toArray();
  426. foreach ($team_array as $value){
  427. //拿班组里的一个人
  428. if(! isset($team_man[$value['team_id']])) $team_man[$value['team_id']] = $value['employee_id'];
  429. }
  430. //工序 班组
  431. $process_map = [
  432. 12 => 65,
  433. 11 => 66,
  434. 15 => 69,
  435. 14 => 67,
  436. 13 => 68,
  437. ];
  438. $service = new FinishedOrderService();
  439. $service1 = new ApplyOrderService();
  440. $dispatch_time = $c_time = $result[0]['crt_time']; //派工单创建时间
  441. $date = date("Ymd", $c_time);
  442. // //派工和包装不在同一天 质检单日期加一天
  443. // if($date != date("Ymd")){
  444. // $c_time = strtotime("+1 day", strtotime($date));
  445. // $date = date("Ymd", $c_time);
  446. // }
  447. $max_id = $service1->getDailyId($c_time);
  448. //是否需要质检
  449. list($status, $return) = $service->isNeedZJ($sale_order_id);
  450. $flag = $return;
  451. //根据派工数据组织
  452. $last_update = $last_update2 = $str_tmp = $insert = $get_order_number = [];
  453. foreach ($result as $value){
  454. $need_zj_process = $flag[$value['sale_orders_product_id']] ?? 0;
  455. $bad_quantity = $scrapp_id = $for_r = 0;
  456. $warehouse_no = $warehouse_title = "";
  457. if(! empty($need_zj_process) && $value['process_id'] == $need_zj_process){
  458. $numbers = [0.08, 0.09, 0.1, 0.11, 0.12];
  459. $bad_quantity = $numbers[array_rand($numbers)];
  460. $flag[$value['sale_orders_product_id']] = 0;
  461. if($need_zj_process == 12){
  462. $scrapp_id = 19;
  463. $for_r = ReportWorking::type_two;
  464. $warehouse_no = "005";
  465. $warehouse_title = "报废仓";
  466. }else{
  467. $scrapp_id = mt_rand(10, 18);
  468. $for_r = ReportWorking::type_one;
  469. }
  470. }
  471. $quantity = bcsub($value['quantity'], $bad_quantity, 3);
  472. //质检
  473. $max_id += 1;
  474. $order_number = $date . str_pad($max_id,3,'0',STR_PAD_LEFT);
  475. $get_order_number[] = $order_number;
  476. $insert[] = [
  477. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  478. 'order_product_id' => $value['order_product_id'],
  479. 'dispatch_sub_id' => $value['id'],
  480. 'out_order_no' => $value['out_order_no'],
  481. 'order_no' => $value['order_no'],
  482. 'customer_no' => $value['customer_no'],
  483. 'customer_name' => $value['customer_name'],
  484. 'product_no' => $value['product_no'],
  485. 'product_title' => $value['product_title'],
  486. 'product_size' => $value['product_size'],
  487. 'product_unit' => $value['product_unit'],
  488. 'technology_material' => $value['technology_material'],
  489. 'technology_name' => $value['technology_name'],
  490. 'wood_name' => $value['wood_name'],
  491. 'price' => $value['price'],
  492. 'process_mark' => $value['process_mark'],
  493. 'table_body_mark' => $value['table_body_mark'],
  494. 'table_header_mark' => $value['table_header_mark'],
  495. 'crt_id' => $user['id'],
  496. 'crt_time' => $c_time,
  497. 'scrapp_num' => $bad_quantity,
  498. 'scrapp_id' => $scrapp_id,
  499. 'team_id' => 73,
  500. 'finished_id' => $user['id'],
  501. 'equipment_id' => $value['device_id'],
  502. 'order_number' => $order_number,
  503. 'process_id' => $value['process_id'],
  504. 'quantity' => $quantity,
  505. 'report_id' => 0,
  506. 'result' => $for_r,
  507. 'warehouse_no' => $warehouse_no,
  508. 'warehouse_title' => $warehouse_title,
  509. 'daily_id' => $max_id,
  510. ];
  511. $tmp = [
  512. 'id' => $value['id'],
  513. 'quantity' => $value['quantity'],
  514. 'product_no' => $value['product_no'] ?? "",
  515. 'product_title' => $value['product_title'] ?? "",
  516. 'product_size' => $value['product_size'] ?? "",
  517. 'product_unit' => $value['product_unit'] ?? "",
  518. ];
  519. $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
  520. if(! in_array($str, $str_tmp)){
  521. $last_update[] = $tmp;
  522. $str_tmp[] = $str;
  523. }
  524. $tmp['process_id'] = $value['process_id'];
  525. $tmp['quantity'] = $quantity;
  526. $team_tmp = $process_map[$value['process_id']] ?? 0; // 班组
  527. $t_m = $team_man[$team_tmp] ?? 0; //班组下的人
  528. $tmp['finished_id'] = $t_m;
  529. $tmp['team_id'] = $team_tmp;
  530. $last_update2[] = $tmp;
  531. }
  532. //生成报工单
  533. list($status, $msg) = $this->createbg($last_update2, $user,$dispatch_time);
  534. if(! $status) return [false, $msg];
  535. //报工单id数组
  536. list($report_id, $report_order_number) = $msg;
  537. if(empty($report_id)) return [false, '报工单返回信息异常'];
  538. //根据报工单创建质检单
  539. list($status,$msg) = $service->createZJFirst($report_id, $insert);
  540. if(! $status) return [false, $msg];
  541. //生成因为有不良品需要的返工的业务单据
  542. list($status, $msg) = $service->createZJSecond($get_order_number, $user, $dispatch_time, $c_time,$max_id,$team_man,$report_order_number);
  543. if(! $status) return [false, $msg];
  544. $get_order_number_result = $msg;
  545. //生成完工入库申请单
  546. list($status, $msg) = $this->createWGSQ($last_update, $user, $c_time);
  547. if(! $status) return [false, $msg];
  548. //反写 根据质检单上的派工单id 反写上游单据
  549. $this->writeDispatchQuantity($get_order_number_result);
  550. DB::commit();
  551. }catch (\Throwable $exception){
  552. DB::rollBack();
  553. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  554. }
  555. return [true, ''];
  556. }
  557. public function createbg($insert, $user,$dispatch_time = 0, $order_number = ""){
  558. //报工单
  559. $em = new EmployeeService();
  560. $auto = $em->is_auto($user, "wgbg_auto");
  561. if(empty($order_number)){
  562. $order_number = (new ApplyOrderService())->setOrderNO2($dispatch_time);
  563. if(empty($order_number)) return [false, '报工单编号生成失败'];
  564. $is_first = true;
  565. }else{
  566. $is_first = false;
  567. }
  568. $process = Process::where('del_time',0)
  569. ->where('is_need_remain',1)
  570. ->select('id')
  571. ->get()->toArray();
  572. $process_id = array_column($process,'id');
  573. try {
  574. DB::beginTransaction();
  575. if(empty($dispatch_time)){
  576. $time = time();
  577. }else{
  578. $time = $dispatch_time;
  579. }
  580. $prefix = substr($order_number, 0, -3);// "20250611"
  581. $startNum = (int)substr($order_number, -3); // 7
  582. $return = $get_order_number = [];
  583. foreach ($insert as $v){
  584. if(! $is_first){
  585. $startNum += 1;
  586. $number = str_pad($startNum,3,'0',STR_PAD_LEFT);
  587. $number = $prefix . $number;
  588. $dispatch_no_t = $number;
  589. }else{
  590. $dispatch_no_t = $order_number;
  591. $is_first = false;
  592. }
  593. $get_order_number[] = $dispatch_no_t;
  594. $model = new ReportWorking();
  595. $model->order_number = $dispatch_no_t;
  596. $model->report_time = $time;
  597. $model->crt_id = $user['id'];
  598. $model->status = $auto;
  599. $model->save();
  600. $id = $model->id;
  601. ReportWorkingDetail::insert([
  602. 'report_working_id' => $id,
  603. 'data_id' => $v['id'],
  604. 'quantity' => $v['quantity'] ?? 0,
  605. 'finished_id' => $v['finished_id'] ?? 0,
  606. 'process_id' => $v['process_id'] ?? 0,
  607. 'team_id' => $v['team_id'] ?? 0,
  608. 'crt_time' => $time,
  609. ]);
  610. $return[$v['id']] = $id;
  611. if(in_array($v['process_id'], $process_id)){
  612. ReportMessage::insert([
  613. 'order_id' => $v['id'],
  614. 'quantity' => $v['quantity'] ?? 0,
  615. 'opt_case' => ReportMessage::type_one,
  616. 'crt_time' => $time,
  617. 'user_id' => $user['id'],
  618. 'is_use' => 1
  619. ]);
  620. }
  621. }
  622. DB::commit();
  623. }catch (\Throwable $exception){
  624. DB::rollBack();
  625. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  626. }
  627. return [true, [$return, $get_order_number]];
  628. }
  629. //反写
  630. public function writeDispatchQuantity($get_order_number_result){
  631. $result = ScrappCount::whereIn('order_number',$get_order_number_result)
  632. ->select('dispatch_sub_id','quantity','scrapp_num as num','crt_time','process_id','order_product_id')
  633. ->get()->toArray();
  634. //是否最后一道工序
  635. $orders = OrdersProduct::whereIn('id', array_unique(array_column($result, 'order_product_id')))
  636. ->select('id','process_id')
  637. ->get()->toArray();
  638. $map = [];
  639. foreach ($orders as $value){
  640. $p = explode(",", $value['process_id']);
  641. $lastElement = end($p);
  642. $map[$value['id']] = $lastElement;
  643. }
  644. //汇总这个派工单一共质检良品数量 但是同一时间下的 只取一次
  645. $update = $flag = $need_update = [];
  646. foreach ($result as $value){
  647. if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id'] && ! in_array($value['order_product_id'], $need_update)){
  648. $need_update[] = [
  649. 'order_product_id' => $value['order_product_id'],
  650. 'process_id' => $value['process_id'],
  651. ];
  652. }
  653. $key = $value['dispatch_sub_id'] . $value['crt_time'];
  654. if(isset($flag[$key])) continue;
  655. if(isset($update[$value['dispatch_sub_id']])){
  656. $tmp = bcadd($value['quantity'], $update[$value['dispatch_sub_id']]['finished_num'], 3);
  657. $tmp1 = bcadd($value['num'], $update[$value['dispatch_sub_id']]['waste_num'], 3);
  658. $update[$value['dispatch_sub_id']] = [
  659. 'finished_num' => $tmp,
  660. 'waste_num' => $tmp1,
  661. ];
  662. }else{
  663. $update[$value['dispatch_sub_id']] = [
  664. 'finished_num' => $value['quantity'],
  665. 'waste_num' => $value['num'],
  666. ];
  667. }
  668. $flag[$key] = 1;
  669. }
  670. unset($flag);
  671. //更新派工单完工数量----------------------------------------------------
  672. foreach ($update as $id => $value){
  673. DispatchSub::where('id', $id)->update([
  674. 'finished_num' => DB::raw('finished_num + '.$value['finished_num']),
  675. 'waste_num' => DB::raw('waste_num + '.$value['waste_num']),
  676. ]);
  677. }
  678. if(! empty($need_update)){
  679. //更新生产订单的完工数量--------------------------------------------------
  680. $list = DispatchSub::where("del_time",0)
  681. ->when(! empty($need_update), function ($query) use ($need_update) {
  682. return $query->where(function ($q) use ($need_update) {
  683. foreach ($need_update as $value) {
  684. $order_product_id = $value['order_product_id'];
  685. $process_id = $value['process_id'];
  686. $q->orWhere(function ($subQ) use ($order_product_id, $process_id) {
  687. $subQ->where('order_product_id', $order_product_id)
  688. ->where('process_id', $process_id);
  689. });
  690. }
  691. });
  692. })
  693. ->select('order_product_id', 'finished_num', 'sale_orders_product_id')
  694. ->get()->toArray();
  695. $map2 = [];
  696. foreach ($list as $value){
  697. if(isset($map2[$value['order_product_id']])){
  698. $tmp = bcadd($map2[$value['order_product_id']], $value['finished_num'], 3);
  699. $map2[$value['order_product_id']] = $tmp;
  700. }else{
  701. $map2[$value['order_product_id']] = $value['finished_num'];
  702. }
  703. }
  704. foreach ($map2 as $id => $num){
  705. OrdersProduct::where('id', $id)->update([
  706. 'finished_num' => $num
  707. ]);
  708. }
  709. //更新销售订单完工数量-------------------------------------------------------
  710. $list2 = OrdersProduct::where('del_time',0)
  711. ->whereIn('sale_orders_product_id', array_unique(array_column($list, 'sale_orders_product_id')))
  712. ->select('finished_num','sale_orders_product_id')
  713. ->get()->toArray();
  714. $map3 = [];
  715. foreach ($list2 as $value){
  716. if(isset($map3[$value['sale_orders_product_id']])){
  717. $tmp = bcadd($map3[$value['sale_orders_product_id']], $value['finished_num'], 3);
  718. $map3[$value['sale_orders_product_id']] = $tmp;
  719. }else{
  720. $map3[$value['sale_orders_product_id']] = $value['finished_num'];
  721. }
  722. }
  723. foreach ($map3 as $id => $num){
  724. SaleOrdersProduct::where('id', $id)->update([
  725. 'finished_num' => $num
  726. ]);
  727. }
  728. }
  729. }
  730. //旧的完工
  731. public function updateFinish($production_list = [], $map,$user){
  732. if(empty($production_list)) return [true,''];
  733. $order_product_id = array_column($production_list,'id');
  734. $order = OrdersProduct::where('del_time',0)
  735. ->where('id', $order_product_id)
  736. ->select('id', 'process_id')
  737. ->get()->toArray();
  738. $order_map = [];
  739. foreach ($order as $value){
  740. $order_map[$value['id']] = explode(',', $value['process_id']);
  741. }
  742. //组织派工数据
  743. $dispatch = DispatchSub::where('del_time',0)
  744. ->whereIn('order_product_id', array_column($production_list,'id'))
  745. ->get()->toArray();
  746. if(empty($dispatch)) return [true,''];
  747. $dispatch_map = [];
  748. foreach ($dispatch as $value){
  749. $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
  750. $dispatch_map[$key][] = $value;
  751. }
  752. $result = []; $tmp = [];
  753. foreach ($dispatch_map as $key => $value){
  754. $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
  755. $finished_num = array_sum(array_column($value, 'finished_num'));
  756. if($dispatch_quantity == $finished_num) continue; //同一批派工 且 全部完工的 跳过
  757. $order_id = explode('|', $key)[0];
  758. if(in_array($order_id, $tmp)) continue;// 生产订单id存在 跳过
  759. foreach ($value as $val){
  760. if($val['dispatch_quantity'] <= $val['finished_num']) continue; //某一个工序完工
  761. //包装数量
  762. $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
  763. if($tmp_num <= 0) continue;
  764. $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
  765. if($tmp_num2 >= $tmp_num){
  766. $val['quantity'] = $tmp_num;
  767. }else{
  768. $val['quantity'] = $tmp_num2;
  769. }
  770. $result[] = $val;
  771. $tmp[] = $order_id;
  772. }
  773. }
  774. if(empty($result)) return [true,''];
  775. $time = time();
  776. try {
  777. DB::beginTransaction();
  778. //班组下的人
  779. $team_man = [];
  780. $team_array = EmployeeTeamPermission::select('employee_id','team_id')->get()->toArray();
  781. foreach ($team_array as $value){
  782. $team_man[$value['team_id']][] = $value['employee_id'];
  783. }
  784. //工序 班组
  785. $process_map = [
  786. 12 => 65,
  787. 11 => 66,
  788. 15 => 69,
  789. 14 => 67,
  790. 13 => 68,
  791. ];
  792. //根据派工数据回写
  793. $last_update = $last_update2 = $str_tmp = [];
  794. foreach ($result as $value){
  795. $finished_num = $value['quantity'] + $value['finished_num'];
  796. DispatchSub::where('id',$value['id'])->update([
  797. 'finished_num' => $finished_num,
  798. 'wg_status' => 1,
  799. ]);
  800. $tmp = [
  801. 'id' => $value['id'],
  802. 'quantity' => $value['quantity'],
  803. 'product_no' => $value['product_no'] ?? "",
  804. 'product_title' => $value['product_title'] ?? "",
  805. 'product_size' => $value['product_size'] ?? "",
  806. 'product_unit' => $value['product_unit'] ?? "",
  807. ];
  808. $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
  809. if(! in_array($str, $str_tmp)){
  810. $last_update[] = $tmp;
  811. $str_tmp[] = $str;
  812. }
  813. $tmp['process_id'] = $value['process_id'];
  814. $team_tmp = $process_map[$value['process_id']] ?? 0; // 班组
  815. if(empty($team_tmp)){
  816. $tmp['finished_id'] = 0;
  817. $tmp['team_id'] = 0;
  818. $last_update2[] = $tmp;
  819. }else{
  820. $t_m = $team_man[$team_tmp] ?? []; //班组下的人
  821. if(empty($t_m)) {
  822. $tmp['finished_id'] = 0;
  823. $tmp['team_id'] = $team_tmp;
  824. $last_update2[] = $tmp;
  825. }else{
  826. foreach ($t_m as $m_v){
  827. $tmp['finished_id'] = $m_v;
  828. $tmp['team_id'] = $team_tmp;
  829. $last_update2[] = $tmp;
  830. }
  831. }
  832. }
  833. $quantity = $value['quantity'] * 1000;
  834. //工序表
  835. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  836. $process_model->where('order_product_id',$value['order_product_id'])
  837. ->where('process_id',$value['process_id'])
  838. ->where('dispatch_no',$value['dispatch_no'])
  839. ->where('status', 1)
  840. ->take($quantity)
  841. ->update([
  842. 'finished_time' => $time,
  843. 'status' => 2,
  844. 'finished_id' => 1,
  845. 'team_id' => $team_tmp,
  846. 'equipment_id' => 13,
  847. ]);
  848. }
  849. $service = new FinishedOrderService();
  850. //反写数量
  851. //生产订单
  852. $id = array_column($result,'order_product_id');
  853. $service->writeFinishedQuantityByOrdersProductId($id);
  854. //销售订单
  855. $service->writeFinishedQuantity($id);
  856. //生成完工入库申请单
  857. list($status, $msg) = $this->createWGSQ($last_update, $user);
  858. if(! $status) return [false, $msg];
  859. //生成报工单
  860. $service = new FinishedOrderService();
  861. list($status, $msg) = $service->createbg($last_update2, $user);
  862. if(! $status) return [false, $msg];
  863. DB::commit();
  864. }catch (\Throwable $exception){
  865. DB::rollBack();
  866. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  867. }
  868. return [true, ''];
  869. }
  870. public function createWGSQ($insert_id, $user, $time = 0){
  871. //完工入库申请单自动审核并同步用友
  872. $em = new EmployeeService();
  873. $auto = $em->is_auto($user, "wgsq_auto");
  874. try {
  875. DB::beginTransaction();
  876. //生成申请单
  877. $service = new ApplyOrderService();
  878. list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_two, $auto, $time);
  879. if(! $status) {
  880. DB::rollBack();
  881. return [false, $msg];
  882. }
  883. if($auto) {
  884. //生成流水
  885. list($status, $msg) = $service->createRecord($msg);
  886. if(! $status) {
  887. DB::rollBack();
  888. return [false, $msg];
  889. }
  890. }
  891. DB::commit();
  892. }catch (\Throwable $exception){
  893. DB::rollBack();
  894. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  895. }
  896. return [true, ''];
  897. }
  898. public function saveOutOrder($data,$user){
  899. list($status,$msg) = $this->saveOutOrderRule($data,$user);
  900. if(! $status) return [false, $msg];
  901. $limit_key = "saveOutOrder" . $user['id'];
  902. list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
  903. if(! $status) return [false, '正在生成销售出库订单数据,请稍后尝试!'];
  904. //不超时
  905. ini_set('max_execution_time', 0);
  906. try {
  907. DB::beginTransaction();
  908. //组织数据
  909. $yongyou_data = $update = [];
  910. foreach ($data['out_data'] as $value){
  911. $pro_tmp = [];
  912. foreach ($value['product'] as $p){
  913. $t_k = $p['cinvcode'] . $p['cfree1'];
  914. $pro_tmp[$t_k] = [
  915. 'cinvcode' => $p['cinvcode'],
  916. 'cfree1' => $p['cfree1'],
  917. 'iquantity' => $p['iquantity'],
  918. 'line' => $p['line'],
  919. ];
  920. }
  921. if(isset($yongyou_data[$value['customer_code']])){
  922. $yongyou_data[$value['customer_code']]['cdlcode_string'] .= ',' . $value['cdlcode'];
  923. foreach ($pro_tmp as $k => $v){
  924. if(isset($yongyou_data[$value['customer_code']]['product'][$k])){
  925. $yongyou_data[$value['customer_code']]['product'][$k]['iquantity'] += $v['iquantity'];
  926. }else{
  927. $yongyou_data[$value['customer_code']]['product'][$k] = $v;
  928. }
  929. }
  930. }else{
  931. $yongyou_data[$value['customer_code']] = [
  932. 'customer_code' => $value['customer_code'],
  933. 'cdlcode_string' => $value['cdlcode'],
  934. 'product' => $pro_tmp
  935. ];
  936. }
  937. foreach ($value['box_no'] as $box_no){
  938. $update[$box_no] = $value['cdlcode'];
  939. }
  940. }
  941. if(empty($yongyou_data)) {
  942. $this->dellimitingSendRequestBackgNeed($limit_key);
  943. return [false, '暂无写入用友发货出库数据!'];
  944. }
  945. //用友 ------发货出库
  946. $service = new FinishedOrderService();
  947. list($status,$msg) = $service->U8Rdrecord32Save($yongyou_data,$user);
  948. if(! $status) {
  949. $this->dellimitingSendRequestBackgNeed($limit_key);
  950. return [false, $msg];
  951. }
  952. //本地数据更新
  953. if(! empty($update)){
  954. foreach ($update as $key => $value){
  955. Box::where('order_no', $key)->update([
  956. 'shipment_order_no' => $value
  957. ]);
  958. }
  959. }
  960. DB::commit();
  961. $this->dellimitingSendRequestBackgNeed($limit_key);
  962. }catch (\Throwable $exception){
  963. $this->dellimitingSendRequestBackgNeed($limit_key);
  964. DB::rollBack();
  965. return [false, $exception->getFile() . $exception->getFile() . $exception->getMessage()];
  966. }
  967. return [true, ''];
  968. }
  969. public function saveOutOrderRule($data,$user){
  970. if(empty($data['out_data'])) return [false, '未获取到发货数据,操作失败!'];
  971. Log::channel('sendData')->info('发货:提交包装单数据', ["param" => $data]);
  972. //客户校验
  973. $customer_code = array_column($data['out_data'],'customer_code');
  974. $isAllSame = count(array_unique($customer_code)) === 1;
  975. if(! $isAllSame) return [false, '发货出库,请选择同一个客户的订单!'];
  976. $box_no = array_column($data['out_data'],'box_no');
  977. if(empty($box_no)) return [false, '包装单信息不能为空'];
  978. $box_arr = [];
  979. foreach ($box_no as $value){
  980. foreach ($value as $val){
  981. $box_arr[] = $val;
  982. }
  983. }
  984. $box_map = Box::where('del_time',0)
  985. ->whereIn('order_no',$box_arr)
  986. ->pluck('shipment_order_no', "order_no")
  987. ->toArray();
  988. foreach ($box_arr as $value){
  989. $tmp = $box_map[$value] ?? "";
  990. if($tmp != "") return [false, "包装单:" . $value . "已在发货单(" . $tmp .")中发出!"];
  991. }
  992. //用友发货数据查询
  993. $cdlcode = array_column($data['out_data'],'cdlcode');
  994. $service = new FyySqlServerService($user);
  995. if($service->error) return [false, $service->error];
  996. $out_data_map = $service->getDataFromDispatchListForCheck(['cdlcode' => $cdlcode]);
  997. if(empty($out_data_map)) return [false, '未在用友发货数据中查找到相关数据!'];
  998. //数据校验
  999. foreach ($data['out_data'] as $value){
  1000. if(empty($value['cdlcode'])) return [false, '发货单号不能为空!'];
  1001. if(! isset($out_data_map[$value['cdlcode']])) return [false, '发货单号:' . $value['cdlcode'] . '不存在或已被删除!'];
  1002. $tmp = $out_data_map[$value['cdlcode']];
  1003. if(empty($value['customer_code'])) return [false, '客户编码不能为空!'];
  1004. if($tmp['customer_code'] != $value['customer_code']) return [false, '客户编码与用友数据中不一致!'];
  1005. if(empty($tmp['product'])) return [false, '发货单号:' . $value['cdlcode'] . '未查找到用友的发货产品数据!'];
  1006. $tmp_pro = [];
  1007. foreach ($tmp['product'] as $t){
  1008. $t_k = $t['cinvcode'] . $t['cfree1'];
  1009. $tmp_pro[$t_k] = bcsub($t['iquantity'] , $t['out_quantity'],3);//未发货数量
  1010. }
  1011. if(empty($value['product'])) return [false, '发货产品不能为空!'];
  1012. foreach ($value['product'] as $p){
  1013. if(empty($p['line'])) return [false, '存货行号不能为空!'];
  1014. if(empty($p['cinvcode'])) return [false, '存货编码不能为空!'];
  1015. if(empty($p['cfree1'])) return [false, '存货颜色不能为空!'];
  1016. $p_k = $p['cinvcode'] . $p['cfree1'];
  1017. if(! isset($tmp_pro[$p_k])) return [false, '发货单号:' . $value['cdlcode'] . '下无该发货产品:编码为' . $p['cinvcode'] . ',颜色为:' . $p['cfree1']];
  1018. if(empty($p['iquantity']) || $p['iquantity'] <= 0) return [false, '发货数量不能为空!'];
  1019. $last = $tmp_pro[$p_k];
  1020. if($p['iquantity'] > $last) return [false, '发货单号:' . $value['cdlcode'] . '下的发货产品:编码为' . $p['cinvcode'] . ',颜色为' . $p['cfree1'] . '的未发货数量为' . $last];
  1021. }
  1022. }
  1023. return [true, ''];
  1024. }
  1025. /**
  1026. * 根据发货单包装(恒成塑业不需要 如果有调用 直接返回)
  1027. * @param $data
  1028. * @return array
  1029. */
  1030. public function transportBoxIn($data)
  1031. {
  1032. return [true,''];
  1033. $param = $data['params'];
  1034. $shipment_order_no = $data['transport_no'];
  1035. $sale_order_nos = [];
  1036. $key_list = [];
  1037. foreach ($param as $v){
  1038. if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no'];
  1039. $key = $this->getKey($v);
  1040. $key_list[$key] = [
  1041. 'num' => $v['num'],
  1042. 'order_no' => $v['order_no'],
  1043. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  1044. ];
  1045. }
  1046. $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray();
  1047. $sale_order_product_key_list = [];
  1048. foreach ($sale_order_product_list as $v){
  1049. $key = $this->getKey($v);
  1050. $sale_order_product_key_list[$key] = $v['id'];
  1051. }
  1052. $param = [];
  1053. foreach ($key_list as $k=>$v){
  1054. if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!'];
  1055. $param[$v['order_no']][] = [
  1056. 'id' => -(intval($sale_order_product_key_list[$k])),
  1057. 'team_id' => $v['team_id'],
  1058. 'params' => [$v['num']]
  1059. ];
  1060. }
  1061. // $box_no = self::$box_hook->setOrderNo(); todo
  1062. foreach ($param as $k=>$v){
  1063. $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no');
  1064. $postParam = [
  1065. 'order_no' => $top_order_no,
  1066. 'param' => $v,
  1067. 'box_no' => $box_no,
  1068. 'transport_no' => $shipment_order_no,
  1069. ];
  1070. $this->boxIn($postParam);
  1071. }
  1072. return [true,''];
  1073. }
  1074. protected function getKey($data){
  1075. return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark'];
  1076. }
  1077. /**
  1078. * 包装详情1用于包装前
  1079. * @param $data
  1080. * @return array
  1081. */
  1082. public function boxDetail($data)
  1083. {
  1084. list($status, $data) = self::$box_hook->boxDetail($data);
  1085. if (!$status) return [false, $data];
  1086. $return = [];
  1087. foreach ($data as $v){
  1088. $key = $v['ext_1'] . $v['ext_3'];
  1089. if(! isset($return[$key])){
  1090. $return[$key] = $v;
  1091. }else{
  1092. $return[$key]['num'] += $v['num'];
  1093. }
  1094. }
  1095. $sale_orders_product_ids = array_unique(array_column($data,'top_id'));
  1096. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  1097. $key_list = [];
  1098. foreach ($list as $v){
  1099. $key_list[$v['id']] = $v;
  1100. }
  1101. foreach ($return as &$v){
  1102. $detail = $key_list[$v['top_id']];
  1103. $v['customer_no'] = $detail['customer_no'];
  1104. $v['customer_name'] = $detail['customer_name'];
  1105. }
  1106. return [true, array_values($return)];
  1107. }
  1108. public function boxProductList($data){
  1109. if(empty($data['id']) && empty($data['out_order_no'])) return [false,'请选择数据!'];
  1110. if(! empty($data['id'])){
  1111. $sale_order_ids = $data['id'];
  1112. }else{
  1113. $ids = SaleOrdersProduct::where('del_time',0)
  1114. ->where('out_order_no','Like','%'. $data['out_order_no'] . '%')
  1115. ->select('id')
  1116. ->get()->toArray();
  1117. $sale_order_ids = array_column($ids,'id');
  1118. if(empty($sale_order_ids)) return [false, '暂无数据!'];
  1119. }
  1120. $model = SaleOrdersProduct::where('del_time',0)
  1121. ->whereIn('id',$sale_order_ids)
  1122. ->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')
  1123. ->orderBy('id','desc')
  1124. ->get()->toArray();
  1125. //原先按照完工数量 未包装 = 完工数量 - 已包数量
  1126. $product_list = ordersProduct::where('del_time',0)
  1127. ->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")
  1128. ->get()->toArray();
  1129. $model_key_list = [];
  1130. foreach ($model as $v){
  1131. $model_key_list[$v['id']] = $v;
  1132. }
  1133. $model = [];
  1134. //存在生产订单,组织数据
  1135. if(! empty($product_list)){
  1136. $product_key_num_list = [];
  1137. foreach ($product_list as $v){
  1138. //下派工数量 就是 能包的数量 相等以后 踢掉数据
  1139. if($v['dispatch_complete_quantity'] == $v['box_num']) continue;
  1140. if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']])) {
  1141. $product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
  1142. 'product_num' => 0 ,
  1143. 'box_num' => 0 ,
  1144. ];
  1145. }
  1146. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
  1147. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
  1148. $detail = $model_key_list[$v['sale_orders_product_id']];
  1149. $detail['box_type'] = 1;
  1150. $detail['id'] = $v['id'];
  1151. $detail['box_num'] = $v['box_num'];
  1152. $detail['dispatch_complete_quantity'] = $v['dispatch_complete_quantity'];
  1153. $detail['un_box_num'] = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
  1154. $detail['type'] = 1;
  1155. $detail['production_no'] = $v['production_no'];
  1156. $detail['production_quantity'] = $v['production_quantity'];
  1157. $model[] = $detail;
  1158. }
  1159. }
  1160. $return = [];
  1161. foreach ($model as $v){
  1162. if(!isset($v['box_type'])) {continue; //没有销售订单直接包了。
  1163. $product_num = 0;
  1164. $box_num = 0;
  1165. if(isset($product_key_num_list[$v['id']])){
  1166. foreach ($product_key_num_list[$v['id']] as $vv){
  1167. $product_num += $vv['product_num'];
  1168. $box_num += $vv['box_num'];
  1169. }
  1170. }
  1171. if(($v['order_quantity'] - $product_num) === 0) continue;
  1172. $product_key_list[$v['sale_orders_product_id']] = [
  1173. 'out_order_no' => $v['out_order_no'],
  1174. 'order_no' => $v['order_no'],
  1175. 'production_no' => "",
  1176. 'production_time' => '未下生产',
  1177. 'customer_no' => $v['customer_no'],
  1178. 'customer_name' => $v['customer_name'],
  1179. 'product_no' => $v['product_no'],
  1180. 'product_title' => $v['product_title'],
  1181. 'product_size' => $v['product_size'],
  1182. 'id' => -$v['sale_orders_product_id'],
  1183. 'technology_material' => $v['technology_material'],
  1184. 'technology_name' => $v['technology_name'],
  1185. 'wood_name' => $v['wood_name'],
  1186. 'process_mark' => $v['process_mark'],
  1187. 'type' => '2',
  1188. 'is_box_num' => $v['box_num'] - $box_num,
  1189. 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
  1190. 'sale_num' => $v['order_quantity'],
  1191. ];
  1192. }else{
  1193. $return[] = [
  1194. 'id' => $v['id'],
  1195. 'technology_material' => $v['technology_material'],
  1196. 'technology_name' => $v['technology_name'],
  1197. 'wood_name' => $v['wood_name'],
  1198. 'order_no' => $v['order_no'],
  1199. 'production_no' => $v['production_no'],
  1200. 'process_mark' => $v['process_mark'],
  1201. 'out_order_no' => $v['out_order_no'],
  1202. 'production_time' => date('Y-m-d',$v['crt_time']),
  1203. 'customer_no' => $v['customer_no'],
  1204. 'customer_name' => $v['customer_name'],
  1205. 'product_no' => $v['product_no'],
  1206. 'product_title' => $v['product_title'],
  1207. 'product_size' => $v['product_size'],
  1208. 'type' => '1',
  1209. 'is_box_num' => $v['box_num'],
  1210. 'un_box_num' => $v['un_box_num'],
  1211. 'sale_num' => $v['order_quantity'],
  1212. 'production_num' => $v['production_quantity'],
  1213. 'dispatch_complete_quantity' => $v['dispatch_complete_quantity']
  1214. ];
  1215. }
  1216. }
  1217. return [true,$return];
  1218. }
  1219. /**
  1220. * 包装详情2用于包装后
  1221. * @param $data
  1222. * @return array
  1223. */
  1224. public function boxOrderDetail($data)
  1225. {
  1226. list($status, $data) = self::$box_hook->boxDetail($data);
  1227. $sale_orders_product_ids = [];
  1228. foreach ($data as $v){
  1229. $sale_orders_product_ids[] = $v['top_id'];
  1230. }
  1231. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  1232. $key_list = [];
  1233. foreach ($list as $v){
  1234. $key_list[$v['id']] = $v;
  1235. }
  1236. foreach ($data as &$v){
  1237. $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
  1238. $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
  1239. $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
  1240. $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
  1241. $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
  1242. }
  1243. if (!$status) return [false, $data];
  1244. return [true, $data];
  1245. }
  1246. /**
  1247. * 通过销售单号获取包装单
  1248. * @param $data
  1249. * @return array
  1250. */
  1251. public function boxOrderDetailByTop($data){
  1252. $list = new Box();
  1253. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  1254. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  1255. // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  1256. // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  1257. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
  1258. return [true,$list];
  1259. }
  1260. /**
  1261. * 通过包装单获取包装详情
  1262. * @param $data
  1263. * @return array
  1264. */
  1265. public function boxOrderDetailByOrderNo($data){
  1266. $order_nos = $data['order_nos'];
  1267. $order_nos = array_unique($order_nos);
  1268. $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();
  1269. $detail_list = [];
  1270. foreach ($list as $v){
  1271. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1272. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  1273. }
  1274. $list = $detail_list;
  1275. $ids = [];
  1276. foreach ($list as $v){
  1277. $ids[] = $v['top_id'];
  1278. }
  1279. $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
  1280. $sale_orders_key_product = [];
  1281. foreach ($sale_orders_product as $v){
  1282. $sale_orders_key_product[$v['id']] = $v;
  1283. }
  1284. $team_key_list = Team::pluck('title','id')->toArray();
  1285. foreach ($list as &$v){
  1286. $detail = $sale_orders_key_product[$v['top_id']];
  1287. $v['customer_name'] = $detail['customer_name'];
  1288. $v['table_body_mark'] = $detail['table_body_mark'];
  1289. $v['ext_4'] = $detail['product_title'];
  1290. $v['ext_5'] = $detail['product_size'];
  1291. $v['customer_name'] = $detail['customer_name'];
  1292. $v['technology_material'] = $detail['technology_material'];
  1293. $v['transport_no'] = $v['shipment_order_no'];
  1294. $v['wood_name'] = $detail['wood_name'];
  1295. $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
  1296. }
  1297. return [true,$list];
  1298. }
  1299. public function boxOrderGroup($data){
  1300. $list = new Box();
  1301. if(empty($data['top_order_no']) && empty($data['transport_no'])) return [false,'请输入单号!'];
  1302. if(! empty($data['top_order_no'])) $list = $list->whereIn('top_order_no',$data['top_order_no']);
  1303. if(! empty($data['transport_no'])) {
  1304. if(is_array($data['transport_no'])){
  1305. $list = $list->whereIn('shipment_order_no',$data['transport_no']);
  1306. }else{
  1307. $list = $list->where('shipment_order_no','Like','%'. $data['transport_no'] . '%');
  1308. }
  1309. }
  1310. $list = $list->select('top_order_no','order_no as box_no','crt_time','shipment_order_no','out_order_no')->where('del_time',0)
  1311. ->get()->toArray();
  1312. foreach ($list as $key => $value){
  1313. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  1314. $tmp = $model->where('del_time',0)
  1315. ->where('order_no',$value['box_no'])
  1316. ->where('out_order_no',$value['out_order_no'])
  1317. ->select('order_no','out_order_no','orders_product_id')
  1318. ->first();
  1319. $production_no = "";
  1320. if(! empty($tmp)){
  1321. $production_no = OrdersProduct::where('id',$tmp->orders_product_id)->select('production_no')->pluck('production_no')->toArray();
  1322. $production_no = $production_no[0];
  1323. }
  1324. $list[$key]['production_no'] = $production_no;
  1325. $list[$key]['crt_time'] = date('Y-m-d H:i:s',$value['crt_time']);
  1326. }
  1327. return [true,$list];
  1328. }
  1329. public function boxOrderGroupGwp($data){
  1330. $out_order_no = $data['out_order_no'] ?? "";
  1331. $model = Box::where('del_time',0)
  1332. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  1333. return $query->where('out_order_no','Like','%'. $out_order_no . '%');
  1334. })
  1335. ->select('out_order_no','ext_1','top_order_no','order_no','crt_time','shipment_order_no',DB::raw("count(id) as package_num"))
  1336. ->groupBy('out_order_no');
  1337. $list = $this->limit($model,'',$data);
  1338. $list = $this->fillData($list);
  1339. return [true, $list];
  1340. }
  1341. public function fillData($data){
  1342. if(empty($data['data'])) return $data;
  1343. foreach ($data['data'] as $key => $value){
  1344. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  1345. $tmp = $model->where('del_time',0)
  1346. ->where('out_order_no',$value['out_order_no'])
  1347. ->select('num')
  1348. ->get()->toArray();
  1349. $data['data'][$key]['num'] = $this->getTotal($tmp, 'num');
  1350. }
  1351. return $data;
  1352. }
  1353. public function boxOrderGroupGwpDetail($data){
  1354. if(empty($data['top_order_no']) || empty($data['out_order_no'])) return [false, '请选择数据!'];
  1355. $top_order_no = $data['top_order_no'];
  1356. $model = new BoxDetail(['channel'=> $top_order_no]);
  1357. $detail_list = $model->where('del_time',0)
  1358. ->where('out_order_no',$data['out_order_no'])
  1359. ->get()->toArray();
  1360. $map = Box::whereIn('order_no',array_unique(array_column($detail_list,'order_no')))
  1361. ->pluck('ext_1','order_no')
  1362. ->toArray();
  1363. $return = [];
  1364. foreach ($detail_list as $value){
  1365. //销售订单号 包装单号 产品编码 颜色
  1366. $key = $value['out_order_no'] . $value['order_no'] . $value['ext_1'] . $value['ext_3'];
  1367. if(isset($return[$key])){
  1368. $return[$key]['num'] += $value['num'];
  1369. }else{
  1370. $return[$key] = [
  1371. 'box_no' => $value['order_no'],
  1372. 'out_order_no' => $value['out_order_no'],
  1373. 'ext_8' => $value['ext_8'],
  1374. 'ext_3' => $value['ext_3'],
  1375. 'num' => $value['num'],
  1376. 'crt_time' => date("Y-m-d",$value['crt_time']),
  1377. 'top_order_no' => $value['top_order_no'],
  1378. 'customer_name' => $map[$value['order_no']] ?? "",
  1379. ];
  1380. }
  1381. }
  1382. return [true, array_values($return)];
  1383. }
  1384. public function transportNo(){
  1385. $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
  1386. return [true,$list];
  1387. }
  1388. public function delBoxDetail($data){
  1389. if(empty($data['order_nos'])) return [false, '请选择包装单!'];
  1390. $order_nos = $data['order_nos'];
  1391. try {
  1392. DB::beginTransaction();
  1393. //更新销售订单 生产订单数据
  1394. $orders_product_id_map = [];
  1395. foreach ($order_nos as $v){
  1396. $list = self::$box_hook->delBox($v);
  1397. if(empty($list)) continue;
  1398. foreach ($list as $vv){
  1399. SaleOrdersProduct::where('id',$vv['top_id'])->update([
  1400. 'box_num' => DB::raw('box_num - '.$vv['num']),
  1401. 'finished_num' => DB::raw('finished_num - '.$vv['num'])
  1402. ]);
  1403. OrdersProduct::where('id',$vv['orders_product_id'])->update([
  1404. 'box_num' => DB::raw('box_num - '.$vv['num']),
  1405. 'finished_num' => DB::raw('finished_num - '.$vv['num'])
  1406. ]);
  1407. if(isset($orders_product_id_map[$vv['orders_product_id']])){
  1408. $num = bcadd($orders_product_id_map[$vv['orders_product_id']], $vv['num'],3);
  1409. $orders_product_id_map[$vv['orders_product_id']] = $num;
  1410. }else{
  1411. $orders_product_id_map[$vv['orders_product_id']] = $vv['num'];
  1412. }
  1413. }
  1414. }
  1415. //更新完工数据 和 底表数据
  1416. list($status,$msg) = $this->updateFinishDel($orders_product_id_map);
  1417. if(! $status) return [false, $msg];
  1418. DB::commit();
  1419. }catch (\Throwable $exception){
  1420. DB::rollBack();
  1421. return [false, $exception->getMessage()];
  1422. }
  1423. return [true, ''];
  1424. }
  1425. public function updateFinishDel($orders_product_id_map){
  1426. if(empty($orders_product_id_map)) return [true,''];
  1427. $production_list = array_keys($orders_product_id_map);
  1428. $dispatch = $this->getDispatch($production_list, $orders_product_id_map);
  1429. if(empty($dispatch)) return [true,''];
  1430. try {
  1431. DB::beginTransaction();
  1432. //根据派工数据回写
  1433. foreach ($dispatch as $value){
  1434. $finished_num = bcsub($value['finished_num'], $value['out_number'],3);
  1435. DispatchSub::where('id',$value['id'])->update([
  1436. 'finished_num' => $finished_num
  1437. ]);
  1438. $quantity = $value['out_number'] * 1000;
  1439. //工序表
  1440. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  1441. $process_model->where('order_product_id',$value['order_product_id'])
  1442. ->where('process_id',$value['process_id'])
  1443. ->where('dispatch_no',$value['dispatch_no'])
  1444. ->where('status', 2)
  1445. ->take($quantity)
  1446. ->update([
  1447. 'finished_time' => 0,
  1448. 'status' => 1,
  1449. 'finished_id' => 0,
  1450. 'team_id' => 0,
  1451. 'equipment_id' => 0,
  1452. ]);
  1453. }
  1454. DB::commit();
  1455. }catch (\Throwable $exception){
  1456. DB::rollBack();
  1457. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  1458. }
  1459. return [true, ''];
  1460. }
  1461. public function getDispatch($order_product_id,$map){
  1462. $order = OrdersProduct::where('del_time',0)
  1463. ->where('id', $order_product_id)
  1464. ->select('id', 'process_id')
  1465. ->get()->toArray();
  1466. $order_map = [];
  1467. foreach ($order as $value){
  1468. $order_map[$value['id']] = explode(',', $value['process_id']);
  1469. }
  1470. //组织派工数据
  1471. $dispatch = DispatchSub::where('del_time',0)
  1472. ->whereIn('order_product_id', $order_product_id)
  1473. ->where('finished_num','>',0)
  1474. ->get()->toArray();
  1475. if(empty($dispatch)) return [];
  1476. $dispatch_map = [];
  1477. foreach ($dispatch as $value){
  1478. $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
  1479. $dispatch_map[$key][] = $value;
  1480. }
  1481. $result = []; $tmp = [];
  1482. foreach ($dispatch_map as $key => $value){
  1483. foreach ($value as $v){
  1484. $process = $order_map[$v['order_product_id']] ?? [];
  1485. $num = $map[$v['order_product_id']];
  1486. if(in_array($v['process_id'], $process)){
  1487. $str = $v['order_product_id'] . $v['process_id'];
  1488. $tmp_v = $tmp[$str] ?? 0;
  1489. $num = bcsub($num, $tmp_v,3);
  1490. if($num <= 0) continue;
  1491. if($v['finished_num'] >= $num){
  1492. $v['out_number'] = $num;
  1493. $result[] = $v;
  1494. }else{
  1495. $v['out_number'] = $v['finished_num'];
  1496. $result[] = $v;
  1497. }
  1498. if(isset($tmp[$str])){
  1499. $num = bcadd($tmp[$str], $v['out_number'],3);
  1500. $tmp[$str] = $num;
  1501. }else{
  1502. $tmp[$str] = $v['out_number'];
  1503. }
  1504. }
  1505. }
  1506. }unset($tmp);
  1507. return $result;
  1508. }
  1509. public function boxAdd($data)
  1510. {
  1511. list($status,$msg) = $this->boxAddRule($data);
  1512. if(! $status) return [false,$msg];
  1513. try{
  1514. DB::beginTransaction();
  1515. $box = new Box();
  1516. $box->order_no = $msg['order_no'];
  1517. $box->save();
  1518. DB::table('box_detail')->insert($msg['detail']);
  1519. DB::commit();
  1520. return [true,''];
  1521. }catch (\Exception $e){
  1522. DB::rollBack();
  1523. return [false,$e->getLine().':'.$e->getMessage()];
  1524. }
  1525. }
  1526. public function boxAddRule($data)
  1527. {
  1528. if(empty($data['param'])) return [false,'请选择包装数据'];
  1529. // $order_no = (new BoxHookService())->setOrderNo();
  1530. $order_no = "";
  1531. if(empty($order_no)) return [false,'包装单号不能为空'];
  1532. $detail = [];
  1533. foreach ($data['param'] as $value) {
  1534. $key = $this->lock_key.'_'.$value['idlsid'];
  1535. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1536. if(! $status) return [false,$msg];
  1537. // if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
  1538. if(empty($value['team_id'])) return [false,'班组必须选择'];
  1539. $tmp = $value;
  1540. $tmp['iquantity'] = $value['submit_quantity'];
  1541. $detail[] = [
  1542. 'order_no' => $order_no,
  1543. 'out_order_no' => $value['csocode']??'',
  1544. 'crt_time' => time(),
  1545. 'upd_time' => time(),
  1546. 'type' => 1,
  1547. 'ext_1' => $value['cinvcode'],
  1548. 'ext_2' => $value['technology_material'],
  1549. 'ext_3' => $value['cfree1'] ?? "",
  1550. 'ext_4' => $value['cfree2'] ?? "",
  1551. 'ext_5' => $value['process_mark'],
  1552. 'ext_6' => $value['customer_name'] ?? "",
  1553. 'ext_7' => $value['cuscode'] ?? "",
  1554. 'ext_8' => $value['product_title'],
  1555. 'ext_9' => $value['product_size'],
  1556. 'ext_10' => $value['table_header_mark'],
  1557. 'ext_11' => $value['table_body_mark'],
  1558. 'top_id' => $value['idlsid'],
  1559. 'state' => 0,
  1560. 'num' => $value['submit_quantity'],
  1561. 'box_type' => 2,
  1562. 'team_id' => $value['team_id'],
  1563. 'shipment_order_no' => $value['cdlcode'],
  1564. 'post' => json_encode($tmp),
  1565. ];
  1566. }
  1567. return [true,['order_no' => $order_no, 'detail' => $detail]];
  1568. }
  1569. public function boxFhDetail($data){
  1570. if(empty($data['idlsid'])) return [false,'请选择数据'];
  1571. $result = DB::table('box_detail')->where('del_time',0)
  1572. ->where('top_id',$data['idlsid'])
  1573. ->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')
  1574. ->get()->toArray();
  1575. if(! empty($result)){
  1576. foreach ($result as $key => $value){
  1577. $result[$key] = (array)$value;
  1578. }
  1579. }
  1580. return [true,$result];
  1581. }
  1582. public function boxFhDel($data){
  1583. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  1584. $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
  1585. DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
  1586. 'del_time' => time()
  1587. ]);
  1588. return [true,''];
  1589. }
  1590. public function transportDetail($data){
  1591. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  1592. $order_nos = $data['order_nos'];
  1593. $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();
  1594. foreach ($list as &$v){
  1595. $v['product_no'] = $v['ext_1'];
  1596. $v['technology_material'] = $v['ext_2'];
  1597. $v['technology_name'] = $v['ext_3'];
  1598. $v['wood_name'] = $v['ext_4'];
  1599. $v['process_mark'] = $v['ext_5'];
  1600. $v['customer_no'] = $v['ext_6'];
  1601. $v['customer_name'] = $v['ext_7'];
  1602. $v['product_title'] = $v['ext_8'];
  1603. $v['product_size'] = $v['ext_9'];
  1604. $v['post'] = json_decode($v['post'],true);
  1605. }
  1606. return [200,$list];
  1607. }
  1608. public function boxFhBzDetail($data){
  1609. if(empty($data['order_no'])) return [false,'请选择包装单数据'];
  1610. $result = DB::table('box_detail')->where('del_time',0)
  1611. ->whereIn('order_no',$data['order_no'])
  1612. ->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')
  1613. ->get()->toArray();
  1614. if(! empty($result)){
  1615. foreach ($result as $key => $value){
  1616. $result[$key] = (array)$value;
  1617. }
  1618. $map = Team::where('id',array_unique(array_column($result,'team_id')))->pluck('title','id')->toArray();
  1619. foreach ($result as $key => $value){
  1620. $result[$key]['team_name'] = $map[$value['team_id']] ?? '';
  1621. }
  1622. }
  1623. return [true,$result];
  1624. }
  1625. public function boxFhBzList($data){
  1626. if(empty($data['shipment_order_no'])) return [false, '请输入发货单号'];
  1627. $model = DB::table('box_detail')->where('del_time',0)
  1628. ->where('shipment_order_no','LIKE', '%'.$data['shipment_order_no'].'%')
  1629. ->select('order_no','crt_time','shipment_order_no');
  1630. $return = [];
  1631. $result = $model->get()->toArray();
  1632. if(! empty($result)){
  1633. foreach ($result as $value){
  1634. $return[$value->order_no] = [
  1635. 'order_no' => $value->order_no,
  1636. 'shipment_order_no' => $value->shipment_order_no,
  1637. 'crt_time' => date("Y-m-d H:i:s",$value->crt_time)
  1638. ];
  1639. }
  1640. }
  1641. return [true,array_values($return)];
  1642. }
  1643. public function boxOrderDetailByOrderNoNew($data){
  1644. if(empty($data['order_nos'])) return [false, '包装单号不能为空'];
  1645. $order_nos = $data['order_nos'];
  1646. $order_nos = array_unique($order_nos);
  1647. Log::channel('sendData')->info('发货:扫码包装单数据', ["param" => $order_nos]);
  1648. $list = Box::where('del_time',0)
  1649. ->whereIn('order_no',$order_nos)
  1650. ->where('shipment_order_no','')
  1651. ->select('order_no','out_order_no','top_order_no','shipment_order_no as transport_no','upd_time')
  1652. ->get()->toArray();
  1653. $detail_list = [];
  1654. foreach ($list as $v){
  1655. // if(! empty($v['transport_no'])) return [false, "包装单号:" . $v['order_no'] . "已在发货单(" . $v['transport_no'] .")中发出!"];
  1656. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1657. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  1658. }
  1659. $return = [];
  1660. foreach ($detail_list as $value){
  1661. //销售订单号 产品编码 颜色
  1662. $return[] = [
  1663. 'ext_1' => $value['ext_1'],
  1664. 'ext_3' => $value['ext_3'],
  1665. 'ext_8' => $value['ext_8'],
  1666. 'num' => $value['num'],
  1667. 'out_order_no' => $value['out_order_no'],
  1668. 'box_no' => $value['order_no']
  1669. ];
  1670. }
  1671. return [true, array_values($return)];
  1672. }
  1673. public function boxList($data,$user){
  1674. $model = Box::where('del_time',0)
  1675. ->select('id','order_no','out_order_no','top_order_no','crt_time','status','shipment_order_no')
  1676. ->orderBy('id','desc');
  1677. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  1678. if(! empty($data['shipment_order_no'])) $model->where('shipment_order_no', 'LIKE', '%'.$data['shipment_order_no'].'%');
  1679. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  1680. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  1681. if(isset($data['status'])) $model->where('status',$data['status']);
  1682. $list = $this->limit($model,'',$data);
  1683. $list = $this->fillBoxListData($list, $data,$user);
  1684. return [true, $list];
  1685. }
  1686. public function fillBoxListData($data, $erg,$user){
  1687. if(empty($data['data'])) return $data;
  1688. $product = $material = [];
  1689. if(! empty($erg['box'])){
  1690. //获取包装单里的产品
  1691. $id = array_column($data['data'], 'id');
  1692. list($product,$product_no) = $this->getBoxDetail($id);
  1693. if(! empty($erg['box_material'])){
  1694. //产品的包装材料
  1695. $service = new FyyOrderService();
  1696. list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
  1697. if($status) $material = $msg;
  1698. }
  1699. }
  1700. foreach ($data['data'] as $key => $value){
  1701. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1702. $data['data'][$key]['status_title'] = Box::$status_name[$value['status']] ?? "";
  1703. $p = $product[$value['id']] ?? [];
  1704. if(! empty($p)){
  1705. foreach ($p as $k => $v){
  1706. $p[$k]['box_material'] = $material[$v['product_no']] ?? [];
  1707. }
  1708. }
  1709. $data['data'][$key]['product'] = $p;
  1710. }
  1711. return $data;
  1712. }
  1713. public function getBoxDetail($id = []){
  1714. $return = $product_no= [];
  1715. if(empty($id)) return [$return,$product_no];
  1716. $box = Box::where('del_time',0)
  1717. ->whereIn('id', $id)
  1718. ->select('id', 'order_no','top_order_no')
  1719. ->get()->toArray();
  1720. if (empty($box)) return [$return,$product_no];
  1721. $map = array_column($box,'id','order_no');
  1722. $result = [];
  1723. foreach ($box as $v){
  1724. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1725. $result = array_merge($result,$model->where('del_time',0)
  1726. ->where('order_no',$v['order_no'])
  1727. ->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());
  1728. }
  1729. foreach ($result as $value){
  1730. $value['id'] = $map[$value['order_no']] ?? 0;
  1731. $value['product_unit'] = "吨";
  1732. $return[$value['id']][] = $value;
  1733. if(! in_array($value['product_no'], $product_no)) $product_no[] = $value['product_no'];
  1734. }
  1735. return [$return, $product_no];
  1736. }
  1737. public function searchDetail($data){
  1738. $out_order_no = $data['out_order_no'] ?? '';
  1739. $box = Box::where('del_time',0)
  1740. ->where('out_order_no', 'LIKE', '%'. $out_order_no .'%')
  1741. ->select('top_order_no')
  1742. ->get()->toArray();
  1743. if(empty($box)) return [];
  1744. $order_no = [];
  1745. foreach ($box as $value){
  1746. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  1747. $result = $model->where('del_time',0)
  1748. ->where('order_no',$box['order_no'])
  1749. ->select('order_no')
  1750. ->get()->toArray();
  1751. $detail_list = array_merge($order_no, array_column($result, 'order_no'));
  1752. }
  1753. $result = BoxDetail::where('del_time',0)
  1754. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  1755. return $query->where('out_order_no', 'LIKE', '%'. $out_order_no .'%');
  1756. })
  1757. ->when(! empty($shipment_order_no), function ($query) use ($shipment_order_no) {
  1758. return $query->where('shipment_order_no', 'LIKE', '%'. $shipment_order_no .'%');
  1759. })
  1760. ->select('order_no')
  1761. ->get()->toArray();
  1762. return array_unique(array_column($result,'order_no'));
  1763. }
  1764. public function boxFhDetailNew($data){
  1765. if(empty($data['id'])) return [false,'请选择数据'];
  1766. $box = Box::where('id', $data['id'])->where('del_time',0)->first();
  1767. if(empty($box)) return [false, '包装单不存在或已被删除'];
  1768. $box = $box->toArray();
  1769. $model = new BoxDetail(['channel'=>$box['top_order_no']]);
  1770. $result = $model->where('del_time',0)
  1771. ->where('order_no',$box['order_no'])
  1772. ->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();
  1773. foreach ($result as $key => $value){
  1774. $result[$key]['product_unit'] = "吨";
  1775. }
  1776. return [true,$result];
  1777. }
  1778. }