ApplyOrderService.php 211 KB

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