BoxService.php 85 KB

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