ApplyOrderService.php 214 KB

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