ApplyOrderService.php 207 KB

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