ApplyOrderService.php 214 KB

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