BoxService.php 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  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. 'report_working_id' => $id,
  687. ]);
  688. }
  689. }
  690. DB::commit();
  691. }catch (\Throwable $exception){
  692. DB::rollBack();
  693. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  694. }
  695. return [true, [$return, $get_order_number]];
  696. }
  697. //反写
  698. public function writeDispatchQuantity($get_order_number_result){
  699. $result = ScrappCount::whereIn('order_number',$get_order_number_result)
  700. ->select('dispatch_sub_id','quantity','scrapp_num as num','crt_time','process_id','order_product_id')
  701. ->get()->toArray();
  702. //是否最后一道工序
  703. $orders = OrdersProduct::whereIn('id', array_unique(array_column($result, 'order_product_id')))
  704. ->select('id','process_id')
  705. ->get()->toArray();
  706. $map = [];
  707. foreach ($orders as $value){
  708. $p = explode(",", $value['process_id']);
  709. $lastElement = end($p);
  710. $map[$value['id']] = $lastElement;
  711. }
  712. //汇总这个派工单一共质检良品数量 但是同一时间下的 只取一次
  713. $update = $flag = $need_update = [];
  714. foreach ($result as $value){
  715. if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id'] && ! in_array($value['order_product_id'], $need_update)){
  716. $need_update[] = [
  717. 'order_product_id' => $value['order_product_id'],
  718. 'process_id' => $value['process_id'],
  719. ];
  720. }
  721. $key = $value['dispatch_sub_id'] . $value['crt_time'];
  722. if(isset($flag[$key])) continue;
  723. if(isset($update[$value['dispatch_sub_id']])){
  724. $tmp = bcadd($value['quantity'], $update[$value['dispatch_sub_id']]['finished_num'], 3);
  725. $tmp1 = bcadd($value['num'], $update[$value['dispatch_sub_id']]['waste_num'], 3);
  726. $update[$value['dispatch_sub_id']] = [
  727. 'finished_num' => $tmp,
  728. 'waste_num' => $tmp1,
  729. ];
  730. }else{
  731. $update[$value['dispatch_sub_id']] = [
  732. 'finished_num' => $value['quantity'],
  733. 'waste_num' => $value['num'],
  734. ];
  735. }
  736. $flag[$key] = 1;
  737. }
  738. unset($flag);
  739. //更新派工单完工数量----------------------------------------------------
  740. foreach ($update as $id => $value){
  741. DispatchSub::where('id', $id)->update([
  742. 'finished_num' => DB::raw('finished_num + '.$value['finished_num']),
  743. 'waste_num' => DB::raw('waste_num + '.$value['waste_num']),
  744. ]);
  745. }
  746. if(! empty($need_update)){
  747. //更新生产订单的完工数量--------------------------------------------------
  748. $list = DispatchSub::where("del_time",0)
  749. ->when(! empty($need_update), function ($query) use ($need_update) {
  750. return $query->where(function ($q) use ($need_update) {
  751. foreach ($need_update as $value) {
  752. $order_product_id = $value['order_product_id'];
  753. $process_id = $value['process_id'];
  754. $q->orWhere(function ($subQ) use ($order_product_id, $process_id) {
  755. $subQ->where('order_product_id', $order_product_id)
  756. ->where('process_id', $process_id);
  757. });
  758. }
  759. });
  760. })
  761. ->select('order_product_id', 'finished_num', 'sale_orders_product_id')
  762. ->get()->toArray();
  763. $map2 = [];
  764. foreach ($list as $value){
  765. if(isset($map2[$value['order_product_id']])){
  766. $tmp = bcadd($map2[$value['order_product_id']], $value['finished_num'], 3);
  767. $map2[$value['order_product_id']] = $tmp;
  768. }else{
  769. $map2[$value['order_product_id']] = $value['finished_num'];
  770. }
  771. }
  772. foreach ($map2 as $id => $num){
  773. OrdersProduct::where('id', $id)->update([
  774. 'finished_num' => $num
  775. ]);
  776. }
  777. //更新销售订单完工数量-------------------------------------------------------
  778. $list2 = OrdersProduct::where('del_time',0)
  779. ->whereIn('sale_orders_product_id', array_unique(array_column($list, 'sale_orders_product_id')))
  780. ->select('finished_num','sale_orders_product_id')
  781. ->get()->toArray();
  782. $map3 = [];
  783. foreach ($list2 as $value){
  784. if(isset($map3[$value['sale_orders_product_id']])){
  785. $tmp = bcadd($map3[$value['sale_orders_product_id']], $value['finished_num'], 3);
  786. $map3[$value['sale_orders_product_id']] = $tmp;
  787. }else{
  788. $map3[$value['sale_orders_product_id']] = $value['finished_num'];
  789. }
  790. }
  791. foreach ($map3 as $id => $num){
  792. SaleOrdersProduct::where('id', $id)->update([
  793. 'finished_num' => $num
  794. ]);
  795. }
  796. }
  797. }
  798. //旧的完工
  799. public function updateFinish($production_list = [], $map,$user){
  800. if(empty($production_list)) return [true,''];
  801. $order_product_id = array_column($production_list,'id');
  802. $order = OrdersProduct::where('del_time',0)
  803. ->where('id', $order_product_id)
  804. ->select('id', 'process_id')
  805. ->get()->toArray();
  806. $order_map = [];
  807. foreach ($order as $value){
  808. $order_map[$value['id']] = explode(',', $value['process_id']);
  809. }
  810. //组织派工数据
  811. $dispatch = DispatchSub::where('del_time',0)
  812. ->whereIn('order_product_id', array_column($production_list,'id'))
  813. ->get()->toArray();
  814. if(empty($dispatch)) return [true,''];
  815. $dispatch_map = [];
  816. foreach ($dispatch as $value){
  817. $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
  818. $dispatch_map[$key][] = $value;
  819. }
  820. $result = []; $tmp = [];
  821. foreach ($dispatch_map as $key => $value){
  822. $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
  823. $finished_num = array_sum(array_column($value, 'finished_num'));
  824. if($dispatch_quantity == $finished_num) continue; //同一批派工 且 全部完工的 跳过
  825. $order_id = explode('|', $key)[0];
  826. if(in_array($order_id, $tmp)) continue;// 生产订单id存在 跳过
  827. foreach ($value as $val){
  828. if($val['dispatch_quantity'] <= $val['finished_num']) continue; //某一个工序完工
  829. //包装数量
  830. $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
  831. if($tmp_num <= 0) continue;
  832. $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
  833. if($tmp_num2 >= $tmp_num){
  834. $val['quantity'] = $tmp_num;
  835. }else{
  836. $val['quantity'] = $tmp_num2;
  837. }
  838. $result[] = $val;
  839. $tmp[] = $order_id;
  840. }
  841. }
  842. if(empty($result)) return [true,''];
  843. $time = time();
  844. try {
  845. DB::beginTransaction();
  846. //班组下的人
  847. $team_man = [];
  848. $team_array = EmployeeTeamPermission::select('employee_id','team_id')->get()->toArray();
  849. foreach ($team_array as $value){
  850. $team_man[$value['team_id']][] = $value['employee_id'];
  851. }
  852. //工序 班组
  853. $process_map = [
  854. 12 => 65,
  855. 11 => 66,
  856. 15 => 69,
  857. 14 => 67,
  858. 13 => 68,
  859. ];
  860. //根据派工数据回写
  861. $last_update = $last_update2 = $str_tmp = [];
  862. foreach ($result as $value){
  863. $finished_num = $value['quantity'] + $value['finished_num'];
  864. DispatchSub::where('id',$value['id'])->update([
  865. 'finished_num' => $finished_num,
  866. 'wg_status' => 1,
  867. ]);
  868. $tmp = [
  869. 'id' => $value['id'],
  870. 'quantity' => $value['quantity'],
  871. 'product_no' => $value['product_no'] ?? "",
  872. 'product_title' => $value['product_title'] ?? "",
  873. 'product_size' => $value['product_size'] ?? "",
  874. 'product_unit' => $value['product_unit'] ?? "",
  875. ];
  876. $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
  877. if(! in_array($str, $str_tmp)){
  878. $last_update[] = $tmp;
  879. $str_tmp[] = $str;
  880. }
  881. $tmp['process_id'] = $value['process_id'];
  882. $team_tmp = $process_map[$value['process_id']] ?? 0; // 班组
  883. if(empty($team_tmp)){
  884. $tmp['finished_id'] = 0;
  885. $tmp['team_id'] = 0;
  886. $last_update2[] = $tmp;
  887. }else{
  888. $t_m = $team_man[$team_tmp] ?? []; //班组下的人
  889. if(empty($t_m)) {
  890. $tmp['finished_id'] = 0;
  891. $tmp['team_id'] = $team_tmp;
  892. $last_update2[] = $tmp;
  893. }else{
  894. foreach ($t_m as $m_v){
  895. $tmp['finished_id'] = $m_v;
  896. $tmp['team_id'] = $team_tmp;
  897. $last_update2[] = $tmp;
  898. }
  899. }
  900. }
  901. $quantity = $value['quantity'] * 1000;
  902. //工序表
  903. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  904. $process_model->where('order_product_id',$value['order_product_id'])
  905. ->where('process_id',$value['process_id'])
  906. ->where('dispatch_no',$value['dispatch_no'])
  907. ->where('status', 1)
  908. ->take($quantity)
  909. ->update([
  910. 'finished_time' => $time,
  911. 'status' => 2,
  912. 'finished_id' => 1,
  913. 'team_id' => $team_tmp,
  914. 'equipment_id' => 13,
  915. ]);
  916. }
  917. $service = new FinishedOrderService();
  918. //反写数量
  919. //生产订单
  920. $id = array_column($result,'order_product_id');
  921. $service->writeFinishedQuantityByOrdersProductId($id);
  922. //销售订单
  923. $service->writeFinishedQuantity($id);
  924. //生成完工入库申请单
  925. // list($status, $msg) = $this->createWGSQ($last_update, $user);
  926. // if(! $status) return [false, $msg];
  927. //生成报工单
  928. $service = new FinishedOrderService();
  929. list($status, $msg) = $service->createbg($last_update2, $user);
  930. if(! $status) return [false, $msg];
  931. DB::commit();
  932. }catch (\Throwable $exception){
  933. DB::rollBack();
  934. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  935. }
  936. return [true, ''];
  937. }
  938. public function createWGSQ($insert_id, $user, $time = 0){
  939. //完工入库申请单自动审核并同步用友
  940. $em = new EmployeeService();
  941. $auto = $em->is_auto($user, "wgsq_auto");
  942. try {
  943. DB::beginTransaction();
  944. //生成申请单
  945. $service = new ApplyOrderService();
  946. list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_two, $auto, $time);
  947. if(! $status) {
  948. DB::rollBack();
  949. return [false, $msg];
  950. }
  951. if($auto) {
  952. //生成流水
  953. list($status, $msg) = $service->createRecord($msg);
  954. if(! $status) {
  955. DB::rollBack();
  956. return [false, $msg];
  957. }
  958. }
  959. DB::commit();
  960. }catch (\Throwable $exception){
  961. DB::rollBack();
  962. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  963. }
  964. return [true, ''];
  965. }
  966. public function saveOutOrder($data,$user){
  967. list($status,$msg) = $this->saveOutOrderRule($data,$user);
  968. if(! $status) return [false, $msg];
  969. $limit_key = "saveOutOrder" . $user['id'];
  970. list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
  971. if(! $status) return [false, '正在生成销售出库订单数据,请稍后尝试!'];
  972. //不超时
  973. ini_set('max_execution_time', 0);
  974. try {
  975. DB::beginTransaction();
  976. //组织数据
  977. $yongyou_data = $update = [];
  978. foreach ($data['out_data'] as $value){
  979. $pro_tmp = [];
  980. foreach ($value['product'] as $p){
  981. $t_k = $p['cinvcode'] . $p['cfree1'];
  982. $pro_tmp[$t_k] = [
  983. 'cinvcode' => $p['cinvcode'],
  984. 'cfree1' => $p['cfree1'],
  985. 'iquantity' => $p['iquantity'],
  986. 'line' => $p['line'],
  987. ];
  988. }
  989. if(isset($yongyou_data[$value['customer_code']])){
  990. $yongyou_data[$value['customer_code']]['cdlcode_string'] .= ',' . $value['cdlcode'];
  991. foreach ($pro_tmp as $k => $v){
  992. if(isset($yongyou_data[$value['customer_code']]['product'][$k])){
  993. $yongyou_data[$value['customer_code']]['product'][$k]['iquantity'] += $v['iquantity'];
  994. }else{
  995. $yongyou_data[$value['customer_code']]['product'][$k] = $v;
  996. }
  997. }
  998. }else{
  999. $yongyou_data[$value['customer_code']] = [
  1000. 'customer_code' => $value['customer_code'],
  1001. 'cdlcode_string' => $value['cdlcode'],
  1002. 'product' => $pro_tmp
  1003. ];
  1004. }
  1005. foreach ($value['box_no'] as $box_no){
  1006. $update[$box_no] = $value['cdlcode'];
  1007. }
  1008. }
  1009. if(empty($yongyou_data)) {
  1010. $this->dellimitingSendRequestBackgNeed($limit_key);
  1011. return [false, '暂无写入用友发货出库数据!'];
  1012. }
  1013. //用友 ------发货出库
  1014. $service = new FinishedOrderService();
  1015. list($status,$msg) = $service->U8Rdrecord32Save($yongyou_data,$user);
  1016. if(! $status) {
  1017. $this->dellimitingSendRequestBackgNeed($limit_key);
  1018. return [false, $msg];
  1019. }
  1020. //本地数据更新
  1021. if(! empty($update)){
  1022. foreach ($update as $key => $value){
  1023. Box::where('order_no', $key)->update([
  1024. 'shipment_order_no' => $value
  1025. ]);
  1026. }
  1027. }
  1028. DB::commit();
  1029. $this->dellimitingSendRequestBackgNeed($limit_key);
  1030. }catch (\Throwable $exception){
  1031. $this->dellimitingSendRequestBackgNeed($limit_key);
  1032. DB::rollBack();
  1033. return [false, $exception->getFile() . $exception->getFile() . $exception->getMessage()];
  1034. }
  1035. return [true, ''];
  1036. }
  1037. public function saveOutOrderRule($data,$user){
  1038. if(empty($data['out_data'])) return [false, '未获取到发货数据,操作失败!'];
  1039. Log::channel('sendData')->info('发货:提交包装单数据', ["param" => $data]);
  1040. //客户校验
  1041. $customer_code = array_column($data['out_data'],'customer_code');
  1042. $isAllSame = count(array_unique($customer_code)) === 1;
  1043. if(! $isAllSame) return [false, '发货出库,请选择同一个客户的订单!'];
  1044. $box_no = array_column($data['out_data'],'box_no');
  1045. if(empty($box_no)) return [false, '包装单信息不能为空'];
  1046. $box_arr = [];
  1047. foreach ($box_no as $value){
  1048. foreach ($value as $val){
  1049. $box_arr[] = $val;
  1050. }
  1051. }
  1052. $box_map = Box::where('del_time',0)
  1053. ->whereIn('order_no',$box_arr)
  1054. ->pluck('shipment_order_no', "order_no")
  1055. ->toArray();
  1056. foreach ($box_arr as $value){
  1057. $tmp = $box_map[$value] ?? "";
  1058. if($tmp != "") return [false, "包装单:" . $value . "已在发货单(" . $tmp .")中发出!"];
  1059. }
  1060. //用友发货数据查询
  1061. $cdlcode = array_column($data['out_data'],'cdlcode');
  1062. $service = new FyySqlServerService($user);
  1063. if($service->error) return [false, $service->error];
  1064. $out_data_map = $service->getDataFromDispatchListForCheck(['cdlcode' => $cdlcode]);
  1065. if(empty($out_data_map)) return [false, '未在用友发货数据中查找到相关数据!'];
  1066. //数据校验
  1067. foreach ($data['out_data'] as $value){
  1068. if(empty($value['cdlcode'])) return [false, '发货单号不能为空!'];
  1069. if(! isset($out_data_map[$value['cdlcode']])) return [false, '发货单号:' . $value['cdlcode'] . '不存在或已被删除!'];
  1070. $tmp = $out_data_map[$value['cdlcode']];
  1071. if(empty($value['customer_code'])) return [false, '客户编码不能为空!'];
  1072. if($tmp['customer_code'] != $value['customer_code']) return [false, '客户编码与用友数据中不一致!'];
  1073. if(empty($tmp['product'])) return [false, '发货单号:' . $value['cdlcode'] . '未查找到用友的发货产品数据!'];
  1074. $tmp_pro = [];
  1075. foreach ($tmp['product'] as $t){
  1076. $t_k = $t['cinvcode'] . $t['cfree1'];
  1077. $tmp_pro[$t_k] = bcsub($t['iquantity'] , $t['out_quantity'],3);//未发货数量
  1078. }
  1079. if(empty($value['product'])) return [false, '发货产品不能为空!'];
  1080. foreach ($value['product'] as $p){
  1081. if(empty($p['line'])) return [false, '存货行号不能为空!'];
  1082. if(empty($p['cinvcode'])) return [false, '存货编码不能为空!'];
  1083. if(empty($p['cfree1'])) return [false, '存货颜色不能为空!'];
  1084. $p_k = $p['cinvcode'] . $p['cfree1'];
  1085. if(! isset($tmp_pro[$p_k])) return [false, '发货单号:' . $value['cdlcode'] . '下无该发货产品:编码为' . $p['cinvcode'] . ',颜色为:' . $p['cfree1']];
  1086. if(empty($p['iquantity']) || $p['iquantity'] <= 0) return [false, '发货数量不能为空!'];
  1087. $last = $tmp_pro[$p_k];
  1088. if($p['iquantity'] > $last) return [false, '发货单号:' . $value['cdlcode'] . '下的发货产品:编码为' . $p['cinvcode'] . ',颜色为' . $p['cfree1'] . '的未发货数量为' . $last];
  1089. }
  1090. }
  1091. return [true, ''];
  1092. }
  1093. /**
  1094. * 根据发货单包装(恒成塑业不需要 如果有调用 直接返回)
  1095. * @param $data
  1096. * @return array
  1097. */
  1098. public function transportBoxIn($data)
  1099. {
  1100. return [true,''];
  1101. $param = $data['params'];
  1102. $shipment_order_no = $data['transport_no'];
  1103. $sale_order_nos = [];
  1104. $key_list = [];
  1105. foreach ($param as $v){
  1106. if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no'];
  1107. $key = $this->getKey($v);
  1108. $key_list[$key] = [
  1109. 'num' => $v['num'],
  1110. 'order_no' => $v['order_no'],
  1111. 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
  1112. ];
  1113. }
  1114. $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray();
  1115. $sale_order_product_key_list = [];
  1116. foreach ($sale_order_product_list as $v){
  1117. $key = $this->getKey($v);
  1118. $sale_order_product_key_list[$key] = $v['id'];
  1119. }
  1120. $param = [];
  1121. foreach ($key_list as $k=>$v){
  1122. if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!'];
  1123. $param[$v['order_no']][] = [
  1124. 'id' => -(intval($sale_order_product_key_list[$k])),
  1125. 'team_id' => $v['team_id'],
  1126. 'params' => [$v['num']]
  1127. ];
  1128. }
  1129. // $box_no = self::$box_hook->setOrderNo(); todo
  1130. foreach ($param as $k=>$v){
  1131. $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no');
  1132. $postParam = [
  1133. 'order_no' => $top_order_no,
  1134. 'param' => $v,
  1135. 'box_no' => $box_no,
  1136. 'transport_no' => $shipment_order_no,
  1137. ];
  1138. $this->boxIn($postParam);
  1139. }
  1140. return [true,''];
  1141. }
  1142. protected function getKey($data){
  1143. return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark'];
  1144. }
  1145. /**
  1146. * 包装详情1用于包装前
  1147. * @param $data
  1148. * @return array
  1149. */
  1150. public function boxDetail($data)
  1151. {
  1152. list($status, $data) = self::$box_hook->boxDetail($data);
  1153. if (!$status) return [false, $data];
  1154. $return = [];
  1155. foreach ($data as $v){
  1156. $key = $v['ext_1'] . $v['ext_3'];
  1157. if(! isset($return[$key])){
  1158. $return[$key] = $v;
  1159. }else{
  1160. $return[$key]['num'] += $v['num'];
  1161. }
  1162. }
  1163. $sale_orders_product_ids = array_unique(array_column($data,'top_id'));
  1164. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  1165. $key_list = [];
  1166. foreach ($list as $v){
  1167. $key_list[$v['id']] = $v;
  1168. }
  1169. foreach ($return as &$v){
  1170. $detail = $key_list[$v['top_id']];
  1171. $v['customer_no'] = $detail['customer_no'];
  1172. $v['customer_name'] = $detail['customer_name'];
  1173. }
  1174. return [true, array_values($return)];
  1175. }
  1176. public function boxProductList($data){
  1177. if(empty($data['id']) && empty($data['out_order_no'])) return [false,'请选择数据!'];
  1178. if(! empty($data['id'])){
  1179. $sale_order_ids = $data['id'];
  1180. }else{
  1181. $ids = SaleOrdersProduct::where('del_time',0)
  1182. ->where('out_order_no','Like','%'. $data['out_order_no'] . '%')
  1183. ->select('id')
  1184. ->get()->toArray();
  1185. $sale_order_ids = array_column($ids,'id');
  1186. if(empty($sale_order_ids)) return [false, '暂无数据!'];
  1187. }
  1188. $model = SaleOrdersProduct::where('del_time',0)
  1189. ->whereIn('id',$sale_order_ids)
  1190. ->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')
  1191. ->orderBy('id','desc')
  1192. ->get()->toArray();
  1193. //原先按照完工数量 未包装 = 完工数量 - 已包数量
  1194. $product_list = ordersProduct::where('del_time',0)
  1195. ->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")
  1196. ->get()->toArray();
  1197. $model_key_list = [];
  1198. foreach ($model as $v){
  1199. $model_key_list[$v['id']] = $v;
  1200. }
  1201. $model = [];
  1202. //存在生产订单,组织数据
  1203. if(! empty($product_list)){
  1204. $product_key_num_list = [];
  1205. foreach ($product_list as $v){
  1206. //下派工数量 就是 能包的数量 相等以后 踢掉数据
  1207. if($v['dispatch_complete_quantity'] == $v['box_num']) continue;
  1208. if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']])) {
  1209. $product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
  1210. 'product_num' => 0 ,
  1211. 'box_num' => 0 ,
  1212. ];
  1213. }
  1214. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
  1215. $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
  1216. $detail = $model_key_list[$v['sale_orders_product_id']];
  1217. $detail['box_type'] = 1;
  1218. $detail['id'] = $v['id'];
  1219. $detail['box_num'] = $v['box_num'];
  1220. $detail['dispatch_complete_quantity'] = $v['dispatch_complete_quantity'];
  1221. $detail['un_box_num'] = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
  1222. $detail['type'] = 1;
  1223. $detail['production_no'] = $v['production_no'];
  1224. $detail['production_quantity'] = $v['production_quantity'];
  1225. $model[] = $detail;
  1226. }
  1227. }
  1228. $return = [];
  1229. foreach ($model as $v){
  1230. if(!isset($v['box_type'])) {continue; //没有销售订单直接包了。
  1231. $product_num = 0;
  1232. $box_num = 0;
  1233. if(isset($product_key_num_list[$v['id']])){
  1234. foreach ($product_key_num_list[$v['id']] as $vv){
  1235. $product_num += $vv['product_num'];
  1236. $box_num += $vv['box_num'];
  1237. }
  1238. }
  1239. if(($v['order_quantity'] - $product_num) === 0) continue;
  1240. $product_key_list[$v['sale_orders_product_id']] = [
  1241. 'out_order_no' => $v['out_order_no'],
  1242. 'order_no' => $v['order_no'],
  1243. 'production_no' => "",
  1244. 'production_time' => '未下生产',
  1245. 'customer_no' => $v['customer_no'],
  1246. 'customer_name' => $v['customer_name'],
  1247. 'product_no' => $v['product_no'],
  1248. 'product_title' => $v['product_title'],
  1249. 'product_size' => $v['product_size'],
  1250. 'id' => -$v['sale_orders_product_id'],
  1251. 'technology_material' => $v['technology_material'],
  1252. 'technology_name' => $v['technology_name'],
  1253. 'wood_name' => $v['wood_name'],
  1254. 'process_mark' => $v['process_mark'],
  1255. 'type' => '2',
  1256. 'is_box_num' => $v['box_num'] - $box_num,
  1257. 'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
  1258. 'sale_num' => $v['order_quantity'],
  1259. ];
  1260. }else{
  1261. $return[] = [
  1262. 'id' => $v['id'],
  1263. 'technology_material' => $v['technology_material'],
  1264. 'technology_name' => $v['technology_name'],
  1265. 'wood_name' => $v['wood_name'],
  1266. 'order_no' => $v['order_no'],
  1267. 'production_no' => $v['production_no'],
  1268. 'process_mark' => $v['process_mark'],
  1269. 'out_order_no' => $v['out_order_no'],
  1270. 'production_time' => date('Y-m-d',$v['crt_time']),
  1271. 'customer_no' => $v['customer_no'],
  1272. 'customer_name' => $v['customer_name'],
  1273. 'product_no' => $v['product_no'],
  1274. 'product_title' => $v['product_title'],
  1275. 'product_size' => $v['product_size'],
  1276. 'type' => '1',
  1277. 'is_box_num' => $v['box_num'],
  1278. 'un_box_num' => $v['un_box_num'],
  1279. 'sale_num' => $v['order_quantity'],
  1280. 'production_num' => $v['production_quantity'],
  1281. 'dispatch_complete_quantity' => $v['dispatch_complete_quantity']
  1282. ];
  1283. }
  1284. }
  1285. return [true,$return];
  1286. }
  1287. /**
  1288. * 包装详情2用于包装后
  1289. * @param $data
  1290. * @return array
  1291. */
  1292. public function boxOrderDetail($data)
  1293. {
  1294. list($status, $data) = self::$box_hook->boxDetail($data);
  1295. $sale_orders_product_ids = [];
  1296. foreach ($data as $v){
  1297. $sale_orders_product_ids[] = $v['top_id'];
  1298. }
  1299. $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
  1300. $key_list = [];
  1301. foreach ($list as $v){
  1302. $key_list[$v['id']] = $v;
  1303. }
  1304. foreach ($data as &$v){
  1305. $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
  1306. $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
  1307. $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
  1308. $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
  1309. $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
  1310. }
  1311. if (!$status) return [false, $data];
  1312. return [true, $data];
  1313. }
  1314. /**
  1315. * 通过销售单号获取包装单
  1316. * @param $data
  1317. * @return array
  1318. */
  1319. public function boxOrderDetailByTop($data){
  1320. $list = new Box();
  1321. if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  1322. if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  1323. // if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
  1324. // if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
  1325. $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
  1326. return [true,$list];
  1327. }
  1328. /**
  1329. * 通过包装单获取包装详情
  1330. * @param $data
  1331. * @return array
  1332. */
  1333. public function boxOrderDetailByOrderNo($data){
  1334. $order_nos = $data['order_nos'];
  1335. $order_nos = array_unique($order_nos);
  1336. $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();
  1337. $detail_list = [];
  1338. foreach ($list as $v){
  1339. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1340. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  1341. }
  1342. $list = $detail_list;
  1343. $ids = [];
  1344. foreach ($list as $v){
  1345. $ids[] = $v['top_id'];
  1346. }
  1347. $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
  1348. $sale_orders_key_product = [];
  1349. foreach ($sale_orders_product as $v){
  1350. $sale_orders_key_product[$v['id']] = $v;
  1351. }
  1352. $team_key_list = Team::pluck('title','id')->toArray();
  1353. foreach ($list as &$v){
  1354. $detail = $sale_orders_key_product[$v['top_id']];
  1355. $v['customer_name'] = $detail['customer_name'];
  1356. $v['table_body_mark'] = $detail['table_body_mark'];
  1357. $v['ext_4'] = $detail['product_title'];
  1358. $v['ext_5'] = $detail['product_size'];
  1359. $v['customer_name'] = $detail['customer_name'];
  1360. $v['technology_material'] = $detail['technology_material'];
  1361. $v['transport_no'] = $v['shipment_order_no'];
  1362. $v['wood_name'] = $detail['wood_name'];
  1363. $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
  1364. }
  1365. return [true,$list];
  1366. }
  1367. public function boxOrderGroup($data){
  1368. $list = new Box();
  1369. if(empty($data['top_order_no']) && empty($data['transport_no'])) return [false,'请输入单号!'];
  1370. if(! empty($data['top_order_no'])) $list = $list->whereIn('top_order_no',$data['top_order_no']);
  1371. if(! empty($data['transport_no'])) {
  1372. if(is_array($data['transport_no'])){
  1373. $list = $list->whereIn('shipment_order_no',$data['transport_no']);
  1374. }else{
  1375. $list = $list->where('shipment_order_no','Like','%'. $data['transport_no'] . '%');
  1376. }
  1377. }
  1378. $list = $list->select('top_order_no','order_no as box_no','crt_time','shipment_order_no','out_order_no')->where('del_time',0)
  1379. ->get()->toArray();
  1380. foreach ($list as $key => $value){
  1381. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  1382. $tmp = $model->where('del_time',0)
  1383. ->where('order_no',$value['box_no'])
  1384. ->where('out_order_no',$value['out_order_no'])
  1385. ->select('order_no','out_order_no','orders_product_id')
  1386. ->first();
  1387. $production_no = "";
  1388. if(! empty($tmp)){
  1389. $production_no = OrdersProduct::where('id',$tmp->orders_product_id)->select('production_no')->pluck('production_no')->toArray();
  1390. $production_no = $production_no[0];
  1391. }
  1392. $list[$key]['production_no'] = $production_no;
  1393. $list[$key]['crt_time'] = date('Y-m-d H:i:s',$value['crt_time']);
  1394. }
  1395. return [true,$list];
  1396. }
  1397. public function boxOrderGroupGwp($data){
  1398. $out_order_no = $data['out_order_no'] ?? "";
  1399. $model = Box::where('del_time',0)
  1400. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  1401. return $query->where('out_order_no','Like','%'. $out_order_no . '%');
  1402. })
  1403. ->select('out_order_no','ext_1','top_order_no','order_no','crt_time','shipment_order_no',DB::raw("count(id) as package_num"))
  1404. ->groupBy('out_order_no');
  1405. $list = $this->limit($model,'',$data);
  1406. $list = $this->fillData($list);
  1407. return [true, $list];
  1408. }
  1409. public function fillData($data){
  1410. if(empty($data['data'])) return $data;
  1411. foreach ($data['data'] as $key => $value){
  1412. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  1413. $tmp = $model->where('del_time',0)
  1414. ->where('out_order_no',$value['out_order_no'])
  1415. ->select('num')
  1416. ->get()->toArray();
  1417. $data['data'][$key]['num'] = $this->getTotal($tmp, 'num');
  1418. }
  1419. return $data;
  1420. }
  1421. public function boxOrderGroupGwpDetail($data){
  1422. if(empty($data['top_order_no']) || empty($data['out_order_no'])) return [false, '请选择数据!'];
  1423. $top_order_no = $data['top_order_no'];
  1424. $model = new BoxDetail(['channel'=> $top_order_no]);
  1425. $detail_list = $model->where('del_time',0)
  1426. ->where('out_order_no',$data['out_order_no'])
  1427. ->get()->toArray();
  1428. $map = Box::whereIn('order_no',array_unique(array_column($detail_list,'order_no')))
  1429. ->pluck('ext_1','order_no')
  1430. ->toArray();
  1431. $return = [];
  1432. foreach ($detail_list as $value){
  1433. //销售订单号 包装单号 产品编码 颜色
  1434. $key = $value['out_order_no'] . $value['order_no'] . $value['ext_1'] . $value['ext_3'];
  1435. if(isset($return[$key])){
  1436. $return[$key]['num'] += $value['num'];
  1437. }else{
  1438. $return[$key] = [
  1439. 'box_no' => $value['order_no'],
  1440. 'out_order_no' => $value['out_order_no'],
  1441. 'ext_8' => $value['ext_8'],
  1442. 'ext_3' => $value['ext_3'],
  1443. 'num' => $value['num'],
  1444. 'crt_time' => date("Y-m-d",$value['crt_time']),
  1445. 'top_order_no' => $value['top_order_no'],
  1446. 'customer_name' => $map[$value['order_no']] ?? "",
  1447. ];
  1448. }
  1449. }
  1450. return [true, array_values($return)];
  1451. }
  1452. public function transportNo(){
  1453. $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
  1454. return [true,$list];
  1455. }
  1456. public function delBoxDetail($data){
  1457. if(empty($data['order_nos'])) return [false, '请选择包装单!'];
  1458. $order_nos = $data['order_nos'];
  1459. try {
  1460. DB::beginTransaction();
  1461. //更新销售订单 生产订单数据
  1462. $orders_product_id_map = [];
  1463. foreach ($order_nos as $v){
  1464. $list = self::$box_hook->delBox($v);
  1465. if(empty($list)) continue;
  1466. foreach ($list as $vv){
  1467. SaleOrdersProduct::where('id',$vv['top_id'])->update([
  1468. 'box_num' => DB::raw('box_num - '.$vv['num']),
  1469. 'finished_num' => DB::raw('finished_num - '.$vv['num'])
  1470. ]);
  1471. OrdersProduct::where('id',$vv['orders_product_id'])->update([
  1472. 'box_num' => DB::raw('box_num - '.$vv['num']),
  1473. 'finished_num' => DB::raw('finished_num - '.$vv['num'])
  1474. ]);
  1475. if(isset($orders_product_id_map[$vv['orders_product_id']])){
  1476. $num = bcadd($orders_product_id_map[$vv['orders_product_id']], $vv['num'],3);
  1477. $orders_product_id_map[$vv['orders_product_id']] = $num;
  1478. }else{
  1479. $orders_product_id_map[$vv['orders_product_id']] = $vv['num'];
  1480. }
  1481. }
  1482. }
  1483. //更新完工数据 和 底表数据
  1484. list($status,$msg) = $this->updateFinishDel($orders_product_id_map);
  1485. if(! $status) return [false, $msg];
  1486. DB::commit();
  1487. }catch (\Throwable $exception){
  1488. DB::rollBack();
  1489. return [false, $exception->getMessage()];
  1490. }
  1491. return [true, ''];
  1492. }
  1493. public function updateFinishDel($orders_product_id_map){
  1494. if(empty($orders_product_id_map)) return [true,''];
  1495. $production_list = array_keys($orders_product_id_map);
  1496. $dispatch = $this->getDispatch($production_list, $orders_product_id_map);
  1497. if(empty($dispatch)) return [true,''];
  1498. try {
  1499. DB::beginTransaction();
  1500. //根据派工数据回写
  1501. foreach ($dispatch as $value){
  1502. $finished_num = bcsub($value['finished_num'], $value['out_number'],3);
  1503. DispatchSub::where('id',$value['id'])->update([
  1504. 'finished_num' => $finished_num
  1505. ]);
  1506. $quantity = $value['out_number'] * 1000;
  1507. //工序表
  1508. $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
  1509. $process_model->where('order_product_id',$value['order_product_id'])
  1510. ->where('process_id',$value['process_id'])
  1511. ->where('dispatch_no',$value['dispatch_no'])
  1512. ->where('status', 2)
  1513. ->take($quantity)
  1514. ->update([
  1515. 'finished_time' => 0,
  1516. 'status' => 1,
  1517. 'finished_id' => 0,
  1518. 'team_id' => 0,
  1519. 'equipment_id' => 0,
  1520. ]);
  1521. }
  1522. DB::commit();
  1523. }catch (\Throwable $exception){
  1524. DB::rollBack();
  1525. return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
  1526. }
  1527. return [true, ''];
  1528. }
  1529. public function getDispatch($order_product_id,$map){
  1530. $order = OrdersProduct::where('del_time',0)
  1531. ->where('id', $order_product_id)
  1532. ->select('id', 'process_id')
  1533. ->get()->toArray();
  1534. $order_map = [];
  1535. foreach ($order as $value){
  1536. $order_map[$value['id']] = explode(',', $value['process_id']);
  1537. }
  1538. //组织派工数据
  1539. $dispatch = DispatchSub::where('del_time',0)
  1540. ->whereIn('order_product_id', $order_product_id)
  1541. ->where('finished_num','>',0)
  1542. ->get()->toArray();
  1543. if(empty($dispatch)) return [];
  1544. $dispatch_map = [];
  1545. foreach ($dispatch as $value){
  1546. $key = $value['order_product_id'] . '|' . $value['crt_time']; //同一次下的派工单
  1547. $dispatch_map[$key][] = $value;
  1548. }
  1549. $result = []; $tmp = [];
  1550. foreach ($dispatch_map as $key => $value){
  1551. foreach ($value as $v){
  1552. $process = $order_map[$v['order_product_id']] ?? [];
  1553. $num = $map[$v['order_product_id']];
  1554. if(in_array($v['process_id'], $process)){
  1555. $str = $v['order_product_id'] . $v['process_id'];
  1556. $tmp_v = $tmp[$str] ?? 0;
  1557. $num = bcsub($num, $tmp_v,3);
  1558. if($num <= 0) continue;
  1559. if($v['finished_num'] >= $num){
  1560. $v['out_number'] = $num;
  1561. $result[] = $v;
  1562. }else{
  1563. $v['out_number'] = $v['finished_num'];
  1564. $result[] = $v;
  1565. }
  1566. if(isset($tmp[$str])){
  1567. $num = bcadd($tmp[$str], $v['out_number'],3);
  1568. $tmp[$str] = $num;
  1569. }else{
  1570. $tmp[$str] = $v['out_number'];
  1571. }
  1572. }
  1573. }
  1574. }unset($tmp);
  1575. return $result;
  1576. }
  1577. public function boxAdd($data)
  1578. {
  1579. list($status,$msg) = $this->boxAddRule($data);
  1580. if(! $status) return [false,$msg];
  1581. try{
  1582. DB::beginTransaction();
  1583. $box = new Box();
  1584. $box->order_no = $msg['order_no'];
  1585. $box->save();
  1586. DB::table('box_detail')->insert($msg['detail']);
  1587. DB::commit();
  1588. return [true,''];
  1589. }catch (\Exception $e){
  1590. DB::rollBack();
  1591. return [false,$e->getLine().':'.$e->getMessage()];
  1592. }
  1593. }
  1594. public function boxAddRule($data)
  1595. {
  1596. if(empty($data['param'])) return [false,'请选择包装数据'];
  1597. // $order_no = (new BoxHookService())->setOrderNo();
  1598. $order_no = "";
  1599. if(empty($order_no)) return [false,'包装单号不能为空'];
  1600. $detail = [];
  1601. foreach ($data['param'] as $value) {
  1602. $key = $this->lock_key.'_'.$value['idlsid'];
  1603. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1604. if(! $status) return [false,$msg];
  1605. // if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
  1606. if(empty($value['team_id'])) return [false,'班组必须选择'];
  1607. $tmp = $value;
  1608. $tmp['iquantity'] = $value['submit_quantity'];
  1609. $detail[] = [
  1610. 'order_no' => $order_no,
  1611. 'out_order_no' => $value['csocode']??'',
  1612. 'crt_time' => time(),
  1613. 'upd_time' => time(),
  1614. 'type' => 1,
  1615. 'ext_1' => $value['cinvcode'],
  1616. 'ext_2' => $value['technology_material'],
  1617. 'ext_3' => $value['cfree1'] ?? "",
  1618. 'ext_4' => $value['cfree2'] ?? "",
  1619. 'ext_5' => $value['process_mark'],
  1620. 'ext_6' => $value['customer_name'] ?? "",
  1621. 'ext_7' => $value['cuscode'] ?? "",
  1622. 'ext_8' => $value['product_title'],
  1623. 'ext_9' => $value['product_size'],
  1624. 'ext_10' => $value['table_header_mark'],
  1625. 'ext_11' => $value['table_body_mark'],
  1626. 'top_id' => $value['idlsid'],
  1627. 'state' => 0,
  1628. 'num' => $value['submit_quantity'],
  1629. 'box_type' => 2,
  1630. 'team_id' => $value['team_id'],
  1631. 'shipment_order_no' => $value['cdlcode'],
  1632. 'post' => json_encode($tmp),
  1633. ];
  1634. }
  1635. return [true,['order_no' => $order_no, 'detail' => $detail]];
  1636. }
  1637. public function boxFhDetail($data){
  1638. if(empty($data['idlsid'])) return [false,'请选择数据'];
  1639. $result = DB::table('box_detail')->where('del_time',0)
  1640. ->where('top_id',$data['idlsid'])
  1641. ->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')
  1642. ->get()->toArray();
  1643. if(! empty($result)){
  1644. foreach ($result as $key => $value){
  1645. $result[$key] = (array)$value;
  1646. }
  1647. }
  1648. return [true,$result];
  1649. }
  1650. public function boxFhDel($data){
  1651. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  1652. $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
  1653. DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
  1654. 'del_time' => time()
  1655. ]);
  1656. return [true,''];
  1657. }
  1658. public function transportDetail($data){
  1659. if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
  1660. $order_nos = $data['order_nos'];
  1661. $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();
  1662. foreach ($list as &$v){
  1663. $v['product_no'] = $v['ext_1'];
  1664. $v['technology_material'] = $v['ext_2'];
  1665. $v['technology_name'] = $v['ext_3'];
  1666. $v['wood_name'] = $v['ext_4'];
  1667. $v['process_mark'] = $v['ext_5'];
  1668. $v['customer_no'] = $v['ext_6'];
  1669. $v['customer_name'] = $v['ext_7'];
  1670. $v['product_title'] = $v['ext_8'];
  1671. $v['product_size'] = $v['ext_9'];
  1672. $v['post'] = json_decode($v['post'],true);
  1673. }
  1674. return [200,$list];
  1675. }
  1676. public function boxFhBzDetail($data){
  1677. if(empty($data['order_no'])) return [false,'请选择包装单数据'];
  1678. $result = DB::table('box_detail')->where('del_time',0)
  1679. ->whereIn('order_no',$data['order_no'])
  1680. ->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')
  1681. ->get()->toArray();
  1682. if(! empty($result)){
  1683. foreach ($result as $key => $value){
  1684. $result[$key] = (array)$value;
  1685. }
  1686. $map = Team::where('id',array_unique(array_column($result,'team_id')))->pluck('title','id')->toArray();
  1687. foreach ($result as $key => $value){
  1688. $result[$key]['team_name'] = $map[$value['team_id']] ?? '';
  1689. }
  1690. }
  1691. return [true,$result];
  1692. }
  1693. public function boxFhBzList($data){
  1694. if(empty($data['shipment_order_no'])) return [false, '请输入发货单号'];
  1695. $model = DB::table('box_detail')->where('del_time',0)
  1696. ->where('shipment_order_no','LIKE', '%'.$data['shipment_order_no'].'%')
  1697. ->select('order_no','crt_time','shipment_order_no');
  1698. $return = [];
  1699. $result = $model->get()->toArray();
  1700. if(! empty($result)){
  1701. foreach ($result as $value){
  1702. $return[$value->order_no] = [
  1703. 'order_no' => $value->order_no,
  1704. 'shipment_order_no' => $value->shipment_order_no,
  1705. 'crt_time' => date("Y-m-d H:i:s",$value->crt_time)
  1706. ];
  1707. }
  1708. }
  1709. return [true,array_values($return)];
  1710. }
  1711. public function boxOrderDetailByOrderNoNew($data){
  1712. if(empty($data['order_nos'])) return [false, '包装单号不能为空'];
  1713. $order_nos = $data['order_nos'];
  1714. $order_nos = array_unique($order_nos);
  1715. Log::channel('sendData')->info('发货:扫码包装单数据', ["param" => $order_nos]);
  1716. $list = Box::where('del_time',0)
  1717. ->whereIn('order_no',$order_nos)
  1718. ->where('shipment_order_no','')
  1719. ->select('order_no','out_order_no','top_order_no','shipment_order_no as transport_no','upd_time')
  1720. ->get()->toArray();
  1721. $detail_list = [];
  1722. foreach ($list as $v){
  1723. // if(! empty($v['transport_no'])) return [false, "包装单号:" . $v['order_no'] . "已在发货单(" . $v['transport_no'] .")中发出!"];
  1724. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1725. $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
  1726. }
  1727. $return = [];
  1728. foreach ($detail_list as $value){
  1729. //销售订单号 产品编码 颜色
  1730. $return[] = [
  1731. 'ext_1' => $value['ext_1'],
  1732. 'ext_3' => $value['ext_3'],
  1733. 'ext_8' => $value['ext_8'],
  1734. 'num' => $value['num'],
  1735. 'out_order_no' => $value['out_order_no'],
  1736. 'box_no' => $value['order_no']
  1737. ];
  1738. }
  1739. return [true, array_values($return)];
  1740. }
  1741. public function boxList($data,$user){
  1742. $model = Box::where('del_time',0)
  1743. ->select('id','order_no','out_order_no','top_order_no','crt_time','status','shipment_order_no')
  1744. ->orderBy('id','desc');
  1745. if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
  1746. if(! empty($data['shipment_order_no'])) $model->where('shipment_order_no', 'LIKE', '%'.$data['shipment_order_no'].'%');
  1747. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  1748. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  1749. if(isset($data['status'])) $model->where('status',$data['status']);
  1750. $list = $this->limit($model,'',$data);
  1751. $list = $this->fillBoxListData($list, $data,$user);
  1752. return [true, $list];
  1753. }
  1754. public function fillBoxListData($data, $erg,$user){
  1755. if(empty($data['data'])) return $data;
  1756. $product = $material = [];
  1757. if(! empty($erg['box'])){
  1758. //获取包装单里的产品
  1759. $id = array_column($data['data'], 'id');
  1760. list($product,$product_no) = $this->getBoxDetail($id);
  1761. if(! empty($erg['box_material'])){
  1762. //产品的包装材料
  1763. $service = new FyyOrderService();
  1764. list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
  1765. if($status) $material = $msg;
  1766. }
  1767. }
  1768. foreach ($data['data'] as $key => $value){
  1769. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1770. $data['data'][$key]['status_title'] = Box::$status_name[$value['status']] ?? "";
  1771. $p = $product[$value['id']] ?? [];
  1772. if(! empty($p)){
  1773. foreach ($p as $k => $v){
  1774. $p[$k]['box_material'] = $material[$v['product_no']] ?? [];
  1775. }
  1776. }
  1777. $data['data'][$key]['product'] = $p;
  1778. }
  1779. return $data;
  1780. }
  1781. public function getBoxDetail($id = []){
  1782. $return = $product_no= [];
  1783. if(empty($id)) return [$return,$product_no];
  1784. $box = Box::where('del_time',0)
  1785. ->whereIn('id', $id)
  1786. ->select('id', 'order_no','top_order_no')
  1787. ->get()->toArray();
  1788. if (empty($box)) return [$return,$product_no];
  1789. $map = array_column($box,'id','order_no');
  1790. $result = [];
  1791. foreach ($box as $v){
  1792. $model = new BoxDetail(['channel'=>$v['top_order_no']]);
  1793. $result = array_merge($result,$model->where('del_time',0)
  1794. ->where('order_no',$v['order_no'])
  1795. ->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());
  1796. }
  1797. foreach ($result as $value){
  1798. $value['id'] = $map[$value['order_no']] ?? 0;
  1799. $value['product_unit'] = "吨";
  1800. $return[$value['id']][] = $value;
  1801. if(! in_array($value['product_no'], $product_no)) $product_no[] = $value['product_no'];
  1802. }
  1803. return [$return, $product_no];
  1804. }
  1805. public function searchDetail($data){
  1806. $out_order_no = $data['out_order_no'] ?? '';
  1807. $box = Box::where('del_time',0)
  1808. ->where('out_order_no', 'LIKE', '%'. $out_order_no .'%')
  1809. ->select('top_order_no')
  1810. ->get()->toArray();
  1811. if(empty($box)) return [];
  1812. $order_no = [];
  1813. foreach ($box as $value){
  1814. $model = new BoxDetail(['channel'=> $value['top_order_no']]);
  1815. $result = $model->where('del_time',0)
  1816. ->where('order_no',$box['order_no'])
  1817. ->select('order_no')
  1818. ->get()->toArray();
  1819. $detail_list = array_merge($order_no, array_column($result, 'order_no'));
  1820. }
  1821. $result = BoxDetail::where('del_time',0)
  1822. ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
  1823. return $query->where('out_order_no', 'LIKE', '%'. $out_order_no .'%');
  1824. })
  1825. ->when(! empty($shipment_order_no), function ($query) use ($shipment_order_no) {
  1826. return $query->where('shipment_order_no', 'LIKE', '%'. $shipment_order_no .'%');
  1827. })
  1828. ->select('order_no')
  1829. ->get()->toArray();
  1830. return array_unique(array_column($result,'order_no'));
  1831. }
  1832. public function boxFhDetailNew($data){
  1833. if(empty($data['id'])) return [false,'请选择数据'];
  1834. $box = Box::where('id', $data['id'])->where('del_time',0)->first();
  1835. if(empty($box)) return [false, '包装单不存在或已被删除'];
  1836. $box = $box->toArray();
  1837. $model = new BoxDetail(['channel'=>$box['top_order_no']]);
  1838. $result = $model->where('del_time',0)
  1839. ->where('order_no',$box['order_no'])
  1840. ->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();
  1841. foreach ($result as $key => $value){
  1842. $result[$key]['product_unit'] = "吨";
  1843. }
  1844. return [true,$result];
  1845. }
  1846. }