ApplyOrderService.php 206 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596
  1. <?php
  2. namespace App\Service;
  3. use App\Model\ApplyOrder;
  4. use App\Model\ApplyOrderDetail;
  5. use App\Model\Box;
  6. use App\Model\BoxDetail;
  7. use App\Model\BoxWithDispatch;
  8. use App\Model\Dispatch;
  9. use App\Model\DispatchSub;
  10. use App\Model\Employee;
  11. use App\Model\EmployeeTeamPermission;
  12. use App\Model\Equipment;
  13. use App\Model\InOutRecord;
  14. use App\Model\OrdersProduct;
  15. use App\Model\Process;
  16. use App\Model\ReportMessage;
  17. use App\Model\ReportWorking;
  18. use App\Model\ReportWorkingDetail;
  19. use App\Model\SaleOrdersProduct;
  20. use App\Model\ScrappCount;
  21. use App\Model\Team;
  22. use App\Service\Box\BoxService;
  23. use Illuminate\Support\Facades\DB;
  24. class ApplyOrderService extends Service
  25. {
  26. public function MaterialEdit($data, $user){
  27. list($status,$msg) = $this->MaterialRule($data,false);
  28. if(!$status) return [$status,$msg];
  29. DB::beginTransaction();
  30. try{
  31. $time = time();
  32. $model = new ApplyOrder();
  33. $model = $model->where('id',$data['id'])->first();
  34. $model->apply_id = $data['apply_id'];
  35. $model->apply_time = $data['apply_time'] ;
  36. $model->mark = $data['mark'] ?? "";
  37. $model->storehouse_id = $data['storehouse_id'];
  38. $model->storehouse_title = $data['storehouse_title'] ?? "";
  39. $model->type = $data['type'];
  40. $model->save();
  41. $id = $model->id;
  42. ApplyOrderDetail::where('del_time',0)->where('apply_order_id',$id)->update([
  43. 'del_time' => $time
  44. ]);
  45. $detail_insert = [];
  46. foreach ($data['order_data'] as $v){
  47. $detail_insert[] = [
  48. 'apply_order_id' => $id,
  49. 'data_id' => $v['id'],
  50. 'quantity' => $v['quantity'] ?? 0,
  51. 'product_no' => $v['product_no'] ?? "",
  52. 'product_title' => $v['product_title'] ?? "",
  53. 'product_size' => $v['product_size'] ?? "",
  54. 'product_unit' => $v['product_unit'] ?? "",
  55. 'technology_name' => $v['technology_name'] ?? "", //颜色
  56. 'top_product_title' => $v['top_product_title'] ?? "",
  57. 'top_product_no' => $v['top_product_title'] ?? "",
  58. 'type' => $data['type'],
  59. 'storehouse_id' => $data['storehouse_id'],
  60. 'crt_time' => $time,
  61. ];
  62. }
  63. ApplyOrderDetail::insert($detail_insert);
  64. DB::commit();
  65. }catch (\Exception $e){
  66. DB::rollBack();
  67. return [false,$e->getMessage()];
  68. }
  69. return [true,''];
  70. }
  71. public function MaterialAdd($data,$user){
  72. list($status,$msg) = $this->MaterialRule($data);
  73. if(!$status) return [$status,$msg];
  74. DB::beginTransaction();
  75. try{
  76. $time = time();
  77. $model = new ApplyOrder();
  78. $model->order_number = $data['order_number'];
  79. $model->apply_id = $data['apply_id'];
  80. $model->apply_time = $data['apply_time'] ;
  81. $model->mark = $data['mark'] ?? "";
  82. $model->storehouse_id = $data['storehouse_id'];
  83. $model->storehouse_title = $data['storehouse_title'] ?? "";
  84. $model->type = $data['type'];
  85. $model->crt_id = $user['id'];
  86. $model->save();
  87. $id = $model->id;
  88. $detail_insert = [];
  89. foreach ($data['order_data'] as $v){
  90. $detail_insert[] = [
  91. 'apply_order_id' => $id,
  92. 'data_id' => $v['id'],
  93. 'quantity' => $v['quantity'] ?? 0,
  94. 'product_no' => $v['product_no'] ?? "",
  95. 'product_title' => $v['product_title'] ?? "",
  96. 'product_size' => $v['product_size'] ?? "",
  97. 'product_unit' => $v['product_unit'] ?? "",
  98. 'technology_name' => $v['technology_name'] ?? "", //颜色
  99. 'top_product_title' => $v['top_product_title'] ?? "",
  100. 'top_product_no' => $v['top_product_title'] ?? "",
  101. 'type' => $data['type'],
  102. 'storehouse_id' => $data['storehouse_id'],
  103. 'crt_time' => $time,
  104. ];
  105. }
  106. ApplyOrderDetail::insert($detail_insert);
  107. DB::commit();
  108. }catch (\Exception $e){
  109. DB::rollBack();
  110. return [false,$e->getMessage()];
  111. }
  112. return [true, ''];
  113. }
  114. public function MaterialDel($data){
  115. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  116. $apply = ApplyOrder::where('id',$data['id'])->first();
  117. if($apply->del_time > 0) return [false,'申请单不存在或已被删除'];
  118. if($apply->state != ApplyOrder::state_zero) return [false, '申请单已审核,删除失败'];
  119. $time = time();
  120. try {
  121. DB::beginTransaction();
  122. $apply->del_time = $time;
  123. $apply->save();
  124. ApplyOrderDetail::where('del_time', 0)->where('apply_order_id',$data['id'])->update([
  125. 'del_time'=>time()
  126. ]);
  127. DB::commit();
  128. }catch (\Throwable $exception){
  129. DB::rollBack();
  130. return [false, $exception->getMessage()];
  131. }
  132. return [true,''];
  133. }
  134. public function MaterialList($data){
  135. $model = ApplyOrder::where('del_time',0)
  136. ->select('*')
  137. ->orderBy('id','desc');
  138. if(isset($data['status'])) $model->where('status', $data['status']);
  139. if(! empty($data['type'])) $model->where('type', $data['type']);
  140. if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
  141. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  142. if(! empty($data['apply_id'])) $model->where('apply_id', $data['apply_id']);
  143. if(! empty($data['pc_number'])) {
  144. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  145. $dispatch_id = DispatchSub::where('del_time',0)
  146. ->where('out_order_no', 'LIKE', '%'.$data['pc_number'].'%')
  147. ->select('id')
  148. ->get()->toArray();
  149. $dispatch_id = array_column($dispatch_id,'id');
  150. $detail = ApplyOrderDetail::where('del_time',0)
  151. ->where('type', $data['type'])
  152. ->whereIn('data_id', $dispatch_id)
  153. ->select('apply_order_id')
  154. ->get()->toArray();
  155. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  156. }elseif($data['type'] == ApplyOrder::type_five){
  157. $zj_id = ScrappCount::where('del_time',0)
  158. ->where('out_order_no', 'LIKE', '%'.$data['pc_number'].'%')
  159. ->select('id')
  160. ->get()->toArray();
  161. $zj_id = array_column($zj_id,'id');
  162. $detail = ApplyOrderDetail::where('del_time',0)
  163. ->where('type', $data['type'])
  164. ->whereIn('data_id', $zj_id)
  165. ->select('apply_order_id')
  166. ->get()->toArray();
  167. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  168. }else{
  169. $no = SaleOrdersProduct::where('del_time',0)
  170. ->where('out_order_no', 'LIKE', '%'.$data['pc_number'].'%')
  171. ->select('order_no')
  172. ->get()->toArray();
  173. $no = array_unique(array_column($no,'order_no'));
  174. $box_id = Box::where('del_time',0)
  175. ->whereIn('top_order_no', $no)
  176. ->select('id')
  177. ->get()->toArray();
  178. $box_id = array_column($box_id,'id');
  179. $detail = ApplyOrderDetail::where('del_time',0)
  180. ->where('type', $data['type'])
  181. ->whereIn('data_id', $box_id)
  182. ->select('apply_order_id')
  183. ->get()->toArray();
  184. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  185. }
  186. }
  187. if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
  188. if(! empty($data['apply_time'][0]) && ! empty($data['apply_time'][1])) $model->whereBetween('apply_time',[$data['apply_time'][0],$data['apply_time'][1]]);
  189. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  190. if(! empty($data['dispatch_no'])){
  191. $dispatch_id = DispatchSub::where('del_time',0)
  192. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  193. ->select('id')
  194. ->get()->toArray();
  195. $dispatch_id = array_column($dispatch_id,'id');
  196. $detail = ApplyOrderDetail::where('del_time',0)
  197. ->where('type', $data['type'])
  198. ->whereIn('data_id', $dispatch_id)
  199. ->select('apply_order_id')
  200. ->get()->toArray();
  201. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  202. }
  203. if(! empty($data['box_no'])){
  204. $box_id = Box::where('del_time',0)
  205. ->where('order_no', 'LIKE', '%'.$data['box_no'].'%')
  206. ->select('id')
  207. ->get()->toArray();
  208. $box_id = array_column($box_id,'id');
  209. $detail = ApplyOrderDetail::where('del_time',0)
  210. ->where('type', $data['type'])
  211. ->whereIn('data_id', $box_id)
  212. ->select('apply_order_id')
  213. ->get()->toArray();
  214. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  215. }
  216. if(! empty($data['sale_order_number'])){
  217. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  218. $dispatch_id = DispatchSub::where('del_time',0)
  219. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  220. ->select('id')
  221. ->get()->toArray();
  222. $dispatch_id = array_column($dispatch_id,'id');
  223. $detail = ApplyOrderDetail::where('del_time',0)
  224. ->where('type', $data['type'])
  225. ->whereIn('data_id', $dispatch_id)
  226. ->select('apply_order_id')
  227. ->get()->toArray();
  228. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  229. }elseif($data['type'] == ApplyOrder::type_five){
  230. $zj_id = ScrappCount::where('del_time',0)
  231. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  232. ->select('id')
  233. ->get()->toArray();
  234. $zj_id = array_column($zj_id,'id');
  235. $detail = ApplyOrderDetail::where('del_time',0)
  236. ->where('type', $data['type'])
  237. ->whereIn('data_id', $zj_id)
  238. ->select('apply_order_id')
  239. ->get()->toArray();
  240. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  241. }else{
  242. $no = SaleOrdersProduct::where('del_time',0)
  243. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  244. ->select('order_no')
  245. ->get()->toArray();
  246. $no = array_unique(array_column($no,'order_no'));
  247. $box_id = Box::where('del_time',0)
  248. ->whereIn('top_order_no', $no)
  249. ->select('id')
  250. ->get()->toArray();
  251. $box_id = array_column($box_id,'id');
  252. $detail = ApplyOrderDetail::where('del_time',0)
  253. ->where('type', $data['type'])
  254. ->whereIn('data_id', $box_id)
  255. ->select('apply_order_id')
  256. ->get()->toArray();
  257. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  258. }
  259. }
  260. $list = $this->limit($model,'',$data);
  261. $list = $this->fillData($list);
  262. return [true,$list];
  263. }
  264. public function fillData($data){
  265. if(empty($data['data'])) return $data;
  266. $emp = Employee::whereIn('id',array_merge_recursive(array_unique(array_column($data['data'],'crt_id')), array_unique(array_column($data['data'],'apply_id'))))
  267. ->pluck('emp_name','id')
  268. ->toArray();
  269. $array1 = $array2 = $array3 = [];
  270. foreach ($data['data'] as $value){
  271. if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  272. $array1[] = $value['id'];
  273. }elseif ($value['type'] == ApplyOrder::type_five){
  274. $array3[] = $value['id'];
  275. }else{
  276. $array2[] = $value['id'];
  277. }
  278. }
  279. $order1 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])
  280. ->whereIn('apply_order_id', $array1)
  281. ->select('data_id','apply_order_id')
  282. ->get()->toArray();
  283. $order1_map = [];
  284. foreach ($order1 as $value){
  285. $order1_map[$value['apply_order_id']][] = $value['data_id'];
  286. }
  287. $order2 = ApplyOrderDetail::whereNotIn('type',[ApplyOrder::type_one,ApplyOrder::type_two,ApplyOrder::type_five])
  288. ->whereIn('apply_order_id', $array2)
  289. ->select('data_id','apply_order_id')
  290. ->get()->toArray();
  291. $order2_map = [];
  292. foreach ($order2 as $value){
  293. $order2_map[$value['apply_order_id']][] = $value['data_id'];
  294. }
  295. $order3 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_five])
  296. ->whereIn('apply_order_id', $array3)
  297. ->select('data_id','apply_order_id')
  298. ->get()->toArray();
  299. $order3_map = [];
  300. foreach ($order3 as $value){
  301. $order3_map[$value['apply_order_id']][] = $value['data_id'];
  302. }
  303. $map1 = DispatchSub::whereIn('id', array_unique(array_column($order1,'data_id')))
  304. ->pluck('out_order_no','id')
  305. ->toArray();
  306. $map2 = Box::whereIn('id', array_unique(array_column($order2,'data_id')))
  307. ->pluck('top_order_no','id')
  308. ->toArray();
  309. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
  310. ->pluck('out_order_no','order_no')
  311. ->toArray();
  312. $map3 = ScrappCount::whereIn('id', array_unique(array_column($order3,'data_id')))
  313. ->pluck('out_order_no','id')
  314. ->toArray();
  315. foreach ($data['data'] as $key => $value){
  316. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  317. $data['data'][$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
  318. $data['data'][$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
  319. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  320. $data['data'][$key]['apply_name'] = $emp[$value['apply_id']] ?? '';
  321. if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  322. $order_tmp = $order1_map[$value['id']] ?? [];
  323. $order_tmp = array_unique($order_tmp);
  324. $sale_order_number = [];
  325. foreach ($order_tmp as $v){
  326. $str = $map1[$v] ?? '';
  327. if(empty($str)) continue;
  328. $sale_order_number[] = $str;
  329. }
  330. $sale_order_number = array_unique($sale_order_number);
  331. $sale_order_number = implode(',',$sale_order_number);
  332. $data['data'][$key]['sale_order_number'] = $sale_order_number;
  333. }elseif ($value['type'] == ApplyOrder::type_five){
  334. $order_tmp = $order3_map[$value['id']] ?? [];
  335. $order_tmp = array_unique($order_tmp);
  336. $sale_order_number = [];
  337. foreach ($order_tmp as $v){
  338. $str = $map3[$v] ?? '';
  339. if(empty($str)) continue;
  340. $sale_order_number[] = $str;
  341. }
  342. $sale_order_number = array_unique($sale_order_number);
  343. $sale_order_number = implode(',',$sale_order_number);
  344. $data['data'][$key]['sale_order_number'] = $sale_order_number;
  345. }else{
  346. $order_tmp = $order2_map[$value['id']] ?? [];
  347. $order_tmp = array_unique($order_tmp);
  348. $sale_order_number = [];
  349. foreach ($order_tmp as $v){
  350. $m2_t = $map2[$v] ?? '';
  351. $m2_t = $map2_fin[$m2_t] ?? "";
  352. if(empty($m2_t)) continue;
  353. $sale_order_number[] = $m2_t;
  354. }
  355. $sale_order_number = array_unique($sale_order_number);
  356. $sale_order_number = implode(',',$sale_order_number);
  357. $data['data'][$key]['sale_order_number'] = $sale_order_number;
  358. }
  359. }
  360. return $data;
  361. }
  362. public function MaterialDetail($data){
  363. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  364. $id = $data['id'];
  365. $detail = ApplyOrder::where('del_time',0)
  366. ->where('id',$id)
  367. ->first();
  368. if(empty($detail)) return [false,'申请单不存在或已被删除'];
  369. $detail = $detail->toArray();
  370. $detail['apply_time'] = $detail['apply_time'] ? date('Y-m-d',$detail['apply_time']) : '';
  371. $detail['apply_title'] = Employee::where('id', $detail['apply_id'])->value('emp_name');
  372. $apply_d = ApplyOrderDetail::where('del_time', 0)
  373. ->where('apply_order_id', $id)
  374. ->get()->toArray();
  375. $apply_id = array_column($apply_d, 'data_id');
  376. $d = [];
  377. $out_order_no = "";
  378. if($detail['type'] == ApplyOrder::type_one){
  379. $d = DispatchSub::where('del_time',0)
  380. ->whereIn('id', $apply_id)
  381. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time','out_order_no')
  382. ->get()->toArray();
  383. $out_order_no = $d[0]['out_order_no'] ?? "";
  384. $args = "";
  385. foreach ($d as $value){
  386. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  387. }
  388. $args = rtrim($args, 'OR ');
  389. $d_no_map = [];
  390. $d_no = DispatchSub::where('del_time',0)
  391. ->whereRaw($args)
  392. ->select('dispatch_no as order_no','order_product_id','crt_time')
  393. ->get()->toArray();
  394. foreach ($d_no as $value){
  395. if(! isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  396. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  397. }
  398. // if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  399. // 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'];
  400. // }else{
  401. // $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  402. // }
  403. }
  404. }elseif ($detail['type'] == ApplyOrder::type_two){
  405. $d = DispatchSub::where('del_time',0)
  406. ->whereIn('id', $apply_id)
  407. ->select('id','out_order_no', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
  408. ->get()->toArray();
  409. $out_order_no = $d[0]['out_order_no'] ?? "";
  410. $args = "";
  411. foreach ($d as $value){
  412. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  413. }
  414. $args = rtrim($args, 'OR ');
  415. $d_no_map = [];
  416. $d_no = DispatchSub::where('del_time',0)
  417. ->whereRaw($args)
  418. ->select('dispatch_no as order_no','order_product_id','crt_time')
  419. ->get()->toArray();
  420. foreach ($d_no as $value){
  421. if(! isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  422. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  423. }
  424. // if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  425. // 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'];
  426. // }else{
  427. // $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  428. // }
  429. }
  430. }elseif ($detail['type'] == ApplyOrder::type_three){
  431. $d = Box::where('del_time',0)
  432. ->whereIn('id', $apply_id)
  433. ->select('id', 'order_no','top_order_no')
  434. ->get()->toArray();
  435. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_column($d,'top_order_no'))
  436. ->pluck('out_order_no','order_no')
  437. ->toArray();
  438. foreach ($d as $value){
  439. $out_order_no = $map2_fin[$value['top_order_no']] ?? "";
  440. }
  441. }elseif ($detail['type'] == ApplyOrder::type_four){
  442. $d = Box::where('del_time',0)
  443. ->whereIn('id', $apply_id)
  444. ->select('id', 'order_no','top_order_no')
  445. ->get()->toArray();
  446. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_column($d,'top_order_no'))
  447. ->pluck('out_order_no','order_no')
  448. ->toArray();
  449. foreach ($d as $value){
  450. $out_order_no = $map2_fin[$value['top_order_no']] ?? "";
  451. }
  452. }elseif ($detail['type'] == ApplyOrder::type_five){ //质检入库申请
  453. $d = ScrappCount::where('del_time',0)
  454. ->whereIn('id', $apply_id)
  455. ->select('id', 'out_order_no','order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  456. ->get()->toArray();
  457. $out_order_no = $d[0]['out_order_no'] ?? "";
  458. }elseif ($detail['type'] == ApplyOrder::type_six){
  459. $d = Box::where('del_time',0)
  460. ->whereIn('id', $apply_id)
  461. ->select('id', 'order_no','top_order_no')
  462. ->get()->toArray();
  463. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_column($d,'top_order_no'))
  464. ->pluck('out_order_no','order_no')
  465. ->toArray();
  466. foreach ($d as $value){
  467. $out_order_no = $map2_fin[$value['top_order_no']] ?? "";
  468. }
  469. }
  470. $detail['sale_order_number'] = $out_order_no;
  471. $d_tmp = array_column($d,null,'id');
  472. $return = [];
  473. foreach ($apply_d as $t){
  474. $tmp = $d_tmp[$t['data_id']] ?? [];
  475. $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
  476. $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
  477. $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
  478. $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
  479. if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
  480. $order_no = implode(',', array_values($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]));
  481. }else{
  482. $order_no = $tmp['order_no'] ?? "";
  483. }
  484. if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
  485. $product_unit = "吨";
  486. }else{
  487. $product_unit = $t['product_unit'] ?? "";
  488. }
  489. $return[] = [
  490. 'id' => $t['data_id'] ?? 0,
  491. 'quantity' => $t['quantity'] ?? 0,
  492. 'product_no' => $t['product_no'] ?? "",
  493. 'product_title' => $t['product_title'] ?? "",
  494. 'product_size' => $t['product_size'] ?? "",
  495. 'product_unit' => $product_unit,
  496. 'top_product_title' => $top_product_title,
  497. 'top_product_no' => $top_product_no,
  498. 'technology_name' => $technology_name, //颜色
  499. 'wood_name' => $wood_name,
  500. 'order_no' => $order_no,
  501. ];
  502. }
  503. $detail['order_data'] = $return;
  504. return [true, $detail];
  505. }
  506. public function MaterialRule(&$data,$is_add = true){
  507. if($this->isEmpty($data,'apply_id')) return [false,'申请人不能为空'];
  508. if($this->isEmpty($data,'apply_time')) return [false,'申请时间不能为空'];
  509. if($this->isEmpty($data,'storehouse_id')) return [false,'仓库不能为空'];
  510. if($this->isEmpty($data,'type')) return [false,'申请单类型不能为空'];
  511. if(empty($data['order_data'])) return [false, '申请单详细信息不能为空'];
  512. foreach ($data['order_data'] as $value){
  513. if(empty($value['id'])) return [false, '申请单详细信息ID不能为空'];
  514. if($data['type'] != ApplyOrder::type_three){
  515. if(empty($value['quantity'])) return [false, '申请单详细信息数量不能为空'];
  516. }
  517. }
  518. $id = array_unique(array_column($data['order_data'],'id'));
  519. if($data['type'] == ApplyOrder::type_one){
  520. $dispatch = DispatchSub::where('del_time',0)
  521. ->whereIn('id', $id)
  522. ->select('id', 'dispatch_no', 'status')
  523. ->get()->toArray();
  524. if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
  525. }elseif ($data['type'] == ApplyOrder::type_two){
  526. $dispatch = DispatchSub::where('del_time',0)
  527. ->whereIn('id', $id)
  528. ->select('id', 'dispatch_no', 'status')
  529. ->get()->toArray();
  530. if(count($dispatch) != count($id)) return [false, '完工单信息错误,请重新选择'];
  531. }elseif ($data['type'] == ApplyOrder::type_three){
  532. $box = Box::where('del_time',0)
  533. ->whereIn('id', $id)
  534. ->select('id', 'order_no', 'status')
  535. ->get()->toArray();
  536. if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
  537. }elseif ($data['type'] == ApplyOrder::type_four){
  538. $box = Box::where('del_time',0)
  539. ->whereIn('id', $id)
  540. ->select('id', 'order_no', 'status')
  541. ->get()->toArray();
  542. if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
  543. }elseif ($data['type'] == ApplyOrder::type_five){
  544. $scrapp = ScrappCount::where('del_time',0)
  545. ->whereIn('id', $id)
  546. ->select('id', 'order_no', 'status')
  547. ->get()->toArray();
  548. if(count($scrapp) != count($id)) return [false, '质检单信息错误,请重新选择'];
  549. }else{
  550. return [false, '申请单类型错误'];
  551. }
  552. if($is_add){
  553. $data['order_number'] = $this->setOrderNO($data['type']);
  554. if(empty($data['order_number'])) return [false, '申请单唯一标识生成失败'];
  555. }else{
  556. if(empty($data['id'])) return [false, '申请单唯一标识不能为空'];
  557. if(empty($data['order_number'])) return [false, '申请单唯一标识不能为空'];
  558. $apply = ApplyOrder::where('id', $data['id'])->where('del_time', 0)->first();
  559. if(empty($apply)) return [false, '申请单不存在或已被删除'];
  560. if($apply->status != 0) return [false, '申请单已审核,编辑失败'];
  561. }
  562. return [true,''];
  563. }
  564. public function setOrderNO($type = "",$time = 0){
  565. if(empty($time)){
  566. $str = date('Ymd',time());
  567. }else{
  568. $str = date('Ymd',$time);
  569. }
  570. $order_number = ApplyOrder::where('order_number','Like','%'. $str . '%')
  571. ->where('type', $type)
  572. ->max('order_number');
  573. if(empty($order_number)){
  574. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  575. $number = $str . $number;
  576. }else{
  577. $tmp = substr($order_number, -3);
  578. $tmp = $tmp + 1;
  579. //超过999
  580. // if(strlen($tmp) > 3) return '';
  581. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  582. $number = $str . $number;
  583. }
  584. return $number;
  585. }
  586. public function createSQ($data, $user, $type = 0, $status = 1,$time = 0){
  587. if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
  588. if($type == ApplyOrder::type_one){
  589. $storehouse_id = "001";
  590. $storehouse_title = "原料仓";
  591. }elseif ($type == ApplyOrder::type_two){
  592. $storehouse_id = "004";
  593. $storehouse_title = "待清洗原材料仓";
  594. }elseif ($type == ApplyOrder::type_three){
  595. $storehouse_id = "002";
  596. $storehouse_title = "产成品仓";
  597. }elseif($type == ApplyOrder::type_four){
  598. $storehouse_id = "003";
  599. $storehouse_title = "包材、辅料仓" ;
  600. }elseif ($type == ApplyOrder::type_six){
  601. $storehouse_id = "004";
  602. $storehouse_title = "待清洗原材料仓";
  603. }
  604. try{
  605. DB::beginTransaction();
  606. $order_number = $this->setOrderNO($type, $time);
  607. // list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type);
  608. // if(! $status) return [false, $msg];
  609. if(empty($time)) $time = time();
  610. $model = new ApplyOrder();
  611. $model->order_number = $order_number;
  612. $model->apply_id = $user['id'];
  613. $model->apply_time = $time;
  614. $model->storehouse_id = $storehouse_id;
  615. $model->storehouse_title = $storehouse_title;
  616. $model->type = $type;
  617. $model->status = $status; //是否审核
  618. $model->save();
  619. $id = $model->id;
  620. $detail_insert = [];
  621. foreach ($data as $v){
  622. $detail_insert[] = [
  623. 'apply_order_id' => $id,
  624. 'data_id' => $v['id'],
  625. 'quantity' => $v['quantity'] ?? 0,
  626. 'product_no' => $v['product_no'] ?? "",
  627. 'product_title' => $v['product_title'] ?? "",
  628. 'product_size' => $v['product_size'] ?? "",
  629. 'product_unit' => $v['product_unit'] ?? "",
  630. 'technology_name' => $v['technology_name'] ?? "",
  631. 'wood_name' => $v['wood_name'] ?? "",
  632. 'top_product_no' => $v['top_product_no'] ?? "",
  633. 'top_product_title' => $v['top_product_title'] ?? "",
  634. 'type' => $type,
  635. 'storehouse_id' => $storehouse_id,
  636. 'crt_time' => $time,
  637. ];
  638. }
  639. ApplyOrderDetail::insert($detail_insert);
  640. DB::commit();
  641. }catch (\Exception $e){
  642. DB::rollBack();
  643. return [false,$e->getMessage()];
  644. }
  645. return [true, $id];
  646. }
  647. public function createSqFive($data, $user, $type = 0, $status = 1, $time = 0){
  648. if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
  649. try{
  650. DB::beginTransaction();
  651. if(empty($time)) $time = time();
  652. $first = $data[0] ?? [];
  653. $order_number = $this->setOrderNO($type, $time);
  654. $model = new ApplyOrder();
  655. $model->order_number = $order_number;
  656. $model->apply_id = $user['id'];
  657. $model->apply_time = $time;
  658. $model->storehouse_id = $first['storehouse_id'];
  659. $model->storehouse_title = $first['storehouse_title'];
  660. $model->type = $type;
  661. $model->status = $status; //是否审核
  662. $model->save();
  663. $id = $model->id;
  664. $insert = [];
  665. foreach ($data as $value){
  666. $insert[] = [
  667. 'apply_order_id' => $id,
  668. 'data_id' => $value['id'],
  669. 'quantity' => $value['quantity'] ?? 0,
  670. 'product_no' => $value['product_no'] ?? "",
  671. 'product_title' => $value['product_title'] ?? "",
  672. 'product_size' => $value['product_size'] ?? "",
  673. 'product_unit' => $value['product_unit'] ?? "",
  674. 'technology_name' => $value['technology_name'] ?? "",
  675. 'wood_name' => $value['wood_name'] ?? "",
  676. 'top_product_no' => $value['top_product_no'] ?? "",
  677. 'top_product_title' => $value['top_product_title'] ?? "",
  678. 'type' => $type,
  679. 'storehouse_id' => $value['storehouse_id'],
  680. 'crt_time' => $time,
  681. ];
  682. }
  683. ApplyOrderDetail::insert($insert);
  684. DB::commit();
  685. }catch (\Exception $e){
  686. DB::rollBack();
  687. return [false,$e->getMessage()];
  688. }
  689. return [true, ''];
  690. }
  691. //流水
  692. public function createRecord($id){return [true, ''];
  693. $record = ApplyOrderDetail::where('apply_order_id', $id)
  694. ->where('del_time',0)
  695. ->get()->toArray();
  696. if(empty($record)) return [false, '申请单明细数据不存在或已被删除'];
  697. try{
  698. DB::beginTransaction();
  699. $time = time();
  700. $insert = [];
  701. foreach ($record as $value){
  702. if(empty($value['product_no'])) continue;
  703. if($value['type'] == ApplyOrder::type_one){
  704. $number = - $value['quantity'];
  705. }else{
  706. $number = $value['quantity'];
  707. }
  708. $insert[] = [
  709. 'apply_order_id' => $id,
  710. 'product_no' => $value['product_no'],
  711. 'number' => $number,
  712. 'type' => $value['type'],
  713. 'storehouse_id' => $value['storehouse_id'],
  714. 'crt_time' => $time,
  715. ];
  716. }
  717. if(! empty($insert)) InOutRecord::insert($insert);
  718. DB::commit();
  719. }catch (\Exception $e){
  720. DB::rollBack();
  721. return [false,$e->getMessage()];
  722. }
  723. return [true, ''];
  724. }
  725. //报工
  726. public function reportWorkingEdit($data, $user){
  727. list($status,$msg) = $this->reportWorkingRule($data,$user,false);
  728. if(!$status) return [$status,$msg];
  729. DB::beginTransaction();
  730. try{
  731. $time = time();
  732. $model = new ReportWorking();
  733. $model = $model->where('id',$data['id'])->first();
  734. $model->report_time = $data['report_time'];
  735. $model->mark = $data['mark']?? "";
  736. $model->status = $data['auto'] ?? 0;
  737. $model->save();
  738. $id = $model->id;
  739. ReportWorkingDetail::where('del_time',0)->where('report_working_id',$id)->update([
  740. 'del_time' => $time
  741. ]);
  742. //班组下的人
  743. $team_man = [];
  744. $model_t = new EmployeeTeamPermission();
  745. $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();
  746. foreach ($team_array as $v){
  747. $team_man[$v['team_id']][] = $v['employee_id'];
  748. }
  749. $detail_insert = [];
  750. foreach ($data['order_data'] as $v){
  751. ReportMessage::insert([
  752. 'order_id' => $v['id'],
  753. 'quantity' => $v['quantity'] ?? 0,
  754. 'opt_case' => ReportMessage::type_one,
  755. 'crt_time' => $time,
  756. 'user_id' => $user['id'],
  757. ]);
  758. $t = $team_man[$v['team_id']] ?? [];
  759. if(! empty($t)){
  760. foreach ($t as $t_v){
  761. $detail_insert[] = [
  762. 'report_working_id' => $id,
  763. 'data_id' => $v['id'],
  764. 'quantity' => $v['quantity'] ?? 0,
  765. 'process_id' => $v['process_id'] ?? 0,
  766. 'finished_id' => $t_v,
  767. 'team_id' => $v['team_id'],
  768. 'crt_time' => $time,
  769. ];
  770. }
  771. }else{
  772. $detail_insert[] = [
  773. 'report_working_id' => $id,
  774. 'data_id' => $v['id'],
  775. 'quantity' => $v['quantity'] ?? 0,
  776. 'process_id' => $v['process_id'] ?? 0,
  777. 'finished_id' => 0,
  778. 'team_id' => $v['team_id'],
  779. 'crt_time' => $time,
  780. ];
  781. }
  782. }
  783. ReportWorkingDetail::insert($detail_insert);
  784. DB::commit();
  785. }catch (\Exception $e){
  786. DB::rollBack();
  787. return [false,$e->getMessage()];
  788. }
  789. return [true,''];
  790. }
  791. public function getReportRate($data, $user){
  792. if(empty($data['dispatch_id'])) return [false, '派工单id不能为空'];
  793. $dispatch = DispatchSub::where('id', $data['dispatch_id'])
  794. ->select('process_id','dispatch_quantity')
  795. ->get()->toArray();
  796. $array = $dispatch[0] ?? [];
  797. $process_id = $array['process_id'] ?? 0;
  798. $dispatch_quantity = $array['dispatch_quantity'] ?? 0;
  799. $process = Process::where('del_time',0)
  800. ->where('is_need_remain',1)
  801. ->select('id','zj_type','zj_rate')
  802. ->get()->toArray();
  803. $process_id_array = array_column($process,'id');
  804. $process_id_map = array_column($process,null,'id');
  805. if(in_array($process_id, $process_id_array)){
  806. $tmp = $process_id_map[$process_id] ?? [];
  807. if(! empty($tmp) && $tmp['zj_type'] == 2) {
  808. $num1 = ScrappCount::where('del_time',0)
  809. ->where('dispatch_sub_id',$data['dispatch_id'])
  810. ->count();
  811. $total = bcdiv($dispatch_quantity, 0.75);
  812. $num = bcmul($total, bcdiv($tmp['zj_rate'],100,2));
  813. dd($num,$num1,$total);
  814. }
  815. }else{
  816. return [true , ['type' => 1]]; // 不需要质检
  817. }
  818. return [true, ['type' => 0]]; // 错误 或者 全检
  819. }
  820. public function getReportInfo($data){
  821. if(empty($data)) return [];
  822. $process = Process::where('del_time',0)
  823. ->where('is_need_remain',1)
  824. ->select('id','zj_type','zj_rate')
  825. ->get()->toArray();
  826. $process_id_array = array_column($process,'id');
  827. $process_id_map = array_column($process,null,'id');
  828. $num1_map = ScrappCount::where('del_time',0)
  829. ->whereIn('dispatch_sub_id',array_column($data,'id'))
  830. ->select(DB::raw('count(*) as number'), 'dispatch_sub_id')
  831. ->groupBy('dispatch_sub_id')
  832. ->get()->toArray();
  833. $num1_map = array_column($num1_map,'number','dispatch_sub_id');
  834. $return = [];
  835. foreach ($data as $value){
  836. $tmp_1 = [
  837. 'zj_type' => 0,
  838. 'zj_rate' => 0,
  839. 'zj_num' => 0,
  840. 'total_num' => 0,
  841. ];
  842. if(in_array($value['process_id'], $process_id_array)){
  843. $tmp = $process_id_map[$value['process_id']] ?? [];
  844. if(! empty($tmp)) {
  845. $num1 = $num1_map[$value['id']] ?? 0;
  846. $total = bcdiv($value['dispatch_quantity'], 0.75);
  847. $total = $total < 1 ? 1 : $total;
  848. $num = bcmul($total, bcdiv($tmp['zj_rate'],100,2));
  849. $return[$value['id']] = [
  850. 'zj_type' => $tmp['zj_type'],
  851. 'zj_rate' => $tmp['zj_rate'],
  852. 'zj_num' => $num1,
  853. 'total_num' => $num,
  854. ];
  855. }
  856. }else{
  857. $return[$value['id']] = $tmp_1;
  858. }
  859. }
  860. return $return;
  861. }
  862. public function reportWorkingAdd($data,$user){
  863. list($status,$msg) = $this->reportWorkingRule($data,$user);
  864. if(!$status) return [$status,$msg];
  865. DB::beginTransaction();
  866. try{
  867. $time = time();
  868. $model = new ReportWorking();
  869. $model->order_number = $data['order_number'];
  870. $model->report_time = $data['report_time'];
  871. $model->mark = $data['mark']?? "";
  872. $model->crt_id = $user['id'];
  873. $model->status = $data['auto'] ?? 0;
  874. $model->save();
  875. $id = $model->id;
  876. //班组下的人
  877. $team_man = [];
  878. $model_t = new EmployeeTeamPermission();
  879. $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();
  880. foreach ($team_array as $v){
  881. $team_man[$v['team_id']][] = $v['employee_id'];
  882. }
  883. $process = Process::where('del_time',0)
  884. ->where('is_need_remain',1)
  885. ->select('id')
  886. ->get()->toArray();
  887. $process_id = array_column($process,'id');
  888. $detail_insert = [];
  889. foreach ($data['order_data'] as $v){
  890. if(in_array($v['process_id'], $process_id)){
  891. ReportMessage::insert([
  892. 'order_id' => $v['id'],
  893. 'quantity' => $v['quantity'] ?? 0,
  894. 'opt_case' => ReportMessage::type_one,
  895. 'crt_time' => $time,
  896. 'user_id' => $user['id'],
  897. ]);
  898. }
  899. $t = $team_man[$v['team_id']] ?? [];
  900. if(! empty($t)){
  901. foreach ($t as $t_v){
  902. $detail_insert[] = [
  903. 'report_working_id' => $id,
  904. 'data_id' => $v['id'],
  905. 'quantity' => $v['quantity'] ?? 0,
  906. 'process_id' => $v['process_id'] ?? 0,
  907. 'finished_id' => $t_v,
  908. 'team_id' => $v['team_id'],
  909. 'crt_time' => $time,
  910. ];
  911. }
  912. }else{
  913. $detail_insert[] = [
  914. 'report_working_id' => $id,
  915. 'data_id' => $v['id'],
  916. 'quantity' => $v['quantity'] ?? 0,
  917. 'process_id' => $v['process_id'] ?? 0,
  918. 'finished_id' => 0,
  919. 'team_id' => $v['team_id'],
  920. 'crt_time' => $time,
  921. ];
  922. }
  923. }
  924. ReportWorkingDetail::insert($detail_insert);
  925. DB::commit();
  926. }catch (\Exception $e){
  927. DB::rollBack();
  928. return [false,$e->getMessage()];
  929. }
  930. return [true, ''];
  931. }
  932. public function reportWorkingDel($data){
  933. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  934. $apply = ReportWorking::where('id',$data['id'])->first();
  935. if($apply->del_time > 0) return [false,'报工单不存在或已被删除'];
  936. if($apply->state != ReportWorking::state_zero) return [false, '报工单已审核,删除失败'];
  937. $time = time();
  938. try {
  939. DB::beginTransaction();
  940. $apply->del_time = $time;
  941. $apply->save();
  942. ReportWorkingDetail::where('del_time', 0)->where('report_working_id',$data['id'])->update([
  943. 'del_time'=>time()
  944. ]);
  945. DB::commit();
  946. }catch (\Throwable $exception){
  947. DB::rollBack();
  948. return [false, $exception->getMessage()];
  949. }
  950. return [true,''];
  951. }
  952. public function reportWorkingList($data){
  953. $model = ReportWorking::where('del_time',0)
  954. ->select('*')
  955. ->orderBy('id','desc');
  956. if(isset($data['status'])) $model->where('status', $data['status']);
  957. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  958. if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
  959. if(! empty($data['report_time'][0]) && ! empty($data['report_time'][1])) $model->whereBetween('report_time',[$data['report_time'][0],$data['report_time'][1]]);
  960. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  961. if(! empty($data['dispatch_no1'])) {
  962. $dispatch_id = DispatchSub::where('del_time',0)
  963. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no1'].'%')
  964. ->select('id')
  965. ->get()->toArray();
  966. $dispatch_id = array_column($dispatch_id,'id');
  967. $detail = ReportWorkingDetail::where('del_time',0)
  968. ->whereIn('data_id', $dispatch_id)
  969. ->select('report_working_id')
  970. ->get()->toArray();
  971. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  972. }
  973. if(! empty($data['dispatch_no'])) {
  974. $dispatch_id = DispatchSub::where('del_time',0)
  975. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  976. ->select('id')
  977. ->get()->toArray();
  978. $dispatch_id = array_column($dispatch_id,'id');
  979. $detail = ReportWorkingDetail::where('del_time',0)
  980. ->whereIn('data_id', $dispatch_id)
  981. ->select('report_working_id')
  982. ->get()->toArray();
  983. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  984. }
  985. if(! empty($data['sale_order_number'])){
  986. $id = SaleOrdersProduct::where('del_time',0)
  987. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  988. ->select('id')
  989. ->get()->toArray();
  990. $dispatch_id = DispatchSub::where('del_time',0)
  991. ->whereIn('sale_orders_product_id',array_column($id,'id'))
  992. ->select('id')
  993. ->get()->toArray();
  994. $detail = ReportWorkingDetail::where('del_time',0)
  995. ->whereIn('data_id', $dispatch_id)
  996. ->select('report_working_id')
  997. ->get()->toArray();
  998. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  999. }
  1000. $list = $this->limit($model,'',$data);
  1001. $list = $this->reportWorkingfillData($list);
  1002. return [true,$list];
  1003. }
  1004. public function reportWorkingfillData($data){
  1005. if(empty($data['data'])) return $data;
  1006. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  1007. ->pluck('emp_name','id')
  1008. ->toArray();
  1009. $detail = ReportWorkingDetail::where('del_time',0)
  1010. ->whereIn('report_working_id', array_column($data['data'],'id'))
  1011. ->select('data_id','report_working_id')
  1012. ->get()->toArray();
  1013. $map = [];
  1014. foreach ($detail as $value){
  1015. $map[$value['data_id']] = $value['report_working_id'];
  1016. }
  1017. $dispatch = DispatchSub::whereIn('id',array_column($detail,'data_id'))
  1018. ->get()->toArray();
  1019. $map1 = [];
  1020. $team_map = Team::where("del_time",0)->pluck('title','id')->toArray();
  1021. $process_map = Process::where("del_time",0)->pluck('title','id')->toArray();
  1022. foreach ($dispatch as $value){
  1023. $report_working_id = $map[$value['id']] ?? 0;
  1024. $value['team_name'] = $team_map[$value['team_id']] ?? "";
  1025. $value['process_name'] = $process_map[$value['process_id']] ?? "";
  1026. $map1[$report_working_id][] = $value;
  1027. }
  1028. foreach ($data['data'] as $key => $value){
  1029. $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d',$value['report_time']) : '';
  1030. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  1031. $data['data'][$key]['status_title'] = ReportWorking::$state_name[$value['status']] ?? "";
  1032. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  1033. $data['data'][$key]['dispatch'] = $map1[$value['id']] ?? [];
  1034. }
  1035. return $data;
  1036. }
  1037. public function reportWorkingDetail($data){
  1038. if(empty($data['id']) && empty($data['order_number'])) return [false,'ID与OrderNumber不能为空!'];
  1039. $id = $data['id'] ?? 0;
  1040. $order_number = $data['order_number'] ?? "";
  1041. $detail = ReportWorking::where('del_time',0)
  1042. ->when(! empty($id), function ($query) use ($id){
  1043. return $query->where('id', $id);
  1044. })
  1045. ->when(! empty($order_number), function ($query) use ($order_number){
  1046. return $query->where('order_number', $order_number);
  1047. })
  1048. ->first();
  1049. if(empty($detail)) return [false,'报工单不存在或已被删除'];
  1050. $detail = $detail->toArray();
  1051. if(empty($id)) $id = $detail['id'];
  1052. $detail['report_time'] = $detail['report_time'] ? date('Y-m-d',$detail['report_time']) : '';
  1053. $apply_d = ReportWorkingDetail::where('del_time', 0)
  1054. ->where('report_working_id', $id)
  1055. ->get()->toArray();
  1056. $apply_id = array_column($apply_d, 'data_id');
  1057. //派工单
  1058. $d = DispatchSub::where('del_time',0)
  1059. ->whereIn('id', $apply_id)
  1060. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','process_id','product_unit','price','product_size','device_id')
  1061. ->get()->toArray();
  1062. $d_tmp = array_column($d,null,'id');
  1063. $process_map = Process::whereIn('id',array_unique(array_column($d,'process_id')))
  1064. ->pluck('title','id')
  1065. ->toArray();
  1066. $team_map = Team::pluck('title','id')->toArray();
  1067. $device_map = Equipment::whereIn('id',array_unique(array_column($d,'device_id')))
  1068. ->pluck('title','id')
  1069. ->toArray();
  1070. $return = [];
  1071. foreach ($apply_d as $t){
  1072. $tmp = $d_tmp[$t['data_id']] ?? [];
  1073. $str = $t['data_id'] . $t['process_id'];
  1074. if(! isset($return[$str])){
  1075. $return[$str] = [
  1076. 'id' => $t['data_id'] ?? 0,
  1077. 'quantity' => $t['quantity'] ?? 0,
  1078. 'product_no' => $tmp['product_no'] ?? "",
  1079. 'product_title' => $tmp['product_title'] ?? "",
  1080. 'product_size' => $tmp['product_size'] ?? "",
  1081. 'product_unit' => $tmp['product_unit'] ?? "",
  1082. 'technology_name' => $tmp['technology_name'] ?? "",
  1083. 'wood_name' => $tmp['wood_name'] ?? "",
  1084. 'order_no' => $tmp['order_no'] ?? "",
  1085. 'process_id' => $tmp['process_id'] ?? 0,
  1086. 'process_name' => $process_map[$tmp['process_id']] ?? "",
  1087. 'team_id' => $t['team_id'] ?? 0,
  1088. 'team_title' => $team_map[$t['team_id']] ?? '',
  1089. 'device_id' => $tmp['device_id'] ?? 0,
  1090. 'device_title' => $device_map[$tmp['device_id']] ?? '',
  1091. ];
  1092. }
  1093. }
  1094. $detail['order_data'] = array_values($return);
  1095. return [true, $detail];
  1096. }
  1097. public function reportWorkingRule(&$data,$user, $is_add = true){
  1098. if($this->isEmpty($data,'report_time')) return [false,'报工时间不能为空'];
  1099. if(empty($data['order_data'])) return [false, '报工单详细信息不能为空'];
  1100. foreach ($data['order_data'] as $value){
  1101. if(empty($value['id'])) return [false, '报工单详细信息ID不能为空'];
  1102. if(empty($value['quantity'])) return [false, '报工单详细信息数量不能为空'];
  1103. if(empty($value['process_id'])) return [false, '报工单详细信息工序不能为空'];
  1104. if(empty($value['team_id'])) return [false, '报工单详细信息班组不能为空'];
  1105. }
  1106. $id = array_unique(array_column($data['order_data'],'id'));
  1107. $dispatch = DispatchSub::where('del_time',0)
  1108. ->whereIn('id', $id)
  1109. ->select('id', 'dispatch_no', 'status', 'dispatch_quantity','order_product_id')
  1110. ->get()->toArray();
  1111. if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
  1112. $count = array_unique(array_column($dispatch,'order_product_id'));
  1113. if(count($count) > 1) return [false, '批量报工时请选择同一生产订单下的派工单'];
  1114. if($is_add){
  1115. $data['order_number'] = $this->setOrderNO2();
  1116. if(empty($data['order_number'])) return [false, '报工单唯一标识生成失败'];
  1117. }else{
  1118. if(empty($data['id'])) return [false, '报工单唯一标识不能为空'];
  1119. if(empty($data['order_number'])) return [false, '报工单唯一标识不能为空'];
  1120. $apply = ReportWorking::where('id', $data['id'])->where('del_time', 0)->first();
  1121. if(empty($apply)) return [false, '报工单不存在或已被删除'];
  1122. if($apply->status != 0) return [false, '报工单已审核,编辑失败'];
  1123. }
  1124. list($status, $msg) = $this->compareQuantity($dispatch, $data);
  1125. if(! $status) return [false, $msg];
  1126. $em = new EmployeeService();
  1127. $data['auto'] = $em->is_auto($user, "wgbg_auto");
  1128. return [true,''];
  1129. }
  1130. public function compareQuantity($dispatch, $data){
  1131. $id = $data['id'] ?? 0;
  1132. $result = ReportWorkingDetail::where('del_time',0)
  1133. ->when(! empty($id), function ($query) use ($id){
  1134. return $query->where('report_working_id', '<>', $id);
  1135. })
  1136. ->where('data_id', array_column($dispatch, 'id'))
  1137. ->select('data_id as dispatch_id','quantity')
  1138. ->get()
  1139. ->toArray();
  1140. $map1 = [];
  1141. foreach ($result as $value){
  1142. if(isset($map1[$value['dispatch_id']])){
  1143. $tmp = bcadd($map1[$value['dispatch_id']], $value['quantity'], 3);
  1144. $map1[$value['dispatch_id']] = $tmp;
  1145. }else{
  1146. $map1[$value['dispatch_id']] = $value['quantity'];
  1147. }
  1148. }
  1149. $map2 = array_column($dispatch,null,'id');
  1150. foreach ($data['order_data'] as $value){
  1151. $tmp = $map2[$value['id']] ?? [];
  1152. $total = $tmp['dispatch_quantity'] ?? 0;
  1153. $dispatch_no = $tmp['dispatch_no'] ?? "";
  1154. $save = $map1[$value['id']] ?? 0;
  1155. $save_total = bcadd($value['quantity'], $save, 3);
  1156. if($save_total >= $total) return [false, "派工单号:" . $dispatch_no . "的报工总数量不能超过" . $total . "吨,目前累计报工" . $save . "吨"];
  1157. }
  1158. return [true, ''];
  1159. }
  1160. public function setOrderNO2($time = 0){
  1161. if(empty($time)) {
  1162. $str = date('Ymd',time());
  1163. }else{
  1164. $str = date('Ymd', $time);
  1165. }
  1166. $model = new ReportWorking();
  1167. $order_number = $model->where('order_number','Like','%'. $str . '%')
  1168. ->max('order_number');
  1169. if(empty($order_number)){
  1170. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  1171. $number = $str . $number;
  1172. }else{
  1173. $tmp = substr($order_number, -3);
  1174. $tmp = $tmp + 1;
  1175. //超过999
  1176. // if(strlen($tmp) > 3) return '';
  1177. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  1178. $number = $str . $number;
  1179. }
  1180. return $number;
  1181. }
  1182. public function applyOrderReportList($data){
  1183. if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!'];
  1184. if(empty($data['type'])) return [false, 'TYPE不能为空!'];
  1185. $model = ApplyOrder::where('del_time',0)
  1186. ->where('apply_time',">=",$data['apply_time'][0])
  1187. ->where('apply_time',"<=",$data['apply_time'][1])
  1188. ->where('type',$data['type'])
  1189. ->select('id','order_number','apply_time','mark','status','storehouse_title')
  1190. ->orderBy('id','desc');
  1191. if(isset($data['status'])) $model->where('status', $data['status']);
  1192. if(! empty($data['pc_number'])) {
  1193. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  1194. $dispatch_id = DispatchSub::where('del_time',0)
  1195. ->where('out_order_no', 'LIKE', '%'.$data['pc_number'].'%')
  1196. ->select('id')
  1197. ->get()->toArray();
  1198. $dispatch_id = array_column($dispatch_id,'id');
  1199. $detail = ApplyOrderDetail::where('del_time',0)
  1200. ->where('type', $data['type'])
  1201. ->whereIn('data_id', $dispatch_id)
  1202. ->select('apply_order_id')
  1203. ->get()->toArray();
  1204. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1205. }elseif($data['type'] == ApplyOrder::type_five){
  1206. $zj_id = ScrappCount::where('del_time',0)
  1207. ->where('out_order_no', 'LIKE', '%'.$data['pc_number'].'%')
  1208. ->select('id')
  1209. ->get()->toArray();
  1210. $zj_id = array_column($zj_id,'id');
  1211. $detail = ApplyOrderDetail::where('del_time',0)
  1212. ->where('type', $data['type'])
  1213. ->whereIn('data_id', $zj_id)
  1214. ->select('apply_order_id')
  1215. ->get()->toArray();
  1216. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1217. }else{
  1218. $no = SaleOrdersProduct::where('del_time',0)
  1219. ->where('out_order_no', 'LIKE', '%'.$data['pc_number'].'%')
  1220. ->select('order_no')
  1221. ->get()->toArray();
  1222. $no = array_unique(array_column($no,'order_no'));
  1223. $box_id = Box::where('del_time',0)
  1224. ->whereIn('top_order_no', $no)
  1225. ->select('id')
  1226. ->get()->toArray();
  1227. $box_id = array_column($box_id,'id');
  1228. $detail = ApplyOrderDetail::where('del_time',0)
  1229. ->where('type', $data['type'])
  1230. ->whereIn('data_id', $box_id)
  1231. ->select('apply_order_id')
  1232. ->get()->toArray();
  1233. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1234. }
  1235. }
  1236. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){
  1237. $model->where('crt_time',">=",$data['crt_time'][0]);
  1238. $model->where('crt_time',"<=",$data['crt_time'][1]);
  1239. }
  1240. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  1241. if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
  1242. if(! empty($data['sale_order_number'])){
  1243. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  1244. $dispatch_id = DispatchSub::where('del_time',0)
  1245. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  1246. ->select('id')
  1247. ->get()->toArray();
  1248. $dispatch_id = array_column($dispatch_id,'id');
  1249. $detail = ApplyOrderDetail::where('del_time',0)
  1250. ->where('type', $data['type'])
  1251. ->whereIn('data_id', $dispatch_id)
  1252. ->select('apply_order_id')
  1253. ->get()->toArray();
  1254. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1255. }elseif($data['type'] == ApplyOrder::type_five){
  1256. $zj_id = ScrappCount::where('del_time',0)
  1257. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  1258. ->select('id')
  1259. ->get()->toArray();
  1260. $zj_id = array_column($zj_id,'id');
  1261. $detail = ApplyOrderDetail::where('del_time',0)
  1262. ->where('type', $data['type'])
  1263. ->whereIn('data_id', $zj_id)
  1264. ->select('apply_order_id')
  1265. ->get()->toArray();
  1266. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1267. }else{
  1268. $no = SaleOrdersProduct::where('del_time',0)
  1269. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  1270. ->select('order_no')
  1271. ->get()->toArray();
  1272. $no = array_unique(array_column($no,'order_no'));
  1273. $box_id = Box::where('del_time',0)
  1274. ->whereIn('top_order_no', $no)
  1275. ->select('id')
  1276. ->get()->toArray();
  1277. $box_id = array_column($box_id,'id');
  1278. $detail = ApplyOrderDetail::where('del_time',0)
  1279. ->where('type', $data['type'])
  1280. ->whereIn('data_id', $box_id)
  1281. ->select('apply_order_id')
  1282. ->get()->toArray();
  1283. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1284. }
  1285. }
  1286. if(! empty($data['dispatch_no'])){
  1287. $dispatch_id = DispatchSub::where('del_time',0)
  1288. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  1289. ->select('id')
  1290. ->get()->toArray();
  1291. $dispatch_id = array_column($dispatch_id,'id');
  1292. $detail = ApplyOrderDetail::where('del_time',0)
  1293. ->where('type', $data['type'])
  1294. ->whereIn('data_id', $dispatch_id)
  1295. ->select('apply_order_id')
  1296. ->get()->toArray();
  1297. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1298. }
  1299. $list = $model->get()->toArray();
  1300. if(empty($list)) return [true, []];
  1301. $apply_id = array_column($list, 'id');
  1302. //明细数据
  1303. $result = ApplyOrderDetail::where('del_time',0)
  1304. ->whereIn('apply_order_id', $apply_id)
  1305. ->get()->toArray();
  1306. if(empty($result)) return [true, []];
  1307. //组织主表需要的数据
  1308. $list_map = $this->makeListMap($list, $result, $data['type']);
  1309. $data_id = array_unique(array_column($result, 'data_id'));
  1310. list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']);
  1311. $return = [];
  1312. $d_tmp = array_column($d,null,'id');
  1313. foreach ($result as $t){
  1314. $tmp = $d_tmp[$t['data_id']] ?? [];
  1315. $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
  1316. $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
  1317. $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
  1318. $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
  1319. if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
  1320. $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
  1321. }else{
  1322. $order_no = $tmp['order_no'] ?? "";
  1323. }
  1324. if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
  1325. $product_unit = "吨";
  1326. }elseif($t['type'] == ApplyOrder::type_four){
  1327. $product_unit = "只";
  1328. }else{
  1329. $product_unit = $t['product_unit'] ?? "";
  1330. }
  1331. $tmp = $list_map[$t['apply_order_id']] ?? [];
  1332. $return[] = [
  1333. 'order_number' => $tmp['order_number'],
  1334. 'sale_order_number' => $tmp['sale_order_number'],
  1335. 'apply_time' => $tmp['apply_time'],
  1336. 'mark' => $tmp['mark'],
  1337. 'status_title' => $tmp['status_title'],
  1338. 'storehouse_title' => $tmp['storehouse_title'],
  1339. 'id' => $t['data_id'] ?? 0,
  1340. // 'main_id' => $t['id'],
  1341. 'quantity' => $t['quantity'] ?? 0,
  1342. 'product_no' => $t['product_no'] ?? "",
  1343. 'product_title' => $t['product_title'] ?? "",
  1344. 'product_size' => $t['product_size'] ?? "",
  1345. 'product_unit' => $product_unit,
  1346. 'top_product_title' => $top_product_title,
  1347. 'top_product_no' => $top_product_no,
  1348. 'technology_name' => $technology_name, //颜色
  1349. 'wood_name' => $wood_name,
  1350. 'order_no' => $order_no,
  1351. ];
  1352. }
  1353. return [true, array_values($return)];
  1354. }
  1355. private function getDetailList($apply_id, $type){
  1356. $d = $d_no_map = [];
  1357. if($type == ApplyOrder::type_one){
  1358. $d = DispatchSub::where('del_time',0)
  1359. ->whereIn('id', $apply_id)
  1360. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  1361. ->get()->toArray();
  1362. $args = "";
  1363. foreach ($d as $value){
  1364. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  1365. }
  1366. $args = rtrim($args, 'OR ');
  1367. $d_no_map = [];
  1368. $d_no = DispatchSub::where('del_time',0)
  1369. ->whereRaw($args)
  1370. ->select('dispatch_no as order_no','order_product_id','crt_time')
  1371. ->get()->toArray();
  1372. foreach ($d_no as $value){
  1373. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  1374. 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'];
  1375. }else{
  1376. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  1377. }
  1378. }
  1379. }elseif ($type == ApplyOrder::type_two){
  1380. $d = DispatchSub::where('del_time',0)
  1381. ->whereIn('id', $apply_id)
  1382. ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
  1383. ->get()->toArray();
  1384. $args = "";
  1385. foreach ($d as $value){
  1386. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  1387. }
  1388. $args = rtrim($args, 'OR ');
  1389. $d_no_map = [];
  1390. $d_no = DispatchSub::where('del_time',0)
  1391. ->whereRaw($args)
  1392. ->select('dispatch_no as order_no','order_product_id','crt_time')
  1393. ->get()->toArray();
  1394. foreach ($d_no as $value){
  1395. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  1396. 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'];
  1397. }else{
  1398. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  1399. }
  1400. }
  1401. }elseif ($type == ApplyOrder::type_three){
  1402. $d = Box::where('del_time',0)
  1403. ->whereIn('id', $apply_id)
  1404. ->select('id', 'order_no')
  1405. ->get()->toArray();
  1406. }elseif ($type == ApplyOrder::type_four){
  1407. $d = Box::where('del_time',0)
  1408. ->whereIn('id', $apply_id)
  1409. ->select('id', 'order_no')
  1410. ->get()->toArray();
  1411. }elseif($type == ApplyOrder::type_five){
  1412. $d = ScrappCount::where('del_time',0)
  1413. ->whereIn('id', $apply_id)
  1414. ->select('id', 'order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  1415. ->get()->toArray();
  1416. }elseif ($type == ApplyOrder::type_six){
  1417. $d = Box::where('del_time',0)
  1418. ->whereIn('id', $apply_id)
  1419. ->select('id', 'order_no')
  1420. ->get()->toArray();
  1421. }
  1422. return [$d, $d_no_map];
  1423. }
  1424. private function makeListMap($data, $order, $type){
  1425. if(empty($data)) return [];
  1426. //申请单 所有的数据id
  1427. $order_map = $data_id = [];
  1428. foreach ($order as $value){
  1429. $order_map[$value['apply_order_id']][] = $value['data_id'];
  1430. $data_id[] = $value['data_id'];
  1431. }
  1432. if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
  1433. $map1 = DispatchSub::whereIn('id', array_unique($data_id))
  1434. ->pluck('out_order_no','id')
  1435. ->toArray();
  1436. }elseif($type == ApplyOrder::type_five){
  1437. $map3 = ScrappCount::whereIn('id', array_unique($data_id))
  1438. ->pluck('out_order_no','id')
  1439. ->toArray();
  1440. }else{
  1441. $map2 = Box::whereIn('id', array_unique($data_id))
  1442. ->pluck('top_order_no','id')
  1443. ->toArray();
  1444. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
  1445. ->pluck('out_order_no','order_no')
  1446. ->toArray();
  1447. }
  1448. foreach ($data as $key => $value){
  1449. $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
  1450. $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
  1451. $order_tmp = $order_map[$value['id']] ?? [];
  1452. $order_tmp = array_unique($order_tmp);
  1453. $sale_order_number = [];
  1454. if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
  1455. foreach ($order_tmp as $v){
  1456. $str = $map1[$v] ?? '';
  1457. if(empty($str)) continue;
  1458. $sale_order_number[] = $str;
  1459. }
  1460. $sale_order_number = array_unique($sale_order_number);
  1461. $sale_order_number = implode(',',$sale_order_number);
  1462. $data[$key]['sale_order_number'] = $sale_order_number;
  1463. }elseif ($type == ApplyOrder::type_five){
  1464. foreach ($order_tmp as $v){
  1465. $str = $map3[$v] ?? '';
  1466. if(empty($str)) continue;
  1467. $sale_order_number[] = $str;
  1468. }
  1469. $sale_order_number = array_unique($sale_order_number);
  1470. $sale_order_number = implode(',',$sale_order_number);
  1471. $data[$key]['sale_order_number'] = $sale_order_number;
  1472. }else{
  1473. foreach ($order_tmp as $v){
  1474. $m2_t = $map2[$v] ?? '';
  1475. $m2_t = $map2_fin[$m2_t] ?? "";
  1476. if(empty($m2_t)) continue;
  1477. $sale_order_number[] = $m2_t;
  1478. }
  1479. $sale_order_number = array_unique($sale_order_number);
  1480. $sale_order_number = implode(',',$sale_order_number);
  1481. $data[$key]['sale_order_number'] = $sale_order_number;
  1482. }
  1483. }
  1484. return array_column($data,null,'id');
  1485. }
  1486. public function editZj($data, $user){
  1487. list($status, $msg) = $this->editZjRule($data);
  1488. if(! $status) return [false, $msg];
  1489. $order_map = array_column($msg,null,'data_id');
  1490. try{
  1491. DB::beginTransaction();
  1492. $time = time();
  1493. $insert = [];
  1494. $max_id = $this->getDailyId();
  1495. list($status,$msg) = $this->limitingSendRequestBackg("quantity_id_" . $max_id);
  1496. if(! $status) return [false,$msg];
  1497. $get_order_number = $insert_wg = [];
  1498. foreach ($data['data'] as $value){
  1499. $order_t = $order_map[$value['data_id']] ?? [];
  1500. $order = $order_t['dispatch'] ?? [];
  1501. if(empty($value['waste'])){
  1502. $max_id += 1;
  1503. $order_number = date("Ymd") . str_pad($max_id,3,'0',STR_PAD_LEFT);
  1504. $get_order_number[] = $order_number;
  1505. $insert[] = [
  1506. 'sale_orders_product_id' => $order['sale_orders_product_id'],
  1507. 'order_product_id' => $order['order_product_id'],
  1508. 'dispatch_sub_id' => $order['id'],
  1509. 'out_order_no' => $order['out_order_no'],
  1510. 'order_no' => $order['order_no'],
  1511. 'customer_no' => $order['customer_no'],
  1512. 'customer_name' => $order['customer_name'],
  1513. 'product_no' => $order['product_no'],
  1514. 'product_title' => $order['product_title'],
  1515. 'product_size' => $order['product_size'],
  1516. 'product_unit' => $order['product_unit'],
  1517. 'technology_material' => $order['technology_material'],
  1518. 'technology_name' => $order['technology_name'],
  1519. 'wood_name' => $order['wood_name'],
  1520. 'price' => $order['price'],
  1521. 'process_mark' => $order['process_mark'],
  1522. 'table_body_mark' => $order['table_body_mark'],
  1523. 'table_header_mark' => $order['table_header_mark'],
  1524. 'crt_id' => $user['id'],
  1525. 'crt_time' => $time,
  1526. 'scrapp_num' => 0,
  1527. 'scrapp_id' => 0,
  1528. 'team_id' => $data['team_id'],
  1529. 'finished_id' => $user['id'],
  1530. 'equipment_id' => $data['equipment_id'],
  1531. 'order_number' => $order_number,
  1532. 'process_id' => $order['process_id'],
  1533. 'quantity' => $value['num'],
  1534. 'report_id' => $data['id'],
  1535. 'result' => 0,
  1536. 'warehouse_no' => "",
  1537. 'warehouse_title' => "",
  1538. 'daily_id' => $max_id,
  1539. ];
  1540. }else{
  1541. foreach ($value['waste'] as $v){
  1542. $max_id += 1;
  1543. $order_number = date("Ymd") . str_pad($max_id,3,'0',STR_PAD_LEFT);
  1544. $get_order_number[] = $order_number;
  1545. $insert[] = [
  1546. 'sale_orders_product_id' => $order['sale_orders_product_id'],
  1547. 'order_product_id' => $order['order_product_id'],
  1548. 'dispatch_sub_id' => $order['id'],
  1549. 'out_order_no' => $order['out_order_no'],
  1550. 'order_no' => $order['order_no'],
  1551. 'customer_no' => $order['customer_no'],
  1552. 'customer_name' => $order['customer_name'],
  1553. 'product_no' => $order['product_no'],
  1554. 'product_title' => $order['product_title'],
  1555. 'product_size' => $order['product_size'],
  1556. 'product_unit' => $order['product_unit'],
  1557. 'technology_material' => $order['technology_material'],
  1558. 'technology_name' => $order['technology_name'],
  1559. 'wood_name' => $order['wood_name'],
  1560. 'price' => $order['price'],
  1561. 'process_mark' => $order['process_mark'],
  1562. 'table_body_mark' => $order['table_body_mark'],
  1563. 'table_header_mark' => $order['table_header_mark'],
  1564. 'crt_id' => $user['id'],
  1565. 'crt_time' => $time,
  1566. 'scrapp_num' => $v['num'],
  1567. 'scrapp_id' => $v['scrapp_id'],
  1568. 'team_id' => $data['team_id'],
  1569. 'finished_id' => $user['id'],
  1570. 'equipment_id' => $data['equipment_id'],
  1571. 'order_number' => $order_number,
  1572. 'process_id' => $order['process_id'],
  1573. 'quantity' => $value['num'],
  1574. 'report_id' => $data['id'],
  1575. 'result' => $v['result'],
  1576. 'warehouse_no' => $v['warehouse_no'],
  1577. 'warehouse_title' => $v['warehouse_title'],
  1578. 'daily_id' => $max_id,
  1579. ];
  1580. }
  1581. }
  1582. if($value['num'] > 0) $insert_wg[] = [
  1583. 'dispatch_id' => $value['data_id'],
  1584. 'num' => $value['num']
  1585. ];
  1586. }
  1587. //质检单 不良品单 会记录报工单的id
  1588. if(! empty($insert)) ScrappCount::insert($insert);
  1589. //获取新增的单据
  1590. $result = ScrappCount::whereIn('order_number',$get_order_number)
  1591. ->select('scrapp_num as num','id','dispatch_sub_id','warehouse_no','warehouse_title','result','product_no')
  1592. ->get()->toArray();
  1593. //完工入库申请单 半成品
  1594. list($status, $msg) = $this->insertWg($insert_wg, $user);
  1595. if(! $status) return [false, $msg];
  1596. //获取写入的数据 写其它的业务逻辑
  1597. list($status, $msg) = $this->insertOther($result, $user, $time);
  1598. if(! $status) return [false, $msg];
  1599. //反写 根据质检单上的派工单id 反写上游单据
  1600. $this->writeDispatchQuantity(array_column($result,'dispatch_sub_id'));
  1601. DB::commit();
  1602. }catch (\Throwable $exception){
  1603. DB::rollBack();
  1604. return [false, $exception->getMessage() . '|' . $exception->getLine()];
  1605. }
  1606. return [true, ''];
  1607. }
  1608. public function editZjRule($data){
  1609. if(empty($data['id'])) return [false, '报工单ID不能为空'];
  1610. $order = ReportWorkingDetail::where('del_time',0)
  1611. ->where('report_working_id', $data['id'])
  1612. ->get()->toArray();
  1613. if(empty($order)) return [false, "报工单信息不存在或已被删除"];
  1614. $map = [];
  1615. foreach ($order as $value){
  1616. if(! isset($map[$value['data_id']])) $map[$value['data_id']] = $value['quantity'];
  1617. }
  1618. $sList = ScrappCount::where('del_time',0)
  1619. ->where('report_id', $data['id'])
  1620. ->select('dispatch_sub_id as dispatch_id','scrapp_num','quantity')
  1621. ->get()->toArray();
  1622. $sMap = [];
  1623. foreach ($sList as $value){
  1624. $num = bcadd($value['scrapp_num'], $value['quantity'],3);
  1625. if(isset($sMap[$value['dispatch_id']])){
  1626. $t = bcadd($sMap[$value['dispatch_id']], $num,3);
  1627. $sMap[$value['dispatch_id']] = $t;
  1628. }else{
  1629. $sMap[$value['dispatch_id']] = $num;
  1630. }
  1631. }
  1632. if(empty($data['team_id'])) return [false, '质检班组不能为空'];
  1633. if(empty($data['equipment_id'])) return [false, '设备不能为空'];
  1634. if(empty($data['data'])) return [false, '质检信息不能为空'];
  1635. $data_id = [];
  1636. foreach ($data['data'] as $value){
  1637. if(empty($value['data_id'])) return [false, 'dataID不能为空'];
  1638. if(! isset($value['num'])) return [false, 'num不能为空'];
  1639. $num = 0;
  1640. if(! empty($value['waste'])){
  1641. foreach ($value['waste'] as $v){
  1642. if(empty($v['num'])) return [false, "质检数量不能为空"];
  1643. if(empty($v['scrapp_id'])) return [false, "请选择质检原因"];
  1644. if(empty($v['result'])) return [false, '请选择质检产品处理结果'];
  1645. if($v['result'] == ReportWorking::type_two && (empty($v['warehouse_no']) || empty($v['warehouse_title']))) return [false, '请选择产品去向仓库'];
  1646. $num = bcadd($v['num'], $num, 3);
  1647. }
  1648. }
  1649. $total = bcadd($num, $value['num'],3);
  1650. $total2 = $map[$value['data_id']] ?? 0;
  1651. if($total > $total2) return [false, '总数量不能超过报工数量'];
  1652. $tmp = $sMap[$value['data_id']] ?? 0;
  1653. if($tmp >= $total) return [false, '报工单已完成报工质检'];
  1654. $data_id[] = $value['data_id'];
  1655. }
  1656. $dispatch = DispatchSub::whereIn('id',$data_id)->get()->toArray();
  1657. $dispatch_map = array_column($dispatch,null,'id');
  1658. foreach ($order as $key => $value){
  1659. if(! in_array($value['data_id'], $data_id)){
  1660. unset($order[$key]);
  1661. continue;
  1662. }
  1663. $order[$key]['dispatch'] = $dispatch_map[$value['data_id']] ?? [];
  1664. }
  1665. return [true, array_values($order)];
  1666. }
  1667. public function getDailyId($time = 0)
  1668. {
  1669. if(empty($time)) $time = time();
  1670. $today_start = strtotime(date("Y-m-d 00:00:00",$time));
  1671. $today_end = strtotime(date("Y-m-d 23:59:59",$time));
  1672. // 获取今天的最大daily_id
  1673. $maxDailyId = ScrappCount::where('crt_time', '>=', $today_start)
  1674. ->where('crt_time', '<=', $today_end)
  1675. ->max(DB::raw('daily_id + 0'));
  1676. return $maxDailyId ?? 0;
  1677. }
  1678. public function insertWg($all_data, $user){
  1679. if(empty($all_data)) return [true, ''];
  1680. $dispatchList = DispatchSub::whereIn('id', array_column($all_data,'dispatch_id'))->get()->toArray();
  1681. //是否最后一道工序
  1682. $orders = OrdersProduct::whereIn('id', array_unique(array_column($dispatchList, 'order_product_id')))
  1683. ->select('id','process_id')
  1684. ->get()->toArray();
  1685. $map = [];
  1686. foreach ($orders as $value){
  1687. $p = explode(",", $value['process_id']);
  1688. $lastElement = end($p);
  1689. $map[$value['id']] = $lastElement;
  1690. }
  1691. $all_data_map = array_column($all_data,null,'dispatch_id');
  1692. $need_insert = [];
  1693. foreach ($dispatchList as $value){
  1694. $num = $all_data_map[$value['id']] ?? [];
  1695. $num = $num['num'] ?? 0;
  1696. if(! $num) continue;
  1697. if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id']){
  1698. $tmp = [
  1699. 'id' => $value['id'],
  1700. 'quantity' => $num,
  1701. 'product_no' => $value['product_no'] ?? "",
  1702. 'product_title' => $value['product_title'] ?? "",
  1703. 'product_size' => $value['product_size'] ?? "",
  1704. 'product_unit' => $value['product_unit'] ?? "",
  1705. ];
  1706. $need_insert[] = $tmp;
  1707. }
  1708. }
  1709. //生成完工入库申请单
  1710. $service = new BoxService();
  1711. list($status, $msg) = $service->createWGSQ($need_insert, $user);
  1712. if(! $status) return [false, $msg];
  1713. return [true, ''];
  1714. }
  1715. public function insertOther($all_data, $user, $time){
  1716. try {
  1717. $result = [];
  1718. foreach ($all_data as $value){
  1719. if($value['result'] > 0) $result[] = $value;
  1720. }
  1721. if(empty($result)) return [true, ''];
  1722. $dispatchList = DispatchSub::whereIn('id', array_column($result,'dispatch_sub_id'))->get()->toArray();
  1723. $dispatch_map = array_column($dispatchList,null,'id');
  1724. //是否自动审核
  1725. $em = new EmployeeService();
  1726. $auto = $em->is_auto($user, "pg_auto");
  1727. $dispatch_no = (new DispatchService())->setOrderNO();
  1728. if(! $dispatch_no) return [false,'返工派工单号生成失败!'];
  1729. $prefix = substr($dispatch_no, 0, -3);// "20250611"
  1730. $startNum = (int)substr($dispatch_no, -3); // 7
  1731. $is_first = true;
  1732. $dispatch_main = $dispatch = $is_need_ll = $is_need_warehouse = [];
  1733. foreach ($result as $value){
  1734. //派工单
  1735. $t = $dispatch_map[$value['dispatch_sub_id']] ?? [];
  1736. if(! $is_first){
  1737. $startNum += 1;
  1738. $number = str_pad($startNum,3,'0',STR_PAD_LEFT);
  1739. $number = $prefix . $number;
  1740. $dispatch_no_t = $number;
  1741. }else{
  1742. $dispatch_no_t = $dispatch_no;
  1743. $is_first = false;
  1744. }
  1745. if($value['result'] == ReportWorking::type_one){
  1746. //重新入生产流程 需要生成返工派工单
  1747. }else{
  1748. //入仓库 那么需要生成质检入库申请单 生成领料申请单 生成返工派工单
  1749. //'id','dispatch_quantity as quantity',"product_no","order_product_id","product_no",'technology_name','crt_time'
  1750. $is_need_ll[] = $dispatch_no_t;
  1751. $is_need_warehouse[] = [
  1752. 'warehouse_no' => $value['warehouse_no'],
  1753. 'warehouse_title' => $value['warehouse_title'],
  1754. ];
  1755. }
  1756. $dispatch_main[] = ['dispatch_no' => $dispatch_no_t,'crt_time' => $time];
  1757. $dispatch[] = [
  1758. 'order_product_id' => $t['order_product_id'],
  1759. 'order_no' => $t['order_no'],
  1760. 'product_no' => $t['product_no'],
  1761. 'product_title' => $t['product_title'],
  1762. 'product_size' => $t['product_size'],
  1763. 'product_unit' => $t['product_unit'],
  1764. 'production_quantity' => $t['production_quantity'],
  1765. 'dispatch_quantity' => $value['num'],
  1766. 'technology_material' => $t['technology_material'],
  1767. 'technology_name' => $t['technology_name'],
  1768. 'wood_name' => $t['wood_name'],
  1769. 'process_mark' => $t['process_mark'],
  1770. 'table_body_mark' => $t['table_body_mark'],
  1771. 'table_header_mark' => $t['table_header_mark'],
  1772. 'crt_time' => $time,
  1773. 'status' => $auto,
  1774. 'dispatch_no' => $dispatch_no_t,
  1775. 'process_id' => $t['process_id'],
  1776. 'dispatch_time_start' => $t['dispatch_time_start'],
  1777. 'dispatch_time_end' => $t['dispatch_time_end'],
  1778. 'crt_id' => $user['id'],
  1779. 'sale_orders_product_id' => $t['sale_orders_product_id'],
  1780. 'out_order_no' => $t['out_order_no'],
  1781. 'out_order_no_time' => $t['out_order_no_time'],
  1782. 'price' => $t['price'],
  1783. 'customer_no' => $t['customer_no'],
  1784. 'customer_name' => $t['customer_name'],
  1785. 'pre_shipment_time' => $t['pre_shipment_time'],
  1786. 'team_id' => $t['team_id'],
  1787. 'device_id' => $t['device_id'],
  1788. 'type' => 1,
  1789. ];
  1790. }
  1791. Dispatch::insert($dispatch_main);
  1792. DispatchSub::insert($dispatch);
  1793. if(! empty($is_need_ll)){
  1794. //获取上一次插入订单的所有数据
  1795. $insert = DispatchSub::whereIn('dispatch_no', $is_need_ll)
  1796. ->where('crt_time',$time)
  1797. ->where('crt_id',$user['id'])
  1798. ->select('id','dispatch_quantity as quantity',"product_no")
  1799. ->get()->toArray();
  1800. //领料申请单
  1801. list($status, $msg) = (new DispatchService())->createSQ($insert, $user);
  1802. if(! $status) return [false, $msg];
  1803. //质检入库申请单 原材料
  1804. list($status, $msg) = $this->createZJSQ($all_data, $user, $is_need_warehouse);
  1805. if(! $status) return [false, $msg];
  1806. }
  1807. return [true, ''];
  1808. }catch (\Throwable $exception){
  1809. return [false, $exception->getMessage()];
  1810. }
  1811. }
  1812. public function createZJSQ($insert_data, $user, $warehouse, $time = 0){
  1813. try {
  1814. DB::beginTransaction();
  1815. //是否自动审核质检入库申请单
  1816. $em = new EmployeeService();
  1817. $auto = $em->is_auto($user, "zjsq_auto");
  1818. $return = [];
  1819. $product_no = array_unique(array_column($insert_data,'product_no'));
  1820. //获取原料
  1821. $service = new FyyOrderService();
  1822. list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
  1823. if($status) $return = $msg;
  1824. //组织原材料写入数据
  1825. $insert = [];
  1826. foreach ($insert_data as $key => $value){
  1827. $quantity = 0;
  1828. if(! empty($value['quantity'])) {
  1829. $quantity = $value['quantity'];
  1830. }elseif (! empty($value['dispatch_quantity'])){
  1831. $quantity = $value['dispatch_quantity'];
  1832. }elseif (! empty($value['num'])){
  1833. $quantity = $value['num'];
  1834. }
  1835. $t = $return[$value['product_no']] ?? [];
  1836. $tmp = [
  1837. 'id' => $value['id'],
  1838. 'quantity' => $quantity,
  1839. 'product_no' => "",
  1840. 'product_title' => "",
  1841. 'product_size' => "",
  1842. 'product_unit' => "",
  1843. 'storehouse_id' => $warehouse[$key]['warehouse_no'] ?? "",
  1844. 'storehouse_title' => $warehouse[$key]['warehouse_title'] ?? "",
  1845. ];
  1846. if(! empty($t)){
  1847. foreach ($t as $v){
  1848. $tmp['product_no'] = $v['product_no'];
  1849. $tmp['product_title'] = $v['product_title'];
  1850. $tmp['product_size'] = $v['product_size'];
  1851. $tmp['product_unit'] = $v['product_unit'];
  1852. $insert[] = $tmp;
  1853. }
  1854. }else{
  1855. $tmp['quantity'] = 0;
  1856. $insert[] = $tmp;
  1857. }
  1858. }
  1859. //生成质检入库申请单
  1860. $service = new ApplyOrderService();
  1861. list($status, $msg) = $service->createSqFive($insert, $user, ApplyOrder::type_five, $auto, $time);
  1862. if(! $status) {
  1863. DB::rollBack();
  1864. return [false, $msg];
  1865. }
  1866. DB::commit();
  1867. }catch (\Throwable $exception){
  1868. DB::rollBack();
  1869. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  1870. }
  1871. return [true, ''];
  1872. }
  1873. //反写
  1874. public function writeDispatchQuantity($dispatch_id){
  1875. $result = ScrappCount::where('del_time',0)
  1876. ->whereIn('dispatch_sub_id', $dispatch_id)
  1877. ->select('dispatch_sub_id','quantity','scrapp_num','crt_time','process_id','order_product_id')
  1878. ->get()->toArray();
  1879. //是否最后一道工序
  1880. $orders = OrdersProduct::whereIn('id', array_unique(array_column($result, 'order_product_id')))
  1881. ->select('id','process_id')
  1882. ->get()->toArray();
  1883. $map = [];
  1884. foreach ($orders as $value){
  1885. $p = explode(",", $value['process_id']);
  1886. $lastElement = end($p);
  1887. $map[$value['id']] = $lastElement;
  1888. }
  1889. //汇总这个派工单一共质检良品数量 但是同一时间下的 只取一次
  1890. $update = $flag = $need_update = [];
  1891. foreach ($result as $value){
  1892. if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id'] && ! in_array($value['order_product_id'], $need_update)){
  1893. $need_update[] = [
  1894. 'order_product_id' => $value['order_product_id'],
  1895. 'process_id' => $value['process_id'],
  1896. ];
  1897. }
  1898. $key = $value['dispatch_sub_id'] . $value['crt_time'];
  1899. if(isset($flag[$key])) continue;
  1900. if(isset($update[$value['dispatch_sub_id']])){
  1901. $tmp = bcadd($value['quantity'], $update[$value['dispatch_sub_id']], 3);
  1902. $update[$value['dispatch_sub_id']] = $tmp;
  1903. }else{
  1904. $update[$value['dispatch_sub_id']] = $value['quantity'];
  1905. }
  1906. $flag[$key] = 1;
  1907. }
  1908. unset($flag);
  1909. //更新派工单完工数量----------------------------------------------------
  1910. foreach ($update as $id => $num){
  1911. DispatchSub::where('id', $id)->update([
  1912. 'finished_num' => $num,
  1913. 'wg_status' => 1,
  1914. ]);
  1915. }
  1916. if(! empty($need_update)){
  1917. //更新生产订单的完工数量--------------------------------------------------
  1918. $list = DispatchSub::where("del_time",0)
  1919. ->when(! empty($need_update), function ($query) use ($need_update) {
  1920. return $query->where(function ($q) use ($need_update) {
  1921. foreach ($need_update as $value) {
  1922. $order_product_id = $value['order_product_id'];
  1923. $process_id = $value['process_id'];
  1924. $q->orWhere(function ($subQ) use ($order_product_id, $process_id) {
  1925. $subQ->where('order_product_id', $order_product_id)
  1926. ->where('process_id', $process_id);
  1927. });
  1928. }
  1929. });
  1930. })
  1931. ->select('order_product_id', 'finished_num', 'sale_orders_product_id')
  1932. ->get()->toArray();
  1933. $map2 = [];
  1934. foreach ($list as $value){
  1935. if(isset($map2[$value['order_product_id']])){
  1936. $tmp = bcadd($map2[$value['order_product_id']], $value['finished_num'], 3);
  1937. $map2[$value['order_product_id']] = $tmp;
  1938. }else{
  1939. $map2[$value['order_product_id']] = $value['finished_num'];
  1940. }
  1941. }
  1942. foreach ($map2 as $id => $num){
  1943. OrdersProduct::where('id', $id)->update([
  1944. 'finished_num' => $num
  1945. ]);
  1946. }
  1947. //更新销售订单完工数量-------------------------------------------------------
  1948. $list2 = OrdersProduct::where('del_time',0)
  1949. ->whereIn('sale_orders_product_id', array_unique(array_column($list, 'sale_orders_product_id')))
  1950. ->select('finished_num','sale_orders_product_id')
  1951. ->get()->toArray();
  1952. $map3 = [];
  1953. foreach ($list2 as $value){
  1954. if(isset($map3[$value['sale_orders_product_id']])){
  1955. $tmp = bcadd($map3[$value['sale_orders_product_id']], $value['finished_num'], 3);
  1956. $map3[$value['sale_orders_product_id']] = $tmp;
  1957. }else{
  1958. $map3[$value['sale_orders_product_id']] = $value['finished_num'];
  1959. }
  1960. }
  1961. foreach ($map3 as $id => $num){
  1962. SaleOrdersProduct::where('id', $id)->update([
  1963. 'finished_num' => $num
  1964. ]);
  1965. }
  1966. }
  1967. }
  1968. public function orderSearchList($data){
  1969. if(empty($data['type'])) return [false, '单据联查类型不能为空'];
  1970. if(empty($data['data_id'])) return [false, '依据单据ID不能为空'];
  1971. //不管进来的是什么单据id 转换为销售订单
  1972. $result = $this->changeIdToOrderNew($data);
  1973. // $sales_id = $this->changeIdToOrder($data);
  1974. // list($status, $msg) = $this->forSearch($data, $sales_id);
  1975. return [true, $result];
  1976. }
  1977. private function changeIdToOrder($data)
  1978. {
  1979. $id = $data['data_id'];
  1980. $search_id = $result = [];
  1981. if ($data['type'] == 1) {
  1982. $result[] = [
  1983. 'id' => $id
  1984. ];
  1985. } elseif ($data['type'] == 2) {
  1986. $result = OrdersProduct::where('del_time', 0)
  1987. ->where('id', $id)
  1988. ->select('sale_orders_product_id as id')
  1989. ->get()->toArray();
  1990. } elseif ($data['type'] == 3) {
  1991. $result = DispatchSub::where('del_time', 0)
  1992. ->where('id', $data['data_id'])
  1993. ->select('sale_orders_product_id as id')
  1994. ->get()->toArray();
  1995. } elseif ($data['type'] == 4) {
  1996. $detail = ReportWorkingDetail::where('report_working_id', $id)
  1997. ->where('del_time', 0)
  1998. ->select('data_id')
  1999. ->get()->toArray();
  2000. $result = DispatchSub::where('del_time', 0)
  2001. ->whereIn('id', array_unique(array_column($detail, 'data_id')))
  2002. ->select('sale_orders_product_id as id')
  2003. ->get()->toArray();
  2004. } elseif ($data['type'] == 5) {
  2005. $result = DispatchSub::where('del_time', 0)
  2006. ->where('id', $id)
  2007. ->where('finished_num', '>', 0)
  2008. ->select('sale_orders_product_id as id')
  2009. ->get()->toArray();
  2010. } elseif ($data['type'] == 6) {
  2011. $first = Box::where('del_time', 0)
  2012. ->where('id', $id)
  2013. ->select("top_order_no", 'order_no')
  2014. ->first();
  2015. if (!empty($first)) {
  2016. $first = $first->toArray();
  2017. $box_detail = new BoxDetail(['channel' => $first['top_order_no']]);
  2018. $result = $box_detail->where('order_no', $first['order_no'])->select('top_id as id')->get()->toArray();
  2019. }
  2020. } elseif ($data['type'] == 7) {
  2021. $result = ScrappCount::where('del_time', 0)
  2022. ->where('id', $id)
  2023. ->select('sale_orders_product_id as id')
  2024. ->get()->toArray();
  2025. } elseif ($data['type'] == 8) {
  2026. $result = ScrappCount::where('del_time', 0)
  2027. ->where('id', $id)
  2028. ->where('scrapp_num', '>', 0)
  2029. ->select('sale_orders_product_id as id')
  2030. ->get()->toArray();
  2031. } elseif ($data['type'] == 9) {
  2032. $detail = ApplyOrderDetail::where('del_time', 0)
  2033. ->where('apply_order_id', $id)
  2034. ->where('type', ApplyOrder::type_one)
  2035. ->select('data_id as dispatch_id')
  2036. ->get()->toArray();
  2037. $result = DispatchSub::where('del_time', 0)
  2038. ->where('id', array_unique(array_column($detail, 'dispatch_id')))
  2039. ->select('sale_orders_product_id as id')
  2040. ->get()->toArray();
  2041. } elseif ($data['type'] == 10) {
  2042. $detail = ApplyOrderDetail::where('del_time', 0)
  2043. ->where('apply_order_id', $id)
  2044. ->where('type', ApplyOrder::type_two)
  2045. ->select('data_id as dispatch_id')
  2046. ->get()->toArray();
  2047. $result = DispatchSub::where('del_time', 0)
  2048. ->where('id', array_unique(array_column($detail, 'dispatch_id')))
  2049. ->select('sale_orders_product_id as id')
  2050. ->get()->toArray();
  2051. } elseif ($data['type'] == 11) {
  2052. $detail = ApplyOrderDetail::where('del_time', 0)
  2053. ->where('apply_order_id', $id)
  2054. ->where('type', ApplyOrder::type_four)
  2055. ->select('data_id as box_id')
  2056. ->get()->toArray();
  2057. $first = Box::where('del_time', 0)
  2058. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  2059. ->select("top_order_no", 'order_no')
  2060. ->get()->toArray();
  2061. if (!empty($first)) {
  2062. foreach ($first as $value) {
  2063. $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
  2064. $t = $box_detail->where('order_no', $value['order_no'])->select('top_id as id')->get()->toArray();
  2065. $result = array_merge($result, $t);
  2066. }
  2067. }
  2068. } elseif ($data['type'] == 12) {
  2069. $detail = ApplyOrderDetail::where('del_time', 0)
  2070. ->where('apply_order_id', $id)
  2071. ->where('type', ApplyOrder::type_three)
  2072. ->select('data_id as box_id')
  2073. ->get()->toArray();
  2074. $first = Box::where('del_time', 0)
  2075. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  2076. ->select("top_order_no", 'order_no')
  2077. ->get()->toArray();
  2078. if (!empty($first)) {
  2079. foreach ($first as $value) {
  2080. $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
  2081. $t = $box_detail->where('order_no', $value['order_no'])->select('top_id as id')->get()->toArray();
  2082. $result = array_merge($result, $t);
  2083. }
  2084. }
  2085. } elseif ($data['type'] == 13) {
  2086. $detail = ApplyOrderDetail::where('del_time', 0)
  2087. ->where('apply_order_id', $id)
  2088. ->where('type', ApplyOrder::type_five)
  2089. ->select('data_id as zj_id')
  2090. ->get()->toArray();
  2091. $result = ScrappCount::where('del_time', 0)
  2092. ->whereIn('id', array_unique(array_column($detail, 'zj_id')))
  2093. ->select('sale_orders_product_id as id')
  2094. ->get()->toArray();
  2095. }
  2096. foreach ($result as $value) {
  2097. if (!in_array($value['id'], $search_id)) {
  2098. $search_id[] = $value['id'];
  2099. }
  2100. }
  2101. return $search_id;
  2102. }
  2103. private function forSearch($data, $sales_id){
  2104. $result = [];
  2105. //通过销售订单查找所有业务单据
  2106. if($data['for_type'] == 1){
  2107. $result = SaleOrdersProduct::where('del_time',0)
  2108. ->whereIn('id', $sales_id)
  2109. ->select('id', 'out_order_no as order_no')
  2110. ->get()->toArray();
  2111. }elseif($data['for_type'] == 2){
  2112. $result = OrdersProduct::where('del_time',0)
  2113. ->whereIn('sale_orders_product_id', $sales_id)
  2114. ->select('id', 'production_no as order_no')
  2115. ->get()->toArray();
  2116. }elseif($data['for_type'] == 3){
  2117. $result = DispatchSub::where('del_time',0)
  2118. ->whereIn('sale_orders_product_id', $sales_id)
  2119. ->select('id', 'dispatch_no as order_no')
  2120. ->get()->toArray();
  2121. }elseif($data['for_type'] == 4){
  2122. $dispatch = DispatchSub::where('del_time',0)
  2123. ->whereIn('sale_orders_product_id', $sales_id)
  2124. ->select('id')
  2125. ->get()->toArray();
  2126. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  2127. ->where('del_time',0)
  2128. ->select('report_working_id')
  2129. ->get()->toArray();
  2130. $result = ReportWorking::where('del_time',0)
  2131. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  2132. ->select('id', 'order_number as order_no')
  2133. ->get()->toArray();
  2134. }elseif($data['for_type'] == 5){
  2135. $result = DispatchSub::where('del_time',0)
  2136. ->whereIn('sale_orders_product_id', $sales_id)
  2137. ->where('finished_num','>',0)
  2138. ->select('id', 'dispatch_no as order_no')
  2139. ->get()->toArray();
  2140. }elseif($data['for_type'] == 6){
  2141. $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
  2142. foreach ($sale as $value){
  2143. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2144. $box_detail_order = $box_detail->where('top_id',$data['data_id'])->select('order_no')->get()->toArray();
  2145. $tmp = Box::where('del_time',0)
  2146. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2147. ->select("id","order_no")
  2148. ->get()->toArray();
  2149. $result = array_merge($result, $tmp);
  2150. }
  2151. }elseif ($data['for_type'] == 7){
  2152. $result = ScrappCount::where('del_time',0)
  2153. ->whereIn('sale_orders_product_id', $sales_id)
  2154. ->select('id', 'order_number as order_no')
  2155. ->get()->toArray();
  2156. }elseif ($data['for_type'] == 8){
  2157. $result = ScrappCount::where('del_time',0)
  2158. ->whereIn('sale_orders_product_id', $sales_id)
  2159. ->where('scrapp_num','>', 0)
  2160. ->select('id', 'order_number as order_no')
  2161. ->get()->toArray();
  2162. }elseif ($data['for_type'] == 9){
  2163. $dispatch = DispatchSub::where('del_time',0)
  2164. ->whereIn('sale_orders_product_id', $sales_id)
  2165. ->select('id')
  2166. ->get()->toArray();
  2167. $detail = ApplyOrderDetail::where('del_time',0)
  2168. ->whereIn('data_id', array_column($dispatch,'id'))
  2169. ->where('type',ApplyOrder::type_one)
  2170. ->select('apply_order_id')
  2171. ->get()->toArray();
  2172. $result = ApplyOrder::where('del_time',0)
  2173. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  2174. ->select('id','order_number as order_no')
  2175. ->get()->toArray();
  2176. }elseif ($data['for_type'] == 10){
  2177. $dispatch = DispatchSub::where('del_time',0)
  2178. ->whereIn('sale_orders_product_id', $sales_id)
  2179. ->select('id')
  2180. ->get()->toArray();
  2181. $detail = ApplyOrderDetail::where('del_time',0)
  2182. ->whereIn('data_id', array_column($dispatch,'id'))
  2183. ->where('type',ApplyOrder::type_two)
  2184. ->select('apply_order_id')
  2185. ->get()->toArray();
  2186. $result = ApplyOrder::where('del_time',0)
  2187. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  2188. ->select('id','order_number as order_no')
  2189. ->get()->toArray();
  2190. }elseif($data['for_type'] == 11){
  2191. $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
  2192. $ids = [];
  2193. foreach ($sale as $value){
  2194. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2195. $box_detail_order = $box_detail->where('top_id',$data['data_id'])->select('order_no')->get()->toArray();
  2196. $tmp = Box::where('del_time',0)
  2197. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2198. ->select("id")
  2199. ->get()->toArray();
  2200. $ids = array_merge($ids, $tmp);
  2201. }
  2202. $detail = ApplyOrderDetail::where('del_time',0)
  2203. ->whereIn('data_id', array_column($ids,'id'))
  2204. ->where('type',ApplyOrder::type_four)
  2205. ->select('apply_order_id')
  2206. ->get()->toArray();
  2207. $result = ApplyOrder::where('del_time',0)
  2208. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  2209. ->select('id','order_number as order_no')
  2210. ->get()->toArray();
  2211. }elseif($data['for_type'] == 12){
  2212. $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
  2213. $ids = [];
  2214. foreach ($sale as $value){
  2215. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2216. $box_detail_order = $box_detail->where('top_id',$data['data_id'])->select('order_no')->get()->toArray();
  2217. $tmp = Box::where('del_time',0)
  2218. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2219. ->select("id")
  2220. ->get()->toArray();
  2221. $ids = array_merge($ids, $tmp);
  2222. }
  2223. $detail = ApplyOrderDetail::where('del_time',0)
  2224. ->whereIn('data_id', array_column($ids,'id'))
  2225. ->where('type',ApplyOrder::type_three)
  2226. ->select('apply_order_id')
  2227. ->get()->toArray();
  2228. $result = ApplyOrder::where('del_time',0)
  2229. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  2230. ->select('id','order_number as order_no')
  2231. ->get()->toArray();
  2232. }elseif($data['for_type'] == 13){
  2233. $zj_id = ScrappCount::where('del_time',0)
  2234. ->where('scrapp_num','>',0)
  2235. ->whereIn('sale_orders_product_id', $sales_id)
  2236. ->select('id')
  2237. ->get()->toArray();
  2238. $detail = ApplyOrderDetail::where('del_time',0)
  2239. ->whereIn('data_id', array_column($zj_id,'id'))
  2240. ->where('type',ApplyOrder::type_five)
  2241. ->select('apply_order_id')
  2242. ->get()->toArray();
  2243. $result = ApplyOrder::where('del_time',0)
  2244. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  2245. ->select('id','order_number as order_no')
  2246. ->get()->toArray();
  2247. }
  2248. return [true, $result];
  2249. }
  2250. private function changeIdToOrderNew1($data)
  2251. {
  2252. $id = $data['data_id'];
  2253. $search_id = $result = [];
  2254. if ($data['type'] == 1) {
  2255. } elseif ($data['type'] == 2) {
  2256. $result[1] = OrdersProduct::where('del_time', 0)
  2257. ->where('id', $id)
  2258. ->pluck('sale_orders_product_id')
  2259. ->toArray();
  2260. } elseif ($data['type'] == 3) {
  2261. $array = DispatchSub::where('del_time', 0)
  2262. ->where('id', $data['data_id'])
  2263. ->select('sale_orders_product_id', 'order_product_id')
  2264. ->get()->toArray();
  2265. foreach ($array as $value){
  2266. $result[1][] = $value['sale_orders_product_id'];
  2267. $result[2][] = $value['order_product_id'];
  2268. }
  2269. } elseif ($data['type'] == 4) {
  2270. $detail = ReportWorkingDetail::where('report_working_id', $id)
  2271. ->where('del_time', 0)
  2272. ->select('data_id')
  2273. ->get()->toArray();
  2274. $array = DispatchSub::where('del_time', 0)
  2275. ->whereIn('id', array_unique(array_column($detail, 'data_id')))
  2276. ->select('sale_orders_product_id', 'order_product_id','id')
  2277. ->get()->toArray();
  2278. foreach ($array as $value){
  2279. $result[1][] = $value['sale_orders_product_id'];
  2280. $result[2][] = $value['order_product_id'];
  2281. $result[3][] = $value['id'];
  2282. }
  2283. } elseif ($data['type'] == 5) {
  2284. $array = DispatchSub::where('del_time', 0)
  2285. ->where('id', $id)
  2286. ->where('finished_num', '>', 0)
  2287. ->select('sale_orders_product_id', 'order_product_id','id')
  2288. ->get()->toArray();
  2289. foreach ($array as $value){
  2290. $result[1][] = $value['sale_orders_product_id'];
  2291. $result[2][] = $value['order_product_id'];
  2292. $result[3][] = $value['id'];
  2293. }
  2294. $detail = ReportWorkingDetail::where('data_id', $id)
  2295. ->where('del_time', 0)
  2296. ->select('report_working_id')
  2297. ->get()->toArray();
  2298. foreach ($detail as $value){
  2299. $result[4][] = $value['report_working_id'];
  2300. }
  2301. } elseif ($data['type'] == 6) {
  2302. $first = Box::where('del_time', 0)
  2303. ->where('id', $id)
  2304. ->select("top_order_no", 'order_no','id')
  2305. ->first();
  2306. if (! empty($first)) {
  2307. $first = $first->toArray();
  2308. $boxArray = BoxWithDispatch::where('del_time',0)
  2309. ->where('box_id',$first['id'])
  2310. ->select('dispatch_id','num')
  2311. ->get()->toArray();
  2312. if(! empty($boxArray)){
  2313. $dispatch_id = array_column($boxArray,'dispatch_id');
  2314. $array = DispatchSub::where('del_time', 0)
  2315. ->whereIn('id',$dispatch_id)
  2316. ->select('sale_orders_product_id', 'order_product_id','id')
  2317. ->get()->toArray();
  2318. foreach ($array as $value){
  2319. $result[1][] = $value['sale_orders_product_id'];
  2320. $result[2][] = $value['order_product_id'];
  2321. $result[3][] = $value['id'];
  2322. }
  2323. $detail = ReportWorkingDetail::whereIn('data_id', $dispatch_id)
  2324. ->where('del_time', 0)
  2325. ->select('report_working_id')
  2326. ->get()->toArray();
  2327. foreach ($detail as $value){
  2328. $result[4][] = $value['report_working_id'];
  2329. }
  2330. $result[5] = $dispatch_id;
  2331. }else{
  2332. $array = [];
  2333. $box_detail = new BoxDetail(['channel' => $first['top_order_no']]);
  2334. $detail = $box_detail->where('order_no', $first['order_no'])->select('top_id as id')->get()->toArray();
  2335. foreach ($detail as $value){
  2336. if(! in_array($value['id'], $array)) $array[] = $value['id'];
  2337. }
  2338. $result[1] = $array;
  2339. $result[2] = OrdersProduct::where('del_time',0)
  2340. ->whereIn('sale_orders_product_id',$array)
  2341. ->pluck('id')->toArray();
  2342. }
  2343. }
  2344. } elseif ($data['type'] == 7) {
  2345. $result = ScrappCount::where('del_time', 0)
  2346. ->where('id', $id)
  2347. ->select('sale_orders_product_id as id')
  2348. ->get()->toArray();
  2349. } elseif ($data['type'] == 8) {
  2350. $result = ScrappCount::where('del_time', 0)
  2351. ->where('id', $id)
  2352. ->where('scrapp_num', '>', 0)
  2353. ->select('sale_orders_product_id as id')
  2354. ->get()->toArray();
  2355. } elseif ($data['type'] == 9) {
  2356. $detail = ApplyOrderDetail::where('del_time', 0)
  2357. ->where('apply_order_id', $id)
  2358. ->where('type', ApplyOrder::type_one)
  2359. ->select('data_id as dispatch_id')
  2360. ->get()->toArray();
  2361. $result = DispatchSub::where('del_time', 0)
  2362. ->where('id', array_unique(array_column($detail, 'dispatch_id')))
  2363. ->select('sale_orders_product_id as id')
  2364. ->get()->toArray();
  2365. } elseif ($data['type'] == 10) {
  2366. $detail = ApplyOrderDetail::where('del_time', 0)
  2367. ->where('apply_order_id', $id)
  2368. ->where('type', ApplyOrder::type_two)
  2369. ->select('data_id as dispatch_id')
  2370. ->get()->toArray();
  2371. $result = DispatchSub::where('del_time', 0)
  2372. ->where('id', array_unique(array_column($detail, 'dispatch_id')))
  2373. ->select('sale_orders_product_id as id')
  2374. ->get()->toArray();
  2375. } elseif ($data['type'] == 11) {
  2376. $detail = ApplyOrderDetail::where('del_time', 0)
  2377. ->where('apply_order_id', $id)
  2378. ->where('type', ApplyOrder::type_four)
  2379. ->select('data_id as box_id')
  2380. ->get()->toArray();
  2381. $first = Box::where('del_time', 0)
  2382. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  2383. ->select("top_order_no", 'order_no')
  2384. ->get()->toArray();
  2385. if (!empty($first)) {
  2386. foreach ($first as $value) {
  2387. $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
  2388. $t = $box_detail->where('order_no', $value['order_no'])->select('top_id as id')->get()->toArray();
  2389. $result = array_merge($result, $t);
  2390. }
  2391. }
  2392. } elseif ($data['type'] == 12) {
  2393. $detail = ApplyOrderDetail::where('del_time', 0)
  2394. ->where('apply_order_id', $id)
  2395. ->where('type', ApplyOrder::type_three)
  2396. ->select('data_id as box_id')
  2397. ->get()->toArray();
  2398. $first = Box::where('del_time', 0)
  2399. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  2400. ->select("top_order_no", 'order_no')
  2401. ->get()->toArray();
  2402. if (!empty($first)) {
  2403. foreach ($first as $value) {
  2404. $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
  2405. $t = $box_detail->where('order_no', $value['order_no'])->select('top_id as id')->get()->toArray();
  2406. $result = array_merge($result, $t);
  2407. }
  2408. }
  2409. } elseif ($data['type'] == 13) {
  2410. $detail = ApplyOrderDetail::where('del_time', 0)
  2411. ->where('apply_order_id', $id)
  2412. ->where('type', ApplyOrder::type_five)
  2413. ->select('data_id as zj_id')
  2414. ->get()->toArray();
  2415. $result = ScrappCount::where('del_time', 0)
  2416. ->whereIn('id', array_unique(array_column($detail, 'zj_id')))
  2417. ->select('sale_orders_product_id as id')
  2418. ->get()->toArray();
  2419. }
  2420. foreach ($result as $value) {
  2421. if (!in_array($value['id'], $search_id)) {
  2422. $search_id[] = $value['id'];
  2423. }
  2424. }
  2425. return $search_id;
  2426. }
  2427. private function changeIdToOrderNew($data)
  2428. {
  2429. $id = $data['data_id'];
  2430. $result = [];
  2431. $type = $data['type'];
  2432. $for_type = $data['for_type'];
  2433. if ($data['type'] == 1) {
  2434. $result = $this->typeOne($for_type, $id);
  2435. } elseif ($type == 2) {
  2436. $result = $this->typeTwo($for_type, $id);
  2437. } elseif ($type == 3) {
  2438. $result = $this->typeThree($for_type, $id);
  2439. } elseif ($type == 4) {
  2440. $result = $this->typeFour($for_type, $id);
  2441. } elseif ($type == 5) {
  2442. $result = $this->typeFive($for_type, $id);
  2443. } elseif ($type == 6) {
  2444. $result = $this->typeSix($for_type, $id);
  2445. } elseif ($type == 7) {
  2446. $result = $this->typeSeven($for_type, $id);
  2447. } elseif ($type == 8) {
  2448. $result = $this->typeEight($for_type, $id);
  2449. } elseif ($type == 9) {
  2450. $result = $this->typeNine($for_type, $id);
  2451. } elseif ($type == 10) {
  2452. $result = $this->typeTen($for_type, $id);
  2453. } elseif ($type == 11) {
  2454. $result = $this->typeEve($for_type, $id);
  2455. } elseif ($type == 12) {
  2456. $result = $this->typeTwl($for_type, $id);
  2457. } elseif ($type == 13) {
  2458. $result = $this->typeThi($for_type, $id);
  2459. } elseif ($type == 14) {
  2460. $result = $this->typeFourTeen($for_type, $id);
  2461. }
  2462. return $result;
  2463. }
  2464. private function typeOne($for_type, $id){
  2465. $result = [];
  2466. if($for_type == 2){
  2467. $result = OrdersProduct::where('del_time',0)
  2468. ->where('sale_orders_product_id', $id)
  2469. ->select('id', 'production_no as order_no')
  2470. ->get()->toArray();
  2471. }elseif ($for_type == 3){
  2472. $result = DispatchSub::where('del_time',0)
  2473. ->where('sale_orders_product_id', $id)
  2474. ->select('id', 'dispatch_no as order_no')
  2475. ->get()->toArray();
  2476. }elseif ($for_type == 4){
  2477. $dispatch = DispatchSub::where('del_time',0)
  2478. ->where('sale_orders_product_id', $id)
  2479. ->select('id')
  2480. ->get()->toArray();
  2481. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  2482. ->where('del_time',0)
  2483. ->select('report_working_id')
  2484. ->get()->toArray();
  2485. $result = ReportWorking::where('del_time',0)
  2486. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  2487. ->select('id', 'order_number as order_no')
  2488. ->get()->toArray();
  2489. }elseif ($for_type == 5){
  2490. $result = DispatchSub::where('del_time',0)
  2491. ->where('sale_orders_product_id', $id)
  2492. ->where('finished_num','>',0)
  2493. ->select('id', 'dispatch_no as order_no')
  2494. ->get()->toArray();
  2495. }elseif ($for_type == 6){
  2496. $sale = SaleOrdersProduct::where('id', $id)->select('order_no')->get()->toArray();
  2497. foreach ($sale as $value){
  2498. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2499. $box_detail_order = $box_detail->where('top_id',$id)->select('order_no')->get()->toArray();
  2500. $tmp = Box::where('del_time',0)
  2501. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2502. ->select("id","order_no")
  2503. ->get()->toArray();
  2504. $result = array_merge($result, $tmp);
  2505. }
  2506. }elseif ($for_type == 7){
  2507. $process = Process::where('del_time',0)
  2508. ->where('is_need_remain',1)
  2509. ->pluck('id')
  2510. ->toArray();
  2511. $result = ScrappCount::where('del_time',0)
  2512. ->where('sale_orders_product_id', $id)
  2513. ->whereIn('process_id', $process)
  2514. ->select('id', 'order_number as order_no')
  2515. ->get()->toArray();
  2516. }elseif ($for_type == 8){
  2517. $result = ScrappCount::where('del_time',0)
  2518. ->where('sale_orders_product_id', $id)
  2519. ->where('scrapp_num','>', 0)
  2520. ->select('id', 'order_number as order_no')
  2521. ->get()->toArray();
  2522. }elseif ($for_type == 9){
  2523. $dispatch_id = DispatchSub::where('del_time',0)
  2524. ->where('sale_orders_product_id', $id)
  2525. ->pluck('id')
  2526. ->toArray();
  2527. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2528. ->whereIn('data_id', $dispatch_id)
  2529. ->where('type', ApplyOrder::type_one)
  2530. ->pluck('apply_order_id')
  2531. ->toArray();
  2532. $result = ApplyOrder::where('del_time', 0)
  2533. ->whereIn('id', $apply_order_id)
  2534. ->select('id','order_number as order_no')
  2535. ->get()->toArray();
  2536. }elseif ($for_type == 10){
  2537. $dispatch_id = DispatchSub::where('del_time',0)
  2538. ->where('sale_orders_product_id', $id)
  2539. ->pluck('id')
  2540. ->toArray();
  2541. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2542. ->whereIn('data_id', $dispatch_id)
  2543. ->where('type', ApplyOrder::type_two)
  2544. ->pluck('apply_order_id')
  2545. ->toArray();
  2546. $result = ApplyOrder::where('del_time', 0)
  2547. ->whereIn('id', $apply_order_id)
  2548. ->select('id','order_number as order_no')
  2549. ->get()->toArray();
  2550. }elseif ($for_type == 11){
  2551. $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray();
  2552. $box_id = [];
  2553. foreach ($sale as $value){
  2554. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2555. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2556. $tmp = Box::where('del_time',0)
  2557. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2558. ->pluck("id")
  2559. ->toArray();
  2560. $box_id = array_merge($box_id, $tmp);
  2561. }
  2562. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2563. ->whereIn('data_id', $box_id)
  2564. ->where('type', ApplyOrder::type_four)
  2565. ->pluck('apply_order_id')
  2566. ->toArray();
  2567. $result = ApplyOrder::where('del_time', 0)
  2568. ->whereIn('id', $apply_order_id)
  2569. ->select('id','order_number as order_no')
  2570. ->get()->toArray();
  2571. }elseif ($for_type == 12){
  2572. $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray();
  2573. $box_id = [];
  2574. foreach ($sale as $value){
  2575. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2576. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2577. $tmp = Box::where('del_time',0)
  2578. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2579. ->pluck("id")
  2580. ->toArray();
  2581. $box_id = array_merge($box_id, $tmp);
  2582. }
  2583. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2584. ->whereIn('data_id', $box_id)
  2585. ->where('type', ApplyOrder::type_three)
  2586. ->pluck('apply_order_id')
  2587. ->toArray();
  2588. $result = ApplyOrder::where('del_time', 0)
  2589. ->whereIn('id', $apply_order_id)
  2590. ->select('id','order_number as order_no')
  2591. ->get()->toArray();
  2592. }elseif ($for_type == 13){
  2593. $s_id = ScrappCount::where('del_time', 0)
  2594. ->where('sale_orders_product_id', $id)
  2595. ->pluck('id')
  2596. ->toArray();
  2597. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2598. ->whereIn('data_id', $s_id)
  2599. ->where('type', ApplyOrder::type_five)
  2600. ->pluck('apply_order_id')
  2601. ->toArray();
  2602. $result = ApplyOrder::where('del_time', 0)
  2603. ->whereIn('id', $apply_order_id)
  2604. ->select('id','order_number as order_no')
  2605. ->get()->toArray();
  2606. }elseif ($for_type == 14){
  2607. $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray();
  2608. $box_id = [];
  2609. foreach ($sale as $value){
  2610. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2611. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2612. $tmp = Box::where('del_time',0)
  2613. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2614. ->pluck("id")
  2615. ->toArray();
  2616. $box_id = array_merge($box_id, $tmp);
  2617. }
  2618. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2619. ->whereIn('data_id', $box_id)
  2620. ->where('type', ApplyOrder::type_six)
  2621. ->pluck('apply_order_id')
  2622. ->toArray();
  2623. $result = ApplyOrder::where('del_time', 0)
  2624. ->whereIn('id', $apply_order_id)
  2625. ->select('id','order_number as order_no')
  2626. ->get()->toArray();
  2627. }
  2628. return $result;
  2629. }
  2630. private function typeTwo($for_type, $id){
  2631. $result = [];
  2632. if($for_type == 1){
  2633. $sale_orders_product_id = OrdersProduct::where('del_time',0)
  2634. ->where('id', $id)
  2635. ->pluck('sale_orders_product_id')
  2636. ->toArray();
  2637. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  2638. ->where('del_time',0)
  2639. ->select('id','out_order_no as order_no')
  2640. ->get()->toArray();
  2641. }elseif ($for_type == 3){
  2642. $result = DispatchSub::where('del_time',0)
  2643. ->where('order_product_id', $id)
  2644. ->select('id', 'dispatch_no as order_no')
  2645. ->get()->toArray();
  2646. }elseif ($for_type == 4){
  2647. $dispatch = DispatchSub::where('del_time',0)
  2648. ->where('order_product_id', $id)
  2649. ->select('id')
  2650. ->get()->toArray();
  2651. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  2652. ->where('del_time',0)
  2653. ->select('report_working_id')
  2654. ->get()->toArray();
  2655. $result = ReportWorking::where('del_time',0)
  2656. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  2657. ->select('id', 'order_number as order_no')
  2658. ->get()->toArray();
  2659. }elseif ($for_type == 5){
  2660. $result = DispatchSub::where('del_time',0)
  2661. ->where('order_product_id', $id)
  2662. ->where('finished_num','>',0)
  2663. ->select('id', 'dispatch_no as order_no')
  2664. ->get()->toArray();
  2665. }elseif ($for_type == 6){
  2666. $sale_orders_product_id = OrdersProduct::where('del_time',0)
  2667. ->where('id', $id)
  2668. ->pluck('sale_orders_product_id')
  2669. ->toArray();
  2670. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  2671. foreach ($sale as $value){
  2672. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2673. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2674. $tmp = Box::where('del_time',0)
  2675. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2676. ->select("id","order_no")
  2677. ->get()->toArray();
  2678. $result = array_merge($result, $tmp);
  2679. }
  2680. }elseif ($for_type == 7){
  2681. $process = Process::where('del_time',0)
  2682. ->where('is_need_remain',1)
  2683. ->pluck('id')
  2684. ->toArray();
  2685. $result = ScrappCount::where('del_time',0)
  2686. ->where('order_product_id', $id)
  2687. ->whereIn('process_id', $process)
  2688. ->select('id', 'order_number as order_no')
  2689. ->get()->toArray();
  2690. }elseif ($for_type == 8){
  2691. $result = ScrappCount::where('del_time',0)
  2692. ->where('order_product_id', $id)
  2693. ->where('scrapp_num','>', 0)
  2694. ->select('id', 'order_number as order_no')
  2695. ->get()->toArray();
  2696. }elseif ($for_type == 9){
  2697. $dispatch_id = DispatchSub::where('del_time',0)
  2698. ->where('order_product_id', $id)
  2699. ->pluck('id')
  2700. ->toArray();
  2701. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2702. ->whereIn('data_id', $dispatch_id)
  2703. ->where('type', ApplyOrder::type_one)
  2704. ->pluck('apply_order_id')
  2705. ->toArray();
  2706. $result = ApplyOrder::where('del_time', 0)
  2707. ->whereIn('id', $apply_order_id)
  2708. ->select('id','order_number as order_no')
  2709. ->get()->toArray();
  2710. }elseif ($for_type == 10){
  2711. $dispatch_id = DispatchSub::where('del_time',0)
  2712. ->where('order_product_id', $id)
  2713. ->pluck('id')
  2714. ->toArray();
  2715. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2716. ->whereIn('data_id', $dispatch_id)
  2717. ->where('type', ApplyOrder::type_two)
  2718. ->pluck('apply_order_id')
  2719. ->toArray();
  2720. $result = ApplyOrder::where('del_time', 0)
  2721. ->whereIn('id', $apply_order_id)
  2722. ->select('id','order_number as order_no')
  2723. ->get()->toArray();
  2724. }elseif ($for_type == 11){
  2725. $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2726. $box_id = [];
  2727. foreach ($sale as $value){
  2728. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2729. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2730. $tmp = Box::where('del_time',0)
  2731. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2732. ->pluck("id")
  2733. ->toArray();
  2734. $box_id = array_merge($box_id, $tmp);
  2735. }
  2736. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2737. ->whereIn('data_id', $box_id)
  2738. ->where('type', ApplyOrder::type_four)
  2739. ->pluck('apply_order_id')
  2740. ->toArray();
  2741. $result = ApplyOrder::where('del_time', 0)
  2742. ->whereIn('id', $apply_order_id)
  2743. ->select('id','order_number as order_no')
  2744. ->get()->toArray();
  2745. }elseif ($for_type == 12){
  2746. $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2747. $box_id = [];
  2748. foreach ($sale as $value){
  2749. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2750. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2751. $tmp = Box::where('del_time',0)
  2752. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2753. ->pluck("id")
  2754. ->toArray();
  2755. $box_id = array_merge($box_id, $tmp);
  2756. }
  2757. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2758. ->whereIn('data_id', $box_id)
  2759. ->where('type', ApplyOrder::type_three)
  2760. ->pluck('apply_order_id')
  2761. ->toArray();
  2762. $result = ApplyOrder::where('del_time', 0)
  2763. ->whereIn('id', $apply_order_id)
  2764. ->select('id','order_number as order_no')
  2765. ->get()->toArray();
  2766. }elseif ($for_type == 13){
  2767. $s_id = ScrappCount::where('del_time', 0)
  2768. ->where('order_product_id', $id)
  2769. ->pluck('id')
  2770. ->toArray();
  2771. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2772. ->whereIn('data_id', $s_id)
  2773. ->where('type', ApplyOrder::type_five)
  2774. ->pluck('apply_order_id')
  2775. ->toArray();
  2776. $result = ApplyOrder::where('del_time', 0)
  2777. ->whereIn('id', $apply_order_id)
  2778. ->select('id','order_number as order_no')
  2779. ->get()->toArray();
  2780. }elseif ($for_type == 14){
  2781. $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2782. $box_id = [];
  2783. foreach ($sale as $value){
  2784. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2785. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2786. $tmp = Box::where('del_time',0)
  2787. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2788. ->pluck("id")
  2789. ->toArray();
  2790. $box_id = array_merge($box_id, $tmp);
  2791. }
  2792. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2793. ->whereIn('data_id', $box_id)
  2794. ->where('type', ApplyOrder::type_six)
  2795. ->pluck('apply_order_id')
  2796. ->toArray();
  2797. $result = ApplyOrder::where('del_time', 0)
  2798. ->whereIn('id', $apply_order_id)
  2799. ->select('id','order_number as order_no')
  2800. ->get()->toArray();
  2801. }
  2802. return $result;
  2803. }
  2804. private function typeThree($for_type, $id){
  2805. $result = [];
  2806. if($for_type == 1){
  2807. $sale_orders_product_id = DispatchSub::where('del_time',0)
  2808. ->where('id', $id)
  2809. ->pluck('sale_orders_product_id')
  2810. ->toArray();
  2811. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  2812. ->where('del_time',0)
  2813. ->select('id','out_order_no as order_no')
  2814. ->get()->toArray();
  2815. }elseif ($for_type == 2){
  2816. $order_product_id = DispatchSub::where('del_time',0)
  2817. ->where('id', $id)
  2818. ->pluck('order_product_id')
  2819. ->toArray();
  2820. $result = OrdersProduct::where('del_time',0)
  2821. ->whereIn('id', $order_product_id)
  2822. ->select('id', 'production_no as order_no')
  2823. ->get()->toArray();
  2824. }elseif ($for_type == 4){
  2825. $dispatch = DispatchSub::where('del_time',0)
  2826. ->where('id', $id)
  2827. ->select('id')
  2828. ->get()->toArray();
  2829. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  2830. ->where('del_time',0)
  2831. ->select('report_working_id')
  2832. ->get()->toArray();
  2833. $result = ReportWorking::where('del_time',0)
  2834. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  2835. ->select('id', 'order_number as order_no')
  2836. ->get()->toArray();
  2837. }elseif ($for_type == 5){
  2838. $result = DispatchSub::where('del_time',0)
  2839. ->where('id', $id)
  2840. ->where('finished_num','>',0)
  2841. ->select('id', 'dispatch_no as order_no')
  2842. ->get()->toArray();
  2843. }elseif ($for_type == 6){
  2844. $sale_orders_product_id = DispatchSub::where('del_time',0)
  2845. ->where('id', $id)
  2846. ->pluck('sale_orders_product_id')
  2847. ->toArray();
  2848. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  2849. foreach ($sale as $value){
  2850. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2851. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2852. $tmp = Box::where('del_time',0)
  2853. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2854. ->select("id","order_no")
  2855. ->get()->toArray();
  2856. $result = array_merge($result, $tmp);
  2857. }
  2858. }elseif ($for_type == 7){
  2859. $process = Process::where('del_time',0)
  2860. ->where('is_need_remain',1)
  2861. ->pluck('id')
  2862. ->toArray();
  2863. $result = ScrappCount::where('del_time',0)
  2864. ->where('dispatch_sub_id', $id)
  2865. ->whereIn('process_id', $process)
  2866. ->select('id', 'order_number as order_no')
  2867. ->get()->toArray();
  2868. }elseif ($for_type == 8){
  2869. $result = ScrappCount::where('del_time',0)
  2870. ->where('dispatch_sub_id', $id)
  2871. ->where('scrapp_num','>', 0)
  2872. ->select('id', 'order_number as order_no')
  2873. ->get()->toArray();
  2874. }elseif ($for_type == 9){
  2875. $dispatch = DispatchSub::where('id',$id)
  2876. ->select('order_product_id','crt_time','product_no','technology_name')
  2877. ->first()->toArray();
  2878. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  2879. ->where('crt_time',$dispatch['crt_time'])
  2880. ->where('product_no',$dispatch['product_no'])
  2881. ->where('technology_name',$dispatch['technology_name'])
  2882. ->pluck('id')->toArray();
  2883. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2884. ->whereIn('data_id', $dispatch_id)
  2885. ->where('type', ApplyOrder::type_one)
  2886. ->pluck('apply_order_id')
  2887. ->toArray();
  2888. $result = ApplyOrder::where('del_time', 0)
  2889. ->whereIn('id', $apply_order_id)
  2890. ->select('id','order_number as order_no')
  2891. ->get()->toArray();
  2892. }elseif ($for_type == 10){
  2893. $dispatch = DispatchSub::where('id',$id)
  2894. ->select('order_product_id','crt_time','product_no','technology_name')
  2895. ->first()->toArray();
  2896. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  2897. ->where('crt_time',$dispatch['crt_time'])
  2898. ->where('product_no',$dispatch['product_no'])
  2899. ->where('technology_name',$dispatch['technology_name'])
  2900. ->pluck('id')->toArray();
  2901. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2902. ->whereIn('data_id', $dispatch_id)
  2903. ->where('type', ApplyOrder::type_two)
  2904. ->pluck('apply_order_id')
  2905. ->toArray();
  2906. $result = ApplyOrder::where('del_time', 0)
  2907. ->whereIn('id', $apply_order_id)
  2908. ->select('id','order_number as order_no')
  2909. ->get()->toArray();
  2910. }elseif ($for_type == 11){
  2911. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2912. $box_id = [];
  2913. foreach ($sale as $value){
  2914. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2915. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2916. $tmp = Box::where('del_time',0)
  2917. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2918. ->pluck("id")
  2919. ->toArray();
  2920. $box_id = array_merge($box_id, $tmp);
  2921. }
  2922. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2923. ->whereIn('data_id', $box_id)
  2924. ->where('type', ApplyOrder::type_four)
  2925. ->pluck('apply_order_id')
  2926. ->toArray();
  2927. $result = ApplyOrder::where('del_time', 0)
  2928. ->whereIn('id', $apply_order_id)
  2929. ->select('id','order_number as order_no')
  2930. ->get()->toArray();
  2931. }elseif ($for_type == 12){
  2932. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2933. $box_id = [];
  2934. foreach ($sale as $value){
  2935. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2936. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2937. $tmp = Box::where('del_time',0)
  2938. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2939. ->pluck("id")
  2940. ->toArray();
  2941. $box_id = array_merge($box_id, $tmp);
  2942. }
  2943. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2944. ->whereIn('data_id', $box_id)
  2945. ->where('type', ApplyOrder::type_three)
  2946. ->pluck('apply_order_id')
  2947. ->toArray();
  2948. $result = ApplyOrder::where('del_time', 0)
  2949. ->whereIn('id', $apply_order_id)
  2950. ->select('id','order_number as order_no')
  2951. ->get()->toArray();
  2952. }elseif ($for_type == 13){
  2953. $dispatch = DispatchSub::where('id',$id)
  2954. ->select('order_product_id','crt_time','product_no','technology_name')
  2955. ->first()->toArray();
  2956. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  2957. ->where('crt_time',$dispatch['crt_time'])
  2958. ->where('product_no',$dispatch['product_no'])
  2959. ->where('technology_name',$dispatch['technology_name'])
  2960. ->pluck('id')->toArray();
  2961. $s_id = ScrappCount::where('del_time', 0)
  2962. ->whereIn('dispatch_sub_id', $dispatch_id)
  2963. ->pluck('id')
  2964. ->toArray();
  2965. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2966. ->whereIn('data_id', $s_id)
  2967. ->where('type', ApplyOrder::type_five)
  2968. ->pluck('apply_order_id')
  2969. ->toArray();
  2970. $result = ApplyOrder::where('del_time', 0)
  2971. ->whereIn('id', $apply_order_id)
  2972. ->select('id','order_number as order_no')
  2973. ->get()->toArray();
  2974. }elseif ($for_type == 14){
  2975. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2976. $box_id = [];
  2977. foreach ($sale as $value){
  2978. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2979. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2980. $tmp = Box::where('del_time',0)
  2981. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2982. ->pluck("id")
  2983. ->toArray();
  2984. $box_id = array_merge($box_id, $tmp);
  2985. }
  2986. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2987. ->whereIn('data_id', $box_id)
  2988. ->where('type', ApplyOrder::type_four)
  2989. ->pluck('apply_order_id')
  2990. ->toArray();
  2991. $result = ApplyOrder::where('del_time', 0)
  2992. ->whereIn('id', $apply_order_id)
  2993. ->select('id','order_number as order_no')
  2994. ->get()->toArray();
  2995. }
  2996. return $result;
  2997. }
  2998. private function typeFour($for_type, $id){
  2999. $result = [];
  3000. if($for_type == 1){
  3001. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3002. ->where('del_time',0)
  3003. ->pluck('data_id')
  3004. ->toArray();
  3005. $sale_orders_product_id = DispatchSub::where('del_time',0)
  3006. ->whereIn('id', $dispatch_id)
  3007. ->pluck('sale_orders_product_id')
  3008. ->toArray();
  3009. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3010. ->where('del_time',0)
  3011. ->select('id','out_order_no as order_no')
  3012. ->get()->toArray();
  3013. }elseif ($for_type == 2){
  3014. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3015. ->where('del_time',0)
  3016. ->pluck('data_id')
  3017. ->toArray();
  3018. $order_product_id = DispatchSub::where('del_time',0)
  3019. ->whereIn('id', $dispatch_id)
  3020. ->pluck('order_product_id')
  3021. ->toArray();
  3022. $result = OrdersProduct::where('del_time',0)
  3023. ->whereIn('id', $order_product_id)
  3024. ->select('id', 'production_no as order_no')
  3025. ->get()->toArray();
  3026. }elseif ($for_type == 3){
  3027. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3028. ->where('del_time',0)
  3029. ->pluck('data_id')
  3030. ->toArray();
  3031. $result = DispatchSub::where('del_time',0)
  3032. ->whereIn('id', $dispatch_id)
  3033. ->select('id','dispatch_no as order_no')
  3034. ->get()->toArray();
  3035. }elseif ($for_type == 5){
  3036. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3037. ->where('del_time',0)
  3038. ->pluck('data_id')
  3039. ->toArray();
  3040. $result = DispatchSub::where('del_time',0)
  3041. ->whereIn('id', $dispatch_id)
  3042. ->where('finished_num','>',0)
  3043. ->select('id', 'dispatch_no as order_no')
  3044. ->get()->toArray();
  3045. }elseif ($for_type == 6){
  3046. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3047. ->where('del_time',0)
  3048. ->pluck('data_id')
  3049. ->toArray();
  3050. $sale_orders_product_id = DispatchSub::where('del_time',0)
  3051. ->whereIn('id', $dispatch_id)
  3052. ->pluck('sale_orders_product_id')
  3053. ->toArray();
  3054. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  3055. $return = [];
  3056. foreach ($sale as $value){
  3057. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3058. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3059. $tmp = Box::where('del_time',0)
  3060. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3061. ->select("id","order_no")
  3062. ->get()->toArray();
  3063. $return = array_merge($return, $tmp);
  3064. }
  3065. $t = $return[0] ?? [];
  3066. if(! empty($t)) $result[] = $t;
  3067. }elseif ($for_type == 7){
  3068. $process = Process::where('del_time',0)
  3069. ->where('is_need_remain',1)
  3070. ->pluck('id')
  3071. ->toArray();
  3072. $result = ScrappCount::where('del_time',0)
  3073. ->where('report_id', $id)
  3074. ->whereIn('process_id',$process)
  3075. ->select('id', 'order_number as order_no')
  3076. ->get()->toArray();
  3077. }elseif ($for_type == 8){
  3078. $result = ScrappCount::where('del_time',0)
  3079. ->where('report_id', $id)
  3080. ->where('scrapp_num','>', 0)
  3081. ->select('id', 'order_number as order_no')
  3082. ->get()->toArray();
  3083. }elseif ($for_type == 9){
  3084. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3085. ->where('del_time',0)
  3086. ->pluck('data_id')
  3087. ->toArray();
  3088. $dispatch_id = $dispatch_id[0] ?? 0;
  3089. $dispatch = DispatchSub::where('id',$dispatch_id)
  3090. ->select('order_product_id','crt_time','product_no','technology_name')
  3091. ->first()->toArray();
  3092. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3093. ->where('crt_time',$dispatch['crt_time'])
  3094. ->where('product_no',$dispatch['product_no'])
  3095. ->where('technology_name',$dispatch['technology_name'])
  3096. ->pluck('id')->toArray();
  3097. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3098. ->whereIn('data_id', $dispatch_id)
  3099. ->where('type', ApplyOrder::type_one)
  3100. ->pluck('apply_order_id')
  3101. ->toArray();
  3102. $result = ApplyOrder::where('del_time', 0)
  3103. ->whereIn('id', $apply_order_id)
  3104. ->select('id','order_number as order_no')
  3105. ->get()->toArray();
  3106. }elseif ($for_type == 10){
  3107. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3108. ->where('del_time',0)
  3109. ->pluck('data_id')
  3110. ->toArray();
  3111. $dispatch_id = $dispatch_id[0] ?? 0;
  3112. $dispatch = DispatchSub::where('id',$dispatch_id)
  3113. ->select('order_product_id','crt_time','product_no','technology_name')
  3114. ->first()->toArray();
  3115. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3116. ->where('crt_time',$dispatch['crt_time'])
  3117. ->where('product_no',$dispatch['product_no'])
  3118. ->where('technology_name',$dispatch['technology_name'])
  3119. ->pluck('id')->toArray();
  3120. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3121. ->whereIn('data_id', $dispatch_id)
  3122. ->where('type', ApplyOrder::type_two)
  3123. ->pluck('apply_order_id')
  3124. ->toArray();
  3125. $return = ApplyOrder::where('del_time', 0)
  3126. ->whereIn('id', $apply_order_id)
  3127. ->select('id','order_number as order_no')
  3128. ->get()->toArray();
  3129. $t = $return[0] ?? [];
  3130. if(! empty($t)) $result[] = $t;
  3131. }elseif ($for_type == 11){
  3132. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3133. ->where('del_time',0)
  3134. ->pluck('data_id')
  3135. ->toArray();
  3136. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3137. $box_id = [];
  3138. foreach ($sale as $value){
  3139. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3140. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3141. $tmp = Box::where('del_time',0)
  3142. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3143. ->pluck("id")
  3144. ->toArray();
  3145. $box_id = array_merge($box_id, $tmp);
  3146. }
  3147. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3148. ->whereIn('data_id', $box_id)
  3149. ->where('type', ApplyOrder::type_four)
  3150. ->pluck('apply_order_id')
  3151. ->toArray();
  3152. $return = ApplyOrder::where('del_time', 0)
  3153. ->whereIn('id', $apply_order_id)
  3154. ->select('id','order_number as order_no')
  3155. ->get()->toArray();
  3156. $t = $return[0] ?? [];
  3157. if(! empty($t)) $result[] = $t;
  3158. }elseif ($for_type == 12){
  3159. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3160. ->where('del_time',0)
  3161. ->pluck('data_id')
  3162. ->toArray();
  3163. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no', 'sale_orders_product_id as id')->get()->toArray();
  3164. $box_id = [];
  3165. foreach ($sale as $value){
  3166. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3167. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3168. $tmp = Box::where('del_time',0)
  3169. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3170. ->pluck("id")
  3171. ->toArray();
  3172. $box_id = array_merge($box_id, $tmp);
  3173. }
  3174. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3175. ->whereIn('data_id', $box_id)
  3176. ->where('type', ApplyOrder::type_three)
  3177. ->pluck('apply_order_id')
  3178. ->toArray();
  3179. $return = ApplyOrder::where('del_time', 0)
  3180. ->whereIn('id', $apply_order_id)
  3181. ->select('id','order_number as order_no')
  3182. ->get()->toArray();
  3183. $t = $return[0] ?? [];
  3184. if(! empty($t)) $result[] = $t;
  3185. }elseif ($for_type == 13){
  3186. $s_id = ScrappCount::where('del_time', 0)
  3187. ->where('report_id', $id)
  3188. ->pluck('id')
  3189. ->toArray();
  3190. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3191. ->whereIn('data_id', $s_id)
  3192. ->where('type', ApplyOrder::type_five)
  3193. ->pluck('apply_order_id')
  3194. ->toArray();
  3195. $result = ApplyOrder::where('del_time', 0)
  3196. ->whereIn('id', $apply_order_id)
  3197. ->select('id','order_number as order_no')
  3198. ->get()->toArray();
  3199. }elseif ($for_type == 14){
  3200. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3201. ->where('del_time',0)
  3202. ->pluck('data_id')
  3203. ->toArray();
  3204. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3205. $box_id = [];
  3206. foreach ($sale as $value){
  3207. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3208. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3209. $tmp = Box::where('del_time',0)
  3210. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3211. ->pluck("id")
  3212. ->toArray();
  3213. $box_id = array_merge($box_id, $tmp);
  3214. }
  3215. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3216. ->whereIn('data_id', $box_id)
  3217. ->where('type', ApplyOrder::type_six)
  3218. ->pluck('apply_order_id')
  3219. ->toArray();
  3220. $return = ApplyOrder::where('del_time', 0)
  3221. ->whereIn('id', $apply_order_id)
  3222. ->select('id','order_number as order_no')
  3223. ->get()->toArray();
  3224. $t = $return[0] ?? [];
  3225. if(! empty($t)) $result[] = $t;
  3226. }
  3227. return $result;
  3228. }
  3229. private function typeFive($for_type, $id){
  3230. $result = [];
  3231. if($for_type == 1){
  3232. $sale_orders_product_id = DispatchSub::where('del_time',0)
  3233. ->where('id', $id)
  3234. ->pluck('sale_orders_product_id')
  3235. ->toArray();
  3236. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3237. ->where('del_time',0)
  3238. ->select('id','out_order_no as order_no')
  3239. ->get()->toArray();
  3240. }elseif ($for_type == 2){
  3241. $order_product_id = DispatchSub::where('del_time',0)
  3242. ->where('id', $id)
  3243. ->pluck('order_product_id')
  3244. ->toArray();
  3245. $result = OrdersProduct::where('del_time',0)
  3246. ->whereIn('id', $order_product_id)
  3247. ->select('id', 'production_no as order_no')
  3248. ->get()->toArray();
  3249. }elseif ($for_type == 3){
  3250. $result = DispatchSub::where('del_time',0)
  3251. ->where('id', $id)
  3252. ->select('id', 'dispatch_no as order_no')
  3253. ->get()->toArray();
  3254. }elseif ($for_type == 4){
  3255. $dispatch = DispatchSub::where('del_time',0)
  3256. ->where('id', $id)
  3257. ->select('id')
  3258. ->get()->toArray();
  3259. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  3260. ->where('del_time',0)
  3261. ->select('report_working_id')
  3262. ->get()->toArray();
  3263. $result = ReportWorking::where('del_time',0)
  3264. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  3265. ->select('id', 'order_number as order_no')
  3266. ->get()->toArray();
  3267. }elseif ($for_type == 6){
  3268. $box_id = BoxWithDispatch::where('del_time',0)
  3269. ->where('dispatch_id', $id)
  3270. ->pluck('box_id')
  3271. ->toArray();
  3272. $box_id = $box_id[0] ?? 0;
  3273. $result = Box::where('del_time',0)
  3274. ->where('id', $box_id)
  3275. ->select("id","order_no")
  3276. ->get()->toArray();
  3277. }elseif ($for_type == 7){
  3278. $process = Process::where('del_time',0)
  3279. ->where('is_need_remain',1)
  3280. ->pluck('id')
  3281. ->toArray();
  3282. $result = ScrappCount::where('del_time',0)
  3283. ->where('dispatch_sub_id', $id)
  3284. ->whereIn('process_id', $process)
  3285. ->select('id', 'order_number as order_no')
  3286. ->get()->toArray();
  3287. }elseif ($for_type == 8){
  3288. $result = ScrappCount::where('del_time',0)
  3289. ->where('dispatch_sub_id', $id)
  3290. ->where('scrapp_num','>', 0)
  3291. ->select('id', 'order_number as order_no')
  3292. ->get()->toArray();
  3293. }elseif ($for_type == 9){
  3294. $dispatch = DispatchSub::where('id',$id)
  3295. ->select('order_product_id','crt_time','product_no','technology_name')
  3296. ->first()->toArray();
  3297. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3298. ->where('crt_time',$dispatch['crt_time'])
  3299. ->where('product_no',$dispatch['product_no'])
  3300. ->where('technology_name',$dispatch['technology_name'])
  3301. ->pluck('id')->toArray();
  3302. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3303. ->whereIn('data_id', $dispatch_id)
  3304. ->where('type', ApplyOrder::type_one)
  3305. ->pluck('apply_order_id')
  3306. ->toArray();
  3307. $result = ApplyOrder::where('del_time', 0)
  3308. ->whereIn('id', $apply_order_id)
  3309. ->select('id','order_number as order_no')
  3310. ->get()->toArray();
  3311. }elseif ($for_type == 10){
  3312. $dispatch = DispatchSub::where('id',$id)
  3313. ->select('order_product_id','crt_time','product_no','technology_name')
  3314. ->first()->toArray();
  3315. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3316. ->where('crt_time',$dispatch['crt_time'])
  3317. ->where('product_no',$dispatch['product_no'])
  3318. ->where('technology_name',$dispatch['technology_name'])
  3319. ->pluck('id')->toArray();
  3320. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3321. ->whereIn('data_id', $dispatch_id)
  3322. ->where('type', ApplyOrder::type_two)
  3323. ->pluck('apply_order_id')
  3324. ->toArray();
  3325. $return = ApplyOrder::where('del_time', 0)
  3326. ->whereIn('id', $apply_order_id)
  3327. ->select('id','order_number as order_no')
  3328. ->get()->toArray();
  3329. $t = $return[0] ?? [];
  3330. if(! empty($t)) $result[] = $t;
  3331. }elseif ($for_type == 11){
  3332. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3333. $box_id = [];
  3334. foreach ($sale as $value){
  3335. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3336. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3337. $tmp = Box::where('del_time',0)
  3338. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3339. ->pluck("id")
  3340. ->toArray();
  3341. $box_id = array_merge($box_id, $tmp);
  3342. }
  3343. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3344. ->whereIn('data_id', $box_id)
  3345. ->where('type', ApplyOrder::type_four)
  3346. ->pluck('apply_order_id')
  3347. ->toArray();
  3348. $return = ApplyOrder::where('del_time', 0)
  3349. ->whereIn('id', $apply_order_id)
  3350. ->select('id','order_number as order_no')
  3351. ->get()->toArray();
  3352. $t = $return[0] ?? [];
  3353. if(! empty($t)) $result[] = $t;
  3354. }elseif ($for_type == 12){
  3355. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3356. $box_id = [];
  3357. foreach ($sale as $value){
  3358. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3359. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3360. $tmp = Box::where('del_time',0)
  3361. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3362. ->pluck("id")
  3363. ->toArray();
  3364. $box_id = array_merge($box_id, $tmp);
  3365. }
  3366. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3367. ->whereIn('data_id', $box_id)
  3368. ->where('type', ApplyOrder::type_three)
  3369. ->pluck('apply_order_id')
  3370. ->toArray();
  3371. $return = ApplyOrder::where('del_time', 0)
  3372. ->whereIn('id', $apply_order_id)
  3373. ->select('id','order_number as order_no')
  3374. ->get()->toArray();
  3375. $t = $return[0] ?? [];
  3376. if(! empty($t)) $result[] = $t;
  3377. }elseif ($for_type == 13){
  3378. $s_id = ScrappCount::where('del_time', 0)
  3379. ->where('dispatch_sub_id', $id)
  3380. ->pluck('id')
  3381. ->toArray();
  3382. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3383. ->whereIn('data_id', $s_id)
  3384. ->where('type', ApplyOrder::type_five)
  3385. ->pluck('apply_order_id')
  3386. ->toArray();
  3387. $result = ApplyOrder::where('del_time', 0)
  3388. ->whereIn('id', $apply_order_id)
  3389. ->select('id','order_number as order_no')
  3390. ->get()->toArray();
  3391. }elseif ($for_type == 14){
  3392. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3393. $box_id = [];
  3394. foreach ($sale as $value){
  3395. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3396. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3397. $tmp = Box::where('del_time',0)
  3398. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3399. ->pluck("id")
  3400. ->toArray();
  3401. $box_id = array_merge($box_id, $tmp);
  3402. }
  3403. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3404. ->whereIn('data_id', $box_id)
  3405. ->where('type', ApplyOrder::type_six)
  3406. ->pluck('apply_order_id')
  3407. ->toArray();
  3408. $return = ApplyOrder::where('del_time', 0)
  3409. ->whereIn('id', $apply_order_id)
  3410. ->select('id','order_number as order_no')
  3411. ->get()->toArray();
  3412. $t = $return[0] ?? [];
  3413. if(! empty($t)) $result[] = $t;
  3414. }
  3415. return $result;
  3416. }
  3417. private function typeSix($for_type, $id){
  3418. $result = [];
  3419. $box = Box::where('id', $id)->first();
  3420. if(empty($box)) return $result;
  3421. $top_order_no = $box->top_order_no;
  3422. $order_no = $box->order_no;
  3423. if($for_type == 1){
  3424. $box_detail = new BoxDetail(['channel'=> $top_order_no]);
  3425. $sale_orders_product_id = $box_detail->where('del_time',0)
  3426. ->where('order_no',$order_no)
  3427. ->pluck('top_id')
  3428. ->toArray();
  3429. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3430. ->where('del_time',0)
  3431. ->select('id','out_order_no as order_no')
  3432. ->get()->toArray();
  3433. }elseif ($for_type == 2){
  3434. $box_detail = new BoxDetail(['channel'=> $top_order_no]);
  3435. $sale_orders_product_id = $box_detail->where('del_time',0)
  3436. ->where('order_no',$order_no)
  3437. ->pluck('top_id')
  3438. ->toArray();
  3439. $result = OrdersProduct::where('del_time',0)
  3440. ->whereIn('sale_orders_product_id', $sale_orders_product_id)
  3441. ->select('id', 'production_no as order_no')
  3442. ->get()->toArray();
  3443. }elseif ($for_type == 3){
  3444. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3445. ->where('box_id', $id)
  3446. ->pluck('dispatch_id')
  3447. ->toArray();
  3448. $result = DispatchSub::where('del_time',0)
  3449. ->whereIn('id', $dispatch_id)
  3450. ->select('id', 'dispatch_no as order_no')
  3451. ->get()->toArray();
  3452. }elseif ($for_type == 4){
  3453. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3454. ->where('box_id', $id)
  3455. ->pluck('dispatch_id')
  3456. ->toArray();
  3457. $detail = ReportWorkingDetail::whereIn('data_id', $dispatch_id)
  3458. ->where('del_time',0)
  3459. ->select('report_working_id')
  3460. ->get()->toArray();
  3461. $return = ReportWorking::where('del_time',0)
  3462. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  3463. ->select('id', 'order_number as order_no')
  3464. ->get()->toArray();
  3465. $t = $return[0] ?? [];
  3466. if(! empty($t)) $result[] = $t;
  3467. }elseif ($for_type == 5){
  3468. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3469. ->where('box_id', $id)
  3470. ->pluck('dispatch_id')
  3471. ->toArray();
  3472. $result = DispatchSub::where('del_time',0)
  3473. ->whereIn('id', $dispatch_id)
  3474. ->select('id', 'dispatch_no as order_no')
  3475. ->get()->toArray();
  3476. }elseif ($for_type == 7){
  3477. $process = Process::where('del_time',0)
  3478. ->where('is_need_remain',1)
  3479. ->pluck('id')
  3480. ->toArray();
  3481. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3482. ->where('box_id', $id)
  3483. ->pluck('dispatch_id')
  3484. ->toArray();
  3485. $report_working_id = ReportWorkingDetail::whereIn('data_id', $dispatch_id)
  3486. ->where('del_time',0)
  3487. ->pluck('report_working_id')
  3488. ->toArray();
  3489. $return = ScrappCount::where('del_time',0)
  3490. ->whereIn('report_id', $report_working_id)
  3491. ->whereIn('process_id', $process)
  3492. ->select('id', 'order_number as order_no')
  3493. ->get()->toArray();
  3494. $t = $return[0] ?? [];
  3495. if(! empty($t)) $result[] = $t;
  3496. }elseif ($for_type == 8){
  3497. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3498. ->where('box_id', $id)
  3499. ->pluck('dispatch_id')
  3500. ->toArray();
  3501. $report_working_id = ReportWorkingDetail::whereIn('data_id', $dispatch_id)
  3502. ->where('del_time',0)
  3503. ->pluck('report_working_id')
  3504. ->toArray();
  3505. $return = ScrappCount::where('del_time',0)
  3506. ->whereIn('report_id', $report_working_id)
  3507. ->where('scrapp_num','>', 0)
  3508. ->select('id', 'order_number as order_no')
  3509. ->get()->toArray();
  3510. $t = $return[0] ?? [];
  3511. if(! empty($t)) $result[] = $t;
  3512. }elseif ($for_type == 9){
  3513. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3514. ->where('box_id', $id)
  3515. ->select('dispatch_id')
  3516. ->first()->toArray();
  3517. $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id'])
  3518. ->select('order_product_id','crt_time','product_no','technology_name')
  3519. ->first()->toArray();
  3520. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3521. ->where('crt_time',$dispatch['crt_time'])
  3522. ->where('product_no',$dispatch['product_no'])
  3523. ->where('technology_name',$dispatch['technology_name'])
  3524. ->pluck('id')->toArray();
  3525. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3526. ->whereIn('data_id', $dispatch_id)
  3527. ->where('type', ApplyOrder::type_one)
  3528. ->pluck('apply_order_id')
  3529. ->toArray();
  3530. $result = ApplyOrder::where('del_time', 0)
  3531. ->whereIn('id', $apply_order_id)
  3532. ->select('id','order_number as order_no')
  3533. ->get()->toArray();
  3534. }elseif ($for_type == 10){
  3535. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3536. ->where('box_id', $id)
  3537. ->select('dispatch_id')
  3538. ->first()->toArray();
  3539. $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id'])
  3540. ->select('order_product_id','crt_time','product_no','technology_name')
  3541. ->first()->toArray();
  3542. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3543. ->where('crt_time',$dispatch['crt_time'])
  3544. ->where('product_no',$dispatch['product_no'])
  3545. ->where('technology_name',$dispatch['technology_name'])
  3546. ->pluck('id')->toArray();
  3547. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3548. ->whereIn('data_id', $dispatch_id)
  3549. ->where('type', ApplyOrder::type_two)
  3550. ->pluck('apply_order_id')
  3551. ->toArray();
  3552. $return = ApplyOrder::where('del_time', 0)
  3553. ->whereIn('id', $apply_order_id)
  3554. ->select('id','order_number as order_no')
  3555. ->get()->toArray();
  3556. $t = $return[0] ?? [];
  3557. if(! empty($t)) $result[] = $t;
  3558. }elseif ($for_type == 11){
  3559. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3560. ->where('box_id', $id)
  3561. ->pluck('dispatch_id')
  3562. ->toArray();
  3563. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3564. $box_id = [];
  3565. foreach ($sale as $value){
  3566. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3567. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3568. $tmp = Box::where('del_time',0)
  3569. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3570. ->pluck("id")
  3571. ->toArray();
  3572. $box_id = array_merge($box_id, $tmp);
  3573. }
  3574. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3575. ->whereIn('data_id', $box_id)
  3576. ->where('type', ApplyOrder::type_four)
  3577. ->pluck('apply_order_id')
  3578. ->toArray();
  3579. $return = ApplyOrder::where('del_time', 0)
  3580. ->whereIn('id', $apply_order_id)
  3581. ->select('id','order_number as order_no')
  3582. ->get()->toArray();
  3583. $t = $return[0] ?? [];
  3584. if(! empty($t)) $result[] = $t;
  3585. }elseif ($for_type == 12){
  3586. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3587. ->where('box_id', $id)
  3588. ->pluck('dispatch_id')
  3589. ->toArray();
  3590. $sale = DispatchSub::where('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3591. $box_id = [];
  3592. foreach ($sale as $value){
  3593. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3594. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3595. $tmp = Box::where('del_time',0)
  3596. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3597. ->pluck("id")
  3598. ->toArray();
  3599. $box_id = array_merge($box_id, $tmp);
  3600. }
  3601. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3602. ->whereIn('data_id', $box_id)
  3603. ->where('type', ApplyOrder::type_three)
  3604. ->pluck('apply_order_id')
  3605. ->toArray();
  3606. $return = ApplyOrder::where('del_time', 0)
  3607. ->whereIn('id', $apply_order_id)
  3608. ->select('id','order_number as order_no')
  3609. ->get()->toArray();
  3610. $t = $return[0] ?? [];
  3611. if(! empty($t)) $result[] = $t;
  3612. }elseif ($for_type == 13){
  3613. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3614. ->where('box_id', $id)
  3615. ->select('dispatch_id')
  3616. ->first()->toArray();
  3617. $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id'])
  3618. ->select('order_product_id','crt_time','product_no','technology_name')
  3619. ->first()->toArray();
  3620. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3621. ->where('crt_time',$dispatch['crt_time'])
  3622. ->where('product_no',$dispatch['product_no'])
  3623. ->where('technology_name',$dispatch['technology_name'])
  3624. ->pluck('id')->toArray();
  3625. $s_id = ScrappCount::where('del_time', 0)
  3626. ->whereIn('dispatch_sub_id', $dispatch_id)
  3627. ->pluck('id')
  3628. ->toArray();
  3629. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3630. ->whereIn('data_id', $s_id)
  3631. ->where('type', ApplyOrder::type_five)
  3632. ->pluck('apply_order_id')
  3633. ->toArray();
  3634. $result = ApplyOrder::where('del_time', 0)
  3635. ->whereIn('id', $apply_order_id)
  3636. ->select('id','order_number as order_no')
  3637. ->get()->toArray();
  3638. }elseif ($for_type == 14){
  3639. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3640. ->where('box_id', $id)
  3641. ->pluck('dispatch_id')
  3642. ->toArray();
  3643. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3644. $box_id = [];
  3645. foreach ($sale as $value){
  3646. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3647. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3648. $tmp = Box::where('del_time',0)
  3649. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3650. ->pluck("id")
  3651. ->toArray();
  3652. $box_id = array_merge($box_id, $tmp);
  3653. }
  3654. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3655. ->whereIn('data_id', $box_id)
  3656. ->where('type', ApplyOrder::type_six)
  3657. ->pluck('apply_order_id')
  3658. ->toArray();
  3659. $return = ApplyOrder::where('del_time', 0)
  3660. ->whereIn('id', $apply_order_id)
  3661. ->select('id','order_number as order_no')
  3662. ->get()->toArray();
  3663. $t = $return[0] ?? [];
  3664. if(! empty($t)) $result[] = $t;
  3665. }
  3666. return $result;
  3667. }
  3668. private function typeSeven($for_type, $id){
  3669. $result = [];
  3670. if($for_type == 1){
  3671. $sale_orders_product_id = ScrappCount::where('del_time',0)
  3672. ->where('id', $id)
  3673. ->pluck('sale_orders_product_id')
  3674. ->toArray();
  3675. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3676. ->where('del_time',0)
  3677. ->select('id','out_order_no as order_no')
  3678. ->get()->toArray();
  3679. }elseif ($for_type == 2){
  3680. $order_product_id = ScrappCount::where('del_time',0)
  3681. ->where('id', $id)
  3682. ->pluck('order_product_id')
  3683. ->toArray();
  3684. $result = OrdersProduct::where('del_time',0)
  3685. ->whereIn('id', $order_product_id)
  3686. ->select('id', 'production_no as order_no')
  3687. ->get()->toArray();
  3688. }elseif ($for_type == 3){
  3689. $dispatch_id = ScrappCount::where('del_time',0)
  3690. ->where('id', $id)
  3691. ->pluck('dispatch_sub_id')
  3692. ->toArray();
  3693. $result = DispatchSub::where('del_time',0)
  3694. ->where('id', $dispatch_id)
  3695. ->select('id', 'dispatch_no as order_no')
  3696. ->get()->toArray();
  3697. }elseif ($for_type == 4){
  3698. $report_id = ScrappCount::where('del_time',0)
  3699. ->where('id', $id)
  3700. ->pluck('report_id')
  3701. ->toArray();
  3702. $result = ReportWorking::where('del_time',0)
  3703. ->whereIn('id', $report_id)
  3704. ->select('id', 'order_number as order_no')
  3705. ->get()->toArray();
  3706. }elseif ($for_type == 5){
  3707. $dispatch_id = ScrappCount::where('del_time',0)
  3708. ->where('id', $id)
  3709. ->pluck('dispatch_sub_id')
  3710. ->toArray();
  3711. $result = DispatchSub::where('del_time',0)
  3712. ->where('id', $dispatch_id)
  3713. ->where('finished_num','>',0)
  3714. ->select('id', 'dispatch_no as order_no')
  3715. ->get()->toArray();
  3716. }elseif ($for_type == 6){
  3717. $sale_orders_product_id = ScrappCount::where('del_time',0)
  3718. ->where('id', $id)
  3719. ->pluck('sale_orders_product_id')
  3720. ->toArray();
  3721. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  3722. $return = [];
  3723. foreach ($sale as $value){
  3724. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3725. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3726. $tmp = Box::where('del_time',0)
  3727. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3728. ->select("id","order_no")
  3729. ->get()->toArray();
  3730. $return = array_merge($return, $tmp);
  3731. }
  3732. $t = $return[0] ?? [];
  3733. if(! empty($t)) $result[] = $t;
  3734. }elseif ($for_type == 8){
  3735. $result = ScrappCount::where('del_time',0)
  3736. ->where('id', $id)
  3737. ->where('scrapp_num','>', 0)
  3738. ->select('id', 'order_number as order_no')
  3739. ->get()->toArray();
  3740. }elseif ($for_type == 9){
  3741. $dispatch_id = ScrappCount::where('del_time',0)
  3742. ->where('id', $id)
  3743. ->pluck('dispatch_sub_id')
  3744. ->toArray();
  3745. $dispatch_id = $dispatch_id[0] ?? 0;
  3746. $dispatch = DispatchSub::where('id',$dispatch_id)
  3747. ->select('order_product_id','crt_time','product_no','technology_name')
  3748. ->first()->toArray();
  3749. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3750. ->where('crt_time',$dispatch['crt_time'])
  3751. ->where('product_no',$dispatch['product_no'])
  3752. ->where('technology_name',$dispatch['technology_name'])
  3753. ->pluck('id')->toArray();
  3754. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3755. ->whereIn('data_id', $dispatch_id)
  3756. ->where('type', ApplyOrder::type_one)
  3757. ->pluck('apply_order_id')
  3758. ->toArray();
  3759. $result = ApplyOrder::where('del_time', 0)
  3760. ->whereIn('id', $apply_order_id)
  3761. ->select('id','order_number as order_no')
  3762. ->get()->toArray();
  3763. }elseif ($for_type == 10){
  3764. $dispatch_id = ScrappCount::where('del_time',0)
  3765. ->where('id', $id)
  3766. ->pluck('dispatch_sub_id')
  3767. ->toArray();
  3768. $dispatch_id = $dispatch_id[0] ?? 0;
  3769. $dispatch = DispatchSub::where('id',$dispatch_id)
  3770. ->select('order_product_id','crt_time','product_no','technology_name')
  3771. ->first()->toArray();
  3772. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3773. ->where('crt_time',$dispatch['crt_time'])
  3774. ->where('product_no',$dispatch['product_no'])
  3775. ->where('technology_name',$dispatch['technology_name'])
  3776. ->pluck('id')->toArray();
  3777. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3778. ->whereIn('data_id', $dispatch_id)
  3779. ->where('type', ApplyOrder::type_two)
  3780. ->pluck('apply_order_id')
  3781. ->toArray();
  3782. $return = ApplyOrder::where('del_time', 0)
  3783. ->whereIn('id', $apply_order_id)
  3784. ->select('id','order_number as order_no')
  3785. ->get()->toArray();
  3786. $t = $return[0] ?? [];
  3787. if(! empty($t)) $result[] = $t;
  3788. }elseif ($for_type == 11){
  3789. $dispatch_id = ScrappCount::where('del_time',0)
  3790. ->where('id', $id)
  3791. ->pluck('dispatch_sub_id')
  3792. ->toArray();
  3793. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3794. $box_id = [];
  3795. foreach ($sale as $value){
  3796. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3797. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3798. $tmp = Box::where('del_time',0)
  3799. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3800. ->pluck("id")
  3801. ->toArray();
  3802. $box_id = array_merge($box_id, $tmp);
  3803. }
  3804. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3805. ->whereIn('data_id', $box_id)
  3806. ->where('type', ApplyOrder::type_four)
  3807. ->pluck('apply_order_id')
  3808. ->toArray();
  3809. $return = ApplyOrder::where('del_time', 0)
  3810. ->whereIn('id', $apply_order_id)
  3811. ->select('id','order_number as order_no')
  3812. ->get()->toArray();
  3813. $t = $return[0] ?? [];
  3814. if(! empty($t)) $result[] = $t;
  3815. }elseif ($for_type == 12){
  3816. $dispatch_id = ScrappCount::where('del_time',0)
  3817. ->where('id', $id)
  3818. ->pluck('dispatch_sub_id')
  3819. ->toArray();
  3820. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3821. $box_id = [];
  3822. foreach ($sale as $value){
  3823. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3824. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3825. $tmp = Box::where('del_time',0)
  3826. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3827. ->pluck("id")
  3828. ->toArray();
  3829. $box_id = array_merge($box_id, $tmp);
  3830. }
  3831. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3832. ->whereIn('data_id', $box_id)
  3833. ->where('type', ApplyOrder::type_three)
  3834. ->pluck('apply_order_id')
  3835. ->toArray();
  3836. $return = ApplyOrder::where('del_time', 0)
  3837. ->whereIn('id', $apply_order_id)
  3838. ->select('id','order_number as order_no')
  3839. ->get()->toArray();
  3840. $t = $return[0] ?? [];
  3841. if(! empty($t)) $result[] = $t;
  3842. }elseif ($for_type == 13){
  3843. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3844. ->where('data_id', $id)
  3845. ->where('type', ApplyOrder::type_five)
  3846. ->pluck('apply_order_id')
  3847. ->toArray();
  3848. $result = ApplyOrder::where('del_time', 0)
  3849. ->whereIn('id', $apply_order_id)
  3850. ->select('id','order_number as order_no')
  3851. ->get()->toArray();
  3852. }elseif ($for_type == 14){
  3853. $dispatch_id = ScrappCount::where('del_time',0)
  3854. ->where('id', $id)
  3855. ->pluck('dispatch_sub_id')
  3856. ->toArray();
  3857. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3858. $box_id = [];
  3859. foreach ($sale as $value){
  3860. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3861. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3862. $tmp = Box::where('del_time',0)
  3863. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3864. ->pluck("id")
  3865. ->toArray();
  3866. $box_id = array_merge($box_id, $tmp);
  3867. }
  3868. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3869. ->whereIn('data_id', $box_id)
  3870. ->where('type', ApplyOrder::type_six)
  3871. ->pluck('apply_order_id')
  3872. ->toArray();
  3873. $return = ApplyOrder::where('del_time', 0)
  3874. ->whereIn('id', $apply_order_id)
  3875. ->select('id','order_number as order_no')
  3876. ->get()->toArray();
  3877. $t = $return[0] ?? [];
  3878. if(! empty($t)) $result[] = $t;
  3879. }
  3880. return $result;
  3881. }
  3882. private function typeEight($for_type, $id){
  3883. $result = [];
  3884. if($for_type == 1){
  3885. $sale_orders_product_id = ScrappCount::where('del_time',0)
  3886. ->where('id', $id)
  3887. ->pluck('sale_orders_product_id')
  3888. ->toArray();
  3889. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3890. ->where('del_time',0)
  3891. ->select('id','out_order_no as order_no')
  3892. ->get()->toArray();
  3893. }elseif ($for_type == 2){
  3894. $order_product_id = ScrappCount::where('del_time',0)
  3895. ->where('id', $id)
  3896. ->pluck('order_product_id')
  3897. ->toArray();
  3898. $result = OrdersProduct::where('del_time',0)
  3899. ->whereIn('id', $order_product_id)
  3900. ->select('id', 'production_no as order_no')
  3901. ->get()->toArray();
  3902. }elseif ($for_type == 3){
  3903. $dispatch_id = ScrappCount::where('del_time',0)
  3904. ->where('id', $id)
  3905. ->pluck('dispatch_sub_id')
  3906. ->toArray();
  3907. $result = DispatchSub::where('del_time',0)
  3908. ->where('id', $dispatch_id)
  3909. ->select('id', 'dispatch_no as order_no')
  3910. ->get()->toArray();
  3911. }elseif ($for_type == 4){
  3912. $report_id = ScrappCount::where('del_time',0)
  3913. ->where('id', $id)
  3914. ->pluck('report_id')
  3915. ->toArray();
  3916. $result = ReportWorking::where('del_time',0)
  3917. ->whereIn('id', $report_id)
  3918. ->select('id', 'order_number as order_no')
  3919. ->get()->toArray();
  3920. }elseif ($for_type == 5){
  3921. $dispatch_id = ScrappCount::where('del_time',0)
  3922. ->where('id', $id)
  3923. ->pluck('dispatch_sub_id')
  3924. ->toArray();
  3925. $result = DispatchSub::where('del_time',0)
  3926. ->where('id', $dispatch_id)
  3927. ->where('finished_num','>',0)
  3928. ->select('id', 'dispatch_no as order_no')
  3929. ->get()->toArray();
  3930. }elseif ($for_type == 6){
  3931. $sale_orders_product_id = ScrappCount::where('del_time',0)
  3932. ->where('id', $id)
  3933. ->pluck('sale_orders_product_id')
  3934. ->toArray();
  3935. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  3936. $return = [];
  3937. foreach ($sale as $value){
  3938. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3939. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3940. $tmp = Box::where('del_time',0)
  3941. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3942. ->select("id","order_no")
  3943. ->get()->toArray();
  3944. $return = array_merge($return, $tmp);
  3945. }
  3946. $t = $return[0] ?? [];
  3947. if(! empty($t)) $result[] = $t;
  3948. }elseif ($for_type == 7){
  3949. $process = Process::where('del_time',0)
  3950. ->where('is_need_remain',1)
  3951. ->pluck('id')
  3952. ->toArray();
  3953. $result = ScrappCount::where('del_time',0)
  3954. ->where('id', $id)
  3955. ->whereIn('process_id', $process)
  3956. ->select('id', 'order_number as order_no')
  3957. ->get()->toArray();
  3958. }elseif ($for_type == 9){
  3959. $dispatch_id = ScrappCount::where('del_time',0)
  3960. ->where('id', $id)
  3961. ->pluck('dispatch_sub_id')
  3962. ->toArray();
  3963. $dispatch_id = $dispatch_id[0] ?? 0;
  3964. $dispatch = DispatchSub::where('id',$dispatch_id)
  3965. ->select('order_product_id','crt_time','product_no','technology_name')
  3966. ->first()->toArray();
  3967. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3968. ->where('crt_time',$dispatch['crt_time'])
  3969. ->where('product_no',$dispatch['product_no'])
  3970. ->where('technology_name',$dispatch['technology_name'])
  3971. ->pluck('id')->toArray();
  3972. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3973. ->whereIn('data_id', $dispatch_id)
  3974. ->where('type', ApplyOrder::type_one)
  3975. ->pluck('apply_order_id')
  3976. ->toArray();
  3977. $result = ApplyOrder::where('del_time', 0)
  3978. ->whereIn('id', $apply_order_id)
  3979. ->select('id','order_number as order_no')
  3980. ->get()->toArray();
  3981. }elseif ($for_type == 10){
  3982. $dispatch_id = ScrappCount::where('del_time',0)
  3983. ->where('id', $id)
  3984. ->pluck('dispatch_sub_id')
  3985. ->toArray();
  3986. $dispatch_id = $dispatch_id[0] ?? 0;
  3987. $dispatch = DispatchSub::where('id',$dispatch_id)
  3988. ->select('order_product_id','crt_time','product_no','technology_name')
  3989. ->first()->toArray();
  3990. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3991. ->where('crt_time',$dispatch['crt_time'])
  3992. ->where('product_no',$dispatch['product_no'])
  3993. ->where('technology_name',$dispatch['technology_name'])
  3994. ->pluck('id')->toArray();
  3995. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3996. ->whereIn('data_id', $dispatch_id)
  3997. ->where('type', ApplyOrder::type_two)
  3998. ->pluck('apply_order_id')
  3999. ->toArray();
  4000. $return = ApplyOrder::where('del_time', 0)
  4001. ->whereIn('id', $apply_order_id)
  4002. ->select('id','order_number as order_no')
  4003. ->get()->toArray();
  4004. $t = $return[0] ?? [];
  4005. if(! empty($t)) $result[] = $t;
  4006. }elseif ($for_type == 11){
  4007. $dispatch_id = ScrappCount::where('del_time',0)
  4008. ->where('id', $id)
  4009. ->pluck('dispatch_sub_id')
  4010. ->toArray();
  4011. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  4012. $box_id = [];
  4013. foreach ($sale as $value){
  4014. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  4015. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  4016. $tmp = Box::where('del_time',0)
  4017. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  4018. ->pluck("id")
  4019. ->toArray();
  4020. $box_id = array_merge($box_id, $tmp);
  4021. }
  4022. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4023. ->whereIn('data_id', $box_id)
  4024. ->where('type', ApplyOrder::type_four)
  4025. ->pluck('apply_order_id')
  4026. ->toArray();
  4027. $return = ApplyOrder::where('del_time', 0)
  4028. ->whereIn('id', $apply_order_id)
  4029. ->select('id','order_number as order_no')
  4030. ->get()->toArray();
  4031. $t = $return[0] ?? [];
  4032. if(! empty($t)) $result[] = $t;
  4033. }elseif ($for_type == 12){
  4034. $dispatch_id = ScrappCount::where('del_time',0)
  4035. ->where('id', $id)
  4036. ->pluck('dispatch_sub_id')
  4037. ->toArray();
  4038. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  4039. $box_id = [];
  4040. foreach ($sale as $value){
  4041. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  4042. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  4043. $tmp = Box::where('del_time',0)
  4044. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  4045. ->pluck("id")
  4046. ->toArray();
  4047. $box_id = array_merge($box_id, $tmp);
  4048. }
  4049. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4050. ->whereIn('data_id', $box_id)
  4051. ->where('type', ApplyOrder::type_three)
  4052. ->pluck('apply_order_id')
  4053. ->toArray();
  4054. $return = ApplyOrder::where('del_time', 0)
  4055. ->whereIn('id', $apply_order_id)
  4056. ->select('id','order_number as order_no')
  4057. ->get()->toArray();
  4058. $t = $return[0] ?? [];
  4059. if(! empty($t)) $result[] = $t;
  4060. }elseif ($for_type == 13){
  4061. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4062. ->where('data_id', $id)
  4063. ->where('type', ApplyOrder::type_five)
  4064. ->pluck('apply_order_id')
  4065. ->toArray();
  4066. $result = ApplyOrder::where('del_time', 0)
  4067. ->whereIn('id', $apply_order_id)
  4068. ->select('id','order_number as order_no')
  4069. ->get()->toArray();
  4070. }elseif ($for_type == 14){
  4071. $dispatch_id = ScrappCount::where('del_time',0)
  4072. ->where('id', $id)
  4073. ->pluck('dispatch_sub_id')
  4074. ->toArray();
  4075. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  4076. $box_id = [];
  4077. foreach ($sale as $value){
  4078. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  4079. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  4080. $tmp = Box::where('del_time',0)
  4081. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  4082. ->pluck("id")
  4083. ->toArray();
  4084. $box_id = array_merge($box_id, $tmp);
  4085. }
  4086. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4087. ->whereIn('data_id', $box_id)
  4088. ->where('type', ApplyOrder::type_six)
  4089. ->pluck('apply_order_id')
  4090. ->toArray();
  4091. $return = ApplyOrder::where('del_time', 0)
  4092. ->whereIn('id', $apply_order_id)
  4093. ->select('id','order_number as order_no')
  4094. ->get()->toArray();
  4095. $t = $return[0] ?? [];
  4096. if(! empty($t)) $result[] = $t;
  4097. }
  4098. return $result;
  4099. }
  4100. private function typeNine($for_type, $id){
  4101. $data_id = ApplyOrderDetail::where('del_time', 0)
  4102. ->where('apply_order_id', $id)
  4103. ->where('type', ApplyOrder::type_one)
  4104. ->pluck('data_id')
  4105. ->toArray();
  4106. $result = DispatchSub::where('del_time',0)
  4107. ->whereIn('id', $data_id)
  4108. ->select('id', 'dispatch_no as order_no')
  4109. ->get()->toArray();
  4110. return $result;
  4111. }
  4112. private function typeTen($for_type, $id){
  4113. $data_id = ApplyOrderDetail::where('del_time', 0)
  4114. ->where('apply_order_id', $id)
  4115. ->where('type', ApplyOrder::type_two)
  4116. ->pluck('data_id')
  4117. ->toArray();
  4118. $detail = ReportWorkingDetail::whereIn('data_id', $data_id)
  4119. ->where('del_time',0)
  4120. ->select('report_working_id')
  4121. ->get()->toArray();
  4122. $return = ReportWorking::where('del_time',0)
  4123. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  4124. ->select('id', 'order_number as order_no')
  4125. ->get()->toArray();
  4126. $result[] = $return[0];
  4127. return $result;
  4128. }
  4129. private function typeEve($for_type, $id){
  4130. $detail = ApplyOrderDetail::where('del_time', 0)
  4131. ->where('apply_order_id', $id)
  4132. ->where('type', ApplyOrder::type_four)
  4133. ->select('data_id as box_id')
  4134. ->get()->toArray();
  4135. $result = Box::where('del_time', 0)
  4136. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  4137. ->select("id", 'order_no')
  4138. ->get()->toArray();
  4139. return $result;
  4140. }
  4141. private function typeTwl($for_type, $id){
  4142. $detail = ApplyOrderDetail::where('del_time', 0)
  4143. ->where('apply_order_id', $id)
  4144. ->where('type', ApplyOrder::type_three)
  4145. ->select('data_id as box_id')
  4146. ->get()->toArray();
  4147. $result = Box::where('del_time', 0)
  4148. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  4149. ->select("id", 'order_no')
  4150. ->get()->toArray();
  4151. return $result;
  4152. }
  4153. private function typeThi($for_type, $id){
  4154. $detail = ApplyOrderDetail::where('del_time', 0)
  4155. ->where('apply_order_id', $id)
  4156. ->where('type', ApplyOrder::type_five)
  4157. ->select('data_id as zj_id')
  4158. ->get()->toArray();
  4159. $result = ScrappCount::where('del_time', 0)
  4160. ->whereIn('id', array_unique(array_column($detail, 'zj_id')))
  4161. ->select('id','order_number as order_no')
  4162. ->get()->toArray();
  4163. return $result;
  4164. }
  4165. private function typeFourTeen($for_type, $id){
  4166. $detail = ApplyOrderDetail::where('del_time', 0)
  4167. ->where('apply_order_id', $id)
  4168. ->where('type', ApplyOrder::type_six)
  4169. ->select('data_id as box_id')
  4170. ->get()->toArray();
  4171. $result = Box::where('del_time', 0)
  4172. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  4173. ->select("id", 'order_no')
  4174. ->get()->toArray();
  4175. return $result;
  4176. }
  4177. public function reportList($data){
  4178. $model = ReportMessage::where('del_time',0)
  4179. ->select('id','order_id','opt_case','quantity','crt_time','user_id','is_use')
  4180. ->orderBy('id','desc');
  4181. $list = $this->limit($model,'',$data);
  4182. $list = $this->fillRData($list);
  4183. $count = ReportMessage::where('del_time',0)
  4184. ->where('is_use',0)
  4185. ->count();
  4186. return [true, ['list'=> $list, 'count' => $count]];
  4187. }
  4188. public function fillRData($data){
  4189. if(empty($data['data'])) return $data;
  4190. $dispatch = DispatchSub::whereIn('id',array_unique(array_column($data['data'],'order_id')))
  4191. ->select('id','dispatch_no','process_id')
  4192. ->get()->toArray();
  4193. $process = Process::where('del_time',0)
  4194. ->whereIn('id',array_unique(array_column($dispatch,'process_id')))
  4195. ->get()->toArray();
  4196. $process = array_column($process,null,'id');
  4197. $map = [];
  4198. foreach ($dispatch as $value){
  4199. $map[$value['id']] = $value;
  4200. }
  4201. $emp_map = Employee::whereIn('id',array_unique(array_column($data['data'],'user_id')))
  4202. ->pluck("emp_name",'id')
  4203. ->toArray();
  4204. foreach ($data['data'] as $key => $value){
  4205. $time = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  4206. $data['data'][$key]['crt_time'] = $time;
  4207. $t = $map[$value['order_id']] ?? [];
  4208. $e = $emp_map[$value['user_id']] ?? "";
  4209. $p = $process[$t['process_id']] ?? [];
  4210. $p_t = $p['title'] ?? "";
  4211. if($p['zj_type'] == 0){
  4212. $p_t .= ":无需质检";
  4213. }elseif($p['zj_type'] == 1){
  4214. $p_t .= ":全检";
  4215. }else{
  4216. $p_t .= ":抽检";
  4217. }
  4218. $data['data'][$key]['message'] = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工,请及时处理";
  4219. }
  4220. return $data;
  4221. }
  4222. public function reportRemainUpdate($data){
  4223. if(empty($data['id'])) return [false, 'ID不能为空'];
  4224. if(empty($data['type'])) return [false, 'type不能为空'];
  4225. $type = $data['type'];
  4226. if($type == 1){
  4227. ReportMessage::where('id', $data['id'])
  4228. ->update(['is_use' => 1]);
  4229. }else{
  4230. ReportMessage::where('id', $data['id'])
  4231. ->update(['del_time' => time()]);
  4232. }
  4233. return [true, ''];
  4234. }
  4235. public function filldatas(){
  4236. $box = Box::where('del_time',0)
  4237. ->where('crt_time','>=',1750727777)
  4238. ->select("top_order_no", 'order_no','id')
  4239. ->get()->toArray();
  4240. $box_id = [];
  4241. foreach ($box as $value) {
  4242. $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
  4243. $t = $box_detail->where('order_no', $value['order_no'])->where('del_time',0)->select('top_id as id')->first()->toArray();
  4244. $box_id[$value['id']] = $t['id'];
  4245. }
  4246. $result = DispatchSub::where('del_time',0)
  4247. ->whereIn('sale_orders_product_id', array_values($box_id))
  4248. ->where('process_id',14)
  4249. ->where('finished_num', '>', '0.000')
  4250. ->select('id','dispatch_quantity','finished_num','order_product_id','sale_orders_product_id','technology_name')
  4251. ->get()->toArray();
  4252. $map = [];
  4253. $return = [];
  4254. foreach ($result as $key => $value){
  4255. if($value['finished_num'] < 1){
  4256. $new_key = $value['order_product_id'] . $value['sale_orders_product_id'] . $value['technology_name'];
  4257. $tmp = bcsub(0.750,$value['finished_num'],3);
  4258. $map[$new_key][] = $tmp;
  4259. $return[] = $value;
  4260. }
  4261. }dump(count($return));
  4262. $return2= [];
  4263. foreach ($result as $key => $value){
  4264. if($value['finished_num'] >= 1){
  4265. $new_key = $value['order_product_id'] . $value['sale_orders_product_id'] . $value['technology_name'];
  4266. if(isset($map[$new_key])){
  4267. foreach ($map[$new_key] as $val){
  4268. $return[] = [
  4269. 'id' => $value['id'],
  4270. 'dispatch_quantity' => $val,
  4271. 'finished_num' => $val,
  4272. 'order_product_id' => $value['order_product_id'],
  4273. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  4274. 'technology_name' => $value['technology_name'],
  4275. ];
  4276. $value['finished_num'] = bcsub($value['finished_num'], $val,3);
  4277. }
  4278. }
  4279. $return2[] = $value;
  4280. }
  4281. }
  4282. dd($return2);
  4283. }
  4284. }