BoxService.php 83 KB

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