BoxService.php 84 KB

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