ApplyOrderService.php 210 KB

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