| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091 | 
							- <?php
 
- namespace App\Service\Box;
 
- use App\Jobs\BoxAddJob;
 
- use App\Model\ApplyOrder;
 
- use App\Model\ApplyOrderForYy;
 
- use App\Model\Box;
 
- use App\Model\BoxDetail;
 
- use App\Model\BoxWithDispatch;
 
- use App\Model\DispatchSub;
 
- use App\Model\EmployeeTeamPermission;
 
- use App\Model\Header_ext;
 
- use App\Model\OrdersProduct;
 
- use App\Model\OrdersProductProcess;
 
- use App\Model\Process;
 
- use App\Model\ReportMessage;
 
- use App\Model\ReportWorking;
 
- use App\Model\ReportWorkingDetail;
 
- use App\Model\SaleOrdersProduct;
 
- use App\Model\ScrappCount;
 
- use App\Model\Setting;
 
- use App\Model\Team;
 
- use App\Service\ApplyOrderService;
 
- use App\Service\EmployeeService;
 
- use App\Service\FinishedOrderService;
 
- use App\Service\FyyOrderService;
 
- use App\Service\FyySqlServerService;
 
- use App\Service\Service;
 
- use Illuminate\Support\Facades\DB;
 
- use Illuminate\Support\Facades\Log;
 
- /**
 
-  * 包装相关
 
-  * @package App\Models
 
-  */
 
- class BoxService extends Service
 
