ApplyOrderService.php 176 KB

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