BoxService.php 84 KB

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