ApplyOrderService.php 173 KB

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