- {
 
-     protected static $instance;
 
-     protected static $box_header;
 
-     protected static $box_detail_header;
 
-     protected static $box_hook;
 
-     public $lock_key = 'hc_box_new_add';
 
-     public function __construct()
 
-     {
 
-         self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
 
-         self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
 
-         self::$box_hook = BoxHookService::getInstance();
 
-     }
 
-     public function boxIn($data,$user)
 
-     {
 
-         list($status, $msg) = $this->boxInRule($data, $user);
 
-         if(! $status) return [false, $msg];
 
-         $data['zt'] = Request()->header('zt');
 
- //list($status, $msg) = $this->boxInSettle($data,$user);dd($status,$msg);
 
-         //限制频率
 
-         $limit_key = $this->lock_key;
 
-         list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
 
-         if(! $status) return [false, '请稍后操作'];
 
-         $job = BoxAddJob::dispatch($data,$user)->onQueue(BoxAddJob::job);
 
-         if(! $job) {
 
-             $this->dellimitingSendRequestBackgNeed($limit_key);
 
-             return [false,'任务未进入后台队列,请联系管理员'];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function boxInRule($data,$user)
 
-     {
 
-         try{
 
-             $ids = [];
 
-             $sale_ids = [];
 
-             $data = $data['param'];
 
-             $key_list = [];
 
-             foreach ($data as $v) {
 
-                 if($v['id'] < 0)  $sale_ids[] = -$v['id'];
 
-                 else $ids[] = $v['id'];
 
-                 if($v['id'] < 0) $v['id'] = -$v['id'];
 
-                 $total = 0;
 
-                 foreach ($v['params'] as $vv) {
 
-                     $total += $vv;
 
-                 }
 
-                 $key_list[$v['id']] = [
 
-                     'detail' => $v['params'],
 
-                     'total' => $total,
 
-                     'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
 
-                 ];
 
-             }
 
-             //数据获取
 
-             $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
 
-             //校验
 
-             $flag = "";
 
-             foreach ($product_list as $value){
 
-                 $key = $value['out_order_no'] . $value['product_no'] . $value['technology_name'];
 
-                 if(! empty($flag)){
 
-                     if($flag != $key) return [false , "一次包装只允许包一种产品的一种颜色!"];
 
-                 }else{
 
-                     $flag = $key;
 
-                 }
 
-                 $num_list = $key_list[$value['id']];
 
-                 $total = $num_list['total'];
 
-                 $un_box_num = bcsub($value['dispatch_complete_quantity'] , $value['box_num'],3);
 
-                 if ($total > $un_box_num) return [false, $value['product_title'] . ' ' . $value['technology_name'] . ' 可包装的数量不足'];
 
-             }
 
-             return [true, ''];
 
-         }catch (\Throwable $e){
 
-             return [false,$e->getFile() . '|' . $e->getLine(). '|' .$e->getMessage()];
 
-         }
 
-     }
 
-     public function boxInSettle($data, $user){
 
-         //设置数据库连接
 
-         $this->setConnect($data);
 
-         $zt = $data['zt'] ?? "";
 
-         $ids = [];
 
-         $sale_ids = [];
 
-         $top_order_no = $data['order_no'];
 
-         $box_no = isset($data['box_no']) ? $data['box_no'] : '';
 
-         $transport_no = isset($data['transport_no']) ? $data['transport_no'] : '';
 
-         $data = $data['param'];
 
-         $key_list = [];
 
-         foreach ($data as $v) {
 
-             if($v['id'] < 0)  $sale_ids[] = -$v['id'];
 
-             else $ids[] = $v['id'];
 
-             if($v['id'] < 0) $v['id'] = -$v['id'];
 
-             $total = 0;
 
-             foreach ($v['params'] as $vv) {
 
-                 $total += $vv;
 
-             }
 
-             $key_list[$v['id']] = [
 
-                 'detail' => $v['params'],
 
-                 'total' => $total,
 
-                 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
 
-             ];
 
-         }
 
-         $insert = [];
 
-         //锁
 
-         $limit_key = $this->lock_key;
 
-         try{
 
-             DB::beginTransaction();
 
-             //数据获取
 
-             $product_list = OrdersProduct::whereIn('id', $ids)->get()->toArray();
 
-             $sale_product_list = SaleOrdersProduct::whereIn('id', $sale_ids)->get()->toArray();
 
-             //生产订单包装----------------
 
-             $box_insert = [];
 
-             foreach ($product_list as $v) {
 
-                 $num_list = $key_list[$v['id']];
 
-                 $total = $num_list['total'];
 
-                 $detail = $num_list['detail'];
 
-                 $team_id = $num_list['team_id'];
 
-                 $ext_1 = $v['product_no'];//产品编号
 
-                 $ext_2 = $v['technology_material']; //工艺材质 废弃
 
-                 $ext_3 = $v['technology_name'];//工艺名称 改为颜色
 
-                 $ext_4 = $v['wood_name'];//木皮 废弃
 
-                 $ext_5 = $v['process_mark'];//工艺备注 废弃
 
-                 $ext_8 = $v['product_title'];//产品名称
 
-                 $out_order_no = $insert['out_order_no'] = $v['out_order_no'];
 
-                 $top_id = $v['sale_orders_product_id'];
 
-                 foreach ($detail as $vv){
 
-                     $box_insert[] = [
 
-                         'out_order_no' => $out_order_no,
 
-                         'top_id' => $top_id,
 
-                         'orders_product_id' => $v['id'],
 
-                         'ext_1' => $ext_1,//产品编号
 
-                         'ext_2' => $ext_2,
 
-                         'ext_3' => $ext_3,//颜色
 
-                         'ext_4' => $ext_4,
 
-                         'ext_5' => $ext_5,
 
-                         'ext_8' => $ext_8,//产品名称
 
-                         'num' => $vv,
 
-                         'price' => $v['price'],
 
-                         'box_type' => 1,
 
-                         'team_id' => $team_id,
 
-                         'shipment_order_no' => $transport_no,
 
-                     ];
 
-                 }
 
-                 //修改销售订单  生产订单 包装数量
 
-                 SaleOrdersProduct::where('id',$v['sale_orders_product_id'])->update([
 
-                     'box_num' => DB::raw('box_num + '.$total),
 
-                 ]);
 
-                 OrdersProduct::where('id',$v['id'])->update([
 
-                     'box_num' => DB::raw('box_num + '.$total),
 
-                 ]);
 
-             }
 
-             //客户名称
 
-             $ext_1 = "";
 
-             if(empty($ext_1) && isset($sale_product_list[0]['customer_name'])) $ext_1 = $sale_product_list[0]['customer_name'];
 
-             if(empty($ext_1) && isset($product_list[0]['customer_name'])) $ext_1 = $product_list[0]['customer_name'];
 
-             $insert['ext_1'] = $ext_1;
 
-             $insert['detail'] = $box_insert;
 
-             $insert['top_order_no'] = $top_order_no;
 
-             $insert['order_no'] = $box_no;
 
-             $insert['shipment_order_no'] = $transport_no;
 
-             //生成包装单
 
-             list($status,$msg) = self::$box_hook->boxInsert($insert,$user);
 
-             if(! $status) {
 
-                 $this->dellimitingSendRequestBackgNeed($limit_key);
 
-                 DB::rollBack();
 
-                 return [false, $msg];
 
-             }
 
-             //反写数量
 
-             $em = new EmployeeService();
 
-             $auto = $em->is_auto($user, "bzwg_auto");
 
-             if($auto){
 
-                 list($status, $msg1) = $this->updateFinishNew($product_list,$key_list,$user);
 
-                 if(! $status) {
 
-                     $this->dellimitingSendRequestBackgNeed($limit_key);
 
-                     DB::rollBack();
 
-                     return [false, $msg1];
 
-                 }
 
-             }
 
-             //包装单
 
-             $package_data = $msg->toArray();
 
-             $dispatch_list = $msg1 ?? [];
 
-             if(! empty($dispatch_list)){
 
-                 foreach ($dispatch_list as $key => $value){
 
-                     $dispatch_list[$key]['dispatch_id'] = $value['id'];
 
-                     unset($dispatch_list[$key]['id']);
 
-                     $dispatch_list[$key]['box_id'] = $package_data['id'];
 
-                 }
 
-                 BoxWithDispatch::insert($dispatch_list);
 
-             }
 
-             //包装单产品
 
-             $boxDetail = new BoxDetail(['channel'=>$package_data['top_order_no']]);
 
-             $boxDetail = $boxDetail->where('del_time',0)
 
-                 ->where('order_no',$package_data['order_no'])
 
-                 ->select('ext_1 as product_no', 'ext_3 as technology_name', 'ext_8 as product_title', 'ext_9 as product_size', 'num as quantity')
 
-                 ->get()->toArray();
 
-             //生成半成品出库申请单
 
-             list($status, $msg1) = $this->createbcpllSQ($package_data,$boxDetail, $user);
 
-             if(! $status) {
 
-                 $this->dellimitingSendRequestBackgNeed($limit_key);
 
-                 DB::rollBack();
 
-                 return [false, $msg1];
 
-             }
 
-             //生成包装领料申请单
 
-             list($status, $msg1) = $this->createllSQ($package_data,$boxDetail, $user);
 
-             if(! $status) {
 
-                 $this->dellimitingSendRequestBackgNeed($limit_key);
 
-                 DB::rollBack();
 
-                 return [false, $msg1];
 
-             }
 
-             //生成包装入库申请单
 
-             list($status, $msg) = $this->createBZSQ($package_data,$boxDetail, $user);
 
-             if(! $status) {
 
-                 $this->dellimitingSendRequestBackgNeed($limit_key);
 
-                 DB::rollBack();
 
-                 return [false, $msg];
 
-             }
 
-             //是否自动审核  包装入库申请单 如果是测试的不写用友
 
-             $auto = $em->is_auto($user,"bzsq_auto");
 
-             if(! empty($package_data) && $auto && $zt != 'cs') {
 
-                 //用友 ------产成品入库
 
-                 $service = new FinishedOrderService();
 
-                 list($status,$msg) = $service->U8Rdrecord10Save($package_data,$user);
 
-                 if(! $status) {
 
-                     $this->dellimitingSendRequestBackgNeed($limit_key);
 
-                     DB::rollBack();
 
-                     return [false, $msg];
 
-                 }
 
-             }
 
-             DB::commit();
 
-             $this->dellimitingSendRequestBackgNeed($limit_key);
 
-             return [true, ['package_data' => $package_data]];
 
-         }catch (\Throwable $e){
 
-             DB::rollBack();
 
-             $this->dellimitingSendRequestBackgNeed($limit_key);
 
-             if (str_contains($e->getMessage(), '1062') || str_contains($e->getMessage(), 'Duplicate entry')) {
 
-                 return [false, '网络波动,请重新操作!'];
 
-             }
 
-             return [false, $e->getFile(). '|' .$e->getLine().'|'.$e->getMessage()];
 
-         }
 
-     }
 
-     private function setConnect($data){
 
-         $storeCode = $data['zt'] ?? '';
 
-         $connectionName = "mysql";
 
-         if($storeCode == 'cs') $connectionName = "mysql_" . $storeCode;
 
-         if (in_array($connectionName, array_keys(config('database.connections')))) {
 
-             // 切换默认数据库连接
 
-             config(['database.default' => $connectionName]);
 
-             // 重新解析 DB Facade 使用新的连接
 
-             app()->forgetInstance('db');
 
-             $db = app('db');
 
-             // 如果用到了模型,还要重置模型连接
 
-             \Illuminate\Database\Eloquent\Model::setConnectionResolver($db);
 
-         }
 
-     }
 
-     public function createbcpllSQ($package_data, $insert_id, $user){
 
-         //是否自动审核  半成品出库申请单
 
-         $em = new EmployeeService();
 
-         $auto = $em->is_auto($user,"bcpckllsq_auto");
 
-         //组织包装原材料写入数据
 
-         $insert = [];
 
-         $box_id = $package_data['id'] ?? 0;
 
-         foreach ($insert_id as $value){
 
-             $insert[] = [
 
-                 'id' => $box_id,
 
-                 'quantity' => $value['quantity'] ?? 0,
 
-                 'product_no' => "",
 
-                 'product_title' => "",
 
-                 'product_size' => "",
 
-                 'product_unit' => "",
 
-                 'top_product_no' => $value['product_no'],
 
-                 'top_product_title' => $value['product_title'],
 
-                 'technology_name' => $value['technology_name'],
 
-             ];
 
-         }
 
-         try {
 
-             DB::beginTransaction();
 
-             //生成申请单
 
-             $service = new ApplyOrderService();
 
-             list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_six, $auto);
 
-             if(! $status) {
 
-                 DB::rollBack();
 
-                 return [false, $msg];
 
-             }
 
-             if($auto) {
 
-                 //生成流水
 
-                 list($status, $msg) = $service->createRecord($msg);
 
-                 if(! $status) {
 
-                     DB::rollBack();
 
-                     return [false, $msg];
 
-                 }
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function createllSQ($package_data, $insert_id, $user){
 
-         //是否自动审核  包装领料申请单
 
-         $em = new EmployeeService();
 
-         $auto = $em->is_auto($user,"bzllsq_auto");
 
-         $return = [];
 
-         $product_no = array_unique(array_column($insert_id,'product_no'));
 
-         //获取包装原料
 
-         $service = new FyyOrderService();
 
-         list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
 
-         if($status) $return = $msg;
 
-         //组织包装原材料写入数据
 
-         $insert = [];
 
-         $box_id = $package_data['id'] ?? 0;
 
-         foreach ($insert_id as $value){
 
-             $t = $return[$value['product_no']] ?? [];
 
-             $tmp = [
 
-                 'id' => $box_id,
 
-                 'quantity' => $value['quantity'] ?? 0,
 
-                 'product_no' => "",
 
-                 'product_title' => "",
 
-                 'product_size' => "",
 
-                 'product_unit' => "",
 
-                 'top_product_no' => $value['product_no'],
 
-                 'top_product_title' => $value['product_title'],
 
-                 'technology_name' => $value['technology_name'],
 
-             ];
 
-             if(! empty($t)){
 
-                 foreach ($t as $v){
 
-                     $tmp['product_no'] = $v['product_no'];
 
-                     $tmp['product_title'] = $v['product_title'];
 
-                     $tmp['product_size'] = $v['product_size'];
 
-                     $tmp['product_unit'] = $v['product_unit'];
 
-                     $insert[] = $tmp;
 
-                 }
 
-             }else{
 
-                 $tmp['quantity'] = 0;
 
-                 $insert[] = $tmp;
 
-             }
 
-         }
 
-         try {
 
-             DB::beginTransaction();
 
-             //生成申请单
 
-             $service = new ApplyOrderService();
 
-             list($status, $msg) = $service->createSQ($insert, $user, ApplyOrder::type_four, $auto);
 
-             if(! $status) {
 
-                 DB::rollBack();
 
-                 return [false, $msg];
 
-             }
 
-             if($auto) {
 
-                 //生成流水
 
-                 list($status, $msg) = $service->createRecord($msg);
 
-                 if(! $status) {
 
-                     DB::rollBack();
 
-                     return [false, $msg];
 
-                 }
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function createBZSQ($package_data, $insert_id, $user){
 
-         //是否自动审核  包装入库申请单
 
-         $em = new EmployeeService();
 
-         $auto = $em->is_auto($user,"bzsq_auto");
 
-         try {
 
-             DB::beginTransaction();
 
-             $box_id = $package_data['id'] ?? 0;
 
-             foreach ($insert_id as $key => $value){
 
-                 $insert_id[$key]['id'] = $box_id;
 
-             }
 
-             //生成申请单
 
-             $service = new ApplyOrderService();
 
-             list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_three, $auto);
 
-             if(! $status) {
 
-                 DB::rollBack();
 
-                 return [false, $msg];
 
-             }
 
-             //用友写入数据信息
 
-             $apply_order_id = $msg;
 
-             ApplyOrderForYy::insert([
 
-                 'apply_order_id' => $apply_order_id,
 
-                 'crt_time' => time(),
 
-                 'post' => json_encode($package_data),
 
-             ]);
 
-             if($auto) {
 
-                 //生成流水
 
-                 list($status, $msg) = $service->createRecord($msg);
 
-                 if(! $status) {
 
-                     DB::rollBack();
 
-                     return [false, $msg];
 
-                 }
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function delBoxLock(){
 
-         $this->dellimitingSendRequestBackgNeed($this->lock_key);
 
-     }
 
-     //新的完工
 
-     public function updateFinishNew($production_list = [], $map, $user){
 
-         if(empty($production_list)) return [true, ''];
 
-         $order_product_id = array_column($production_list,'id');
 
-         $order = OrdersProduct::where('del_time',0)
 
-             ->where('id', $order_product_id)
 
-             ->select('id', 'process_id')
 
-             ->get()->toArray();
 
-         $order_map = [];
 
-         //生产订单 =》 工序
 
-         foreach ($order as $value){
 
-             $order_map[$value['id']] = explode(',', $value['process_id']);
 
-         }
 
-         //组织派工数据
 
-         $dispatch = DispatchSub::where('del_time',0)
 
-             ->whereIn('order_product_id', array_column($production_list,'id'))
 
-             ->get()->toArray();
 
-         if(empty($dispatch)) return [true,''];
 
-         $sale_order_id = array_unique(array_column($dispatch,'sale_orders_product_id'));
 
-         $dispatch_map = [];
 
-         foreach ($dispatch as $value){
 
-             $key = $value['order_product_id'] . '|' .  $value['crt_time']; //同一次下的派工单
 
-             $dispatch_map[$key][] = $value;
 
-         }
 
-         $result = []; $tmp = [];
 
-         foreach ($dispatch_map as $key => $value){
 
-             $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
 
-             $finished_num = array_sum(array_column($value, 'finished_num'));
 
-             //同一批派工 且 全部完工的 跳过
 
-             if($dispatch_quantity == $finished_num) continue;
 
-             $order_id = explode('|', $key)[0];
 
-             // 生产订单id存在 跳过(原因 同一个生产订单可能下多次派工,那么这一次包装数量只需要一次派工的数据)
 
-             if(in_array($order_id, $tmp)) continue;
 
-             foreach ($value as $val){
 
-                 $num = bcadd($val['finished_num'], $val['waste_num'], 3);
 
-                 if($num >= $val['dispatch_quantity']) continue; //某一个工序完工
 
-                 //包装数量
 
-                 $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
 
-                 if($tmp_num <= 0) continue;
 
-                 $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
 
-                 if($tmp_num2 >= $tmp_num){
 
-                     $val['quantity'] = $tmp_num;
 
-                 }else{
 
-                     $val['quantity'] = $tmp_num2;
 
-                 }
 
-                 $result[] = $val;
 
-                 $tmp[] = $order_id;
 
-             }
 
-         }
 
-         if(empty($result)) return [true,''];
 
-         try {
 
-             DB::beginTransaction();
 
-             //班组下的人
 
-             $team_man = [];
 
-             $team_array = EmployeeTeamPermission::select('employee_id','team_id')->get()->toArray();
 
-             foreach ($team_array as $value){
 
-                 //拿班组里的一个人
 
-                 if(! isset($team_man[$value['team_id']]))  $team_man[$value['team_id']] = $value['employee_id'];
 
-             }
 
-             //工序 班组
 
-             $process_map = [
 
-                 12 => 65,
 
-                 11 => 66,
 
-                 15 => 69,
 
-                 14 => 67,
 
-                 13 => 68,
 
-             ];
 
-             $service = new FinishedOrderService();
 
-             $service1 = new ApplyOrderService();
 
-             $dispatch_time = $c_time = $result[0]['crt_time']; //派工单创建时间
 
-             $date = date("Ymd", $c_time);
 
- //            //派工和包装不在同一天 质检单日期加一天
 
- //            if($date != date("Ymd")){
 
- //                $c_time = strtotime("+1 day", strtotime($date));
 
- //                $date = date("Ymd", $c_time);
 
- //            }
 
-             $max_id = $service1->getDailyId($c_time);
 
-             //是否需要质检
 
-             list($status, $return) = $service->isNeedZJ($sale_order_id);
 
-             $flag = $return;
 
-             //根据派工数据组织
 
-             $last_update = $last_update2 = $str_tmp = $insert = $get_order_number = $return_dispatch =  [];
 
-             foreach ($result as $value){
 
-                 $need_zj_process = $flag[$value['sale_orders_product_id']] ?? 0;
 
-                 $bad_quantity = $scrapp_id = $for_r = 0;
 
-                 $warehouse_no = $warehouse_title = "";
 
-                 if(! empty($need_zj_process) && $value['process_id'] == $need_zj_process){
 
-                     $numbers = [0.08, 0.09, 0.1, 0.11, 0.12];
 
-                     $bad_quantity = $numbers[array_rand($numbers)];
 
-                     $flag[$value['sale_orders_product_id']] = 0;
 
-                     if($need_zj_process == 12){
 
-                         $scrapp_id = 19;
 
-                         $for_r = ReportWorking::type_two;
 
-                         $warehouse_no = "005";
 
-                         $warehouse_title = "报废仓";
 
-                     }else{
 
-                         $scrapp_id = mt_rand(10, 18);
 
-                         $for_r = ReportWorking::type_one;
 
-                     }
 
-                 }
 
-                 $quantity = bcsub($value['quantity'], $bad_quantity, 3);
 
-                 //质检
 
-                 $max_id += 1;
 
-                 $order_number = $date . str_pad($max_id,3,'0',STR_PAD_LEFT);
 
-                 $get_order_number[] = $order_number;
 
-                 $insert[] = [
 
-                     'sale_orders_product_id' => $value['sale_orders_product_id'],
 
-                     'order_product_id' => $value['order_product_id'],
 
-                     'dispatch_sub_id' => $value['id'],
 
-                     'out_order_no' => $value['out_order_no'],
 
-                     'order_no' => $value['order_no'],
 
-                     'customer_no' => $value['customer_no'],
 
-                     'customer_name' => $value['customer_name'],
 
-                     'product_no' => $value['product_no'],
 
-                     'product_title' => $value['product_title'],
 
-                     'product_size' => $value['product_size'],
 
-                     'product_unit' => $value['product_unit'],
 
-                     'technology_material' => $value['technology_material'],
 
-                     'technology_name' => $value['technology_name'],
 
-                     'wood_name' => $value['wood_name'],
 
-                     'price' => $value['price'],
 
-                     'process_mark' => $value['process_mark'],
 
-                     'table_body_mark' => $value['table_body_mark'],
 
-                     'table_header_mark' => $value['table_header_mark'],
 
-                     'crt_id' => $user['id'],
 
-                     'crt_time' => $c_time,
 
-                     'scrapp_num' => $bad_quantity,
 
-                     'scrapp_id' => $scrapp_id,
 
-                     'team_id' => 73,
 
-                     'finished_id' => $user['id'],
 
-                     'equipment_id' => $value['device_id'],
 
-                     'order_number' => $order_number,
 
-                     'process_id' => $value['process_id'],
 
-                     'quantity' => $quantity,
 
-                     'report_id' => 0,
 
-                     'result' => $for_r,
 
-                     'warehouse_no' => $warehouse_no,
 
-                     'warehouse_title' => $warehouse_title,
 
-                     'daily_id' => $max_id,
 
-                 ];
 
-                 $tmp = [
 
-                     'id' => $value['id'],
 
-                     'quantity' => $value['quantity'],
 
-                     'product_no' => $value['product_no'] ?? "",
 
-                     'product_title' => $value['product_title'] ?? "",
 
-                     'product_size' => $value['product_size'] ?? "",
 
-                     'product_unit' => $value['product_unit'] ?? "",
 
-                 ];
 
-                 $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
 
-                 if(! in_array($str, $str_tmp)){
 
-                     $last_update[] = $tmp;
 
-                     $str_tmp[] = $str;
 
-                 }
 
-                 $tmp['process_id'] = $value['process_id'];
 
-                 $tmp['quantity'] = $quantity;
 
-                 $team_tmp = $process_map[$value['process_id']] ?? 0; // 班组
 
-                 $t_m = $team_man[$team_tmp] ?? 0; //班组下的人
 
-                 $tmp['finished_id'] = $t_m;
 
-                 $tmp['team_id'] = $team_tmp;
 
-                 $last_update2[] = $tmp;
 
-                 if($value['process_id'] == 14) {
 
-                     $return_dispatch[] = [
 
-                         'id' => $value['id'],
 
-                         'num' => $value['quantity'],
 
-                     ];
 
-                 }
 
-             }
 
-             //生成报工单
 
-             list($status, $msg) = $this->createbg($last_update2, $user,$dispatch_time);
 
-             if(! $status) return [false, $msg];
 
-             //报工单id数组
 
-             list($report_id, $report_order_number) = $msg;
 
-             if(empty($report_id)) return [false, '报工单返回信息异常'];
 
-             //根据报工单创建质检单
 
-             list($status,$msg) = $service->createZJFirst($report_id, $insert);
 
-             if(! $status) return [false, $msg];
 
-             //生成因为有不良品需要的返工的业务单据
 
-             list($status, $msg) = $service->createZJSecond($get_order_number, $user, $dispatch_time, $c_time,$max_id,$team_man,$report_order_number,$return_dispatch);
 
-             if(! $status) return [false, $msg];
 
-             $get_order_number_result = $msg;
 
-             //生成完工入库申请单
 
-             list($status, $msg) = $this->createWGSQ($last_update, $user, $c_time);
 
-             if(! $status) return [false, $msg];
 
-             //反写 根据质检单上的派工单id 反写上游单据
 
-             $this->writeDispatchQuantity($get_order_number_result);
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
 
-         }
 
-         return [true, $return_dispatch];
 
-     }
 
-     public function createbg($insert, $user,$dispatch_time = 0, $order_number = ""){
 
-         //报工单
 
-         $em = new EmployeeService();
 
-         $auto = $em->is_auto($user, "wgbg_auto");
 
-         if(empty($order_number)){
 
-             $order_number = (new ApplyOrderService())->setOrderNO2($dispatch_time);
 
-             if(empty($order_number)) return [false, '报工单编号生成失败'];
 
-             $is_first = true;
 
-         }else{
 
-             $is_first = false;
 
-         }
 
-         $process = Process::where('del_time',0)
 
-             ->where('is_need_remain',1)
 
-             ->select('id')
 
-             ->get()->toArray();
 
-         $process_id = array_column($process,'id');
 
-         try {
 
-             DB::beginTransaction();
 
-             if(empty($dispatch_time)){
 
-                 $time = time();
 
-             }else{
 
-                 $time = $dispatch_time;
 
-             }
 
-             $prefix = substr($order_number, 0, -3);// "20250611"
 
-             $startNum = (int)substr($order_number, -3); // 7
 
-             $return = $get_order_number = [];
 
-             foreach ($insert as $v){
 
-                 if(! $is_first){
 
-                     $startNum += 1;
 
-                     $number = str_pad($startNum,3,'0',STR_PAD_LEFT);
 
-                     $number = $prefix . $number;
 
-                     $dispatch_no_t = $number;
 
-                 }else{
 
-                     $dispatch_no_t = $order_number;
 
-                     $is_first = false;
 
-                 }
 
-                 $get_order_number[] = $dispatch_no_t;
 
-                 $model = new ReportWorking();
 
-                 $model->order_number = $dispatch_no_t;
 
-                 $model->report_time = $time;
 
-                 $model->crt_id = $user['id'];
 
-                 $model->status = $auto;
 
-                 $model->save();
 
-                 $id = $model->id;
 
-                 ReportWorkingDetail::insert([
 
-                     'report_working_id' => $id,
 
-                     'data_id' => $v['id'],
 
-                     'quantity' => $v['quantity'] ?? 0,
 
-                     'finished_id' => $v['finished_id'] ?? 0,
 
-                     'process_id' => $v['process_id'] ?? 0,
 
-                     'team_id' => $v['team_id'] ?? 0,
 
-                     'crt_time' => $time,
 
-                 ]);
 
-                 $return[$v['id']] = $id;
 
-                 if(in_array($v['process_id'], $process_id)){
 
-                     ReportMessage::insert([
 
-                         'order_id' => $v['id'],
 
-                         'quantity' => $v['quantity'] ?? 0,
 
-                         'opt_case' => ReportMessage::type_one,
 
-                         'crt_time' => $time,
 
-                         'user_id' => $user['id'],
 
-                         'is_use' => 1,
 
-                         'report_working_id' => $id,
 
-                     ]);
 
-                 }
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
 
-         }
 
-         return [true, [$return, $get_order_number]];
 
-     }
 
-     //反写
 
-     public function writeDispatchQuantity($get_order_number_result){
 
-         $result = ScrappCount::whereIn('order_number',$get_order_number_result)
 
-             ->select('dispatch_sub_id','quantity','scrapp_num as num','crt_time','process_id','order_product_id')
 
-             ->get()->toArray();
 
-         //是否最后一道工序
 
-         $orders = OrdersProduct::whereIn('id', array_unique(array_column($result, 'order_product_id')))
 
-             ->select('id','process_id')
 
-             ->get()->toArray();
 
-         $map = [];
 
-         foreach ($orders as $value){
 
-             $p = explode(",", $value['process_id']);
 
-             $lastElement = end($p);
 
-             $map[$value['id']] = $lastElement;
 
-         }
 
-         //汇总这个派工单一共质检良品数量 但是同一时间下的 只取一次
 
-         $update = $flag = $need_update = [];
 
-         foreach ($result as $value){
 
-             if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id'] && ! in_array($value['order_product_id'], $need_update)){
 
-                 $need_update[] = [
 
-                     'order_product_id' => $value['order_product_id'],
 
-                     'process_id' => $value['process_id'],
 
-                 ];
 
-             }
 
-             $key = $value['dispatch_sub_id'] . $value['crt_time'];
 
-             if(isset($flag[$key])) continue;
 
-             if(isset($update[$value['dispatch_sub_id']])){
 
-                 $tmp = bcadd($value['quantity'], $update[$value['dispatch_sub_id']]['finished_num'], 3);
 
-                 $tmp1 = bcadd($value['num'], $update[$value['dispatch_sub_id']]['waste_num'], 3);
 
-                 $update[$value['dispatch_sub_id']] = [
 
-                     'finished_num' => $tmp,
 
-                     'waste_num' => $tmp1,
 
-                 ];
 
-             }else{
 
-                 $update[$value['dispatch_sub_id']] = [
 
-                     'finished_num' => $value['quantity'],
 
-                     'waste_num' => $value['num'],
 
-                 ];
 
-             }
 
-             $flag[$key] = 1;
 
-         }
 
-         unset($flag);
 
-         //更新派工单完工数量----------------------------------------------------
 
-         foreach ($update as $id => $value){
 
-             DispatchSub::where('id', $id)->update([
 
-                 'finished_num' => DB::raw('finished_num + '.$value['finished_num']),
 
-                 'waste_num' => DB::raw('waste_num + '.$value['waste_num']),
 
-                 'wg_status' => 1,
 
-             ]);
 
-         }
 
-         if(! empty($need_update)){
 
-             //更新生产订单的完工数量--------------------------------------------------
 
-             $list = DispatchSub::where("del_time",0)
 
-                 ->when(! empty($need_update), function ($query) use ($need_update) {
 
-                     return $query->where(function ($q) use ($need_update) {
 
-                         foreach ($need_update as $value) {
 
-                             $order_product_id = $value['order_product_id'];
 
-                             $process_id = $value['process_id'];
 
-                             $q->orWhere(function ($subQ) use ($order_product_id, $process_id) {
 
-                                 $subQ->where('order_product_id', $order_product_id)
 
-                                     ->where('process_id', $process_id);
 
-                             });
 
-                         }
 
-                     });
 
-                 })
 
-                 ->select('order_product_id', 'finished_num', 'sale_orders_product_id')
 
-                 ->get()->toArray();
 
-             $map2 = [];
 
-             foreach ($list as $value){
 
-                 if(isset($map2[$value['order_product_id']])){
 
-                     $tmp = bcadd($map2[$value['order_product_id']], $value['finished_num'], 3);
 
-                     $map2[$value['order_product_id']] = $tmp;
 
-                 }else{
 
-                     $map2[$value['order_product_id']] = $value['finished_num'];
 
-                 }
 
-             }
 
-             foreach ($map2 as $id => $num){
 
-                 OrdersProduct::where('id', $id)->update([
 
-                     'finished_num' => $num
 
-                 ]);
 
-             }
 
-             //更新销售订单完工数量-------------------------------------------------------
 
-             $list2 = OrdersProduct::where('del_time',0)
 
-                 ->whereIn('sale_orders_product_id', array_unique(array_column($list, 'sale_orders_product_id')))
 
-                 ->select('finished_num','sale_orders_product_id')
 
-                 ->get()->toArray();
 
-             $map3 = [];
 
-             foreach ($list2 as $value){
 
-                 if(isset($map3[$value['sale_orders_product_id']])){
 
-                     $tmp = bcadd($map3[$value['sale_orders_product_id']], $value['finished_num'], 3);
 
-                     $map3[$value['sale_orders_product_id']] = $tmp;
 
-                 }else{
 
-                     $map3[$value['sale_orders_product_id']] = $value['finished_num'];
 
-                 }
 
-             }
 
-             foreach ($map3 as $id => $num){
 
-                 SaleOrdersProduct::where('id', $id)->update([
 
-                     'finished_num' => $num
 
-                 ]);
 
-             }
 
-         }
 
-     }
 
-     //旧的完工
 
-     public function updateFinish($production_list = [], $map,$user){
 
-         if(empty($production_list)) return [true,''];
 
-         $order_product_id = array_column($production_list,'id');
 
-         $order = OrdersProduct::where('del_time',0)
 
-             ->where('id', $order_product_id)
 
-             ->select('id', 'process_id')
 
-             ->get()->toArray();
 
-         $order_map = [];
 
-         foreach ($order as $value){
 
-             $order_map[$value['id']] = explode(',', $value['process_id']);
 
-         }
 
-         //组织派工数据
 
-         $dispatch = DispatchSub::where('del_time',0)
 
-             ->whereIn('order_product_id', array_column($production_list,'id'))
 
-             ->get()->toArray();
 
-         if(empty($dispatch)) return [true,''];
 
-         $dispatch_map = [];
 
-         foreach ($dispatch as $value){
 
-             $key = $value['order_product_id'] . '|' .  $value['crt_time']; //同一次下的派工单
 
-             $dispatch_map[$key][] = $value;
 
-         }
 
-         $result = []; $tmp = [];
 
-         foreach ($dispatch_map as $key => $value){
 
-             $dispatch_quantity = array_sum(array_column($value, 'dispatch_quantity'));
 
-             $finished_num = array_sum(array_column($value, 'finished_num'));
 
-             if($dispatch_quantity == $finished_num) continue; //同一批派工 且 全部完工的 跳过
 
-             $order_id = explode('|', $key)[0];
 
-             if(in_array($order_id, $tmp)) continue;// 生产订单id存在 跳过
 
-             foreach ($value as $val){
 
-                 if($val['dispatch_quantity'] <= $val['finished_num']) continue; //某一个工序完工
 
-                 //包装数量
 
-                 $tmp_num = $map[$val['order_product_id']]['total'] ?? 0;
 
-                 if($tmp_num <= 0) continue;
 
-                 $tmp_num2 = bcsub($val['dispatch_quantity'] , $val['finished_num'],3);
 
-                 if($tmp_num2 >= $tmp_num){
 
-                     $val['quantity'] = $tmp_num;
 
-                 }else{
 
-                     $val['quantity'] = $tmp_num2;
 
-                 }
 
-                 $result[] = $val;
 
-                 $tmp[] = $order_id;
 
-             }
 
-         }
 
-         if(empty($result)) return [true,''];
 
-         $time = time();
 
-         try {
 
-             DB::beginTransaction();
 
-             //班组下的人
 
-             $team_man = [];
 
-             $team_array = EmployeeTeamPermission::select('employee_id','team_id')->get()->toArray();
 
-             foreach ($team_array as $value){
 
-                 $team_man[$value['team_id']][] = $value['employee_id'];
 
-             }
 
-             //工序 班组
 
-             $process_map = [
 
-                 12 => 65,
 
-                 11 => 66,
 
-                 15 => 69,
 
-                 14 => 67,
 
-                 13 => 68,
 
-             ];
 
-             //根据派工数据回写
 
-             $last_update = $last_update2 = $str_tmp = [];
 
-             foreach ($result as $value){
 
-                 $finished_num = $value['quantity'] + $value['finished_num'];
 
-                 DispatchSub::where('id',$value['id'])->update([
 
-                     'finished_num' => $finished_num,
 
-                     'wg_status' => 1,
 
-                 ]);
 
-                 $tmp = [
 
-                     'id' => $value['id'],
 
-                     'quantity' => $value['quantity'],
 
-                     'product_no' => $value['product_no'] ?? "",
 
-                     'product_title' => $value['product_title'] ?? "",
 
-                     'product_size' => $value['product_size'] ?? "",
 
-                     'product_unit' => $value['product_unit'] ?? "",
 
-                 ];
 
-                 $str = $value['order_product_id'] . $value['crt_time'] . $value['product_no'] . $value['technology_name'];
 
-                 if(! in_array($str, $str_tmp)){
 
-                     $last_update[] = $tmp;
 
-                     $str_tmp[] = $str;
 
-                 }
 
-                 $tmp['process_id'] = $value['process_id'];
 
-                 $team_tmp = $process_map[$value['process_id']] ?? 0; // 班组
 
-                 if(empty($team_tmp)){
 
-                     $tmp['finished_id'] = 0;
 
-                     $tmp['team_id'] = 0;
 
-                     $last_update2[] = $tmp;
 
-                 }else{
 
-                     $t_m = $team_man[$team_tmp] ?? []; //班组下的人
 
-                     if(empty($t_m)) {
 
-                         $tmp['finished_id'] = 0;
 
-                         $tmp['team_id'] = $team_tmp;
 
-                         $last_update2[] = $tmp;
 
-                     }else{
 
-                         foreach ($t_m as $m_v){
 
-                             $tmp['finished_id'] = $m_v;
 
-                             $tmp['team_id'] = $team_tmp;
 
-                             $last_update2[] = $tmp;
 
-                         }
 
-                     }
 
-                 }
 
-                 $quantity = $value['quantity'] * 1000;
 
-                 //工序表
 
-                 $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
 
-                 $process_model->where('order_product_id',$value['order_product_id'])
 
-                     ->where('process_id',$value['process_id'])
 
-                     ->where('dispatch_no',$value['dispatch_no'])
 
-                     ->where('status', 1)
 
-                     ->take($quantity)
 
-                     ->update([
 
-                         'finished_time' => $time,
 
-                         'status' => 2,
 
-                         'finished_id' => 1,
 
-                         'team_id' => $team_tmp,
 
-                         'equipment_id' => 13,
 
-                     ]);
 
-             }
 
-             $service = new FinishedOrderService();
 
-             //反写数量
 
-             //生产订单
 
-             $id = array_column($result,'order_product_id');
 
-             $service->writeFinishedQuantityByOrdersProductId($id);
 
-             //销售订单
 
-             $service->writeFinishedQuantity($id);
 
-             //生成完工入库申请单
 
- //            list($status, $msg) = $this->createWGSQ($last_update, $user);
 
- //            if(! $status) return [false, $msg];
 
-             //生成报工单
 
-             $service = new FinishedOrderService();
 
-             list($status, $msg) = $service->createbg($last_update2, $user);
 
-             if(! $status) return [false, $msg];
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function createWGSQ($insert_id, $user, $time = 0){
 
-         //完工入库申请单自动审核并同步用友
 
-         $em = new EmployeeService();
 
-         $auto = $em->is_auto($user, "wgsq_auto");
 
-         try {
 
-             DB::beginTransaction();
 
-             //生成申请单
 
-             $service = new ApplyOrderService();
 
-             list($status, $msg) = $service->createSQ($insert_id, $user, ApplyOrder::type_two, $auto, $time);
 
-             if(! $status) {
 
-                 DB::rollBack();
 
-                 return [false, $msg];
 
-             }
 
-             if($auto) {
 
-                 //生成流水
 
-                 list($status, $msg) = $service->createRecord($msg);
 
-                 if(! $status) {
 
-                     DB::rollBack();
 
-                     return [false, $msg];
 
-                 }
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function saveOutOrder($data,$user){
 
-         list($status,$msg) = $this->saveOutOrderRule($data,$user);
 
-         if(! $status) return [false, $msg];
 
-         $out_order_no = $msg;
 
-         $limit_key = "saveOutOrder" . $user['id'];
 
-         list($status,$msg) = $this->limitingSendRequestBackgNeed($limit_key);
 
-         if(! $status) return [false, '正在生成销售出库订单数据,请稍后尝试!'];
 
-         //不超时
 
-         ini_set('max_execution_time', 0);
 
-         try {
 
-             DB::beginTransaction();
 
-             //组织数据
 
-             $yongyou_data = $update = [];
 
-             foreach ($data['out_data'] as $value){
 
-                 $pro_tmp = [];
 
-                 foreach ($value['product'] as $p){
 
-                     $t_k = $p['cinvcode'] . $p['cfree1'];
 
-                     $pro_tmp[$t_k] = [
 
-                         'cinvcode' => $p['cinvcode'],
 
-                         'cfree1' => $p['cfree1'],
 
-                         'iquantity' => $p['iquantity'],
 
-                         'line' => $p['line'],
 
-                     ];
 
-                 }
 
-                 if(isset($yongyou_data[$value['customer_code']])){
 
-                     $yongyou_data[$value['customer_code']]['cdlcode_string'] .= ',' . $value['cdlcode'];
 
-                     foreach ($pro_tmp as $k => $v){
 
-                         if(isset($yongyou_data[$value['customer_code']]['product'][$k])){
 
-                             $yongyou_data[$value['customer_code']]['product'][$k]['iquantity'] += $v['iquantity'];
 
-                         }else{
 
-                             $yongyou_data[$value['customer_code']]['product'][$k] = $v;
 
-                         }
 
-                     }
 
-                 }else{
 
-                     $yongyou_data[$value['customer_code']] = [
 
-                         'customer_code' => $value['customer_code'],
 
-                         'cdlcode_string' => $value['cdlcode'],
 
-                         'product' => $pro_tmp
 
-                     ];
 
-                 }
 
-                 foreach ($value['box_no'] as $box_no){
 
-                     $update[$box_no] = $value['cdlcode'];
 
-                 }
 
-             }
 
-             if(empty($yongyou_data)) {
 
-                 $this->dellimitingSendRequestBackgNeed($limit_key);
 
-                 return [false, '暂无写入用友发货出库数据!'];
 
-             }
 
-             //用友 ------发货出库
 
-             $service = new FinishedOrderService();
 
-             list($status,$msg) = $service->U8Rdrecord32Save($yongyou_data,$out_order_no,$user);
 
-             if(! $status) {
 
-                 $this->dellimitingSendRequestBackgNeed($limit_key);
 
-                 return [false, $msg];
 
-             }
 
-             //本地数据更新
 
-             if(! empty($update)){
 
-                 foreach ($update as $key => $value){
 
-                     Box::where('order_no', $key)->update([
 
-                         'shipment_order_no' => $value
 
-                     ]);
 
-                 }
 
-             }
 
-             DB::commit();
 
-             $this->dellimitingSendRequestBackgNeed($limit_key);
 
-         }catch (\Throwable $exception){
 
-             $this->dellimitingSendRequestBackgNeed($limit_key);
 
-             DB::rollBack();
 
-             return [false, $exception->getFile() . $exception->getFile() . $exception->getMessage()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function saveOutOrderRule($data,$user){
 
-         if(empty($data['out_data'])) return [false, '未获取到发货数据,操作失败!'];
 
-         Log::channel('sendData')->info('发货:提交包装单数据', ["param" => $data]);
 
-         //客户校验
 
-         $customer_code = array_column($data['out_data'],'customer_code');
 
-         $isAllSame = count(array_unique($customer_code)) === 1;
 
-         if(! $isAllSame) return [false, '发货出库,请选择同一个客户的订单!'];
 
-         $box_no = array_column($data['out_data'],'box_no');
 
-         if(empty($box_no)) return [false, '包装单信息不能为空'];
 
-         $box_arr = [];
 
-         foreach ($box_no as $value){
 
-             foreach ($value as $val){
 
-                 $box_arr[] = $val;
 
-             }
 
-         }
 
-         $box = Box::where('del_time',0)
 
-             ->whereIn('order_no',$box_arr)
 
-             ->select('shipment_order_no', "order_no","out_order_no")
 
-             ->get()->toArray();
 
-         $box_map = array_column($box,'shipment_order_no','order_no');
 
-         $out_order_no = array_unique(array_column($box,'out_order_no'));
 
-         foreach ($box_arr as $value){
 
-             $tmp = $box_map[$value] ?? "";
 
-             if($tmp != "") return [false, "包装单:" . $value . "已在发货单(" . $tmp .")中发出!"];
 
-         }
 
-         //用友发货数据查询
 
-         $cdlcode = array_column($data['out_data'],'cdlcode');
 
-         $service = new FyySqlServerService($user);
 
-         if($service->error) return [false, $service->error];
 
-         $out_data_map = $service->getDataFromDispatchListForCheck(['cdlcode' => $cdlcode]);
 
-         if(empty($out_data_map)) return [false, '未在用友发货数据中查找到相关数据!'];
 
-         //数据校验
 
-         foreach ($data['out_data'] as $value){
 
-             if(empty($value['cdlcode'])) return [false, '发货单号不能为空!'];
 
-             if(! isset($out_data_map[$value['cdlcode']])) return [false, '发货单号:' . $value['cdlcode'] . '不存在或已被删除!'];
 
-             $tmp = $out_data_map[$value['cdlcode']];
 
-             if(empty($value['customer_code'])) return [false, '客户编码不能为空!'];
 
-             if($tmp['customer_code'] != $value['customer_code']) return [false, '客户编码与用友数据中不一致!'];
 
-             if(empty($tmp['product'])) return [false, '发货单号:' . $value['cdlcode'] . '未查找到用友的发货产品数据!'];
 
-             $tmp_pro = [];
 
-             foreach ($tmp['product'] as $t){
 
-                 $t_k = $t['cinvcode'] . $t['cfree1'];
 
-                 $tmp_pro[$t_k] = bcsub($t['iquantity'] , $t['out_quantity'],3);//未发货数量
 
-             }
 
-             if(empty($value['product'])) return [false, '发货产品不能为空!'];
 
-             foreach ($value['product'] as $p){
 
-                 if(empty($p['line'])) return [false, '存货行号不能为空!'];
 
-                 if(empty($p['cinvcode'])) return [false, '存货编码不能为空!'];
 
-                 if(empty($p['cfree1'])) return [false, '存货颜色不能为空!'];
 
-                 $p_k = $p['cinvcode'] . $p['cfree1'];
 
-                 if(! isset($tmp_pro[$p_k])) return [false, '发货单号:' . $value['cdlcode'] . '下无该发货产品:编码为' . $p['cinvcode'] . ',颜色为:' . $p['cfree1']];
 
-                 if(empty($p['iquantity']) || $p['iquantity'] <= 0) return [false, '发货数量不能为空!'];
 
-                 $last = $tmp_pro[$p_k];
 
-                 if($p['iquantity'] > $last) return [false, '发货单号:' . $value['cdlcode'] . '下的发货产品:编码为' . $p['cinvcode'] . ',颜色为' . $p['cfree1'] . '的未发货数量为' . $last];
 
-             }
 
-         }
 
-         return [true, $out_order_no];
 
-     }
 
-     /**
 
-      * 根据发货单包装(恒成塑业不需要 如果有调用 直接返回)
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function transportBoxIn($data)
 
-     {
 
-         return [true,''];
 
-         $param = $data['params'];
 
-         $shipment_order_no = $data['transport_no'];
 
-         $sale_order_nos = [];
 
-         $key_list = [];
 
-         foreach ($param as $v){
 
-             if(!in_array($v['order_no'],$sale_order_nos)) $sale_order_nos[] = $v['order_no'];
 
-             $key = $this->getKey($v);
 
-             $key_list[$key] = [
 
-                 'num' => $v['num'],
 
-                 'order_no' => $v['order_no'],
 
-                 'team_id' => isset($v['team_id']) ? $v['team_id'] : 0,
 
-             ];
 
-         }
 
-         $sale_order_product_list = SaleOrdersProduct::wherein('out_order_no',$sale_order_nos)->get()->toArray();
 
-         $sale_order_product_key_list = [];
 
-         foreach ($sale_order_product_list as $v){
 
-             $key = $this->getKey($v);
 
-             $sale_order_product_key_list[$key] = $v['id'];
 
-         }
 
-         $param = [];
 
-         foreach ($key_list as $k=>$v){
 
-             if(!isset($sale_order_product_key_list[$k])) return [false,$k.' 不存在!'];
 
-             $param[$v['order_no']][] = [
 
-                 'id' => -(intval($sale_order_product_key_list[$k])),
 
-                 'team_id' => $v['team_id'],
 
-                 'params' => [$v['num']]
 
-             ];
 
-         }
 
- //        $box_no = self::$box_hook->setOrderNo(); todo
 
-         foreach ($param as $k=>$v){
 
-             $top_order_no = SaleOrdersProduct::where('out_order_no',$k)->value('order_no');
 
-             $postParam = [
 
-                 'order_no' => $top_order_no,
 
-                 'param' => $v,
 
-                 'box_no' => $box_no,
 
-                 'transport_no' => $shipment_order_no,
 
-             ];
 
-             $this->boxIn($postParam);
 
-         }
 
-         return [true,''];
 
-     }
 
-     protected function getKey($data){
 
-         return $data['product_no'].'_'.$data['technology_material'].'_'.$data['technology_name'].'_'.$data['wood_name'].'_'.$data['process_mark'];
 
-     }
 
-     /**
 
-      * 包装详情1用于包装前
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function boxDetail($data)
 
-     {
 
-         list($status, $data) = self::$box_hook->boxDetail($data);
 
-         if (!$status) return [false, $data];
 
-         $return = [];
 
-         foreach ($data as $v){
 
-             $key = $v['ext_1'] . $v['ext_3'];
 
-             if(! isset($return[$key])){
 
-                 $return[$key] = $v;
 
-             }else{
 
-                 $return[$key]['num'] += $v['num'];
 
-             }
 
-         }
 
-         $sale_orders_product_ids = array_unique(array_column($data,'top_id'));
 
-         $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
 
-         $key_list = [];
 
-         foreach ($list as $v){
 
-             $key_list[$v['id']] = $v;
 
-         }
 
-         foreach ($return as &$v){
 
-             $detail =  $key_list[$v['top_id']];
 
-             $v['customer_no'] = $detail['customer_no'];
 
-             $v['customer_name'] = $detail['customer_name'];
 
-         }
 
-         return [true, array_values($return)];
 
-     }
 
-     public function boxProductList($data){
 
-         if(empty($data['id']) && empty($data['out_order_no'])) return [false,'请选择数据!'];
 
-         if(! empty($data['id'])){
 
-             $sale_order_ids = $data['id'];
 
-         }else{
 
-             $ids = SaleOrdersProduct::where('del_time',0)
 
-                 ->where('out_order_no','Like','%'. $data['out_order_no'] . '%')
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $sale_order_ids = array_column($ids,'id');
 
-             if(empty($sale_order_ids)) return [false, '暂无数据!'];
 
-         }
 
-         $model = SaleOrdersProduct::where('del_time',0)
 
-             ->whereIn('id',$sale_order_ids)
 
-             ->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')
 
-             ->orderBy('id','desc')
 
-             ->get()->toArray();
 
-         //原先按照完工数量 未包装 = 完工数量 - 已包数量
 
-         $product_list = ordersProduct::where('del_time',0)
 
-             ->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")
 
-             ->get()->toArray();
 
-         $model_key_list = [];
 
-         foreach ($model as $v){
 
-             $model_key_list[$v['id']] = $v;
 
-         }
 
-         $model = [];
 
-         //存在生产订单,组织数据
 
-         if(! empty($product_list)){
 
-             $product_key_num_list = [];
 
-             foreach ($product_list as $v){
 
-                 //下派工数量 就是 能包的数量 相等以后 踢掉数据
 
-                 if($v['dispatch_complete_quantity'] == $v['box_num']) continue;
 
-                 if(!isset($product_key_num_list[$v['sale_orders_product_id']][$v['id']])) {
 
-                     $product_key_num_list[$v['sale_orders_product_id']][$v['id']] = [
 
-                         'product_num' => 0 ,
 
-                         'box_num' => 0 ,
 
-                     ];
 
-                 }
 
-                 $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['product_num'] += $v['production_quantity'];
 
-                 $product_key_num_list[$v['sale_orders_product_id']][$v['id']]['box_num'] += $v['box_num'];
 
-                 $detail = $model_key_list[$v['sale_orders_product_id']];
 
-                 $detail['box_type'] = 1;
 
-                 $detail['id'] = $v['id'];
 
-                 $detail['box_num'] = $v['box_num'];
 
-                 $detail['dispatch_complete_quantity'] = $v['dispatch_complete_quantity'];
 
-                 $detail['un_box_num'] = bcsub($v['dispatch_complete_quantity'] , $v['box_num'],3);
 
-                 $detail['type'] = 1;
 
-                 $detail['production_no'] = $v['production_no'];
 
-                 $detail['production_quantity'] = $v['production_quantity'];
 
-                 $model[] = $detail;
 
-             }
 
-         }
 
-         $return = [];
 
-         foreach ($model as $v){
 
-             if(!isset($v['box_type'])) {continue; //没有销售订单直接包了。
 
-                 $product_num = 0;
 
-                 $box_num = 0;
 
-                 if(isset($product_key_num_list[$v['id']])){
 
-                     foreach ($product_key_num_list[$v['id']] as $vv){
 
-                         $product_num += $vv['product_num'];
 
-                         $box_num += $vv['box_num'];
 
-                     }
 
-                 }
 
-                 if(($v['order_quantity'] - $product_num) === 0) continue;
 
-                 $product_key_list[$v['sale_orders_product_id']] = [
 
-                     'out_order_no' => $v['out_order_no'],
 
-                     'order_no' => $v['order_no'],
 
-                     'production_no' => "",
 
-                     'production_time' => '未下生产',
 
-                     'customer_no' => $v['customer_no'],
 
-                     'customer_name' => $v['customer_name'],
 
-                     'product_no' => $v['product_no'],
 
-                     'product_title' => $v['product_title'],
 
-                     'product_size' => $v['product_size'],
 
-                     'id' => -$v['sale_orders_product_id'],
 
-                     'technology_material' => $v['technology_material'],
 
-                     'technology_name' => $v['technology_name'],
 
-                     'wood_name' => $v['wood_name'],
 
-                     'process_mark' => $v['process_mark'],
 
-                     'type' => '2',
 
-                     'is_box_num' => $v['box_num'] - $box_num,
 
-                     'un_box_num' => $v['order_quantity'] - ($v['box_num'] - $box_num) - $product_num,
 
-                     'sale_num' => $v['order_quantity'],
 
-                 ];
 
-             }else{
 
-                 $return[] = [
 
-                     'id' => $v['id'],
 
-                     'technology_material' => $v['technology_material'],
 
-                     'technology_name' => $v['technology_name'],
 
-                     'wood_name' => $v['wood_name'],
 
-                     'order_no' => $v['order_no'],
 
-                     'production_no' => $v['production_no'],
 
-                     'process_mark' => $v['process_mark'],
 
-                     'out_order_no' => $v['out_order_no'],
 
-                     'production_time' => date('Y-m-d',$v['crt_time']),
 
-                     'customer_no' => $v['customer_no'],
 
-                     'customer_name' => $v['customer_name'],
 
-                     'product_no' => $v['product_no'],
 
-                     'product_title' => $v['product_title'],
 
-                     'product_size' => $v['product_size'],
 
-                     'type' => '1',
 
-                     'is_box_num' => $v['box_num'],
 
-                     'un_box_num' => $v['un_box_num'],
 
-                     'sale_num' => $v['order_quantity'],
 
-                     'production_num' => $v['production_quantity'],
 
-                     'dispatch_complete_quantity' => $v['dispatch_complete_quantity']
 
-                 ];
 
-             }
 
-         }
 
-         return [true,$return];
 
-     }
 
-     /**
 
-      * 包装详情2用于包装后
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function boxOrderDetail($data)
 
-     {
 
-         list($status, $data) = self::$box_hook->boxDetail($data);
 
-         $sale_orders_product_ids = [];
 
-         foreach ($data as $v){
 
-             $sale_orders_product_ids[] = $v['top_id'];
 
-         }
 
-         $list = SaleOrdersProduct::wherein('id',$sale_orders_product_ids)->get()->toArray();
 
-         $key_list = [];
 
-         foreach ($list as $v){
 
-             $key_list[$v['id']] = $v;
 
-         }
 
-         foreach ($data as &$v){
 
-             $v['ext_3'] = $key_list[$v['top_id']]['product_no'];
 
-             $v['wood_name'] = $key_list[$v['top_id']]['wood_name'];
 
-             $v['technology_name'] = $key_list[$v['top_id']]['technology_name'];
 
-             $v['process_mark'] = $key_list[$v['top_id']]['process_mark'];
 
-             $v['technology_material'] = $key_list[$v['top_id']]['technology_material'];
 
-         }
 
-         if (!$status) return [false, $data];
 
-         return [true, $data];
 
-     }
 
-     /**
 
-      * 通过销售单号获取包装单
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function boxOrderDetailByTop($data){
 
-         $list = new Box();
 
-         if(isset($data['top_order_no'])&&!empty($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
 
-         if(isset($data['transport_no'])&&!empty($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
 
- //        if(isset($data['top_order_no'])) $list = $list->wherein('top_order_no',$data['top_order_no']);
 
- //        if(isset($data['transport_no'])) $list = $list->wherein('shipment_order_no',$data['transport_no']);
 
-         $list = $list->select('top_order_no','order_no','crt_time')->where('del_time',0)->groupBy('order_no')->get()->toArray();
 
-         return [true,$list];
 
-     }
 
-     /**
 
-      * 通过包装单获取包装详情
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function boxOrderDetailByOrderNo($data){
 
-         $order_nos = $data['order_nos'];
 
-         $order_nos = array_unique($order_nos);
 
-         $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();
 
-         $detail_list = [];
 
-         foreach ($list as $v){
 
-             $model = new BoxDetail(['channel'=>$v['top_order_no']]);
 
-             $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
 
-         }
 
-         $list = $detail_list;
 
-         $ids = [];
 
-         foreach ($list as $v){
 
-             $ids[] = $v['top_id'];
 
-         }
 
-         $sale_orders_product = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
 
-         $sale_orders_key_product = [];
 
-         foreach ($sale_orders_product as $v){
 
-             $sale_orders_key_product[$v['id']] = $v;
 
-         }
 
-         $team_key_list = Team::pluck('title','id')->toArray();
 
-         foreach ($list as &$v){
 
-             $detail = $sale_orders_key_product[$v['top_id']];
 
-             $v['customer_name'] = $detail['customer_name'];
 
-             $v['table_body_mark'] = $detail['table_body_mark'];
 
-             $v['ext_4'] = $detail['product_title'];
 
-             $v['ext_5'] = $detail['product_size'];
 
-             $v['customer_name'] = $detail['customer_name'];
 
-             $v['technology_material'] = $detail['technology_material'];
 
-             $v['transport_no'] = $v['shipment_order_no'];
 
-             $v['wood_name'] = $detail['wood_name'];
 
-             $v['team_name'] = isset($team_key_list[$v['team_id']]) ? $team_key_list[$v['team_id']] : '' ;
 
-         }
 
-         return [true,$list];
 
-     }
 
-     public function boxOrderGroup($data){
 
-         $list = new Box();
 
-         if(empty($data['top_order_no']) && empty($data['transport_no'])) return [false,'请输入单号!'];
 
-         if(! empty($data['top_order_no'])) $list = $list->whereIn('top_order_no',$data['top_order_no']);
 
-         if(! empty($data['transport_no'])) {
 
-             if(is_array($data['transport_no'])){
 
-                 $list = $list->whereIn('shipment_order_no',$data['transport_no']);
 
-             }else{
 
-                 $list = $list->where('shipment_order_no','Like','%'. $data['transport_no'] . '%');
 
-             }
 
-         }
 
-         $list = $list->select('top_order_no','order_no as box_no','crt_time','shipment_order_no','out_order_no')->where('del_time',0)
 
-             ->get()->toArray();
 
-         foreach ($list as $key => $value){
 
-             $model = new BoxDetail(['channel'=> $value['top_order_no']]);
 
-             $tmp = $model->where('del_time',0)
 
-                 ->where('order_no',$value['box_no'])
 
-                 ->where('out_order_no',$value['out_order_no'])
 
-                 ->select('order_no','out_order_no','orders_product_id')
 
-                 ->first();
 
-             $production_no = "";
 
-             if(! empty($tmp)){
 
-                 $production_no = OrdersProduct::where('id',$tmp->orders_product_id)->select('production_no')->pluck('production_no')->toArray();
 
-                 $production_no = $production_no[0];
 
-             }
 
-             $list[$key]['production_no'] = $production_no;
 
-             $list[$key]['crt_time'] = date('Y-m-d H:i:s',$value['crt_time']);
 
-         }
 
-         return [true,$list];
 
-     }
 
-     public function boxOrderGroupGwp($data){
 
-         $out_order_no = $data['out_order_no'] ?? "";
 
-         $model = Box::where('del_time',0)
 
-             ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
 
-             return $query->where('out_order_no','Like','%'. $out_order_no . '%');
 
-         })
 
-             ->select('out_order_no','ext_1','top_order_no','order_no','crt_time','shipment_order_no',DB::raw("count(id) as package_num"))
 
-             ->groupBy('out_order_no');
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->fillData($list);
 
-         return [true, $list];
 
-     }
 
-     public function fillData($data){
 
-         if(empty($data['data'])) return $data;
 
-         foreach ($data['data'] as $key => $value){
 
-             $model = new BoxDetail(['channel'=> $value['top_order_no']]);
 
-             $tmp = $model->where('del_time',0)
 
-                 ->where('out_order_no',$value['out_order_no'])
 
-                 ->select('num')
 
-                 ->get()->toArray();
 
-             $data['data'][$key]['num'] = $this->getTotal($tmp, 'num');
 
-         }
 
-         return $data;
 
-     }
 
-     public function boxOrderGroupGwpDetail($data){
 
-         if(empty($data['top_order_no']) || empty($data['out_order_no'])) return [false, '请选择数据!'];
 
-         $top_order_no = $data['top_order_no'];
 
-         $model = new BoxDetail(['channel'=> $top_order_no]);
 
-         $detail_list = $model->where('del_time',0)
 
-             ->where('out_order_no',$data['out_order_no'])
 
-             ->get()->toArray();
 
-         $map = Box::whereIn('order_no',array_unique(array_column($detail_list,'order_no')))
 
-             ->pluck('ext_1','order_no')
 
-             ->toArray();
 
-         $return = [];
 
-         foreach ($detail_list as $value){
 
-             //销售订单号  包装单号  产品编码  颜色
 
-             $key = $value['out_order_no'] . $value['order_no'] . $value['ext_1'] . $value['ext_3'];
 
-             if(isset($return[$key])){
 
-                 $return[$key]['num'] += $value['num'];
 
-             }else{
 
-                 $return[$key] = [
 
-                     'box_no' => $value['order_no'],
 
-                     'out_order_no' =>  $value['out_order_no'],
 
-                     'ext_8' =>  $value['ext_8'],
 
-                     'ext_3' =>  $value['ext_3'],
 
-                     'num' =>  $value['num'],
 
-                     'crt_time' => date("Y-m-d",$value['crt_time']),
 
-                     'top_order_no' => $value['top_order_no'],
 
-                     'customer_name' => $map[$value['order_no']] ?? "",
 
-                 ];
 
-             }
 
-         }
 
-         return [true, array_values($return)];
 
-     }
 
-     public function transportNo(){
 
-         $list = Box::where('del_time',0)->where('shipment_order_no','<>','')->groupBy('shipment_order_no')->pluck('shipment_order_no')->toArray();
 
-         return [true,$list];
 
-     }
 
-     public function delBoxDetail($data){
 
-         if(empty($data['order_nos'])) return [false, '请选择包装单!'];
 
-         $order_nos = $data['order_nos'];
 
-         try {
 
-             DB::beginTransaction();
 
-             //更新销售订单 生产订单数据
 
-             $orders_product_id_map = [];
 
-             foreach ($order_nos as $v){
 
-                 $list = self::$box_hook->delBox($v);
 
-                 if(empty($list)) continue;
 
-                 foreach ($list as $vv){
 
-                     SaleOrdersProduct::where('id',$vv['top_id'])->update([
 
-                         'box_num' => DB::raw('box_num - '.$vv['num']),
 
-                         'finished_num' => DB::raw('finished_num - '.$vv['num'])
 
-                     ]);
 
-                     OrdersProduct::where('id',$vv['orders_product_id'])->update([
 
-                         'box_num' => DB::raw('box_num - '.$vv['num']),
 
-                         'finished_num' => DB::raw('finished_num - '.$vv['num'])
 
-                     ]);
 
-                     if(isset($orders_product_id_map[$vv['orders_product_id']])){
 
-                         $num = bcadd($orders_product_id_map[$vv['orders_product_id']], $vv['num'],3);
 
-                         $orders_product_id_map[$vv['orders_product_id']] = $num;
 
-                     }else{
 
-                         $orders_product_id_map[$vv['orders_product_id']] = $vv['num'];
 
-                     }
 
-                 }
 
-             }
 
-             //更新完工数据 和 底表数据
 
-             list($status,$msg) = $this->updateFinishDel($orders_product_id_map);
 
-             if(! $status) return [false, $msg];
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function updateFinishDel($orders_product_id_map){
 
-         if(empty($orders_product_id_map)) return [true,''];
 
-         $production_list = array_keys($orders_product_id_map);
 
-         $dispatch = $this->getDispatch($production_list, $orders_product_id_map);
 
-         if(empty($dispatch)) return [true,''];
 
-         try {
 
-             DB::beginTransaction();
 
-             //根据派工数据回写
 
-             foreach ($dispatch as $value){
 
-                 $finished_num = bcsub($value['finished_num'], $value['out_number'],3);
 
-                 DispatchSub::where('id',$value['id'])->update([
 
-                     'finished_num' => $finished_num
 
-                 ]);
 
-                 $quantity = $value['out_number'] * 1000;
 
-                 //工序表
 
-                 $process_model = new OrdersProductProcess(['channel' => date("Ymd",$value['out_order_no_time'])]);
 
-                 $process_model->where('order_product_id',$value['order_product_id'])
 
-                     ->where('process_id',$value['process_id'])
 
-                     ->where('dispatch_no',$value['dispatch_no'])
 
-                     ->where('status', 2)
 
-                     ->take($quantity)
 
-                     ->update([
 
-                         'finished_time' => 0,
 
-                         'status' => 1,
 
-                         'finished_id' => 0,
 
-                         'team_id' => 0,
 
-                         'equipment_id' => 0,
 
-                     ]);
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getLine().':'.$exception->getMessage() . ':' . $exception->getFile()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function getDispatch($order_product_id,$map){
 
-         $order = OrdersProduct::where('del_time',0)
 
-             ->where('id', $order_product_id)
 
-             ->select('id', 'process_id')
 
-             ->get()->toArray();
 
-         $order_map = [];
 
-         foreach ($order as $value){
 
-             $order_map[$value['id']] = explode(',', $value['process_id']);
 
-         }
 
-         //组织派工数据
 
-         $dispatch = DispatchSub::where('del_time',0)
 
-             ->whereIn('order_product_id', $order_product_id)
 
-             ->where('finished_num','>',0)
 
-             ->get()->toArray();
 
-         if(empty($dispatch)) return [];
 
-         $dispatch_map = [];
 
-         foreach ($dispatch as $value){
 
-             $key = $value['order_product_id'] . '|' .  $value['crt_time']; //同一次下的派工单
 
-             $dispatch_map[$key][] = $value;
 
-         }
 
-         $result = []; $tmp = [];
 
-         foreach ($dispatch_map as $key => $value){
 
-             foreach ($value as $v){
 
-                 $process = $order_map[$v['order_product_id']] ?? [];
 
-                 $num = $map[$v['order_product_id']];
 
-                 if(in_array($v['process_id'], $process)){
 
-                     $str = $v['order_product_id'] . $v['process_id'];
 
-                     $tmp_v = $tmp[$str] ?? 0;
 
-                     $num = bcsub($num, $tmp_v,3);
 
-                     if($num <= 0) continue;
 
-                     if($v['finished_num'] >= $num){
 
-                         $v['out_number'] = $num;
 
-                         $result[] = $v;
 
-                     }else{
 
-                         $v['out_number'] = $v['finished_num'];
 
-                         $result[] = $v;
 
-                     }
 
-                     if(isset($tmp[$str])){
 
-                         $num = bcadd($tmp[$str], $v['out_number'],3);
 
-                         $tmp[$str] = $num;
 
-                     }else{
 
-                         $tmp[$str] = $v['out_number'];
 
-                     }
 
-                 }
 
-             }
 
-         }unset($tmp);
 
-         return $result;
 
-     }
 
-     public function boxAdd($data)
 
-     {
 
-         list($status,$msg) = $this->boxAddRule($data);
 
-         if(! $status) return [false,$msg];
 
-         try{
 
-             DB::beginTransaction();
 
-             $box = new Box();
 
-             $box->order_no = $msg['order_no'];
 
-             $box->save();
 
-             DB::table('box_detail')->insert($msg['detail']);
 
-             DB::commit();
 
-             return [true,''];
 
-         }catch (\Exception $e){
 
-             DB::rollBack();
 
-             return [false,$e->getLine().':'.$e->getMessage()];
 
-         }
 
-     }
 
-     public function boxAddRule($data)
 
-     {
 
-         if(empty($data['param'])) return [false,'请选择包装数据'];
 
- //        $order_no = (new BoxHookService())->setOrderNo();
 
-         $order_no = "";
 
-         if(empty($order_no)) return [false,'包装单号不能为空'];
 
-         $detail = [];
 
-         foreach ($data['param'] as $value) {
 
-             $key = $this->lock_key.'_'.$value['idlsid'];
 
-             list($status,$msg) = $this->limitingSendRequestBackg($key);
 
-             if(! $status) return [false,$msg];
 
- //            if(empty($value['submit_quantity']) || ! is_numeric($value['submit_quantity']) || $value['submit_quantity'] > $value['quantity']) return [false,'包装数量错误'];
 
-             if(empty($value['team_id'])) return [false,'班组必须选择'];
 
-             $tmp = $value;
 
-             $tmp['iquantity'] = $value['submit_quantity'];
 
-             $detail[] = [
 
-                 'order_no' => $order_no,
 
-                 'out_order_no' => $value['csocode']??'',
 
-                 'crt_time' => time(),
 
-                 'upd_time' => time(),
 
-                 'type' => 1,
 
-                 'ext_1' => $value['cinvcode'],
 
-                 'ext_2' => $value['technology_material'],
 
-                 'ext_3' => $value['cfree1'] ?? "",
 
-                 'ext_4' => $value['cfree2'] ?? "",
 
-                 'ext_5' => $value['process_mark'],
 
-                 'ext_6' => $value['customer_name'] ?? "",
 
-                 'ext_7' => $value['cuscode'] ?? "",
 
-                 'ext_8' => $value['product_title'],
 
-                 'ext_9' => $value['product_size'],
 
-                 'ext_10' => $value['table_header_mark'],
 
-                 'ext_11' => $value['table_body_mark'],
 
-                 'top_id' => $value['idlsid'],
 
-                 'state' => 0,
 
-                 'num' => $value['submit_quantity'],
 
-                 'box_type' => 2,
 
-                 'team_id' => $value['team_id'],
 
-                 'shipment_order_no' => $value['cdlcode'],
 
-                 'post' => json_encode($tmp),
 
-             ];
 
-         }
 
-         return [true,['order_no' => $order_no, 'detail' => $detail]];
 
-     }
 
-     public function boxFhDetail($data){
 
-         if(empty($data['idlsid'])) return [false,'请选择数据'];
 
-         $result = DB::table('box_detail')->where('del_time',0)
 
-             ->where('top_id',$data['idlsid'])
 
-             ->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')
 
-             ->get()->toArray();
 
-         if(! empty($result)){
 
-             foreach ($result as $key => $value){
 
-                 $result[$key] = (array)$value;
 
-             }
 
-         }
 
-         return [true,$result];
 
-     }
 
-     public function boxFhDel($data){
 
-         if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
 
-         $box = Box::whereIn('order_no',$data['order_nos'])->update(['del_time' => time()]);
 
-         DB::table('box_detail')->whereIn('order_no',$data['order_nos'])->where('del_time',0)->update([
 
-             'del_time' => time()
 
-         ]);
 
-         return [true,''];
 
-     }
 
-     public function transportDetail($data){
 
-         if(empty($data['order_nos'])) return [false,'包装单号不能为空'];
 
-         $order_nos = $data['order_nos'];
 
-         $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();
 
-         foreach ($list as &$v){
 
-             $v['product_no'] = $v['ext_1'];
 
-             $v['technology_material'] = $v['ext_2'];
 
-             $v['technology_name'] = $v['ext_3'];
 
-             $v['wood_name'] = $v['ext_4'];
 
-             $v['process_mark'] = $v['ext_5'];
 
-             $v['customer_no'] = $v['ext_6'];
 
-             $v['customer_name'] = $v['ext_7'];
 
-             $v['product_title'] = $v['ext_8'];
 
-             $v['product_size'] = $v['ext_9'];
 
-             $v['post'] = json_decode($v['post'],true);
 
-         }
 
-         return [200,$list];
 
-     }
 
-     public function boxFhBzDetail($data){
 
-         if(empty($data['order_no'])) return [false,'请选择包装单数据'];
 
-         $result = DB::table('box_detail')->where('del_time',0)
 
-             ->whereIn('order_no',$data['order_no'])
 
-             ->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')
 
-             ->get()->toArray();
 
-         if(! empty($result)){
 
-             foreach ($result as $key => $value){
 
-                 $result[$key] = (array)$value;
 
-             }
 
-             $map = Team::where('id',array_unique(array_column($result,'team_id')))->pluck('title','id')->toArray();
 
-             foreach ($result as $key => $value){
 
-                 $result[$key]['team_name'] = $map[$value['team_id']] ?? '';
 
-             }
 
-         }
 
-         return [true,$result];
 
-     }
 
-     public function boxFhBzList($data){
 
-         if(empty($data['shipment_order_no'])) return [false, '请输入发货单号'];
 
-         $model = DB::table('box_detail')->where('del_time',0)
 
-             ->where('shipment_order_no','LIKE', '%'.$data['shipment_order_no'].'%')
 
-             ->select('order_no','crt_time','shipment_order_no');
 
-         $return = [];
 
-         $result = $model->get()->toArray();
 
-         if(! empty($result)){
 
-             foreach ($result as $value){
 
-                 $return[$value->order_no] = [
 
-                     'order_no' => $value->order_no,
 
-                     'shipment_order_no' => $value->shipment_order_no,
 
-                     'crt_time' => date("Y-m-d H:i:s",$value->crt_time)
 
-                 ];
 
-             }
 
-         }
 
-         return [true,array_values($return)];
 
-     }
 
-     public function boxOrderDetailByOrderNoNew($data){
 
-         if(empty($data['order_nos'])) return [false, '包装单号不能为空'];
 
-         $order_nos = $data['order_nos'];
 
-         $order_nos = array_unique($order_nos);
 
-         Log::channel('sendData')->info('发货:扫码包装单数据', ["param" => $order_nos]);
 
-         $list = Box::where('del_time',0)
 
-             ->whereIn('order_no',$order_nos)
 
-             ->where('shipment_order_no','')
 
-             ->select('order_no','out_order_no','top_order_no','shipment_order_no as transport_no','upd_time')
 
-             ->get()->toArray();
 
-         $detail_list = [];
 
-         foreach ($list as $v){
 
- //            if(! empty($v['transport_no'])) return [false, "包装单号:" . $v['order_no'] . "已在发货单(" . $v['transport_no'] .")中发出!"];
 
-             $model = new BoxDetail(['channel'=>$v['top_order_no']]);
 
-             $detail_list = array_merge($detail_list,$model->where('order_no',$v['order_no'])->where('top_order_no',$v['top_order_no'])->get()->toArray());
 
-         }
 
-         $return = [];
 
-         foreach ($detail_list as $value){
 
-             //销售订单号  产品编码  颜色
 
-             $return[] = [
 
-                 'ext_1' =>  $value['ext_1'],
 
-                 'ext_3' =>  $value['ext_3'],
 
-                 'ext_8' =>  $value['ext_8'],
 
-                 'num' =>  $value['num'],
 
-                 'out_order_no' => $value['out_order_no'],
 
-                 'box_no' => $value['order_no']
 
-             ];
 
-         }
 
-         return [true, array_values($return)];
 
-     }
 
-     public function boxList($data,$user){
 
-         $model = Box::where('del_time',0)
 
-             ->select('id','order_no','out_order_no','top_order_no','crt_time','status','shipment_order_no')
 
-             ->orderBy('id','desc');
 
-         if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
 
-         if(! empty($data['shipment_order_no'])) $model->where('shipment_order_no', 'LIKE', '%'.$data['shipment_order_no'].'%');
 
-         if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
 
-         if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
 
-         if(isset($data['status'])) $model->where('status',$data['status']);
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->fillBoxListData($list, $data,$user);
 
-         return [true, $list];
 
-     }
 
-     public function fillBoxListData($data, $erg,$user){
 
-         if(empty($data['data'])) return $data;
 
-         $product = $material = [];
 
-         if(! empty($erg['box'])){
 
-             //获取包装单里的产品
 
-             $id = array_column($data['data'], 'id');
 
-             list($product,$product_no) = $this->getBoxDetail($id);
 
-             if(! empty($erg['box_material'])){
 
-                 //产品的包装材料
 
-                 $service = new FyyOrderService();
 
-                 list($status, $msg) = $service->getProductBzDataFromSqlServer(['product_no' => $product_no], $user);
 
-                 if($status) $material = $msg;
 
-             }
 
-         }
 
-         foreach ($data['data'] as $key => $value){
 
-             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
 
-             $data['data'][$key]['status_title'] = Box::$status_name[$value['status']] ?? "";
 
-             $p = $product[$value['id']] ?? [];
 
-             if(! empty($p)){
 
-                 foreach ($p as $k => $v){
 
-                     $p[$k]['box_material'] = $material[$v['product_no']] ?? [];
 
-                 }
 
-             }
 
-             $data['data'][$key]['product'] = $p;
 
-         }
 
-         return $data;
 
-     }
 
-     public function getBoxDetail($id = []){
 
-         $return = $product_no= [];
 
-         if(empty($id)) return [$return,$product_no];
 
-         $box = Box::where('del_time',0)
 
-             ->whereIn('id', $id)
 
-             ->select('id', 'order_no','top_order_no')
 
-             ->get()->toArray();
 
-         if (empty($box)) return [$return,$product_no];
 
-         $map = array_column($box,'id','order_no');
 
-         $result = [];
 
-         foreach ($box as $v){
 
-             $model = new BoxDetail(['channel'=>$v['top_order_no']]);
 
-             $result = array_merge($result,$model->where('del_time',0)
 
-                 ->where('order_no',$v['order_no'])
 
-                 ->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());
 
-         }
 
-         foreach ($result as $value){
 
-             $value['id'] = $map[$value['order_no']] ?? 0;
 
-             $value['product_unit'] = "吨";
 
-             $return[$value['id']][] = $value;
 
-             if(! in_array($value['product_no'], $product_no)) $product_no[] = $value['product_no'];
 
-         }
 
-         return [$return, $product_no];
 
-     }
 
-     public function searchDetail($data){
 
-         $out_order_no = $data['out_order_no'] ?? '';
 
-         $box = Box::where('del_time',0)
 
-             ->where('out_order_no', 'LIKE', '%'. $out_order_no .'%')
 
-             ->select('top_order_no')
 
-             ->get()->toArray();
 
-         if(empty($box)) return [];
 
-         $order_no = [];
 
-         foreach ($box as $value){
 
-             $model = new BoxDetail(['channel'=> $value['top_order_no']]);
 
-             $result = $model->where('del_time',0)
 
-                 ->where('order_no',$box['order_no'])
 
-                 ->select('order_no')
 
-                 ->get()->toArray();
 
-             $detail_list = array_merge($order_no, array_column($result, 'order_no'));
 
-         }
 
-         $result = BoxDetail::where('del_time',0)
 
-             ->when(! empty($out_order_no), function ($query) use ($out_order_no) {
 
-                 return $query->where('out_order_no', 'LIKE', '%'. $out_order_no .'%');
 
-             })
 
-             ->when(! empty($shipment_order_no), function ($query) use ($shipment_order_no) {
 
-                 return $query->where('shipment_order_no', 'LIKE', '%'. $shipment_order_no .'%');
 
-             })
 
-             ->select('order_no')
 
-             ->get()->toArray();
 
-         return array_unique(array_column($result,'order_no'));
 
-     }
 
-     public function boxFhDetailNew($data){
 
-         if(empty($data['id'])) return [false,'请选择数据'];
 
-         $box = Box::where('id', $data['id'])->where('del_time',0)->first();
 
-         if(empty($box)) return [false, '包装单不存在或已被删除'];
 
-         $box = $box->toArray();
 
-         $model = new BoxDetail(['channel'=>$box['top_order_no']]);
 
-         $result = $model->where('del_time',0)
 
-             ->where('order_no',$box['order_no'])
 
-             ->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();
 
-         foreach ($result as $key => $value){
 
-             $result[$key]['product_unit'] = "吨";
 
-         }
 
-         return [true,$result];
 
-     }
 
- }
 
 
  |