| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204 | <?phpnamespace App\Service;use App\Model\ApplyOrder;use App\Model\ApplyOrderDetail;use App\Model\Box;use App\Model\BoxDetail;use App\Model\Dispatch;use App\Model\DispatchSub;use App\Model\Employee;use App\Model\EmployeeTeamPermission;use App\Model\InOutRecord;use App\Model\OrdersProduct;use App\Model\Process;use App\Model\ReportWorking;use App\Model\ReportWorkingDetail;use App\Model\SaleOrdersProduct;use App\Model\ScrappCount;use App\Model\Team;use App\Service\Box\BoxService;use Illuminate\Support\Facades\DB;class ApplyOrderService extends Service{    public function MaterialEdit($data, $user){        list($status,$msg) = $this->MaterialRule($data,false);        if(!$status) return [$status,$msg];        DB::beginTransaction();        try{            $time = time();            $model = new ApplyOrder();            $model = $model->where('id',$data['id'])->first();            $model->apply_id = $data['apply_id'];            $model->apply_time = $data['apply_time'] ;            $model->mark = $data['mark'] ?? "";            $model->storehouse_id = $data['storehouse_id'];            $model->storehouse_title = $data['storehouse_title'] ?? "";            $model->type = $data['type'];            $model->save();            $id = $model->id;            ApplyOrderDetail::where('del_time',0)->where('apply_order_id',$id)->update([                'del_time' => $time            ]);            $detail_insert = [];            foreach ($data['order_data'] as $v){                $detail_insert[] = [                    'apply_order_id' => $id,                    'data_id' => $v['id'],                    'quantity' => $v['quantity'] ?? 0,                    'product_no' => $v['product_no'] ?? "",                    'product_title' => $v['product_title'] ?? "",                    'product_size' => $v['product_size'] ?? "",                    'product_unit' => $v['product_unit'] ?? "",                    'technology_name' => $v['technology_name'] ?? "", //颜色                    'top_product_title' => $v['top_product_title'] ?? "",                    'top_product_no' => $v['top_product_title'] ?? "",                    'type' => $data['type'],                    'storehouse_id' => $data['storehouse_id'],                    'crt_time' => $time,                ];            }            ApplyOrderDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true,''];    }    public function MaterialAdd($data,$user){        list($status,$msg) = $this->MaterialRule($data);        if(!$status) return [$status,$msg];        DB::beginTransaction();        try{            $time = time();            $model = new ApplyOrder();            $model->order_number = $data['order_number'];            $model->apply_id = $data['apply_id'];            $model->apply_time = $data['apply_time'] ;            $model->mark = $data['mark'] ?? "";            $model->storehouse_id = $data['storehouse_id'];            $model->storehouse_title = $data['storehouse_title'] ?? "";            $model->type = $data['type'];            $model->crt_id = $user['id'];            $model->save();            $id = $model->id;            $detail_insert = [];            foreach ($data['order_data'] as $v){                $detail_insert[] = [                    'apply_order_id' => $id,                    'data_id' => $v['id'],                    'quantity' => $v['quantity'] ?? 0,                    'product_no' => $v['product_no'] ?? "",                    'product_title' => $v['product_title'] ?? "",                    'product_size' => $v['product_size'] ?? "",                    'product_unit' => $v['product_unit'] ?? "",                    'technology_name' => $v['technology_name'] ?? "", //颜色                    'top_product_title' => $v['top_product_title'] ?? "",                    'top_product_no' => $v['top_product_title'] ?? "",                    'type' => $data['type'],                    'storehouse_id' => $data['storehouse_id'],                    'crt_time' => $time,                ];            }            ApplyOrderDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, ''];    }    public function MaterialDel($data){        if($this->isEmpty($data,'id')) return [false,'ID必须!'];        $apply = ApplyOrder::where('id',$data['id'])->first();        if($apply->del_time > 0) return [false,'申请单不存在或已被删除'];        if($apply->state != ApplyOrder::state_zero) return [false, '申请单已审核,删除失败'];        $time = time();        try {            DB::beginTransaction();            $apply->del_time = $time;            $apply->save();            ApplyOrderDetail::where('del_time', 0)->where('apply_order_id',$data['id'])->update([                'del_time'=>time()            ]);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true,''];    }    public function MaterialList($data){        $model = ApplyOrder::where('del_time',0)            ->select('*')            ->orderBy('id','desc');        if(isset($data['status'])) $model->where('status', $data['status']);        if(! empty($data['type'])) $model->where('type', $data['type']);        if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');        if(! empty($data['apply_id'])) $model->where('apply_id', $data['apply_id']);        if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);        if(! empty($data['apply_time'][0]) && ! empty($data['apply_time'][1])) $model->whereBetween('apply_time',[$data['apply_time'][0],$data['apply_time'][1]]);        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);        if(! empty($data['dispatch_no'])){            $dispatch_id = DispatchSub::where('del_time',0)                ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')                ->select('id')                ->get()->toArray();            $dispatch_id = array_column($dispatch_id,'id');            $detail = ApplyOrderDetail::where('del_time',0)                ->where('type', $data['type'])                ->whereIn('data_id', $dispatch_id)                ->select('apply_order_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));        }        if(! empty($data['box_no'])){            $box_id = Box::where('del_time',0)                ->where('order_no', 'LIKE', '%'.$data['box_no'].'%')                ->select('id')                ->get()->toArray();            $box_id = array_column($box_id,'id');            $detail = ApplyOrderDetail::where('del_time',0)                ->where('type', $data['type'])                ->whereIn('data_id', $box_id)                ->select('apply_order_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));        }        if(! empty($data['sale_order_number'])){            if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){                $dispatch_id = DispatchSub::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('id')                    ->get()->toArray();                $dispatch_id = array_column($dispatch_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $dispatch_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }elseif($data['type'] == ApplyOrder::type_five){                $zj_id = ScrappCount::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('id')                    ->get()->toArray();                $zj_id = array_column($zj_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $zj_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }else{                $no = SaleOrdersProduct::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('order_no')                    ->get()->toArray();                $no = array_unique(array_column($no,'order_no'));                $box_id = Box::where('del_time',0)                    ->whereIn('top_order_no', $no)                    ->select('id')                    ->get()->toArray();                $box_id = array_column($box_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $box_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }        }        $list = $this->limit($model,'',$data);        $list = $this->fillData($list);        return [true,$list];    }    public function fillData($data){        if(empty($data['data'])) return $data;        $emp = Employee::whereIn('id',array_merge_recursive(array_unique(array_column($data['data'],'crt_id')), array_unique(array_column($data['data'],'apply_id'))))            ->pluck('emp_name','id')            ->toArray();        $array1 = $array2 = $array3 = [];        foreach ($data['data'] as $value){            if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){                $array1[] = $value['id'];            }elseif ($value['type'] == ApplyOrder::type_five){                $array3[] = $value['id'];            }else{                $array2[] = $value['id'];            }        }        $order1 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])            ->whereIn('apply_order_id', $array1)            ->select('data_id','apply_order_id')            ->get()->toArray();        $order1_map = [];        foreach ($order1 as $value){            $order1_map[$value['apply_order_id']][] = $value['data_id'];        }        $order2 = ApplyOrderDetail::whereNotIn('type',[ApplyOrder::type_one,ApplyOrder::type_two,ApplyOrder::type_five])            ->whereIn('apply_order_id', $array2)            ->select('data_id','apply_order_id')            ->get()->toArray();        $order2_map = [];        foreach ($order2 as $value){            $order2_map[$value['apply_order_id']][] = $value['data_id'];        }        $order3 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_five])            ->whereIn('apply_order_id', $array3)            ->select('data_id','apply_order_id')            ->get()->toArray();        $order3_map = [];        foreach ($order3 as $value){            $order3_map[$value['apply_order_id']][] = $value['data_id'];        }        $map1 = DispatchSub::whereIn('id', array_unique(array_column($order1,'data_id')))            ->pluck('out_order_no','id')            ->toArray();        $map2 = Box::whereIn('id', array_unique(array_column($order2,'data_id')))            ->pluck('top_order_no','id')            ->toArray();        $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))            ->pluck('out_order_no','order_no')            ->toArray();        $map3 = ScrappCount::whereIn('id', array_unique(array_column($order3,'data_id')))            ->pluck('out_order_no','id')            ->toArray();        foreach ($data['data'] as $key => $value){            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';            $data['data'][$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';            $data['data'][$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';            $data['data'][$key]['apply_name'] = $emp[$value['apply_id']] ?? '';            if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){                $order_tmp = $order1_map[$value['id']] ?? [];                $order_tmp = array_unique($order_tmp);                $sale_order_number = [];                foreach ($order_tmp as $v){                    $str = $map1[$v] ?? '';                    if(empty($str)) continue;                    $sale_order_number[] = $str;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data['data'][$key]['sale_order_number'] = $sale_order_number;            }elseif ($value['type'] == ApplyOrder::type_five){                $order_tmp = $order3_map[$value['id']] ?? [];                $order_tmp = array_unique($order_tmp);                $sale_order_number = [];                foreach ($order_tmp as $v){                    $str = $map3[$v] ?? '';                    if(empty($str)) continue;                    $sale_order_number[] = $str;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data['data'][$key]['sale_order_number'] = $sale_order_number;            }else{                $order_tmp = $order2_map[$value['id']] ?? [];                $order_tmp = array_unique($order_tmp);                $sale_order_number = [];                foreach ($order_tmp as $v){                    $m2_t = $map2[$v] ?? '';                    $m2_t = $map2_fin[$m2_t] ?? "";                    if(empty($m2_t)) continue;                    $sale_order_number[] = $m2_t;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data['data'][$key]['sale_order_number'] = $sale_order_number;            }        }        return $data;    }    public function MaterialDetail($data){        if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];        $id = $data['id'];        $detail = ApplyOrder::where('del_time',0)            ->where('id',$id)            ->first();        if(empty($detail)) return [false,'申请单不存在或已被删除'];        $detail = $detail->toArray();        $detail['apply_time'] = $detail['apply_time'] ? date('Y-m-d',$detail['apply_time']) : '';        $detail['apply_title'] = Employee::where('id', $detail['apply_id'])->value('emp_name');        $apply_d = ApplyOrderDetail::where('del_time', 0)            ->where('apply_order_id', $id)            ->get()->toArray();        $apply_id = array_column($apply_d, 'data_id');        $d = [];        if($detail['type'] == ApplyOrder::type_one){            $d = DispatchSub::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')                ->get()->toArray();            $args = "";            foreach ($d as $value){                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";            }            $args = rtrim($args, 'OR ');            $d_no_map = [];            $d_no = DispatchSub::where('del_time',0)                ->whereRaw($args)                ->select('dispatch_no as order_no','order_product_id','crt_time')                ->get()->toArray();            foreach ($d_no as $value){                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }else{                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }            }        }elseif ($detail['type'] == ApplyOrder::type_two){            $d = DispatchSub::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')                ->get()->toArray();            $args = "";            foreach ($d as $value){                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";            }            $args = rtrim($args, 'OR ');            $d_no_map = [];            $d_no = DispatchSub::where('del_time',0)                ->whereRaw($args)                ->select('dispatch_no as order_no','order_product_id','crt_time')                ->get()->toArray();            foreach ($d_no as $value){                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }else{                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }            }        }elseif ($detail['type'] == ApplyOrder::type_three){            $d = Box::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_no')                ->get()->toArray();        }elseif ($detail['type'] == ApplyOrder::type_four){            $d = Box::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_no')                ->get()->toArray();        }elseif ($detail['type'] == ApplyOrder::type_five){ //质检入库申请            $d = ScrappCount::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')                ->get()->toArray();        }        $d_tmp = array_column($d,null,'id');        $return = [];        foreach ($apply_d as $t){            $tmp = $d_tmp[$t['data_id']] ?? [];            $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";            $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";            $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";            $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";            if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){                $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);            }else{                $order_no = $tmp['order_no'] ?? "";            }            if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){                $product_unit = "吨";            }else{                $product_unit = $t['product_unit'] ?? "";            }            $return[] = [                'id' => $t['data_id'] ?? 0,                'quantity' => $t['quantity'] ?? 0,                'product_no' => $t['product_no'] ?? "",                'product_title' => $t['product_title'] ?? "",                'product_size' => $t['product_size'] ?? "",                'product_unit' => $product_unit,                'top_product_title' => $top_product_title,                'top_product_no' => $top_product_no,                'technology_name' => $technology_name, //颜色                'wood_name' => $wood_name,                'order_no' => $order_no,            ];        }        $detail['order_data'] = $return;        return [true, $detail];    }    public function MaterialRule(&$data,$is_add = true){        if($this->isEmpty($data,'apply_id')) return [false,'申请人不能为空'];        if($this->isEmpty($data,'apply_time')) return [false,'申请时间不能为空'];        if($this->isEmpty($data,'storehouse_id')) return [false,'仓库不能为空'];        if($this->isEmpty($data,'type')) return [false,'申请单类型不能为空'];        if(empty($data['order_data'])) return [false, '申请单详细信息不能为空'];        foreach ($data['order_data'] as $value){            if(empty($value['id'])) return [false, '申请单详细信息ID不能为空'];            if($data['type'] != ApplyOrder::type_three){                if(empty($value['quantity'])) return [false, '申请单详细信息数量不能为空'];            }        }        $id = array_unique(array_column($data['order_data'],'id'));        if($data['type'] == ApplyOrder::type_one){            $dispatch = DispatchSub::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'dispatch_no', 'status')                ->get()->toArray();            if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];        }elseif ($data['type'] == ApplyOrder::type_two){            $dispatch = DispatchSub::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'dispatch_no', 'status')                ->get()->toArray();            if(count($dispatch) != count($id)) return [false, '完工单信息错误,请重新选择'];        }elseif ($data['type'] == ApplyOrder::type_three){            $box = Box::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'order_no', 'status')                ->get()->toArray();            if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];        }elseif ($data['type'] == ApplyOrder::type_four){            $box = Box::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'order_no', 'status')                ->get()->toArray();            if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];        }elseif ($data['type'] == ApplyOrder::type_five){            $scrapp = ScrappCount::where('del_time',0)                ->whereIn('id', $id)                ->select('id', 'order_no', 'status')                ->get()->toArray();            if(count($scrapp) != count($id)) return [false, '质检单信息错误,请重新选择'];        }else{            return [false, '申请单类型错误'];        }        if($is_add){            $data['order_number'] = $this->setOrderNO($data['type']);            if(empty($data['order_number'])) return [false, '申请单唯一标识生成失败'];        }else{            if(empty($data['id'])) return [false, '申请单唯一标识不能为空'];            if(empty($data['order_number'])) return [false, '申请单唯一标识不能为空'];            $apply = ApplyOrder::where('id', $data['id'])->where('del_time', 0)->first();            if(empty($apply)) return [false, '申请单不存在或已被删除'];            if($apply->status != 0) return [false, '申请单已审核,编辑失败'];        }        return [true,''];    }    public function setOrderNO($type = "",$time = 0){        if(empty($time)){            $str = date('Ymd',time());        }else{            $str = date('Ymd',$time);        }        $order_number = ApplyOrder::where('order_number','Like','%'. $str . '%')            ->where('type', $type)            ->max('order_number');        if(empty($order_number)){            $number = str_pad(1,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }else{            $tmp = substr($order_number, -3);            $tmp = $tmp + 1;            //超过999            if(strlen($tmp) > 3) return '';            $number = str_pad($tmp,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }        return $number;    }    public function createSQ($data, $user, $type = 0, $status = 1,$time = 0){        if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];        if($type == ApplyOrder::type_one){            $storehouse_id = "001";            $storehouse_title = "原料仓";        }elseif ($type == ApplyOrder::type_two){            $storehouse_id = "004";            $storehouse_title = "待清洗原材料仓";        }elseif ($type == ApplyOrder::type_three){            $storehouse_id = "002";            $storehouse_title = "产成品仓";        }elseif($type == ApplyOrder::type_four){            $storehouse_id = "003";            $storehouse_title = "包材、辅料仓" ;        }        try{            DB::beginTransaction();            $order_number = $this->setOrderNO($type, $time);//            list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type);//            if(! $status) return [false, $msg];            if(empty($time)) $time = time();            $model = new ApplyOrder();            $model->order_number = $order_number;            $model->apply_id = $user['id'];            $model->apply_time = $time;            $model->storehouse_id = $storehouse_id;            $model->storehouse_title = $storehouse_title;            $model->type = $type;            $model->status = $status; //是否审核            $model->save();            $id = $model->id;            $detail_insert = [];            foreach ($data as $v){                $detail_insert[] = [                    'apply_order_id' => $id,                    'data_id' => $v['id'],                    'quantity' => $v['quantity'] ?? 0,                    'product_no' => $v['product_no'] ?? "",                    'product_title' => $v['product_title'] ?? "",                    'product_size' => $v['product_size'] ?? "",                    'product_unit' => $v['product_unit'] ?? "",                    'technology_name' => $v['technology_name'] ?? "",                    'wood_name' => $v['wood_name'] ?? "",                    'top_product_no' => $v['top_product_no'] ?? "",                    'top_product_title' => $v['top_product_title'] ?? "",                    'type' => $type,                    'storehouse_id' => $storehouse_id,                    'crt_time' => $time,                ];            }            ApplyOrderDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, $id];    }    public function createSqFive($data, $user, $type = 0, $status = 1, $time = 0){        if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];        try{            DB::beginTransaction();            if(empty($time)) $time = time();            $first = $data[0] ?? [];            $order_number = $this->setOrderNO($type, $time);            $model = new ApplyOrder();            $model->order_number = $order_number;            $model->apply_id = $user['id'];            $model->apply_time = $time;            $model->storehouse_id = $first['storehouse_id'];            $model->storehouse_title = $first['storehouse_title'];            $model->type = $type;            $model->status = $status; //是否审核            $model->save();            $id = $model->id;            $insert = [];            foreach ($data as $value){                $insert[] = [                    'apply_order_id' => $id,                    'data_id' => $value['id'],                    'quantity' => $value['quantity'] ?? 0,                    'product_no' => $value['product_no'] ?? "",                    'product_title' => $value['product_title'] ?? "",                    'product_size' => $value['product_size'] ?? "",                    'product_unit' => $value['product_unit'] ?? "",                    'technology_name' => $value['technology_name'] ?? "",                    'wood_name' => $value['wood_name'] ?? "",                    'top_product_no' => $value['top_product_no'] ?? "",                    'top_product_title' => $value['top_product_title'] ?? "",                    'type' => $type,                    'storehouse_id' => $value['storehouse_id'],                    'crt_time' => $time,                ];            }            ApplyOrderDetail::insert($insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, ''];    }    //流水    public function createRecord($id){return [true, ''];        $record = ApplyOrderDetail::where('apply_order_id', $id)            ->where('del_time',0)            ->get()->toArray();        if(empty($record)) return [false, '申请单明细数据不存在或已被删除'];        try{            DB::beginTransaction();            $time = time();            $insert = [];            foreach ($record as $value){                if(empty($value['product_no'])) continue;                if($value['type'] == ApplyOrder::type_one){                    $number = - $value['quantity'];                }else{                    $number = $value['quantity'];                }                $insert[] = [                    'apply_order_id' => $id,                    'product_no' => $value['product_no'],                    'number' => $number,                    'type' => $value['type'],                    'storehouse_id' => $value['storehouse_id'],                    'crt_time' => $time,                ];            }            if(! empty($insert)) InOutRecord::insert($insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, ''];    }    //报工    public function reportWorkingEdit($data, $user){        list($status,$msg) = $this->reportWorkingRule($data,$user,false);        if(!$status) return [$status,$msg];        DB::beginTransaction();        try{            $time = time();            $model = new ReportWorking();            $model = $model->where('id',$data['id'])->first();            $model->report_time = $data['report_time'];            $model->mark = $data['mark']?? "";            $model->status = $data['auto'] ?? 0;            $model->save();            $id = $model->id;            ReportWorkingDetail::where('del_time',0)->where('report_working_id',$id)->update([                'del_time' => $time            ]);            //班组下的人            $team_man = [];            $model_t = new EmployeeTeamPermission();            $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();            foreach ($team_array as $v){                $team_man[$v['team_id']][] = $v['employee_id'];            }            $detail_insert = [];            foreach ($data['order_data'] as $v){                $t = $team_man[$v['team_id']] ?? [];                if(! empty($t)){                    foreach ($t as $t_v){                        $detail_insert[] = [                            'report_working_id' => $id,                            'data_id' => $v['id'],                            'quantity' => $v['quantity'] ?? 0,                            'process_id' => $v['process_id'] ?? 0,                            'finished_id' => $t_v,                            'team_id' => $v['team_id'],                            'crt_time' => $time,                        ];                    }                }else{                    $detail_insert[] = [                        'report_working_id' => $id,                        'data_id' => $v['id'],                        'quantity' => $v['quantity'] ?? 0,                        'process_id' => $v['process_id'] ?? 0,                        'finished_id' => 0,                        'team_id' => $v['team_id'],                        'crt_time' => $time,                    ];                }            }            ReportWorkingDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true,''];    }    public function reportWorkingAdd($data,$user){        list($status,$msg) = $this->reportWorkingRule($data,$user);        if(!$status) return [$status,$msg];        DB::beginTransaction();        try{            $time = time();            $model = new ReportWorking();            $model->order_number = $data['order_number'];            $model->report_time = $data['report_time'];            $model->mark = $data['mark']?? "";            $model->crt_id = $user['id'];            $model->status = $data['auto'] ?? 0;            $model->save();            $id = $model->id;            //班组下的人            $team_man = [];            $model_t = new EmployeeTeamPermission();            $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();            foreach ($team_array as $v){                $team_man[$v['team_id']][] = $v['employee_id'];            }            $detail_insert = [];            foreach ($data['order_data'] as $v){                $t = $team_man[$v['team_id']] ?? [];                if(! empty($t)){                    foreach ($t as $t_v){                        $detail_insert[] = [                            'report_working_id' => $id,                            'data_id' => $v['id'],                            'quantity' => $v['quantity'] ?? 0,                            'process_id' => $v['process_id'] ?? 0,                            'finished_id' => $t_v,                            'team_id' => $v['team_id'],                            'crt_time' => $time,                        ];                    }                }else{                    $detail_insert[] = [                        'report_working_id' => $id,                        'data_id' => $v['id'],                        'quantity' => $v['quantity'] ?? 0,                        'process_id' => $v['process_id'] ?? 0,                        'finished_id' => 0,                        'team_id' => $v['team_id'],                        'crt_time' => $time,                    ];                }            }            ReportWorkingDetail::insert($detail_insert);            DB::commit();        }catch (\Exception $e){            DB::rollBack();            return [false,$e->getMessage()];        }        return [true, ''];    }    public function reportWorkingDel($data){        if($this->isEmpty($data,'id')) return [false,'ID必须!'];        $apply = ReportWorking::where('id',$data['id'])->first();        if($apply->del_time > 0) return [false,'报工单不存在或已被删除'];        if($apply->state != ReportWorking::state_zero) return [false, '报工单已审核,删除失败'];        $time = time();        try {            DB::beginTransaction();            $apply->del_time = $time;            $apply->save();            ReportWorkingDetail::where('del_time', 0)->where('report_working_id',$data['id'])->update([                'del_time'=>time()            ]);            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage()];        }        return [true,''];    }    public function reportWorkingList($data){        $model = ReportWorking::where('del_time',0)            ->select('*')            ->orderBy('id','desc');        if(isset($data['status'])) $model->where('status', $data['status']);        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');        if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);        if(! empty($data['report_time'][0]) && ! empty($data['report_time'][1])) $model->whereBetween('report_time',[$data['report_time'][0],$data['report_time'][1]]);        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);        if(! empty($data['dispatch_no1'])) {            $dispatch_id = DispatchSub::where('del_time',0)                ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no1'].'%')                ->select('id')                ->get()->toArray();            $dispatch_id = array_column($dispatch_id,'id');            $detail = ReportWorkingDetail::where('del_time',0)                ->whereIn('data_id', $dispatch_id)                ->select('report_working_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));        }        if(! empty($data['dispatch_no'])) {            $dispatch_id = DispatchSub::where('del_time',0)                ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')                ->select('id')                ->get()->toArray();            $dispatch_id = array_column($dispatch_id,'id');            $detail = ReportWorkingDetail::where('del_time',0)                ->whereIn('data_id', $dispatch_id)                ->select('report_working_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));        }        if(! empty($data['sale_order_number'])){            $id = SaleOrdersProduct::where('del_time',0)                ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                ->select('id')                ->get()->toArray();            $dispatch_id = DispatchSub::where('del_time',0)                ->whereIn('sale_orders_product_id',array_column($id,'id'))                ->select('id')                ->get()->toArray();            $detail = ReportWorkingDetail::where('del_time',0)                ->whereIn('data_id', $dispatch_id)                ->select('report_working_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));        }        $list = $this->limit($model,'',$data);        $list = $this->reportWorkingfillData($list);        return [true,$list];    }    public function reportWorkingfillData($data){        if(empty($data['data'])) return $data;        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))            ->pluck('emp_name','id')            ->toArray();        $detail = ReportWorkingDetail::where('del_time',0)            ->whereIn('report_working_id', array_column($data['data'],'id'))            ->select('data_id','report_working_id')            ->get()->toArray();        $map = [];        foreach ($detail as $value){            $map[$value['data_id']] = $value['report_working_id'];        }        $dispatch = DispatchSub::whereIn('id',array_column($detail,'data_id'))            ->get()->toArray();        $map1 = [];        $team_map = Team::where("del_time",0)->pluck('title','id')->toArray();        $process_map = Process::where("del_time",0)->pluck('title','id')->toArray();        foreach ($dispatch as $value){            $report_working_id = $map[$value['id']] ?? 0;            $value['team_name'] = $team_map[$value['team_id']] ?? "";            $value['process_name'] = $process_map[$value['process_id']] ?? "";            $map1[$report_working_id][] = $value;        }        foreach ($data['data'] as $key => $value){            $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d',$value['report_time']) : '';            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';            $data['data'][$key]['status_title'] = ReportWorking::$state_name[$value['status']] ?? "";            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';            $data['data'][$key]['dispatch'] = $map1[$value['id']] ?? [];        }        return $data;    }    public function reportWorkingDetail($data){        if(empty($data['id']) && empty($data['order_number'])) return [false,'ID与OrderNumber不能为空!'];        $id = $data['id'] ?? 0;        $order_number = $data['order_number'] ?? "";        $detail = ReportWorking::where('del_time',0)            ->when(! empty($id), function ($query) use ($id){                return $query->where('id', $id);            })            ->when(! empty($order_number), function ($query) use ($order_number){                return $query->where('order_number', $order_number);            })            ->first();        if(empty($detail)) return [false,'报工单不存在或已被删除'];        $detail = $detail->toArray();        if(empty($id)) $id = $detail['id'];        $detail['report_time'] = $detail['report_time'] ? date('Y-m-d',$detail['report_time']) : '';        $apply_d = ReportWorkingDetail::where('del_time', 0)            ->where('report_working_id', $id)            ->get()->toArray();        $apply_id = array_column($apply_d, 'data_id');        //派工单        $d = DispatchSub::where('del_time',0)            ->whereIn('id', $apply_id)            ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','process_id','product_unit','price','product_size')            ->get()->toArray();        $d_tmp = array_column($d,null,'id');        $process_map = Process::whereIn('id',array_unique(array_column($d,'process_id')))            ->pluck('title','id')            ->toArray();        $team_map = Team::pluck('title','id')->toArray();        $return = [];        foreach ($apply_d as $t){            $tmp = $d_tmp[$t['data_id']] ?? [];            $str = $t['data_id'] . $t['process_id'];            if(! isset($return[$str])){                $return[$str] = [                    'id' => $t['data_id'] ?? 0,                    'quantity' => $t['quantity'] ?? 0,                    'product_no' => $tmp['product_no'] ?? "",                    'product_title' => $tmp['product_title'] ?? "",                    'product_size' => $tmp['product_size'] ?? "",                    'product_unit' => $tmp['product_unit'] ?? "",                    'technology_name' => $tmp['technology_name'] ?? "",                    'wood_name' => $tmp['wood_name'] ?? "",                    'order_no' => $tmp['order_no'] ?? "",                    'process_id' => $tmp['process_id'] ?? 0,                    'process_name' => $process_map[$tmp['process_id']] ?? "",                    'team_id' => $t['team_id'] ?? 0,                    'team_title' => $team_map[$t['team_id']] ?? '',                ];            }        }        $detail['order_data'] = array_values($return);        return [true, $detail];    }    public function reportWorkingRule(&$data,$user, $is_add = true){        if($this->isEmpty($data,'report_time')) return [false,'报工时间不能为空'];        if(empty($data['order_data'])) return [false, '报工单详细信息不能为空'];        foreach ($data['order_data'] as $value){            if(empty($value['id'])) return [false, '报工单详细信息ID不能为空'];            if(empty($value['quantity'])) return [false, '报工单详细信息数量不能为空'];            if(empty($value['process_id'])) return [false, '报工单详细信息工序不能为空'];            if(empty($value['team_id'])) return [false, '报工单详细信息班组不能为空'];        }        $id = array_unique(array_column($data['order_data'],'id'));        $dispatch = DispatchSub::where('del_time',0)            ->whereIn('id', $id)            ->select('id', 'dispatch_no', 'status', 'dispatch_quantity','order_product_id')            ->get()->toArray();        if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];        $count = array_unique(array_column($dispatch,'order_product_id'));        if(count($count) > 1) return [false, '批量报工时请选择同一生产订单下的派工单'];        if($is_add){            $data['order_number'] = $this->setOrderNO2();            if(empty($data['order_number'])) return [false, '报工单唯一标识生成失败'];        }else{            if(empty($data['id'])) return [false, '报工单唯一标识不能为空'];            if(empty($data['order_number'])) return [false, '报工单唯一标识不能为空'];            $apply = ReportWorking::where('id', $data['id'])->where('del_time', 0)->first();            if(empty($apply)) return [false, '报工单不存在或已被删除'];            if($apply->status != 0) return [false, '报工单已审核,编辑失败'];        }        list($status, $msg) = $this->compareQuantity($dispatch, $data);        if(! $status) return [false, $msg];        $em = new EmployeeService();        $data['auto'] = $em->is_auto($user, "wgbg_auto");        return [true,''];    }    public function compareQuantity($dispatch, $data){        $id = $data['id'] ?? 0;        $result = ReportWorkingDetail::where('del_time',0)            ->when(! empty($id), function ($query) use ($id){                return $query->where('report_working_id', '<>', $id);            })            ->where('data_id', array_column($dispatch, 'id'))            ->select('data_id as dispatch_id','quantity')            ->get()            ->toArray();        $map1 = [];        foreach ($result as $value){            if(isset($map1[$value['dispatch_id']])){                $tmp = bcadd($map1[$value['dispatch_id']], $value['quantity'], 3);                $map1[$value['dispatch_id']] = $tmp;            }else{                $map1[$value['dispatch_id']] = $value['quantity'];            }        }        $map2 = array_column($dispatch,null,'id');        foreach ($data['order_data'] as $value){            $tmp = $map2[$value['id']] ?? [];            $total = $tmp['dispatch_quantity'] ?? 0;            $dispatch_no = $tmp['dispatch_no'] ?? "";            $save = $map1[$value['id']] ?? 0;            $save_total = bcadd($value['quantity'], $save, 2);            if($save_total > $total) return [false, "派工单号:" . $dispatch_no . "的报工总数量不能超过" . $total . "吨,目前累计报工" . $save . "吨"];        }        return [true, ''];    }    public function setOrderNO2($time = 0){        if(empty($time)) {            $str = date('Ymd',time());        }else{            $str = date('Ymd', $time);        }        $model = new ReportWorking();        $order_number = $model->where('order_number','Like','%'. $str . '%')            ->max('order_number');        if(empty($order_number)){            $number = str_pad(1,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }else{            $tmp = substr($order_number, -3);            $tmp = $tmp + 1;            //超过999            if(strlen($tmp) > 3) return '';            $number = str_pad($tmp,3,'0',STR_PAD_LEFT);            $number = $str . $number;        }        return $number;    }    public function applyOrderReportList($data){        if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!'];        if(empty($data['type'])) return [false, 'TYPE不能为空!'];        $model = ApplyOrder::where('del_time',0)            ->where('apply_time',">=",$data['apply_time'][0])            ->where('apply_time',"<=",$data['apply_time'][1])            ->where('type',$data['type'])            ->select('id','order_number','apply_time','mark','status','storehouse_title')            ->orderBy('id','desc');        if(isset($data['status'])) $model->where('status', $data['status']);        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){            $model->where('crt_time',">=",$data['crt_time'][0]);            $model->where('crt_time',"<=",$data['crt_time'][1]);        }        if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');        if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');        if(! empty($data['sale_order_number'])){            if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){                $dispatch_id = DispatchSub::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('id')                    ->get()->toArray();                $dispatch_id = array_column($dispatch_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $dispatch_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }elseif($data['type'] == ApplyOrder::type_five){                $zj_id = ScrappCount::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('id')                    ->get()->toArray();                $zj_id = array_column($zj_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $zj_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }else{                $no = SaleOrdersProduct::where('del_time',0)                    ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')                    ->select('order_no')                    ->get()->toArray();                $no = array_unique(array_column($no,'order_no'));                $box_id = Box::where('del_time',0)                    ->whereIn('top_order_no', $no)                    ->select('id')                    ->get()->toArray();                $box_id = array_column($box_id,'id');                $detail = ApplyOrderDetail::where('del_time',0)                    ->where('type', $data['type'])                    ->whereIn('data_id', $box_id)                    ->select('apply_order_id')                    ->get()->toArray();                $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));            }        }        if(! empty($data['dispatch_no'])){            $dispatch_id = DispatchSub::where('del_time',0)                ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')                ->select('id')                ->get()->toArray();            $dispatch_id = array_column($dispatch_id,'id');            $detail = ApplyOrderDetail::where('del_time',0)                ->where('type', $data['type'])                ->whereIn('data_id', $dispatch_id)                ->select('apply_order_id')                ->get()->toArray();            $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));        }        $list = $model->get()->toArray();        if(empty($list)) return [true, []];        $apply_id = array_column($list, 'id');        //明细数据        $result = ApplyOrderDetail::where('del_time',0)            ->whereIn('apply_order_id', $apply_id)            ->get()->toArray();        if(empty($result)) return [true, []];        //组织主表需要的数据        $list_map = $this->makeListMap($list, $result, $data['type']);        $data_id = array_unique(array_column($result, 'data_id'));        list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']);        $return = [];        $d_tmp = array_column($d,null,'id');        foreach ($result as $t){            $tmp = $d_tmp[$t['data_id']] ?? [];            $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";            $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";            $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";            $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";            if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){                $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);            }else{                $order_no = $tmp['order_no'] ?? "";            }            if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){                $product_unit = "吨";            }elseif($t['type'] == ApplyOrder::type_four){                $product_unit = "只";            }else{                $product_unit = $t['product_unit'] ?? "";            }            $tmp = $list_map[$t['apply_order_id']] ?? [];            $return[] = [                'order_number' => $tmp['order_number'],                'sale_order_number' => $tmp['sale_order_number'],                'apply_time' => $tmp['apply_time'],                'mark' => $tmp['mark'],                'status_title' => $tmp['status_title'],                'storehouse_title' => $tmp['storehouse_title'],                'id' => $t['data_id'] ?? 0,//                'main_id' => $t['id'],                'quantity' => $t['quantity'] ?? 0,                'product_no' => $t['product_no'] ?? "",                'product_title' => $t['product_title'] ?? "",                'product_size' => $t['product_size'] ?? "",                'product_unit' => $product_unit,                'top_product_title' => $top_product_title,                'top_product_no' => $top_product_no,                'technology_name' => $technology_name, //颜色                'wood_name' => $wood_name,                'order_no' => $order_no,            ];        }        return [true, array_values($return)];    }    private function getDetailList($apply_id, $type){        $d = $d_no_map = [];        if($type == ApplyOrder::type_one){            $d = DispatchSub::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')                ->get()->toArray();            $args = "";            foreach ($d as $value){                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";            }            $args = rtrim($args, 'OR ');            $d_no_map = [];            $d_no = DispatchSub::where('del_time',0)                ->whereRaw($args)                ->select('dispatch_no as order_no','order_product_id','crt_time')                ->get()->toArray();            foreach ($d_no as $value){                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }else{                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }            }        }elseif ($type == ApplyOrder::type_two){            $d = DispatchSub::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')                ->get()->toArray();            $args = "";            foreach ($d as $value){                $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";            }            $args = rtrim($args, 'OR ');            $d_no_map = [];            $d_no = DispatchSub::where('del_time',0)                ->whereRaw($args)                ->select('dispatch_no as order_no','order_product_id','crt_time')                ->get()->toArray();            foreach ($d_no as $value){                if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){                    if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }else{                    $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];                }            }        }elseif ($type == ApplyOrder::type_three){            $d = Box::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_no')                ->get()->toArray();        }elseif ($type == ApplyOrder::type_four){            $d = Box::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_no')                ->get()->toArray();        }elseif($type == ApplyOrder::type_five){            $d = ScrappCount::where('del_time',0)                ->whereIn('id', $apply_id)                ->select('id', 'order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')                ->get()->toArray();        }        return [$d, $d_no_map];    }    private function makeListMap($data, $order, $type){        if(empty($data)) return [];        //申请单 所有的数据id        $order_map = $data_id = [];        foreach ($order as $value){            $order_map[$value['apply_order_id']][] = $value['data_id'];            $data_id[] = $value['data_id'];        }        if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){            $map1 = DispatchSub::whereIn('id', array_unique($data_id))                ->pluck('out_order_no','id')                ->toArray();        }elseif($type == ApplyOrder::type_five){            $map3 = ScrappCount::whereIn('id', array_unique($data_id))                ->pluck('out_order_no','id')                ->toArray();        }else{            $map2 = Box::whereIn('id', array_unique($data_id))                ->pluck('top_order_no','id')                ->toArray();            $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))                ->pluck('out_order_no','order_no')                ->toArray();        }        foreach ($data as $key => $value){            $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';            $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";            $order_tmp = $order_map[$value['id']] ?? [];            $order_tmp = array_unique($order_tmp);            $sale_order_number = [];            if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){                foreach ($order_tmp as $v){                    $str = $map1[$v] ?? '';                    if(empty($str)) continue;                    $sale_order_number[] = $str;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data[$key]['sale_order_number'] = $sale_order_number;            }elseif ($type == ApplyOrder::type_five){                foreach ($order_tmp as $v){                    $str = $map3[$v] ?? '';                    if(empty($str)) continue;                    $sale_order_number[] = $str;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data[$key]['sale_order_number'] = $sale_order_number;            }else{                foreach ($order_tmp as $v){                    $m2_t = $map2[$v] ?? '';                    $m2_t = $map2_fin[$m2_t] ?? "";                    if(empty($m2_t)) continue;                    $sale_order_number[] = $m2_t;                }                $sale_order_number = array_unique($sale_order_number);                $sale_order_number = implode(',',$sale_order_number);                $data[$key]['sale_order_number'] = $sale_order_number;            }        }        return array_column($data,null,'id');    }    public function editZj($data, $user){       list($status, $msg) = $this->editZjRule($data);        if(! $status) return [false, $msg];        $order_map = array_column($msg,null,'data_id');        try{            DB::beginTransaction();            $time = time();            $insert = [];            $max_id = $this->getDailyId();            list($status,$msg) = $this->limitingSendRequestBackg("quantity_id_" . $max_id);            if(! $status) return [false,$msg];            $get_order_number = $insert_wg = [];            foreach ($data['data'] as $value){                $order_t = $order_map[$value['data_id']] ?? [];                $order = $order_t['dispatch'] ?? [];                if(empty($value['waste'])){                    $max_id += 1;                    $order_number = date("Ymd") . str_pad($max_id,3,'0',STR_PAD_LEFT);                    $get_order_number[] = $order_number;                    $insert[] = [                        'sale_orders_product_id' => $order['sale_orders_product_id'],                        'order_product_id' => $order['order_product_id'],                        'dispatch_sub_id' => $order['id'],                        'out_order_no' => $order['out_order_no'],                        'order_no' => $order['order_no'],                        'customer_no' => $order['customer_no'],                        'customer_name' => $order['customer_name'],                        'product_no' => $order['product_no'],                        'product_title' => $order['product_title'],                        'product_size' => $order['product_size'],                        'product_unit' => $order['product_unit'],                        'technology_material' => $order['technology_material'],                        'technology_name' => $order['technology_name'],                        'wood_name' => $order['wood_name'],                        'price' => $order['price'],                        'process_mark' => $order['process_mark'],                        'table_body_mark' => $order['table_body_mark'],                        'table_header_mark' => $order['table_header_mark'],                        'crt_id' => $user['id'],                        'crt_time' => $time,                        'scrapp_num' => 0,                        'scrapp_id' => 0,                        'team_id' => $data['team_id'],                        'finished_id' => $user['id'],                        'equipment_id' => $data['equipment_id'],                        'order_number' => $order_number,                        'process_id' => $order['process_id'],                        'quantity' => $value['num'],                        'report_id' => $data['id'],                        'result' => 0,                        'warehouse_no' => "",                        'warehouse_title' => "",                        'daily_id' => $max_id,                    ];                }else{                    foreach ($value['waste'] as $v){                        $max_id += 1;                        $order_number = date("Ymd") . str_pad($max_id,3,'0',STR_PAD_LEFT);                        $get_order_number[] = $order_number;                        $insert[] = [                            'sale_orders_product_id' => $order['sale_orders_product_id'],                            'order_product_id' => $order['order_product_id'],                            'dispatch_sub_id' => $order['id'],                            'out_order_no' => $order['out_order_no'],                            'order_no' => $order['order_no'],                            'customer_no' => $order['customer_no'],                            'customer_name' => $order['customer_name'],                            'product_no' => $order['product_no'],                            'product_title' => $order['product_title'],                            'product_size' => $order['product_size'],                            'product_unit' => $order['product_unit'],                            'technology_material' => $order['technology_material'],                            'technology_name' => $order['technology_name'],                            'wood_name' => $order['wood_name'],                            'price' => $order['price'],                            'process_mark' => $order['process_mark'],                            'table_body_mark' => $order['table_body_mark'],                            'table_header_mark' => $order['table_header_mark'],                            'crt_id' => $user['id'],                            'crt_time' => $time,                            'scrapp_num' => $v['num'],                            'scrapp_id' => $v['scrapp_id'],                            'team_id' => $data['team_id'],                            'finished_id' => $user['id'],                            'equipment_id' => $data['equipment_id'],                            'order_number' => $order_number,                            'process_id' => $order['process_id'],                            'quantity' => $value['num'],                            'report_id' => $data['id'],                            'result' => $v['result'],                            'warehouse_no' => $v['warehouse_no'],                            'warehouse_title' => $v['warehouse_title'],                            'daily_id' => $max_id,                        ];                    }                }                if($value['num'] > 0) $insert_wg[] = [                    'dispatch_id' => $value['data_id'],                    'num' => $value['num']                ];            }            //质检单 不良品单 会记录报工单的id            if(! empty($insert)) ScrappCount::insert($insert);            //获取新增的单据            $result = ScrappCount::whereIn('order_number',$get_order_number)                ->select('scrapp_num as num','id','dispatch_sub_id','warehouse_no','warehouse_title')                ->get()->toArray();            //完工入库申请单 半成品            list($status, $msg) = $this->insertWg($insert_wg, $user);            if(! $status) return [false, $msg];            //获取写入的数据 写其它的业务逻辑            list($status, $msg) = $this->insertOther($result, $user, $time);            if(! $status) return [false, $msg];            //反写 根据质检单上的派工单id 反写上游单据            $this->writeDispatchQuantity(array_column($result,'dispatch_sub_id'));            DB::commit();        }catch (\Throwable $exception){            DB::rollBack();            return [false, $exception->getMessage() . '|' . $exception->getLine()];        }        return [true, ''];    }    public function editZjRule($data){        if(empty($data['id'])) return [false, '报工单ID不能为空'];        $order = ReportWorkingDetail::where('del_time',0)            ->where('report_working_id', $data['id'])            ->get()->toArray();        if(empty($order)) return [false, "报工单信息不存在或已被删除"];        $map = [];        foreach ($order as $value){            if(! isset($map[$value['data_id']])) $map[$value['data_id']] = $value['quantity'];        }        if(empty($data['team_id'])) return [false, '质检班组不能为空'];        if(empty($data['equipment_id'])) return [false, '设备不能为空'];        if(empty($data['data'])) return [false, '质检信息不能为空'];        $data_id = [];        foreach ($data['data'] as $value){            if(empty($value['data_id'])) return [false, 'dataID不能为空'];            if(! isset($value['num'])) return [false, 'num不能为空'];            $num = 0;            if(! empty($value['waste'])){                foreach ($value['waste'] as $v){                    if(empty($v['num'])) return [false, "质检数量不能为空"];                    if(empty($v['scrapp_id'])) return [false, "请选择质检原因"];                    if(empty($v['result'])) return [false, '请选择质检产品处理结果'];                    if($v['result'] == ReportWorking::type_two && (empty($v['warehouse_no']) || empty($v['warehouse_title']))) return [false, '请选择产品去向仓库'];                    $num = bcadd($v['num'], $num, 3);                }            }            $total = bcadd($num, $value['num'],3);            $total2 = $map[$value['data_id']] ?? 0;            if($total > $total2) return [false, '总数量不能超过报工数量'];            $data_id[] = $value['data_id'];        }        $dispatch = DispatchSub::whereIn('id',$data_id)->get()->toArray();        $dispatch_map = array_column($dispatch,null,'id');        foreach ($order as $key => $value){            if(! in_array($value['data_id'], $data_id)){                unset($order[$key]);                continue;            }            $order[$key]['dispatch'] = $dispatch_map[$value['data_id']] ?? [];        }        return [true, array_values($order)];    }    public function getDailyId($time = 0)    {        if(empty($time)) $time = time();        $today_start = strtotime(date("Y-m-d 00:00:00",$time));        $today_end = strtotime(date("Y-m-d 23:59:59",$time));        // 获取今天的最大daily_id        $maxDailyId = ScrappCount::where('crt_time', '>=', $today_start)            ->where('crt_time', '<=', $today_end)            ->max(DB::raw('daily_id + 0'));        return $maxDailyId ?? 0;    }    public function insertWg($all_data, $user){        if(empty($all_data)) return [true, ''];        $dispatchList = DispatchSub::whereIn('id', array_column($all_data,'dispatch_id'))->get()->toArray();        //是否最后一道工序        $orders = OrdersProduct::whereIn('id', array_unique(array_column($dispatchList, '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;        }        $all_data_map = array_column($all_data,null,'dispatch_id');        $need_insert = [];        foreach ($dispatchList as $value){            $num = $all_data_map[$value['id']] ?? 0;            if(! $num) continue;            if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id']){                $tmp = [                    'id' => $value['id'],                    'quantity' => $num,                    'product_no' => $value['product_no'] ?? "",                    'product_title' => $value['product_title'] ?? "",                    'product_size' => $value['product_size'] ?? "",                    'product_unit' => $value['product_unit'] ?? "",                ];                $need_insert[] = $tmp;            }        }        //生成完工入库申请单        $service = new BoxService();        list($status, $msg) = $service->createWGSQ($need_insert, $user);        if(! $status) return [false, $msg];        return [true, ''];    }    public function insertOther($all_data, $user, $time){        try {            $result = [];            foreach ($all_data as $value){                if($value['result'] > 0) $result[] = $value;            }            if(empty($result)) return [true, ''];            $dispatchList = DispatchSub::whereIn('id', array_column($result,'dispatch_sub_id'))->get()->toArray();            $dispatch_map = array_column($dispatchList,null,'id');            //是否自动审核            $em = new EmployeeService();            $auto = $em->is_auto($user, "pg_auto");            $dispatch_no = (new DispatchService())->setOrderNO();            if(! $dispatch_no) return [false,'返工派工单号生成失败!'];            $prefix = substr($dispatch_no, 0, -3);// "20250611"            $startNum = (int)substr($dispatch_no, -3); // 7            $is_first = true;            $dispatch_main = $dispatch = $is_need_ll = $is_need_warehouse = [];            foreach ($result as $value){                //派工单                $t = $dispatch_map[$value['dispatch_sub_id']] ?? [];                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 = $dispatch_no;                    $is_first = false;                }                if($value['result'] == ReportWorking::type_one){                    //重新入生产流程 需要生成返工派工单                }else{                    //入仓库 那么需要生成质检入库申请单 生成领料申请单 生成返工派工单                    //'id','dispatch_quantity as quantity',"product_no","order_product_id","product_no",'technology_name','crt_time'                    $is_need_ll[] = $dispatch_no_t;                    $is_need_warehouse[] = [                        'warehouse_no' => $value['warehouse_no'],                        'warehouse_title' => $value['warehouse_title'],                    ];                }                $dispatch_main[] = ['dispatch_no' => $dispatch_no_t,'crt_time' => $time];                $dispatch[] = [                    'order_product_id' => $t['order_product_id'],                    'order_no' => $t['order_no'],                    'product_no' => $t['product_no'],                    'product_title' => $t['product_title'],                    'product_size' => $t['product_size'],                    'product_unit' => $t['product_unit'],                    'production_quantity' => $t['production_quantity'],                    'dispatch_quantity' => $value['num'],                    'technology_material' => $t['technology_material'],                    'technology_name' => $t['technology_name'],                    'wood_name' => $t['wood_name'],                    'process_mark' => $t['process_mark'],                    'table_body_mark' => $t['table_body_mark'],                    'table_header_mark' => $t['table_header_mark'],                    'crt_time' => $time,                    'status' => $auto,                    'dispatch_no' => $dispatch_no_t,                    'process_id' => $t['process_id'],                    'dispatch_time_start' => $t['dispatch_time_start'],                    'dispatch_time_end' => $t['dispatch_time_end'],                    'crt_id' => $user['id'],                    'sale_orders_product_id' => $t['sale_orders_product_id'],                    'out_order_no' => $t['out_order_no'],                    'out_order_no_time' => $t['out_order_no_time'],                    'price' => $t['price'],                    'customer_no' => $t['customer_no'],                    'customer_name' => $t['customer_name'],                    'pre_shipment_time' => $t['pre_shipment_time'],                    'team_id' => $t['team_id'],                    'device_id' => $t['device_id'],                    'type' => 1,                ];            }            Dispatch::insert($dispatch_main);            DispatchSub::insert($dispatch);            if(! empty($is_need_ll)){                //获取上一次插入订单的所有数据                $insert = DispatchSub::whereIn('dispatch_no', $is_need_ll)                    ->where('crt_time',$time)                    ->where('crt_id',$user['id'])                    ->select('id','dispatch_quantity as quantity',"product_no")                    ->get()->toArray();                //领料申请单                list($status, $msg) = (new DispatchService())->createSQ($insert, $user);                if(! $status) return [false, $msg];                //质检入库申请单 原材料                list($status, $msg) = $this->createZJSQ($insert, $user, $is_need_warehouse);                if(! $status) return [false, $msg];            }            return [true, ''];        }catch (\Throwable $exception){            return [false, $exception->getMessage()];        }    }    public function createZJSQ($insert_data, $user, $warehouse, $time = 0){        try {            DB::beginTransaction();            //是否自动审核质检入库申请单            $em = new EmployeeService();            $auto = $em->is_auto($user, "zjsq_auto");            $return = [];            $product_no = array_unique(array_column($insert_data,'product_no'));            //获取原料            $service = new FyyOrderService();            list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);            if($status) $return = $msg;            //组织原材料写入数据            $insert = [];            foreach ($insert_data as $key => $value){                $quantity = 0;                if(! empty($value['quantity'])) {                    $quantity = $value['quantity'];                }elseif (! empty($value['dispatch_quantity'])){                    $quantity = $value['dispatch_quantity'];                }                $t = $return[$value['product_no']] ?? [];                $tmp = [                    'id' => $value['id'],                    'quantity' => $quantity,                    'product_no' => "",                    'product_title' => "",                    'product_size' => "",                    'product_unit' => "",                    'storehouse_id' => $warehouse[$key]['warehouse_no'] ?? "",                    'storehouse_title' => $warehouse[$key]['warehouse_title'] ?? "",                ];                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;                }            }            //生成质检入库申请单            $service = new ApplyOrderService();            list($status, $msg) = $service->createSqFive($insert, $user, ApplyOrder::type_five, $auto, $time);            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 writeDispatchQuantity($dispatch_id){        $result = ScrappCount::where('del_time',0)            ->whereIn('dispatch_sub_id', $dispatch_id)            ->select('dispatch_sub_id','quantity','scrapp_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']], 3);                $update[$value['dispatch_sub_id']] = $tmp;            }else{                $update[$value['dispatch_sub_id']] = $value['quantity'];            }            $flag[$key] = 1;        }        unset($flag);        //更新派工单完工数量----------------------------------------------------        foreach ($update as $id => $num){            DispatchSub::where('id', $id)->update([                'finished_num' => $num            ]);        }        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 orderSearchList($data){        if(empty($data['type'])) return [false, '单据联查类型不能为空'];        if(empty($data['data_id'])) return [false, '依据单据ID不能为空'];        //不管进来的是什么单据id 转换为销售订单        $sales_id = $this->changeIdToOrder($data);        list($status, $msg) = $this->forSearch($data, $sales_id);        return [$status, $msg];    }    private function changeIdToOrder($data)    {        $id = $data['id'];        $search_id = $result = [];        if ($data['type'] == 1) {            $result[] = [                'id' => $id            ];        } elseif ($data['type'] == 2) {            $result = OrdersProduct::where('del_time', 0)                ->where('id', $id)                ->select('sale_orders_product_id as id')                ->get()->toArray();        } elseif ($data['type'] == 3) {            $result = DispatchSub::where('del_time', 0)                ->where('id', $data['data_id'])                ->select('sale_orders_product_id as id')                ->get()->toArray();        } elseif ($data['type'] == 4) {            $detail = ReportWorkingDetail::where('data_id', $id)                ->where('del_time', 0)                ->select('data_id')                ->get()->toArray();            $result = DispatchSub::where('del_time', 0)                ->whereIn('id', array_unique(array_column($detail, 'data_id')))                ->select('sale_orders_product_id as id')                ->get()->toArray();        } elseif ($data['type'] == 5) {            $result = DispatchSub::where('del_time', 0)                ->where('id', $id)                ->where('finished_num', '>', 0)                ->select('sale_orders_product_id as id')                ->get()->toArray();        } elseif ($data['type'] == 6) {            $first = Box::where('del_time', 0)                ->where('id', $id)                ->select("top_order_no", 'order_no')                ->first();            if (!empty($first)) {                $first = $first->toArray();                $box_detail = new BoxDetail(['channel' => $first['top_order_no']]);                $result = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();            }        } elseif ($data['type'] == 7) {            $result = ScrappCount::where('del_time', 0)                ->where('id', $id)                ->select('sale_orders_product_id as id')                ->get()->toArray();        } elseif ($data['type'] == 8) {            $result = ScrappCount::where('del_time', 0)                ->where('id', $id)                ->where('scrapp_num', '>', 0)                ->select('sale_orders_product_id as id')                ->get()->toArray();        } elseif ($data['type'] == 9) {            $detail = ApplyOrderDetail::where('del_time', 0)                ->where('apply_order_id', $id)                ->where('type', ApplyOrder::type_one)                ->select('data_id as dispatch_id')                ->get()->toArray();            $result = DispatchSub::where('del_time', 0)                ->where('id', array_unique(array_column($detail, 'dispatch_id')))                ->select('sale_orders_product_id as id')                ->get()->toArray();        } elseif ($data['type'] == 10) {            $detail = ApplyOrderDetail::where('del_time', 0)                ->where('apply_order_id', $id)                ->where('type', ApplyOrder::type_two)                ->select('data_id as dispatch_id')                ->get()->toArray();            $result = DispatchSub::where('del_time', 0)                ->where('id', array_unique(array_column($detail, 'dispatch_id')))                ->select('sale_orders_product_id as id')                ->get()->toArray();        } elseif ($data['type'] == 11) {            $detail = ApplyOrderDetail::where('del_time', 0)                ->where('apply_order_id', $id)                ->where('type', ApplyOrder::type_four)                ->select('data_id as box_id')                ->get()->toArray();            $first = Box::where('del_time', 0)                ->whereIn('id', array_unique(array_column($detail, 'box_id')))                ->select("top_order_no", 'order_no')                ->get()->toArray();            if (!empty($first)) {                foreach ($first as $value) {                    $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);                    $t = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();                    $result = array_merge($result, $t);                }            }        } elseif ($data['type'] == 12) {            $detail = ApplyOrderDetail::where('del_time', 0)                ->where('apply_order_id', $id)                ->where('type', ApplyOrder::type_three)                ->select('data_id as box_id')                ->get()->toArray();            $first = Box::where('del_time', 0)                ->whereIn('id', array_unique(array_column($detail, 'box_id')))                ->select("top_order_no", 'order_no')                ->get()->toArray();            if (!empty($first)) {                foreach ($first as $value) {                    $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);                    $t = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();                    $result = array_merge($result, $t);                }            }        } elseif ($data['type'] == 13) {            $detail = ApplyOrderDetail::where('del_time', 0)                ->where('apply_order_id', $id)                ->where('type', ApplyOrder::type_five)                ->select('data_id as zj_id')                ->get()->toArray();            $result = ScrappCount::where('del_time', 0)                ->whereIn('id', array_unique(array_column($detail, 'zj_id')))                ->select('sale_orders_product_id as id')                ->get()->toArray();        }        foreach ($result as $value) {            if (!in_array($value['id'], $search_id)) {                $search_id[] = $value['id'];            }        }        return $search_id;    }    private function forSearch($data, $sales_id){        $result = [];        //通过销售订单查找所有业务单据        if($data['for_type'] == 1){            $result = SaleOrdersProduct::where('del_time',0)                ->whereIn('id', $sales_id)                ->select('id', 'out_order_no as order_no')                ->get()->toArray();        }elseif($data['for_type'] == 2){            $result = OrdersProduct::where('del_time',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->select('id', 'production_no as order_no')                ->get()->toArray();        }elseif($data['for_type'] == 3){            $result = DispatchSub::where('del_time',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->select('id', 'dispatch_no as order_no')                ->get()->toArray();        }elseif($data['for_type'] == 4){            $dispatch = DispatchSub::where('del_time',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->select('id')                ->get()->toArray();            $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))                ->where('del_time',0)                ->select('data_id')                ->get()->toArray();            $result = ReportWorking::where('del_time',0)                ->whereIn('id', array_unique(array_column($detail,'data_id')))                ->select('id', 'order_number as order_no')                ->get()->toArray();        }elseif($data['for_type'] == 5){            $result = DispatchSub::where('del_time',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->where('finished_num','>',0)                ->select('id', 'dispatch_no as order_no')                ->get()->toArray();        }elseif($data['for_type'] == 6){            $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();            foreach ($sale as $value){                $box_detail = new BoxDetail(['channel'=> $value['order_no']]);                $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();                $tmp = Box::where('del_time',0)                    ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))                    ->select("id","order_no")                    ->get()->toArray();                $result = array_merge($result, $tmp);            }        }elseif ($data['for_type'] == 7){            $result = ScrappCount::where('del_time',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->select('id', 'order_number as order_no')                ->get()->toArray();        }elseif ($data['for_type'] == 8){            $result = ScrappCount::where('del_time',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->where('scrapp_num','>', 0)                ->select('id', 'order_number as order_no')                ->get()->toArray();        }elseif ($data['for_type'] == 9){            $dispatch = DispatchSub::where('del_time',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->select('id')                ->get()->toArray();            $detail = ApplyOrderDetail::where('del_time',0)                ->whereIn('data_id', array_column($dispatch,'id'))                ->where('type',ApplyOrder::type_one)                ->select('apply_order_id')                ->get()->toArray();            $result = ApplyOrder::where('del_time',0)                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))                ->select('id','order_number as order_no')                ->get()->toArray();        }elseif ($data['for_type'] == 10){            $dispatch = DispatchSub::where('del_time',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->select('id')                ->get()->toArray();            $detail = ApplyOrderDetail::where('del_time',0)                ->whereIn('data_id', array_column($dispatch,'id'))                ->where('type',ApplyOrder::type_two)                ->select('apply_order_id')                ->get()->toArray();            $result = ApplyOrder::where('del_time',0)                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))                ->select('id','order_number as order_no')                ->get()->toArray();        }elseif($data['for_type'] == 11){            $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();            $ids = [];            foreach ($sale as $value){                $box_detail = new BoxDetail(['channel'=> $value['order_no']]);                $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();                $tmp = Box::where('del_time',0)                    ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))                    ->select("id")                    ->get()->toArray();                $ids = array_merge($ids, $tmp);            }            $detail = ApplyOrderDetail::where('del_time',0)                ->whereIn('data_id', array_column($ids,'id'))                ->where('type',ApplyOrder::type_four)                ->select('apply_order_id')                ->get()->toArray();            $result = ApplyOrder::where('del_time',0)                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))                ->select('id','order_number as order_no')                ->get()->toArray();        }elseif($data['for_type'] == 12){            $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();            $ids = [];            foreach ($sale as $value){                $box_detail = new BoxDetail(['channel'=> $value['order_no']]);                $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();                $tmp = Box::where('del_time',0)                    ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))                    ->select("id")                    ->get()->toArray();                $ids = array_merge($ids, $tmp);            }            $detail = ApplyOrderDetail::where('del_time',0)                ->whereIn('data_id', array_column($ids,'id'))                ->where('type',ApplyOrder::type_three)                ->select('apply_order_id')                ->get()->toArray();            $result = ApplyOrder::where('del_time',0)                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))                ->select('id','order_number as order_no')                ->get()->toArray();        }elseif($data['for_type'] == 13){            $zj_id = ScrappCount::where('del_time',0)                ->where('scrapp_num','>',0)                ->whereIn('sale_orders_product_id', $sales_id)                ->select('id')                ->get()->toArray();            $detail = ApplyOrderDetail::where('del_time',0)                ->whereIn('data_id', array_column($zj_id,'id'))                ->where('type',ApplyOrder::type_five)                ->select('apply_order_id')                ->get()->toArray();            $result = ApplyOrder::where('del_time',0)                ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))                ->select('id','order_number as order_no')                ->get()->toArray();        }        return [true, $result];    }}
 |