ApplyOrderService.php 210 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668
  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('order_number');
  1173. if(empty($order_number)){
  1174. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  1175. $number = $str . $number;
  1176. }else{
  1177. $tmp = substr($order_number, -3);
  1178. $tmp = $tmp + 1;
  1179. //超过999
  1180. // if(strlen($tmp) > 3) return '';
  1181. $number = str_pad($tmp,3,'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. if ($data['type'] == 1) {
  2440. $result = $this->typeOne($for_type, $id);
  2441. } elseif ($type == 2) {
  2442. $result = $this->typeTwo($for_type, $id);
  2443. } elseif ($type == 3) {
  2444. $result = $this->typeThree($for_type, $id);
  2445. } elseif ($type == 4) {
  2446. $result = $this->typeFour($for_type, $id);
  2447. } elseif ($type == 5) {
  2448. $result = $this->typeFive($for_type, $id);
  2449. } elseif ($type == 6) {
  2450. $result = $this->typeSix($for_type, $id);
  2451. } elseif ($type == 7) {
  2452. $result = $this->typeSeven($for_type, $id);
  2453. } elseif ($type == 8) {
  2454. $result = $this->typeEight($for_type, $id);
  2455. } elseif ($type == 9) {
  2456. $result = $this->typeNine($for_type, $id);
  2457. } elseif ($type == 10) {
  2458. $result = $this->typeTen($for_type, $id);
  2459. } elseif ($type == 11) {
  2460. $result = $this->typeEve($for_type, $id);
  2461. } elseif ($type == 12) {
  2462. $result = $this->typeTwl($for_type, $id);
  2463. } elseif ($type == 13) {
  2464. $result = $this->typeThi($for_type, $id);
  2465. } elseif ($type == 14) {
  2466. $result = $this->typeFourTeen($for_type, $id);
  2467. }
  2468. return $result;
  2469. }
  2470. private function typeOne($for_type, $id){
  2471. $result = [];
  2472. if($for_type == 2){
  2473. $result = OrdersProduct::where('del_time',0)
  2474. ->where('sale_orders_product_id', $id)
  2475. ->select('id', 'production_no as order_no')
  2476. ->get()->toArray();
  2477. }elseif ($for_type == 3){
  2478. $result = DispatchSub::where('del_time',0)
  2479. ->where('sale_orders_product_id', $id)
  2480. ->select('id', 'dispatch_no as order_no')
  2481. ->get()->toArray();
  2482. }elseif ($for_type == 4){
  2483. $dispatch = DispatchSub::where('del_time',0)
  2484. ->where('sale_orders_product_id', $id)
  2485. ->select('id')
  2486. ->get()->toArray();
  2487. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  2488. ->where('del_time',0)
  2489. ->select('report_working_id')
  2490. ->get()->toArray();
  2491. $result = ReportWorking::where('del_time',0)
  2492. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  2493. ->select('id', 'order_number as order_no')
  2494. ->get()->toArray();
  2495. }elseif ($for_type == 5){
  2496. $result = DispatchSub::where('del_time',0)
  2497. ->where('sale_orders_product_id', $id)
  2498. ->where('finished_num','>',0)
  2499. ->select('id', 'dispatch_no as order_no')
  2500. ->get()->toArray();
  2501. }elseif ($for_type == 6){
  2502. $sale = SaleOrdersProduct::where('id', $id)->select('order_no')->get()->toArray();
  2503. foreach ($sale as $value){
  2504. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2505. $box_detail_order = $box_detail->where('top_id',$id)->select('order_no')->get()->toArray();
  2506. $tmp = Box::where('del_time',0)
  2507. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2508. ->select("id","order_no")
  2509. ->get()->toArray();
  2510. $result = array_merge($result, $tmp);
  2511. }
  2512. }elseif ($for_type == 7){
  2513. $process = Process::where('del_time',0)
  2514. ->where('is_need_remain',1)
  2515. ->pluck('id')
  2516. ->toArray();
  2517. $result = ScrappCount::where('del_time',0)
  2518. ->where('sale_orders_product_id', $id)
  2519. ->whereIn('process_id', $process)
  2520. ->select('id', 'order_number as order_no')
  2521. ->get()->toArray();
  2522. }elseif ($for_type == 8){
  2523. $result = ScrappCount::where('del_time',0)
  2524. ->where('sale_orders_product_id', $id)
  2525. ->where('scrapp_num','>', 0)
  2526. ->select('id', 'order_number as order_no')
  2527. ->get()->toArray();
  2528. }elseif ($for_type == 9){
  2529. $dispatch_id = DispatchSub::where('del_time',0)
  2530. ->where('sale_orders_product_id', $id)
  2531. ->pluck('id')
  2532. ->toArray();
  2533. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2534. ->whereIn('data_id', $dispatch_id)
  2535. ->where('type', ApplyOrder::type_one)
  2536. ->pluck('apply_order_id')
  2537. ->toArray();
  2538. $result = ApplyOrder::where('del_time', 0)
  2539. ->whereIn('id', $apply_order_id)
  2540. ->select('id','order_number as order_no')
  2541. ->get()->toArray();
  2542. }elseif ($for_type == 10){
  2543. $dispatch_id = DispatchSub::where('del_time',0)
  2544. ->where('sale_orders_product_id', $id)
  2545. ->pluck('id')
  2546. ->toArray();
  2547. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2548. ->whereIn('data_id', $dispatch_id)
  2549. ->where('type', ApplyOrder::type_two)
  2550. ->pluck('apply_order_id')
  2551. ->toArray();
  2552. $result = ApplyOrder::where('del_time', 0)
  2553. ->whereIn('id', $apply_order_id)
  2554. ->select('id','order_number as order_no')
  2555. ->get()->toArray();
  2556. }elseif ($for_type == 11){
  2557. $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray();
  2558. $box_id = [];
  2559. foreach ($sale as $value){
  2560. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2561. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2562. $tmp = Box::where('del_time',0)
  2563. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2564. ->pluck("id")
  2565. ->toArray();
  2566. $box_id = array_merge($box_id, $tmp);
  2567. }
  2568. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2569. ->whereIn('data_id', $box_id)
  2570. ->where('type', ApplyOrder::type_four)
  2571. ->pluck('apply_order_id')
  2572. ->toArray();
  2573. $result = ApplyOrder::where('del_time', 0)
  2574. ->whereIn('id', $apply_order_id)
  2575. ->select('id','order_number as order_no')
  2576. ->get()->toArray();
  2577. }elseif ($for_type == 12){
  2578. $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray();
  2579. $box_id = [];
  2580. foreach ($sale as $value){
  2581. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2582. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2583. $tmp = Box::where('del_time',0)
  2584. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2585. ->pluck("id")
  2586. ->toArray();
  2587. $box_id = array_merge($box_id, $tmp);
  2588. }
  2589. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2590. ->whereIn('data_id', $box_id)
  2591. ->where('type', ApplyOrder::type_three)
  2592. ->pluck('apply_order_id')
  2593. ->toArray();
  2594. $result = ApplyOrder::where('del_time', 0)
  2595. ->whereIn('id', $apply_order_id)
  2596. ->select('id','order_number as order_no')
  2597. ->get()->toArray();
  2598. }elseif ($for_type == 13){
  2599. $s_id = ScrappCount::where('del_time', 0)
  2600. ->where('sale_orders_product_id', $id)
  2601. ->pluck('id')
  2602. ->toArray();
  2603. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2604. ->whereIn('data_id', $s_id)
  2605. ->where('type', ApplyOrder::type_five)
  2606. ->pluck('apply_order_id')
  2607. ->toArray();
  2608. $result = ApplyOrder::where('del_time', 0)
  2609. ->whereIn('id', $apply_order_id)
  2610. ->select('id','order_number as order_no')
  2611. ->get()->toArray();
  2612. }elseif ($for_type == 14){
  2613. $sale = SaleOrdersProduct::where('id', $id)->select('order_no','id')->get()->toArray();
  2614. $box_id = [];
  2615. foreach ($sale as $value){
  2616. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2617. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2618. $tmp = Box::where('del_time',0)
  2619. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2620. ->pluck("id")
  2621. ->toArray();
  2622. $box_id = array_merge($box_id, $tmp);
  2623. }
  2624. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2625. ->whereIn('data_id', $box_id)
  2626. ->where('type', ApplyOrder::type_six)
  2627. ->pluck('apply_order_id')
  2628. ->toArray();
  2629. $result = ApplyOrder::where('del_time', 0)
  2630. ->whereIn('id', $apply_order_id)
  2631. ->select('id','order_number as order_no')
  2632. ->get()->toArray();
  2633. }
  2634. return $result;
  2635. }
  2636. private function typeTwo($for_type, $id){
  2637. $result = [];
  2638. if($for_type == 1){
  2639. $sale_orders_product_id = OrdersProduct::where('del_time',0)
  2640. ->where('id', $id)
  2641. ->pluck('sale_orders_product_id')
  2642. ->toArray();
  2643. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  2644. ->where('del_time',0)
  2645. ->select('id','out_order_no as order_no')
  2646. ->get()->toArray();
  2647. }elseif ($for_type == 3){
  2648. $result = DispatchSub::where('del_time',0)
  2649. ->where('order_product_id', $id)
  2650. ->select('id', 'dispatch_no as order_no')
  2651. ->get()->toArray();
  2652. }elseif ($for_type == 4){
  2653. $dispatch = DispatchSub::where('del_time',0)
  2654. ->where('order_product_id', $id)
  2655. ->select('id')
  2656. ->get()->toArray();
  2657. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  2658. ->where('del_time',0)
  2659. ->select('report_working_id')
  2660. ->get()->toArray();
  2661. $result = ReportWorking::where('del_time',0)
  2662. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  2663. ->select('id', 'order_number as order_no')
  2664. ->get()->toArray();
  2665. }elseif ($for_type == 5){
  2666. $result = DispatchSub::where('del_time',0)
  2667. ->where('order_product_id', $id)
  2668. ->where('finished_num','>',0)
  2669. ->select('id', 'dispatch_no as order_no')
  2670. ->get()->toArray();
  2671. }elseif ($for_type == 6){
  2672. $sale_orders_product_id = OrdersProduct::where('del_time',0)
  2673. ->where('id', $id)
  2674. ->pluck('sale_orders_product_id')
  2675. ->toArray();
  2676. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  2677. foreach ($sale as $value){
  2678. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2679. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2680. $tmp = Box::where('del_time',0)
  2681. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2682. ->select("id","order_no")
  2683. ->get()->toArray();
  2684. $result = array_merge($result, $tmp);
  2685. }
  2686. }elseif ($for_type == 7){
  2687. $process = Process::where('del_time',0)
  2688. ->where('is_need_remain',1)
  2689. ->pluck('id')
  2690. ->toArray();
  2691. $result = ScrappCount::where('del_time',0)
  2692. ->where('order_product_id', $id)
  2693. ->whereIn('process_id', $process)
  2694. ->select('id', 'order_number as order_no')
  2695. ->get()->toArray();
  2696. }elseif ($for_type == 8){
  2697. $result = ScrappCount::where('del_time',0)
  2698. ->where('order_product_id', $id)
  2699. ->where('scrapp_num','>', 0)
  2700. ->select('id', 'order_number as order_no')
  2701. ->get()->toArray();
  2702. }elseif ($for_type == 9){
  2703. $dispatch_id = DispatchSub::where('del_time',0)
  2704. ->where('order_product_id', $id)
  2705. ->pluck('id')
  2706. ->toArray();
  2707. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2708. ->whereIn('data_id', $dispatch_id)
  2709. ->where('type', ApplyOrder::type_one)
  2710. ->pluck('apply_order_id')
  2711. ->toArray();
  2712. $result = ApplyOrder::where('del_time', 0)
  2713. ->whereIn('id', $apply_order_id)
  2714. ->select('id','order_number as order_no')
  2715. ->get()->toArray();
  2716. }elseif ($for_type == 10){
  2717. $dispatch_id = DispatchSub::where('del_time',0)
  2718. ->where('order_product_id', $id)
  2719. ->pluck('id')
  2720. ->toArray();
  2721. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2722. ->whereIn('data_id', $dispatch_id)
  2723. ->where('type', ApplyOrder::type_two)
  2724. ->pluck('apply_order_id')
  2725. ->toArray();
  2726. $result = ApplyOrder::where('del_time', 0)
  2727. ->whereIn('id', $apply_order_id)
  2728. ->select('id','order_number as order_no')
  2729. ->get()->toArray();
  2730. }elseif ($for_type == 11){
  2731. $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2732. $box_id = [];
  2733. foreach ($sale as $value){
  2734. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2735. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2736. $tmp = Box::where('del_time',0)
  2737. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2738. ->pluck("id")
  2739. ->toArray();
  2740. $box_id = array_merge($box_id, $tmp);
  2741. }
  2742. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2743. ->whereIn('data_id', $box_id)
  2744. ->where('type', ApplyOrder::type_four)
  2745. ->pluck('apply_order_id')
  2746. ->toArray();
  2747. $result = ApplyOrder::where('del_time', 0)
  2748. ->whereIn('id', $apply_order_id)
  2749. ->select('id','order_number as order_no')
  2750. ->get()->toArray();
  2751. }elseif ($for_type == 12){
  2752. $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2753. $box_id = [];
  2754. foreach ($sale as $value){
  2755. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2756. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2757. $tmp = Box::where('del_time',0)
  2758. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2759. ->pluck("id")
  2760. ->toArray();
  2761. $box_id = array_merge($box_id, $tmp);
  2762. }
  2763. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2764. ->whereIn('data_id', $box_id)
  2765. ->where('type', ApplyOrder::type_three)
  2766. ->pluck('apply_order_id')
  2767. ->toArray();
  2768. $result = ApplyOrder::where('del_time', 0)
  2769. ->whereIn('id', $apply_order_id)
  2770. ->select('id','order_number as order_no')
  2771. ->get()->toArray();
  2772. }elseif ($for_type == 13){
  2773. $s_id = ScrappCount::where('del_time', 0)
  2774. ->where('order_product_id', $id)
  2775. ->pluck('id')
  2776. ->toArray();
  2777. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2778. ->whereIn('data_id', $s_id)
  2779. ->where('type', ApplyOrder::type_five)
  2780. ->pluck('apply_order_id')
  2781. ->toArray();
  2782. $result = ApplyOrder::where('del_time', 0)
  2783. ->whereIn('id', $apply_order_id)
  2784. ->select('id','order_number as order_no')
  2785. ->get()->toArray();
  2786. }elseif ($for_type == 14){
  2787. $sale = OrdersProduct::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2788. $box_id = [];
  2789. foreach ($sale as $value){
  2790. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2791. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2792. $tmp = Box::where('del_time',0)
  2793. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2794. ->pluck("id")
  2795. ->toArray();
  2796. $box_id = array_merge($box_id, $tmp);
  2797. }
  2798. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2799. ->whereIn('data_id', $box_id)
  2800. ->where('type', ApplyOrder::type_six)
  2801. ->pluck('apply_order_id')
  2802. ->toArray();
  2803. $result = ApplyOrder::where('del_time', 0)
  2804. ->whereIn('id', $apply_order_id)
  2805. ->select('id','order_number as order_no')
  2806. ->get()->toArray();
  2807. }
  2808. return $result;
  2809. }
  2810. private function typeThree($for_type, $id){
  2811. $result = [];
  2812. if($for_type == 1){
  2813. $sale_orders_product_id = DispatchSub::where('del_time',0)
  2814. ->where('id', $id)
  2815. ->pluck('sale_orders_product_id')
  2816. ->toArray();
  2817. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  2818. ->where('del_time',0)
  2819. ->select('id','out_order_no as order_no')
  2820. ->get()->toArray();
  2821. }elseif ($for_type == 2){
  2822. $order_product_id = DispatchSub::where('del_time',0)
  2823. ->where('id', $id)
  2824. ->pluck('order_product_id')
  2825. ->toArray();
  2826. $result = OrdersProduct::where('del_time',0)
  2827. ->whereIn('id', $order_product_id)
  2828. ->select('id', 'production_no as order_no')
  2829. ->get()->toArray();
  2830. }elseif ($for_type == 4){
  2831. $dispatch = DispatchSub::where('del_time',0)
  2832. ->where('id', $id)
  2833. ->select('id')
  2834. ->get()->toArray();
  2835. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  2836. ->where('del_time',0)
  2837. ->select('report_working_id')
  2838. ->get()->toArray();
  2839. $result = ReportWorking::where('del_time',0)
  2840. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  2841. ->select('id', 'order_number as order_no')
  2842. ->get()->toArray();
  2843. }elseif ($for_type == 5){
  2844. $result = DispatchSub::where('del_time',0)
  2845. ->where('id', $id)
  2846. ->where('finished_num','>',0)
  2847. ->select('id', 'dispatch_no as order_no')
  2848. ->get()->toArray();
  2849. }elseif ($for_type == 6){
  2850. $sale_orders_product_id = DispatchSub::where('del_time',0)
  2851. ->where('id', $id)
  2852. ->pluck('sale_orders_product_id')
  2853. ->toArray();
  2854. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  2855. foreach ($sale as $value){
  2856. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2857. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2858. $tmp = Box::where('del_time',0)
  2859. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2860. ->select("id","order_no")
  2861. ->get()->toArray();
  2862. $result = array_merge($result, $tmp);
  2863. }
  2864. }elseif ($for_type == 7){
  2865. $process = Process::where('del_time',0)
  2866. ->where('is_need_remain',1)
  2867. ->pluck('id')
  2868. ->toArray();
  2869. $result = ScrappCount::where('del_time',0)
  2870. ->where('dispatch_sub_id', $id)
  2871. ->whereIn('process_id', $process)
  2872. ->select('id', 'order_number as order_no')
  2873. ->get()->toArray();
  2874. }elseif ($for_type == 8){
  2875. $result = ScrappCount::where('del_time',0)
  2876. ->where('dispatch_sub_id', $id)
  2877. ->where('scrapp_num','>', 0)
  2878. ->select('id', 'order_number as order_no')
  2879. ->get()->toArray();
  2880. }elseif ($for_type == 9){
  2881. $dispatch = DispatchSub::where('id',$id)
  2882. ->select('order_product_id','crt_time','product_no','technology_name')
  2883. ->first();
  2884. if(! empty($dispatch)){
  2885. $dispatch = $dispatch->toArray();
  2886. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  2887. ->where('crt_time',$dispatch['crt_time'])
  2888. ->where('product_no',$dispatch['product_no'])
  2889. ->where('technology_name',$dispatch['technology_name'])
  2890. ->pluck('id')->toArray();
  2891. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2892. ->whereIn('data_id', $dispatch_id)
  2893. ->where('type', ApplyOrder::type_one)
  2894. ->pluck('apply_order_id')
  2895. ->toArray();
  2896. $result = ApplyOrder::where('del_time', 0)
  2897. ->whereIn('id', $apply_order_id)
  2898. ->select('id','order_number as order_no')
  2899. ->get()->toArray();
  2900. }
  2901. }elseif ($for_type == 10){
  2902. $dispatch = DispatchSub::where('id',$id)
  2903. ->select('order_product_id','crt_time','product_no','technology_name')
  2904. ->first();
  2905. if(! empty($dispatch)){
  2906. $dispatch = $dispatch->toArray();
  2907. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  2908. ->where('crt_time',$dispatch['crt_time'])
  2909. ->where('product_no',$dispatch['product_no'])
  2910. ->where('technology_name',$dispatch['technology_name'])
  2911. ->pluck('id')->toArray();
  2912. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2913. ->whereIn('data_id', $dispatch_id)
  2914. ->where('type', ApplyOrder::type_two)
  2915. ->pluck('apply_order_id')
  2916. ->toArray();
  2917. $result = ApplyOrder::where('del_time', 0)
  2918. ->whereIn('id', $apply_order_id)
  2919. ->select('id','order_number as order_no')
  2920. ->get()->toArray();
  2921. }
  2922. }elseif ($for_type == 11){
  2923. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2924. $box_id = [];
  2925. foreach ($sale as $value){
  2926. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2927. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2928. $tmp = Box::where('del_time',0)
  2929. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2930. ->pluck("id")
  2931. ->toArray();
  2932. $box_id = array_merge($box_id, $tmp);
  2933. }
  2934. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2935. ->whereIn('data_id', $box_id)
  2936. ->where('type', ApplyOrder::type_four)
  2937. ->pluck('apply_order_id')
  2938. ->toArray();
  2939. $result = ApplyOrder::where('del_time', 0)
  2940. ->whereIn('id', $apply_order_id)
  2941. ->select('id','order_number as order_no')
  2942. ->get()->toArray();
  2943. }elseif ($for_type == 12){
  2944. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2945. $box_id = [];
  2946. foreach ($sale as $value){
  2947. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2948. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2949. $tmp = Box::where('del_time',0)
  2950. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2951. ->pluck("id")
  2952. ->toArray();
  2953. $box_id = array_merge($box_id, $tmp);
  2954. }
  2955. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2956. ->whereIn('data_id', $box_id)
  2957. ->where('type', ApplyOrder::type_three)
  2958. ->pluck('apply_order_id')
  2959. ->toArray();
  2960. $result = ApplyOrder::where('del_time', 0)
  2961. ->whereIn('id', $apply_order_id)
  2962. ->select('id','order_number as order_no')
  2963. ->get()->toArray();
  2964. }elseif ($for_type == 13){
  2965. $dispatch = DispatchSub::where('id',$id)
  2966. ->select('order_product_id','crt_time','product_no','technology_name')
  2967. ->first();
  2968. if(! empty($dispatch)){
  2969. $dispatch = $dispatch->toArray();
  2970. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  2971. ->where('crt_time',$dispatch['crt_time'])
  2972. ->where('product_no',$dispatch['product_no'])
  2973. ->where('technology_name',$dispatch['technology_name'])
  2974. ->pluck('id')->toArray();
  2975. $s_id = ScrappCount::where('del_time', 0)
  2976. ->whereIn('dispatch_sub_id', $dispatch_id)
  2977. ->pluck('id')
  2978. ->toArray();
  2979. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  2980. ->whereIn('data_id', $s_id)
  2981. ->where('type', ApplyOrder::type_five)
  2982. ->pluck('apply_order_id')
  2983. ->toArray();
  2984. $result = ApplyOrder::where('del_time', 0)
  2985. ->whereIn('id', $apply_order_id)
  2986. ->select('id','order_number as order_no')
  2987. ->get()->toArray();
  2988. }
  2989. }elseif ($for_type == 14){
  2990. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  2991. $box_id = [];
  2992. foreach ($sale as $value){
  2993. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  2994. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  2995. $tmp = Box::where('del_time',0)
  2996. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  2997. ->pluck("id")
  2998. ->toArray();
  2999. $box_id = array_merge($box_id, $tmp);
  3000. }
  3001. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3002. ->whereIn('data_id', $box_id)
  3003. ->where('type', ApplyOrder::type_four)
  3004. ->pluck('apply_order_id')
  3005. ->toArray();
  3006. $result = ApplyOrder::where('del_time', 0)
  3007. ->whereIn('id', $apply_order_id)
  3008. ->select('id','order_number as order_no')
  3009. ->get()->toArray();
  3010. }
  3011. return $result;
  3012. }
  3013. private function typeFour($for_type, $id){
  3014. $result = [];
  3015. if($for_type == 1){
  3016. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3017. ->where('del_time',0)
  3018. ->pluck('data_id')
  3019. ->toArray();
  3020. $sale_orders_product_id = DispatchSub::where('del_time',0)
  3021. ->whereIn('id', $dispatch_id)
  3022. ->pluck('sale_orders_product_id')
  3023. ->toArray();
  3024. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3025. ->where('del_time',0)
  3026. ->select('id','out_order_no as order_no')
  3027. ->get()->toArray();
  3028. }elseif ($for_type == 2){
  3029. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3030. ->where('del_time',0)
  3031. ->pluck('data_id')
  3032. ->toArray();
  3033. $order_product_id = DispatchSub::where('del_time',0)
  3034. ->whereIn('id', $dispatch_id)
  3035. ->pluck('order_product_id')
  3036. ->toArray();
  3037. $result = OrdersProduct::where('del_time',0)
  3038. ->whereIn('id', $order_product_id)
  3039. ->select('id', 'production_no as order_no')
  3040. ->get()->toArray();
  3041. }elseif ($for_type == 3){
  3042. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3043. ->where('del_time',0)
  3044. ->pluck('data_id')
  3045. ->toArray();
  3046. $result = DispatchSub::where('del_time',0)
  3047. ->whereIn('id', $dispatch_id)
  3048. ->select('id','dispatch_no as order_no')
  3049. ->get()->toArray();
  3050. }elseif ($for_type == 5){
  3051. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3052. ->where('del_time',0)
  3053. ->pluck('data_id')
  3054. ->toArray();
  3055. $result = DispatchSub::where('del_time',0)
  3056. ->whereIn('id', $dispatch_id)
  3057. ->where('finished_num','>',0)
  3058. ->select('id', 'dispatch_no as order_no')
  3059. ->get()->toArray();
  3060. }elseif ($for_type == 6){
  3061. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3062. ->where('del_time',0)
  3063. ->pluck('data_id')
  3064. ->toArray();
  3065. $sale_orders_product_id = DispatchSub::where('del_time',0)
  3066. ->whereIn('id', $dispatch_id)
  3067. ->pluck('sale_orders_product_id')
  3068. ->toArray();
  3069. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  3070. $return = [];
  3071. foreach ($sale as $value){
  3072. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3073. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3074. $tmp = Box::where('del_time',0)
  3075. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3076. ->select("id","order_no")
  3077. ->get()->toArray();
  3078. $return = array_merge($return, $tmp);
  3079. }
  3080. $t = $return[0] ?? [];
  3081. if(! empty($t)) $result[] = $t;
  3082. }elseif ($for_type == 7){
  3083. $process = Process::where('del_time',0)
  3084. ->where('is_need_remain',1)
  3085. ->pluck('id')
  3086. ->toArray();
  3087. $result = ScrappCount::where('del_time',0)
  3088. ->where('report_id', $id)
  3089. ->whereIn('process_id',$process)
  3090. ->select('id', 'order_number as order_no')
  3091. ->get()->toArray();
  3092. }elseif ($for_type == 8){
  3093. $result = ScrappCount::where('del_time',0)
  3094. ->where('report_id', $id)
  3095. ->where('scrapp_num','>', 0)
  3096. ->select('id', 'order_number as order_no')
  3097. ->get()->toArray();
  3098. }elseif ($for_type == 9){
  3099. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3100. ->where('del_time',0)
  3101. ->pluck('data_id')
  3102. ->toArray();
  3103. $dispatch_id = $dispatch_id[0] ?? 0;
  3104. $dispatch = DispatchSub::where('id',$dispatch_id)
  3105. ->select('order_product_id','crt_time','product_no','technology_name')
  3106. ->first();
  3107. if(! empty($dispatch)){
  3108. $dispatch = $dispatch->toArray();
  3109. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3110. ->where('crt_time',$dispatch['crt_time'])
  3111. ->where('product_no',$dispatch['product_no'])
  3112. ->where('technology_name',$dispatch['technology_name'])
  3113. ->pluck('id')->toArray();
  3114. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3115. ->whereIn('data_id', $dispatch_id)
  3116. ->where('type', ApplyOrder::type_one)
  3117. ->pluck('apply_order_id')
  3118. ->toArray();
  3119. $result = ApplyOrder::where('del_time', 0)
  3120. ->whereIn('id', $apply_order_id)
  3121. ->select('id','order_number as order_no')
  3122. ->get()->toArray();
  3123. }
  3124. }elseif ($for_type == 10){
  3125. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3126. ->where('del_time',0)
  3127. ->pluck('data_id')
  3128. ->toArray();
  3129. $dispatch_id = $dispatch_id[0] ?? 0;
  3130. $dispatch = DispatchSub::where('id',$dispatch_id)
  3131. ->select('order_product_id','crt_time','product_no','technology_name')
  3132. ->first();
  3133. if(! empty($dispatch)){
  3134. $dispatch = $dispatch->toArray();
  3135. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3136. ->where('crt_time',$dispatch['crt_time'])
  3137. ->where('product_no',$dispatch['product_no'])
  3138. ->where('technology_name',$dispatch['technology_name'])
  3139. ->pluck('id')->toArray();
  3140. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3141. ->whereIn('data_id', $dispatch_id)
  3142. ->where('type', ApplyOrder::type_two)
  3143. ->pluck('apply_order_id')
  3144. ->toArray();
  3145. $return = ApplyOrder::where('del_time', 0)
  3146. ->whereIn('id', $apply_order_id)
  3147. ->select('id','order_number as order_no')
  3148. ->get()->toArray();
  3149. $t = $return[0] ?? [];
  3150. if(! empty($t)) $result[] = $t;
  3151. }
  3152. }elseif ($for_type == 11){
  3153. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3154. ->where('del_time',0)
  3155. ->pluck('data_id')
  3156. ->toArray();
  3157. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3158. $box_id = [];
  3159. foreach ($sale as $value){
  3160. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3161. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3162. $tmp = Box::where('del_time',0)
  3163. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3164. ->pluck("id")
  3165. ->toArray();
  3166. $box_id = array_merge($box_id, $tmp);
  3167. }
  3168. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3169. ->whereIn('data_id', $box_id)
  3170. ->where('type', ApplyOrder::type_four)
  3171. ->pluck('apply_order_id')
  3172. ->toArray();
  3173. $return = ApplyOrder::where('del_time', 0)
  3174. ->whereIn('id', $apply_order_id)
  3175. ->select('id','order_number as order_no')
  3176. ->get()->toArray();
  3177. $t = $return[0] ?? [];
  3178. if(! empty($t)) $result[] = $t;
  3179. }elseif ($for_type == 12){
  3180. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3181. ->where('del_time',0)
  3182. ->pluck('data_id')
  3183. ->toArray();
  3184. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no', 'sale_orders_product_id as id')->get()->toArray();
  3185. $box_id = [];
  3186. foreach ($sale as $value){
  3187. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3188. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3189. $tmp = Box::where('del_time',0)
  3190. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3191. ->pluck("id")
  3192. ->toArray();
  3193. $box_id = array_merge($box_id, $tmp);
  3194. }
  3195. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3196. ->whereIn('data_id', $box_id)
  3197. ->where('type', ApplyOrder::type_three)
  3198. ->pluck('apply_order_id')
  3199. ->toArray();
  3200. $return = ApplyOrder::where('del_time', 0)
  3201. ->whereIn('id', $apply_order_id)
  3202. ->select('id','order_number as order_no')
  3203. ->get()->toArray();
  3204. $t = $return[0] ?? [];
  3205. if(! empty($t)) $result[] = $t;
  3206. }elseif ($for_type == 13){
  3207. $s_id = ScrappCount::where('del_time', 0)
  3208. ->where('report_id', $id)
  3209. ->pluck('id')
  3210. ->toArray();
  3211. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3212. ->whereIn('data_id', $s_id)
  3213. ->where('type', ApplyOrder::type_five)
  3214. ->pluck('apply_order_id')
  3215. ->toArray();
  3216. $result = ApplyOrder::where('del_time', 0)
  3217. ->whereIn('id', $apply_order_id)
  3218. ->select('id','order_number as order_no')
  3219. ->get()->toArray();
  3220. }elseif ($for_type == 14){
  3221. $dispatch_id = ReportWorkingDetail::where('report_working_id', $id)
  3222. ->where('del_time',0)
  3223. ->pluck('data_id')
  3224. ->toArray();
  3225. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3226. $box_id = [];
  3227. foreach ($sale as $value){
  3228. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3229. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3230. $tmp = Box::where('del_time',0)
  3231. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3232. ->pluck("id")
  3233. ->toArray();
  3234. $box_id = array_merge($box_id, $tmp);
  3235. }
  3236. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3237. ->whereIn('data_id', $box_id)
  3238. ->where('type', ApplyOrder::type_six)
  3239. ->pluck('apply_order_id')
  3240. ->toArray();
  3241. $return = ApplyOrder::where('del_time', 0)
  3242. ->whereIn('id', $apply_order_id)
  3243. ->select('id','order_number as order_no')
  3244. ->get()->toArray();
  3245. $t = $return[0] ?? [];
  3246. if(! empty($t)) $result[] = $t;
  3247. }
  3248. return $result;
  3249. }
  3250. private function typeFive($for_type, $id){
  3251. $result = [];
  3252. if($for_type == 1){
  3253. $sale_orders_product_id = DispatchSub::where('del_time',0)
  3254. ->where('id', $id)
  3255. ->pluck('sale_orders_product_id')
  3256. ->toArray();
  3257. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3258. ->where('del_time',0)
  3259. ->select('id','out_order_no as order_no')
  3260. ->get()->toArray();
  3261. }elseif ($for_type == 2){
  3262. $order_product_id = DispatchSub::where('del_time',0)
  3263. ->where('id', $id)
  3264. ->pluck('order_product_id')
  3265. ->toArray();
  3266. $result = OrdersProduct::where('del_time',0)
  3267. ->whereIn('id', $order_product_id)
  3268. ->select('id', 'production_no as order_no')
  3269. ->get()->toArray();
  3270. }elseif ($for_type == 3){
  3271. $result = DispatchSub::where('del_time',0)
  3272. ->where('id', $id)
  3273. ->select('id', 'dispatch_no as order_no')
  3274. ->get()->toArray();
  3275. }elseif ($for_type == 4){
  3276. $dispatch = DispatchSub::where('del_time',0)
  3277. ->where('id', $id)
  3278. ->select('id')
  3279. ->get()->toArray();
  3280. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  3281. ->where('del_time',0)
  3282. ->select('report_working_id')
  3283. ->get()->toArray();
  3284. $result = ReportWorking::where('del_time',0)
  3285. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  3286. ->select('id', 'order_number as order_no')
  3287. ->get()->toArray();
  3288. }elseif ($for_type == 6){
  3289. $box_id = BoxWithDispatch::where('del_time',0)
  3290. ->where('dispatch_id', $id)
  3291. ->pluck('box_id')
  3292. ->toArray();
  3293. $box_id = $box_id[0] ?? 0;
  3294. $result = Box::where('del_time',0)
  3295. ->where('id', $box_id)
  3296. ->select("id","order_no")
  3297. ->get()->toArray();
  3298. }elseif ($for_type == 7){
  3299. $process = Process::where('del_time',0)
  3300. ->where('is_need_remain',1)
  3301. ->pluck('id')
  3302. ->toArray();
  3303. $result = ScrappCount::where('del_time',0)
  3304. ->where('dispatch_sub_id', $id)
  3305. ->whereIn('process_id', $process)
  3306. ->select('id', 'order_number as order_no')
  3307. ->get()->toArray();
  3308. }elseif ($for_type == 8){
  3309. $result = ScrappCount::where('del_time',0)
  3310. ->where('dispatch_sub_id', $id)
  3311. ->where('scrapp_num','>', 0)
  3312. ->select('id', 'order_number as order_no')
  3313. ->get()->toArray();
  3314. }elseif ($for_type == 9){
  3315. $dispatch = DispatchSub::where('id',$id)
  3316. ->select('order_product_id','crt_time','product_no','technology_name')
  3317. ->first();
  3318. if(! empty($dispatch)){
  3319. $dispatch = $dispatch->toArray();
  3320. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3321. ->where('crt_time',$dispatch['crt_time'])
  3322. ->where('product_no',$dispatch['product_no'])
  3323. ->where('technology_name',$dispatch['technology_name'])
  3324. ->pluck('id')->toArray();
  3325. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3326. ->whereIn('data_id', $dispatch_id)
  3327. ->where('type', ApplyOrder::type_one)
  3328. ->pluck('apply_order_id')
  3329. ->toArray();
  3330. $result = ApplyOrder::where('del_time', 0)
  3331. ->whereIn('id', $apply_order_id)
  3332. ->select('id','order_number as order_no')
  3333. ->get()->toArray();
  3334. }
  3335. }elseif ($for_type == 10){
  3336. $dispatch = DispatchSub::where('id',$id)
  3337. ->select('order_product_id','crt_time','product_no','technology_name')
  3338. ->first();
  3339. if(! empty($dispatch)){
  3340. $dispatch = $dispatch->toArray();
  3341. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3342. ->where('crt_time',$dispatch['crt_time'])
  3343. ->where('product_no',$dispatch['product_no'])
  3344. ->where('technology_name',$dispatch['technology_name'])
  3345. ->pluck('id')->toArray();
  3346. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3347. ->whereIn('data_id', $dispatch_id)
  3348. ->where('type', ApplyOrder::type_two)
  3349. ->pluck('apply_order_id')
  3350. ->toArray();
  3351. $return = ApplyOrder::where('del_time', 0)
  3352. ->whereIn('id', $apply_order_id)
  3353. ->select('id','order_number as order_no')
  3354. ->get()->toArray();
  3355. $t = $return[0] ?? [];
  3356. if(! empty($t)) $result[] = $t;
  3357. }
  3358. }elseif ($for_type == 11){
  3359. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3360. $box_id = [];
  3361. foreach ($sale as $value){
  3362. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3363. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3364. $tmp = Box::where('del_time',0)
  3365. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3366. ->pluck("id")
  3367. ->toArray();
  3368. $box_id = array_merge($box_id, $tmp);
  3369. }
  3370. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3371. ->whereIn('data_id', $box_id)
  3372. ->where('type', ApplyOrder::type_four)
  3373. ->pluck('apply_order_id')
  3374. ->toArray();
  3375. $return = ApplyOrder::where('del_time', 0)
  3376. ->whereIn('id', $apply_order_id)
  3377. ->select('id','order_number as order_no')
  3378. ->get()->toArray();
  3379. $t = $return[0] ?? [];
  3380. if(! empty($t)) $result[] = $t;
  3381. }elseif ($for_type == 12){
  3382. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3383. $box_id = [];
  3384. foreach ($sale as $value){
  3385. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3386. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3387. $tmp = Box::where('del_time',0)
  3388. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3389. ->pluck("id")
  3390. ->toArray();
  3391. $box_id = array_merge($box_id, $tmp);
  3392. }
  3393. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3394. ->whereIn('data_id', $box_id)
  3395. ->where('type', ApplyOrder::type_three)
  3396. ->pluck('apply_order_id')
  3397. ->toArray();
  3398. $return = ApplyOrder::where('del_time', 0)
  3399. ->whereIn('id', $apply_order_id)
  3400. ->select('id','order_number as order_no')
  3401. ->get()->toArray();
  3402. $t = $return[0] ?? [];
  3403. if(! empty($t)) $result[] = $t;
  3404. }elseif ($for_type == 13){
  3405. $s_id = ScrappCount::where('del_time', 0)
  3406. ->where('dispatch_sub_id', $id)
  3407. ->pluck('id')
  3408. ->toArray();
  3409. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3410. ->whereIn('data_id', $s_id)
  3411. ->where('type', ApplyOrder::type_five)
  3412. ->pluck('apply_order_id')
  3413. ->toArray();
  3414. $result = ApplyOrder::where('del_time', 0)
  3415. ->whereIn('id', $apply_order_id)
  3416. ->select('id','order_number as order_no')
  3417. ->get()->toArray();
  3418. }elseif ($for_type == 14){
  3419. $sale = DispatchSub::where('id', $id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3420. $box_id = [];
  3421. foreach ($sale as $value){
  3422. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3423. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3424. $tmp = Box::where('del_time',0)
  3425. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3426. ->pluck("id")
  3427. ->toArray();
  3428. $box_id = array_merge($box_id, $tmp);
  3429. }
  3430. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3431. ->whereIn('data_id', $box_id)
  3432. ->where('type', ApplyOrder::type_six)
  3433. ->pluck('apply_order_id')
  3434. ->toArray();
  3435. $return = ApplyOrder::where('del_time', 0)
  3436. ->whereIn('id', $apply_order_id)
  3437. ->select('id','order_number as order_no')
  3438. ->get()->toArray();
  3439. $t = $return[0] ?? [];
  3440. if(! empty($t)) $result[] = $t;
  3441. }
  3442. return $result;
  3443. }
  3444. private function typeSix($for_type, $id){
  3445. $result = [];
  3446. $box = Box::where('id', $id)->first();
  3447. if(empty($box)) return $result;
  3448. $top_order_no = $box->top_order_no;
  3449. $order_no = $box->order_no;
  3450. if($for_type == 1){
  3451. $box_detail = new BoxDetail(['channel'=> $top_order_no]);
  3452. $sale_orders_product_id = $box_detail->where('del_time',0)
  3453. ->where('order_no',$order_no)
  3454. ->pluck('top_id')
  3455. ->toArray();
  3456. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3457. ->where('del_time',0)
  3458. ->select('id','out_order_no as order_no')
  3459. ->get()->toArray();
  3460. }elseif ($for_type == 2){
  3461. $box_detail = new BoxDetail(['channel'=> $top_order_no]);
  3462. $sale_orders_product_id = $box_detail->where('del_time',0)
  3463. ->where('order_no',$order_no)
  3464. ->pluck('top_id')
  3465. ->toArray();
  3466. $result = OrdersProduct::where('del_time',0)
  3467. ->whereIn('sale_orders_product_id', $sale_orders_product_id)
  3468. ->select('id', 'production_no as order_no')
  3469. ->get()->toArray();
  3470. }elseif ($for_type == 3){
  3471. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3472. ->where('box_id', $id)
  3473. ->pluck('dispatch_id')
  3474. ->toArray();
  3475. $result = DispatchSub::where('del_time',0)
  3476. ->whereIn('id', $dispatch_id)
  3477. ->select('id', 'dispatch_no as order_no')
  3478. ->get()->toArray();
  3479. }elseif ($for_type == 4){
  3480. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3481. ->where('box_id', $id)
  3482. ->pluck('dispatch_id')
  3483. ->toArray();
  3484. $detail = ReportWorkingDetail::whereIn('data_id', $dispatch_id)
  3485. ->where('del_time',0)
  3486. ->select('report_working_id')
  3487. ->get()->toArray();
  3488. $return = ReportWorking::where('del_time',0)
  3489. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  3490. ->select('id', 'order_number as order_no')
  3491. ->get()->toArray();
  3492. $t = $return[0] ?? [];
  3493. if(! empty($t)) $result[] = $t;
  3494. }elseif ($for_type == 5){
  3495. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3496. ->where('box_id', $id)
  3497. ->pluck('dispatch_id')
  3498. ->toArray();
  3499. $result = DispatchSub::where('del_time',0)
  3500. ->whereIn('id', $dispatch_id)
  3501. ->select('id', 'dispatch_no as order_no')
  3502. ->get()->toArray();
  3503. }elseif ($for_type == 7){
  3504. $process = Process::where('del_time',0)
  3505. ->where('is_need_remain',1)
  3506. ->pluck('id')
  3507. ->toArray();
  3508. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3509. ->where('box_id', $id)
  3510. ->pluck('dispatch_id')
  3511. ->toArray();
  3512. $report_working_id = ReportWorkingDetail::whereIn('data_id', $dispatch_id)
  3513. ->where('del_time',0)
  3514. ->pluck('report_working_id')
  3515. ->toArray();
  3516. $return = ScrappCount::where('del_time',0)
  3517. ->whereIn('report_id', $report_working_id)
  3518. ->whereIn('process_id', $process)
  3519. ->select('id', 'order_number as order_no')
  3520. ->get()->toArray();
  3521. $t = $return[0] ?? [];
  3522. if(! empty($t)) $result[] = $t;
  3523. }elseif ($for_type == 8){
  3524. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3525. ->where('box_id', $id)
  3526. ->pluck('dispatch_id')
  3527. ->toArray();
  3528. $report_working_id = ReportWorkingDetail::whereIn('data_id', $dispatch_id)
  3529. ->where('del_time',0)
  3530. ->pluck('report_working_id')
  3531. ->toArray();
  3532. $return = ScrappCount::where('del_time',0)
  3533. ->whereIn('report_id', $report_working_id)
  3534. ->where('scrapp_num','>', 0)
  3535. ->select('id', 'order_number as order_no')
  3536. ->get()->toArray();
  3537. $t = $return[0] ?? [];
  3538. if(! empty($t)) $result[] = $t;
  3539. }elseif ($for_type == 9){
  3540. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3541. ->where('box_id', $id)
  3542. ->select('dispatch_id')
  3543. ->first();
  3544. if(! empty($dispatch_id)){
  3545. $dispatch_id = $dispatch_id->toArray();
  3546. $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id'])
  3547. ->select('order_product_id','crt_time','product_no','technology_name')
  3548. ->first();
  3549. if(! empty($dispatch)){
  3550. $dispatch = $dispatch->toArray();
  3551. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3552. ->where('crt_time',$dispatch['crt_time'])
  3553. ->where('product_no',$dispatch['product_no'])
  3554. ->where('technology_name',$dispatch['technology_name'])
  3555. ->pluck('id')->toArray();
  3556. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3557. ->whereIn('data_id', $dispatch_id)
  3558. ->where('type', ApplyOrder::type_one)
  3559. ->pluck('apply_order_id')
  3560. ->toArray();
  3561. $result = ApplyOrder::where('del_time', 0)
  3562. ->whereIn('id', $apply_order_id)
  3563. ->select('id','order_number as order_no')
  3564. ->get()->toArray();
  3565. }
  3566. }
  3567. }elseif ($for_type == 10){
  3568. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3569. ->where('box_id', $id)
  3570. ->select('dispatch_id')
  3571. ->first();
  3572. if(! empty($dispatch_id)){
  3573. $dispatch_id = $dispatch_id->toArray();
  3574. $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id'])
  3575. ->select('order_product_id','crt_time','product_no','technology_name')
  3576. ->first();
  3577. if(! empty($dispatch)){
  3578. $dispatch = $dispatch->toArray();
  3579. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3580. ->where('crt_time',$dispatch['crt_time'])
  3581. ->where('product_no',$dispatch['product_no'])
  3582. ->where('technology_name',$dispatch['technology_name'])
  3583. ->pluck('id')->toArray();
  3584. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3585. ->whereIn('data_id', $dispatch_id)
  3586. ->where('type', ApplyOrder::type_two)
  3587. ->pluck('apply_order_id')
  3588. ->toArray();
  3589. $return = ApplyOrder::where('del_time', 0)
  3590. ->whereIn('id', $apply_order_id)
  3591. ->select('id','order_number as order_no')
  3592. ->get()->toArray();
  3593. $t = $return[0] ?? [];
  3594. if(! empty($t)) $result[] = $t;
  3595. }
  3596. }
  3597. }elseif ($for_type == 11){
  3598. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3599. ->where('box_id', $id)
  3600. ->pluck('dispatch_id')
  3601. ->toArray();
  3602. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3603. $box_id = [];
  3604. foreach ($sale as $value){
  3605. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3606. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3607. $tmp = Box::where('del_time',0)
  3608. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3609. ->pluck("id")
  3610. ->toArray();
  3611. $box_id = array_merge($box_id, $tmp);
  3612. }
  3613. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3614. ->whereIn('data_id', $box_id)
  3615. ->where('type', ApplyOrder::type_four)
  3616. ->pluck('apply_order_id')
  3617. ->toArray();
  3618. $return = ApplyOrder::where('del_time', 0)
  3619. ->whereIn('id', $apply_order_id)
  3620. ->select('id','order_number as order_no')
  3621. ->get()->toArray();
  3622. $t = $return[0] ?? [];
  3623. if(! empty($t)) $result[] = $t;
  3624. }elseif ($for_type == 12){
  3625. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3626. ->where('box_id', $id)
  3627. ->pluck('dispatch_id')
  3628. ->toArray();
  3629. $sale = DispatchSub::where('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3630. $box_id = [];
  3631. foreach ($sale as $value){
  3632. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3633. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3634. $tmp = Box::where('del_time',0)
  3635. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3636. ->pluck("id")
  3637. ->toArray();
  3638. $box_id = array_merge($box_id, $tmp);
  3639. }
  3640. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3641. ->whereIn('data_id', $box_id)
  3642. ->where('type', ApplyOrder::type_three)
  3643. ->pluck('apply_order_id')
  3644. ->toArray();
  3645. $return = ApplyOrder::where('del_time', 0)
  3646. ->whereIn('id', $apply_order_id)
  3647. ->select('id','order_number as order_no')
  3648. ->get()->toArray();
  3649. $t = $return[0] ?? [];
  3650. if(! empty($t)) $result[] = $t;
  3651. }elseif ($for_type == 13){
  3652. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3653. ->where('box_id', $id)
  3654. ->select('dispatch_id')
  3655. ->first();
  3656. if(! empty($dispatch_id)){
  3657. $dispatch_id = $dispatch_id->toArray();
  3658. $dispatch = DispatchSub::where('id',$dispatch_id['dispatch_id'])
  3659. ->select('order_product_id','crt_time','product_no','technology_name')
  3660. ->first();
  3661. if(! empty($dispatch)){
  3662. $dispatch = $dispatch->toArray();
  3663. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3664. ->where('crt_time',$dispatch['crt_time'])
  3665. ->where('product_no',$dispatch['product_no'])
  3666. ->where('technology_name',$dispatch['technology_name'])
  3667. ->pluck('id')->toArray();
  3668. $s_id = ScrappCount::where('del_time', 0)
  3669. ->whereIn('dispatch_sub_id', $dispatch_id)
  3670. ->pluck('id')
  3671. ->toArray();
  3672. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3673. ->whereIn('data_id', $s_id)
  3674. ->where('type', ApplyOrder::type_five)
  3675. ->pluck('apply_order_id')
  3676. ->toArray();
  3677. $result = ApplyOrder::where('del_time', 0)
  3678. ->whereIn('id', $apply_order_id)
  3679. ->select('id','order_number as order_no')
  3680. ->get()->toArray();
  3681. }
  3682. }
  3683. }elseif ($for_type == 14){
  3684. $dispatch_id = BoxWithDispatch::where('del_time',0)
  3685. ->where('box_id', $id)
  3686. ->pluck('dispatch_id')
  3687. ->toArray();
  3688. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3689. $box_id = [];
  3690. foreach ($sale as $value){
  3691. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3692. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3693. $tmp = Box::where('del_time',0)
  3694. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3695. ->pluck("id")
  3696. ->toArray();
  3697. $box_id = array_merge($box_id, $tmp);
  3698. }
  3699. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3700. ->whereIn('data_id', $box_id)
  3701. ->where('type', ApplyOrder::type_six)
  3702. ->pluck('apply_order_id')
  3703. ->toArray();
  3704. $return = ApplyOrder::where('del_time', 0)
  3705. ->whereIn('id', $apply_order_id)
  3706. ->select('id','order_number as order_no')
  3707. ->get()->toArray();
  3708. $t = $return[0] ?? [];
  3709. if(! empty($t)) $result[] = $t;
  3710. }
  3711. return $result;
  3712. }
  3713. private function typeSeven($for_type, $id){
  3714. $result = [];
  3715. if($for_type == 1){
  3716. $sale_orders_product_id = ScrappCount::where('del_time',0)
  3717. ->where('id', $id)
  3718. ->pluck('sale_orders_product_id')
  3719. ->toArray();
  3720. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3721. ->where('del_time',0)
  3722. ->select('id','out_order_no as order_no')
  3723. ->get()->toArray();
  3724. }elseif ($for_type == 2){
  3725. $order_product_id = ScrappCount::where('del_time',0)
  3726. ->where('id', $id)
  3727. ->pluck('order_product_id')
  3728. ->toArray();
  3729. $result = OrdersProduct::where('del_time',0)
  3730. ->whereIn('id', $order_product_id)
  3731. ->select('id', 'production_no as order_no')
  3732. ->get()->toArray();
  3733. }elseif ($for_type == 3){
  3734. $dispatch_id = ScrappCount::where('del_time',0)
  3735. ->where('id', $id)
  3736. ->pluck('dispatch_sub_id')
  3737. ->toArray();
  3738. $result = DispatchSub::where('del_time',0)
  3739. ->where('id', $dispatch_id)
  3740. ->select('id', 'dispatch_no as order_no')
  3741. ->get()->toArray();
  3742. }elseif ($for_type == 4){
  3743. $report_id = ScrappCount::where('del_time',0)
  3744. ->where('id', $id)
  3745. ->pluck('report_id')
  3746. ->toArray();
  3747. $result = ReportWorking::where('del_time',0)
  3748. ->whereIn('id', $report_id)
  3749. ->select('id', 'order_number as order_no')
  3750. ->get()->toArray();
  3751. }elseif ($for_type == 5){
  3752. $dispatch_id = ScrappCount::where('del_time',0)
  3753. ->where('id', $id)
  3754. ->pluck('dispatch_sub_id')
  3755. ->toArray();
  3756. $result = DispatchSub::where('del_time',0)
  3757. ->where('id', $dispatch_id)
  3758. ->where('finished_num','>',0)
  3759. ->select('id', 'dispatch_no as order_no')
  3760. ->get()->toArray();
  3761. }elseif ($for_type == 6){
  3762. $sale_orders_product_id = ScrappCount::where('del_time',0)
  3763. ->where('id', $id)
  3764. ->pluck('sale_orders_product_id')
  3765. ->toArray();
  3766. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  3767. $return = [];
  3768. foreach ($sale as $value){
  3769. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3770. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3771. $tmp = Box::where('del_time',0)
  3772. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3773. ->select("id","order_no")
  3774. ->get()->toArray();
  3775. $return = array_merge($return, $tmp);
  3776. }
  3777. $t = $return[0] ?? [];
  3778. if(! empty($t)) $result[] = $t;
  3779. }elseif ($for_type == 8){
  3780. $result = ScrappCount::where('del_time',0)
  3781. ->where('id', $id)
  3782. ->where('scrapp_num','>', 0)
  3783. ->select('id', 'order_number as order_no')
  3784. ->get()->toArray();
  3785. }elseif ($for_type == 9){
  3786. $dispatch_id = ScrappCount::where('del_time',0)
  3787. ->where('id', $id)
  3788. ->pluck('dispatch_sub_id')
  3789. ->toArray();
  3790. $dispatch_id = $dispatch_id[0] ?? 0;
  3791. $dispatch = DispatchSub::where('id',$dispatch_id)
  3792. ->select('order_product_id','crt_time','product_no','technology_name')
  3793. ->first();
  3794. if(! empty($dispatch)){
  3795. $dispatch = $dispatch->toArray();
  3796. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3797. ->where('crt_time',$dispatch['crt_time'])
  3798. ->where('product_no',$dispatch['product_no'])
  3799. ->where('technology_name',$dispatch['technology_name'])
  3800. ->pluck('id')->toArray();
  3801. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3802. ->whereIn('data_id', $dispatch_id)
  3803. ->where('type', ApplyOrder::type_one)
  3804. ->pluck('apply_order_id')
  3805. ->toArray();
  3806. $result = ApplyOrder::where('del_time', 0)
  3807. ->whereIn('id', $apply_order_id)
  3808. ->select('id','order_number as order_no')
  3809. ->get()->toArray();
  3810. }
  3811. }elseif ($for_type == 10){
  3812. $dispatch_id = ScrappCount::where('del_time',0)
  3813. ->where('id', $id)
  3814. ->pluck('dispatch_sub_id')
  3815. ->toArray();
  3816. $dispatch_id = $dispatch_id[0] ?? 0;
  3817. $dispatch = DispatchSub::where('id',$dispatch_id)
  3818. ->select('order_product_id','crt_time','product_no','technology_name')
  3819. ->first();
  3820. if(! empty($dispatch)){
  3821. $dispatch = $dispatch->toArray();
  3822. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  3823. ->where('crt_time',$dispatch['crt_time'])
  3824. ->where('product_no',$dispatch['product_no'])
  3825. ->where('technology_name',$dispatch['technology_name'])
  3826. ->pluck('id')->toArray();
  3827. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3828. ->whereIn('data_id', $dispatch_id)
  3829. ->where('type', ApplyOrder::type_two)
  3830. ->pluck('apply_order_id')
  3831. ->toArray();
  3832. $return = ApplyOrder::where('del_time', 0)
  3833. ->whereIn('id', $apply_order_id)
  3834. ->select('id','order_number as order_no')
  3835. ->get()->toArray();
  3836. $t = $return[0] ?? [];
  3837. if(! empty($t)) $result[] = $t;
  3838. }
  3839. }elseif ($for_type == 11){
  3840. $dispatch_id = ScrappCount::where('del_time',0)
  3841. ->where('id', $id)
  3842. ->pluck('dispatch_sub_id')
  3843. ->toArray();
  3844. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3845. $box_id = [];
  3846. foreach ($sale as $value){
  3847. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3848. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3849. $tmp = Box::where('del_time',0)
  3850. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3851. ->pluck("id")
  3852. ->toArray();
  3853. $box_id = array_merge($box_id, $tmp);
  3854. }
  3855. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3856. ->whereIn('data_id', $box_id)
  3857. ->where('type', ApplyOrder::type_four)
  3858. ->pluck('apply_order_id')
  3859. ->toArray();
  3860. $return = ApplyOrder::where('del_time', 0)
  3861. ->whereIn('id', $apply_order_id)
  3862. ->select('id','order_number as order_no')
  3863. ->get()->toArray();
  3864. $t = $return[0] ?? [];
  3865. if(! empty($t)) $result[] = $t;
  3866. }elseif ($for_type == 12){
  3867. $dispatch_id = ScrappCount::where('del_time',0)
  3868. ->where('id', $id)
  3869. ->pluck('dispatch_sub_id')
  3870. ->toArray();
  3871. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3872. $box_id = [];
  3873. foreach ($sale as $value){
  3874. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3875. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3876. $tmp = Box::where('del_time',0)
  3877. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3878. ->pluck("id")
  3879. ->toArray();
  3880. $box_id = array_merge($box_id, $tmp);
  3881. }
  3882. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3883. ->whereIn('data_id', $box_id)
  3884. ->where('type', ApplyOrder::type_three)
  3885. ->pluck('apply_order_id')
  3886. ->toArray();
  3887. $return = ApplyOrder::where('del_time', 0)
  3888. ->whereIn('id', $apply_order_id)
  3889. ->select('id','order_number as order_no')
  3890. ->get()->toArray();
  3891. $t = $return[0] ?? [];
  3892. if(! empty($t)) $result[] = $t;
  3893. }elseif ($for_type == 13){
  3894. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3895. ->where('data_id', $id)
  3896. ->where('type', ApplyOrder::type_five)
  3897. ->pluck('apply_order_id')
  3898. ->toArray();
  3899. $result = ApplyOrder::where('del_time', 0)
  3900. ->whereIn('id', $apply_order_id)
  3901. ->select('id','order_number as order_no')
  3902. ->get()->toArray();
  3903. }elseif ($for_type == 14){
  3904. $dispatch_id = ScrappCount::where('del_time',0)
  3905. ->where('id', $id)
  3906. ->pluck('dispatch_sub_id')
  3907. ->toArray();
  3908. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  3909. $box_id = [];
  3910. foreach ($sale as $value){
  3911. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3912. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3913. $tmp = Box::where('del_time',0)
  3914. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3915. ->pluck("id")
  3916. ->toArray();
  3917. $box_id = array_merge($box_id, $tmp);
  3918. }
  3919. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  3920. ->whereIn('data_id', $box_id)
  3921. ->where('type', ApplyOrder::type_six)
  3922. ->pluck('apply_order_id')
  3923. ->toArray();
  3924. $return = ApplyOrder::where('del_time', 0)
  3925. ->whereIn('id', $apply_order_id)
  3926. ->select('id','order_number as order_no')
  3927. ->get()->toArray();
  3928. $t = $return[0] ?? [];
  3929. if(! empty($t)) $result[] = $t;
  3930. }
  3931. return $result;
  3932. }
  3933. private function typeEight($for_type, $id){
  3934. $result = [];
  3935. if($for_type == 1){
  3936. $sale_orders_product_id = ScrappCount::where('del_time',0)
  3937. ->where('id', $id)
  3938. ->pluck('sale_orders_product_id')
  3939. ->toArray();
  3940. $result = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)
  3941. ->where('del_time',0)
  3942. ->select('id','out_order_no as order_no')
  3943. ->get()->toArray();
  3944. }elseif ($for_type == 2){
  3945. $order_product_id = ScrappCount::where('del_time',0)
  3946. ->where('id', $id)
  3947. ->pluck('order_product_id')
  3948. ->toArray();
  3949. $result = OrdersProduct::where('del_time',0)
  3950. ->whereIn('id', $order_product_id)
  3951. ->select('id', 'production_no as order_no')
  3952. ->get()->toArray();
  3953. }elseif ($for_type == 3){
  3954. $dispatch_id = ScrappCount::where('del_time',0)
  3955. ->where('id', $id)
  3956. ->pluck('dispatch_sub_id')
  3957. ->toArray();
  3958. $result = DispatchSub::where('del_time',0)
  3959. ->where('id', $dispatch_id)
  3960. ->select('id', 'dispatch_no as order_no')
  3961. ->get()->toArray();
  3962. }elseif ($for_type == 4){
  3963. $report_id = ScrappCount::where('del_time',0)
  3964. ->where('id', $id)
  3965. ->pluck('report_id')
  3966. ->toArray();
  3967. $result = ReportWorking::where('del_time',0)
  3968. ->whereIn('id', $report_id)
  3969. ->select('id', 'order_number as order_no')
  3970. ->get()->toArray();
  3971. }elseif ($for_type == 5){
  3972. $dispatch_id = ScrappCount::where('del_time',0)
  3973. ->where('id', $id)
  3974. ->pluck('dispatch_sub_id')
  3975. ->toArray();
  3976. $result = DispatchSub::where('del_time',0)
  3977. ->where('id', $dispatch_id)
  3978. ->where('finished_num','>',0)
  3979. ->select('id', 'dispatch_no as order_no')
  3980. ->get()->toArray();
  3981. }elseif ($for_type == 6){
  3982. $sale_orders_product_id = ScrappCount::where('del_time',0)
  3983. ->where('id', $id)
  3984. ->pluck('sale_orders_product_id')
  3985. ->toArray();
  3986. $sale = SaleOrdersProduct::whereIn('id', $sale_orders_product_id)->select('order_no','id')->get()->toArray();
  3987. $return = [];
  3988. foreach ($sale as $value){
  3989. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  3990. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  3991. $tmp = Box::where('del_time',0)
  3992. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  3993. ->select("id","order_no")
  3994. ->get()->toArray();
  3995. $return = array_merge($return, $tmp);
  3996. }
  3997. $t = $return[0] ?? [];
  3998. if(! empty($t)) $result[] = $t;
  3999. }elseif ($for_type == 7){
  4000. $process = Process::where('del_time',0)
  4001. ->where('is_need_remain',1)
  4002. ->pluck('id')
  4003. ->toArray();
  4004. $result = ScrappCount::where('del_time',0)
  4005. ->where('id', $id)
  4006. ->whereIn('process_id', $process)
  4007. ->select('id', 'order_number as order_no')
  4008. ->get()->toArray();
  4009. }elseif ($for_type == 9){
  4010. $dispatch_id = ScrappCount::where('del_time',0)
  4011. ->where('id', $id)
  4012. ->pluck('dispatch_sub_id')
  4013. ->toArray();
  4014. $dispatch_id = $dispatch_id[0] ?? 0;
  4015. $dispatch = DispatchSub::where('id',$dispatch_id)
  4016. ->select('order_product_id','crt_time','product_no','technology_name')
  4017. ->first();
  4018. if(! empty($dispatch)){
  4019. $dispatch = $dispatch->toArray();
  4020. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  4021. ->where('crt_time',$dispatch['crt_time'])
  4022. ->where('product_no',$dispatch['product_no'])
  4023. ->where('technology_name',$dispatch['technology_name'])
  4024. ->pluck('id')->toArray();
  4025. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4026. ->whereIn('data_id', $dispatch_id)
  4027. ->where('type', ApplyOrder::type_one)
  4028. ->pluck('apply_order_id')
  4029. ->toArray();
  4030. $result = ApplyOrder::where('del_time', 0)
  4031. ->whereIn('id', $apply_order_id)
  4032. ->select('id','order_number as order_no')
  4033. ->get()->toArray();
  4034. }
  4035. }elseif ($for_type == 10){
  4036. $dispatch_id = ScrappCount::where('del_time',0)
  4037. ->where('id', $id)
  4038. ->pluck('dispatch_sub_id')
  4039. ->toArray();
  4040. $dispatch_id = $dispatch_id[0] ?? 0;
  4041. $dispatch = DispatchSub::where('id',$dispatch_id)
  4042. ->select('order_product_id','crt_time','product_no','technology_name')
  4043. ->first();
  4044. if(! empty($dispatch)){
  4045. $dispatch = $dispatch->toArray();
  4046. $dispatch_id = DispatchSub::where('order_product_id', $dispatch['order_product_id'])
  4047. ->where('crt_time',$dispatch['crt_time'])
  4048. ->where('product_no',$dispatch['product_no'])
  4049. ->where('technology_name',$dispatch['technology_name'])
  4050. ->pluck('id')->toArray();
  4051. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4052. ->whereIn('data_id', $dispatch_id)
  4053. ->where('type', ApplyOrder::type_two)
  4054. ->pluck('apply_order_id')
  4055. ->toArray();
  4056. $return = ApplyOrder::where('del_time', 0)
  4057. ->whereIn('id', $apply_order_id)
  4058. ->select('id','order_number as order_no')
  4059. ->get()->toArray();
  4060. $t = $return[0] ?? [];
  4061. if(! empty($t)) $result[] = $t;
  4062. }
  4063. }elseif ($for_type == 11){
  4064. $dispatch_id = ScrappCount::where('del_time',0)
  4065. ->where('id', $id)
  4066. ->pluck('dispatch_sub_id')
  4067. ->toArray();
  4068. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  4069. $box_id = [];
  4070. foreach ($sale as $value){
  4071. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  4072. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  4073. $tmp = Box::where('del_time',0)
  4074. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  4075. ->pluck("id")
  4076. ->toArray();
  4077. $box_id = array_merge($box_id, $tmp);
  4078. }
  4079. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4080. ->whereIn('data_id', $box_id)
  4081. ->where('type', ApplyOrder::type_four)
  4082. ->pluck('apply_order_id')
  4083. ->toArray();
  4084. $return = ApplyOrder::where('del_time', 0)
  4085. ->whereIn('id', $apply_order_id)
  4086. ->select('id','order_number as order_no')
  4087. ->get()->toArray();
  4088. $t = $return[0] ?? [];
  4089. if(! empty($t)) $result[] = $t;
  4090. }elseif ($for_type == 12){
  4091. $dispatch_id = ScrappCount::where('del_time',0)
  4092. ->where('id', $id)
  4093. ->pluck('dispatch_sub_id')
  4094. ->toArray();
  4095. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  4096. $box_id = [];
  4097. foreach ($sale as $value){
  4098. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  4099. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  4100. $tmp = Box::where('del_time',0)
  4101. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  4102. ->pluck("id")
  4103. ->toArray();
  4104. $box_id = array_merge($box_id, $tmp);
  4105. }
  4106. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4107. ->whereIn('data_id', $box_id)
  4108. ->where('type', ApplyOrder::type_three)
  4109. ->pluck('apply_order_id')
  4110. ->toArray();
  4111. $return = ApplyOrder::where('del_time', 0)
  4112. ->whereIn('id', $apply_order_id)
  4113. ->select('id','order_number as order_no')
  4114. ->get()->toArray();
  4115. $t = $return[0] ?? [];
  4116. if(! empty($t)) $result[] = $t;
  4117. }elseif ($for_type == 13){
  4118. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4119. ->where('data_id', $id)
  4120. ->where('type', ApplyOrder::type_five)
  4121. ->pluck('apply_order_id')
  4122. ->toArray();
  4123. $result = ApplyOrder::where('del_time', 0)
  4124. ->whereIn('id', $apply_order_id)
  4125. ->select('id','order_number as order_no')
  4126. ->get()->toArray();
  4127. }elseif ($for_type == 14){
  4128. $dispatch_id = ScrappCount::where('del_time',0)
  4129. ->where('id', $id)
  4130. ->pluck('dispatch_sub_id')
  4131. ->toArray();
  4132. $sale = DispatchSub::whereIn('id', $dispatch_id)->select('order_no','sale_orders_product_id as id')->get()->toArray();
  4133. $box_id = [];
  4134. foreach ($sale as $value){
  4135. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  4136. $box_detail_order = $box_detail->where('top_id',$value['id'])->select('order_no')->get()->toArray();
  4137. $tmp = Box::where('del_time',0)
  4138. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  4139. ->pluck("id")
  4140. ->toArray();
  4141. $box_id = array_merge($box_id, $tmp);
  4142. }
  4143. $apply_order_id = ApplyOrderDetail::where('del_time', 0)
  4144. ->whereIn('data_id', $box_id)
  4145. ->where('type', ApplyOrder::type_six)
  4146. ->pluck('apply_order_id')
  4147. ->toArray();
  4148. $return = ApplyOrder::where('del_time', 0)
  4149. ->whereIn('id', $apply_order_id)
  4150. ->select('id','order_number as order_no')
  4151. ->get()->toArray();
  4152. $t = $return[0] ?? [];
  4153. if(! empty($t)) $result[] = $t;
  4154. }
  4155. return $result;
  4156. }
  4157. private function typeNine($for_type, $id){
  4158. $data_id = ApplyOrderDetail::where('del_time', 0)
  4159. ->where('apply_order_id', $id)
  4160. ->where('type', ApplyOrder::type_one)
  4161. ->pluck('data_id')
  4162. ->toArray();
  4163. $result = DispatchSub::where('del_time',0)
  4164. ->whereIn('id', $data_id)
  4165. ->select('id', 'dispatch_no as order_no')
  4166. ->get()->toArray();
  4167. return $result;
  4168. }
  4169. private function typeTen($for_type, $id){
  4170. $data_id = ApplyOrderDetail::where('del_time', 0)
  4171. ->where('apply_order_id', $id)
  4172. ->where('type', ApplyOrder::type_two)
  4173. ->pluck('data_id')
  4174. ->toArray();
  4175. $detail = ReportWorkingDetail::whereIn('data_id', $data_id)
  4176. ->where('del_time',0)
  4177. ->select('report_working_id')
  4178. ->get()->toArray();
  4179. $return = ReportWorking::where('del_time',0)
  4180. ->whereIn('id', array_unique(array_column($detail,'report_working_id')))
  4181. ->select('id', 'order_number as order_no')
  4182. ->get()->toArray();
  4183. $result[] = $return[0];
  4184. return $result;
  4185. }
  4186. private function typeEve($for_type, $id){
  4187. $detail = ApplyOrderDetail::where('del_time', 0)
  4188. ->where('apply_order_id', $id)
  4189. ->where('type', ApplyOrder::type_four)
  4190. ->select('data_id as box_id')
  4191. ->get()->toArray();
  4192. $result = Box::where('del_time', 0)
  4193. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  4194. ->select("id", 'order_no')
  4195. ->get()->toArray();
  4196. return $result;
  4197. }
  4198. private function typeTwl($for_type, $id){
  4199. $detail = ApplyOrderDetail::where('del_time', 0)
  4200. ->where('apply_order_id', $id)
  4201. ->where('type', ApplyOrder::type_three)
  4202. ->select('data_id as box_id')
  4203. ->get()->toArray();
  4204. $result = Box::where('del_time', 0)
  4205. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  4206. ->select("id", 'order_no')
  4207. ->get()->toArray();
  4208. return $result;
  4209. }
  4210. private function typeThi($for_type, $id){
  4211. $detail = ApplyOrderDetail::where('del_time', 0)
  4212. ->where('apply_order_id', $id)
  4213. ->where('type', ApplyOrder::type_five)
  4214. ->select('data_id as zj_id')
  4215. ->get()->toArray();
  4216. $result = ScrappCount::where('del_time', 0)
  4217. ->whereIn('id', array_unique(array_column($detail, 'zj_id')))
  4218. ->select('id','order_number as order_no')
  4219. ->get()->toArray();
  4220. return $result;
  4221. }
  4222. private function typeFourTeen($for_type, $id){
  4223. $detail = ApplyOrderDetail::where('del_time', 0)
  4224. ->where('apply_order_id', $id)
  4225. ->where('type', ApplyOrder::type_six)
  4226. ->select('data_id as box_id')
  4227. ->get()->toArray();
  4228. $result = Box::where('del_time', 0)
  4229. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  4230. ->select("id", 'order_no')
  4231. ->get()->toArray();
  4232. return $result;
  4233. }
  4234. public function reportList($data){
  4235. $model = ReportMessage::where('del_time',0)
  4236. ->select('id','order_id','opt_case','quantity','crt_time','user_id','is_use','report_working_id')
  4237. ->orderBy('id','desc');
  4238. $list = $this->limit($model,'',$data);
  4239. $list = $this->fillRData($list);
  4240. $count = ReportMessage::where('del_time',0)
  4241. ->where('is_use',0)
  4242. ->count();
  4243. return [true, ['list'=> $list, 'count' => $count]];
  4244. }
  4245. public function fillRData($data){
  4246. if(empty($data['data'])) return $data;
  4247. $dispatch = DispatchSub::whereIn('id',array_unique(array_column($data['data'],'order_id')))
  4248. ->select('id','dispatch_no','process_id')
  4249. ->get()->toArray();
  4250. $process = Process::where('del_time',0)
  4251. ->whereIn('id',array_unique(array_column($dispatch,'process_id')))
  4252. ->get()->toArray();
  4253. $process = array_column($process,null,'id');
  4254. $map = [];
  4255. foreach ($dispatch as $value){
  4256. $map[$value['id']] = $value;
  4257. }
  4258. $emp_map = Employee::whereIn('id',array_unique(array_column($data['data'],'user_id')))
  4259. ->pluck("emp_name",'id')
  4260. ->toArray();
  4261. $order_map = ReportWorking::whereIn('id',array_filter(array_column($data['data'],'report_working_id')))
  4262. ->pluck('order_number','id')
  4263. ->toArray();
  4264. foreach ($data['data'] as $key => $value){
  4265. $time = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  4266. $data['data'][$key]['crt_time'] = $time;
  4267. $t = $map[$value['order_id']] ?? [];
  4268. $e = $emp_map[$value['user_id']] ?? "";
  4269. $p = $process[$t['process_id']] ?? [];
  4270. $p_t = $p['title'] ?? "";
  4271. if($p['zj_type'] == 0){
  4272. $p_t .= ":无需质检";
  4273. }elseif($p['zj_type'] == 1){
  4274. $p_t .= ":全检";
  4275. }else{
  4276. $p_t .= ":抽检";
  4277. }
  4278. if(empty($value['report_working_id'])){
  4279. $str = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工,数量{$value['quantity']}吨,请及时处理";
  4280. }else{
  4281. $tmp = $order_map[$value['report_working_id']] ?? "";
  4282. if(empty($tmp)){
  4283. $str = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工,数量{$value['quantity']}吨,请及时处理";
  4284. }else{
  4285. $str = "派工单:" . $t['dispatch_no'] . "于" . $time . "由($e)完成工序($p_t)报工({$tmp}),数量{$value['quantity']}吨,请及时处理";
  4286. }
  4287. }
  4288. $data['data'][$key]['message'] = $str;
  4289. }
  4290. return $data;
  4291. }
  4292. public function reportRemainUpdate($data){
  4293. if(empty($data['id'])) return [false, 'ID不能为空'];
  4294. if(empty($data['type'])) return [false, 'type不能为空'];
  4295. $type = $data['type'];
  4296. if($type == 1){
  4297. ReportMessage::where('id', $data['id'])
  4298. ->update(['is_use' => 1]);
  4299. }else{
  4300. ReportMessage::where('id', $data['id'])
  4301. ->update(['del_time' => time()]);
  4302. }
  4303. return [true, ''];
  4304. }
  4305. public function filldatas(){
  4306. $box = Box::where('del_time',0)
  4307. ->where('crt_time','>=',1750727777)
  4308. ->select("top_order_no", 'order_no','id')
  4309. ->get()->toArray();
  4310. $box_id = [];
  4311. foreach ($box as $value) {
  4312. $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
  4313. $t = $box_detail->where('order_no', $value['order_no'])->where('del_time',0)->select('top_id as id')->first()->toArray();
  4314. $box_id[$value['id']] = $t['id'];
  4315. }
  4316. $result = DispatchSub::where('del_time',0)
  4317. ->whereIn('sale_orders_product_id', array_values($box_id))
  4318. ->where('process_id',14)
  4319. ->where('finished_num', '>', '0.000')
  4320. ->select('id','dispatch_quantity','finished_num','order_product_id','sale_orders_product_id','technology_name')
  4321. ->get()->toArray();
  4322. $map = [];
  4323. $return = [];
  4324. foreach ($result as $key => $value){
  4325. if($value['finished_num'] < 1){
  4326. $new_key = $value['order_product_id'] . $value['sale_orders_product_id'] . $value['technology_name'];
  4327. $tmp = bcsub(0.750,$value['finished_num'],3);
  4328. $map[$new_key][] = $tmp;
  4329. $return[] = $value;
  4330. }
  4331. }dump(count($return));
  4332. $return2= [];
  4333. foreach ($result as $key => $value){
  4334. if($value['finished_num'] >= 1){
  4335. $new_key = $value['order_product_id'] . $value['sale_orders_product_id'] . $value['technology_name'];
  4336. if(isset($map[$new_key])){
  4337. foreach ($map[$new_key] as $val){
  4338. $return[] = [
  4339. 'id' => $value['id'],
  4340. 'dispatch_quantity' => $val,
  4341. 'finished_num' => $val,
  4342. 'order_product_id' => $value['order_product_id'],
  4343. 'sale_orders_product_id' => $value['sale_orders_product_id'],
  4344. 'technology_name' => $value['technology_name'],
  4345. ];
  4346. $value['finished_num'] = bcsub($value['finished_num'], $val,3);
  4347. }
  4348. }
  4349. $return2[] = $value;
  4350. }
  4351. }
  4352. dd($return2);
  4353. }
  4354. }