BoxService.php 85 KB

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