ApplyOrderService.php 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  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\Dispatch;
  8. use App\Model\DispatchSub;
  9. use App\Model\Employee;
  10. use App\Model\EmployeeTeamPermission;
  11. use App\Model\Equipment;
  12. use App\Model\InOutRecord;
  13. use App\Model\OrdersProduct;
  14. use App\Model\Process;
  15. use App\Model\ReportWorking;
  16. use App\Model\ReportWorkingDetail;
  17. use App\Model\SaleOrdersProduct;
  18. use App\Model\ScrappCount;
  19. use App\Model\Team;
  20. use App\Service\Box\BoxService;
  21. use Illuminate\Support\Facades\DB;
  22. class ApplyOrderService extends Service
  23. {
  24. public function MaterialEdit($data, $user){
  25. list($status,$msg) = $this->MaterialRule($data,false);
  26. if(!$status) return [$status,$msg];
  27. DB::beginTransaction();
  28. try{
  29. $time = time();
  30. $model = new ApplyOrder();
  31. $model = $model->where('id',$data['id'])->first();
  32. $model->apply_id = $data['apply_id'];
  33. $model->apply_time = $data['apply_time'] ;
  34. $model->mark = $data['mark'] ?? "";
  35. $model->storehouse_id = $data['storehouse_id'];
  36. $model->storehouse_title = $data['storehouse_title'] ?? "";
  37. $model->type = $data['type'];
  38. $model->save();
  39. $id = $model->id;
  40. ApplyOrderDetail::where('del_time',0)->where('apply_order_id',$id)->update([
  41. 'del_time' => $time
  42. ]);
  43. $detail_insert = [];
  44. foreach ($data['order_data'] as $v){
  45. $detail_insert[] = [
  46. 'apply_order_id' => $id,
  47. 'data_id' => $v['id'],
  48. 'quantity' => $v['quantity'] ?? 0,
  49. 'product_no' => $v['product_no'] ?? "",
  50. 'product_title' => $v['product_title'] ?? "",
  51. 'product_size' => $v['product_size'] ?? "",
  52. 'product_unit' => $v['product_unit'] ?? "",
  53. 'technology_name' => $v['technology_name'] ?? "", //颜色
  54. 'top_product_title' => $v['top_product_title'] ?? "",
  55. 'top_product_no' => $v['top_product_title'] ?? "",
  56. 'type' => $data['type'],
  57. 'storehouse_id' => $data['storehouse_id'],
  58. 'crt_time' => $time,
  59. ];
  60. }
  61. ApplyOrderDetail::insert($detail_insert);
  62. DB::commit();
  63. }catch (\Exception $e){
  64. DB::rollBack();
  65. return [false,$e->getMessage()];
  66. }
  67. return [true,''];
  68. }
  69. public function MaterialAdd($data,$user){
  70. list($status,$msg) = $this->MaterialRule($data);
  71. if(!$status) return [$status,$msg];
  72. DB::beginTransaction();
  73. try{
  74. $time = time();
  75. $model = new ApplyOrder();
  76. $model->order_number = $data['order_number'];
  77. $model->apply_id = $data['apply_id'];
  78. $model->apply_time = $data['apply_time'] ;
  79. $model->mark = $data['mark'] ?? "";
  80. $model->storehouse_id = $data['storehouse_id'];
  81. $model->storehouse_title = $data['storehouse_title'] ?? "";
  82. $model->type = $data['type'];
  83. $model->crt_id = $user['id'];
  84. $model->save();
  85. $id = $model->id;
  86. $detail_insert = [];
  87. foreach ($data['order_data'] as $v){
  88. $detail_insert[] = [
  89. 'apply_order_id' => $id,
  90. 'data_id' => $v['id'],
  91. 'quantity' => $v['quantity'] ?? 0,
  92. 'product_no' => $v['product_no'] ?? "",
  93. 'product_title' => $v['product_title'] ?? "",
  94. 'product_size' => $v['product_size'] ?? "",
  95. 'product_unit' => $v['product_unit'] ?? "",
  96. 'technology_name' => $v['technology_name'] ?? "", //颜色
  97. 'top_product_title' => $v['top_product_title'] ?? "",
  98. 'top_product_no' => $v['top_product_title'] ?? "",
  99. 'type' => $data['type'],
  100. 'storehouse_id' => $data['storehouse_id'],
  101. 'crt_time' => $time,
  102. ];
  103. }
  104. ApplyOrderDetail::insert($detail_insert);
  105. DB::commit();
  106. }catch (\Exception $e){
  107. DB::rollBack();
  108. return [false,$e->getMessage()];
  109. }
  110. return [true, ''];
  111. }
  112. public function MaterialDel($data){
  113. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  114. $apply = ApplyOrder::where('id',$data['id'])->first();
  115. if($apply->del_time > 0) return [false,'申请单不存在或已被删除'];
  116. if($apply->state != ApplyOrder::state_zero) return [false, '申请单已审核,删除失败'];
  117. $time = time();
  118. try {
  119. DB::beginTransaction();
  120. $apply->del_time = $time;
  121. $apply->save();
  122. ApplyOrderDetail::where('del_time', 0)->where('apply_order_id',$data['id'])->update([
  123. 'del_time'=>time()
  124. ]);
  125. DB::commit();
  126. }catch (\Throwable $exception){
  127. DB::rollBack();
  128. return [false, $exception->getMessage()];
  129. }
  130. return [true,''];
  131. }
  132. public function MaterialList($data){
  133. $model = ApplyOrder::where('del_time',0)
  134. ->select('*')
  135. ->orderBy('id','desc');
  136. if(isset($data['status'])) $model->where('status', $data['status']);
  137. if(! empty($data['type'])) $model->where('type', $data['type']);
  138. if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
  139. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  140. if(! empty($data['apply_id'])) $model->where('apply_id', $data['apply_id']);
  141. if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
  142. if(! empty($data['apply_time'][0]) && ! empty($data['apply_time'][1])) $model->whereBetween('apply_time',[$data['apply_time'][0],$data['apply_time'][1]]);
  143. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  144. if(! empty($data['dispatch_no'])){
  145. $dispatch_id = DispatchSub::where('del_time',0)
  146. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  147. ->select('id')
  148. ->get()->toArray();
  149. $dispatch_id = array_column($dispatch_id,'id');
  150. $detail = ApplyOrderDetail::where('del_time',0)
  151. ->where('type', $data['type'])
  152. ->whereIn('data_id', $dispatch_id)
  153. ->select('apply_order_id')
  154. ->get()->toArray();
  155. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  156. }
  157. if(! empty($data['box_no'])){
  158. $box_id = Box::where('del_time',0)
  159. ->where('order_no', 'LIKE', '%'.$data['box_no'].'%')
  160. ->select('id')
  161. ->get()->toArray();
  162. $box_id = array_column($box_id,'id');
  163. $detail = ApplyOrderDetail::where('del_time',0)
  164. ->where('type', $data['type'])
  165. ->whereIn('data_id', $box_id)
  166. ->select('apply_order_id')
  167. ->get()->toArray();
  168. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  169. }
  170. if(! empty($data['sale_order_number'])){
  171. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  172. $dispatch_id = DispatchSub::where('del_time',0)
  173. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  174. ->select('id')
  175. ->get()->toArray();
  176. $dispatch_id = array_column($dispatch_id,'id');
  177. $detail = ApplyOrderDetail::where('del_time',0)
  178. ->where('type', $data['type'])
  179. ->whereIn('data_id', $dispatch_id)
  180. ->select('apply_order_id')
  181. ->get()->toArray();
  182. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  183. }elseif($data['type'] == ApplyOrder::type_five){
  184. $zj_id = ScrappCount::where('del_time',0)
  185. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  186. ->select('id')
  187. ->get()->toArray();
  188. $zj_id = array_column($zj_id,'id');
  189. $detail = ApplyOrderDetail::where('del_time',0)
  190. ->where('type', $data['type'])
  191. ->whereIn('data_id', $zj_id)
  192. ->select('apply_order_id')
  193. ->get()->toArray();
  194. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  195. }else{
  196. $no = SaleOrdersProduct::where('del_time',0)
  197. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  198. ->select('order_no')
  199. ->get()->toArray();
  200. $no = array_unique(array_column($no,'order_no'));
  201. $box_id = Box::where('del_time',0)
  202. ->whereIn('top_order_no', $no)
  203. ->select('id')
  204. ->get()->toArray();
  205. $box_id = array_column($box_id,'id');
  206. $detail = ApplyOrderDetail::where('del_time',0)
  207. ->where('type', $data['type'])
  208. ->whereIn('data_id', $box_id)
  209. ->select('apply_order_id')
  210. ->get()->toArray();
  211. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  212. }
  213. }
  214. $list = $this->limit($model,'',$data);
  215. $list = $this->fillData($list);
  216. return [true,$list];
  217. }
  218. public function fillData($data){
  219. if(empty($data['data'])) return $data;
  220. $emp = Employee::whereIn('id',array_merge_recursive(array_unique(array_column($data['data'],'crt_id')), array_unique(array_column($data['data'],'apply_id'))))
  221. ->pluck('emp_name','id')
  222. ->toArray();
  223. $array1 = $array2 = $array3 = [];
  224. foreach ($data['data'] as $value){
  225. if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  226. $array1[] = $value['id'];
  227. }elseif ($value['type'] == ApplyOrder::type_five){
  228. $array3[] = $value['id'];
  229. }else{
  230. $array2[] = $value['id'];
  231. }
  232. }
  233. $order1 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])
  234. ->whereIn('apply_order_id', $array1)
  235. ->select('data_id','apply_order_id')
  236. ->get()->toArray();
  237. $order1_map = [];
  238. foreach ($order1 as $value){
  239. $order1_map[$value['apply_order_id']][] = $value['data_id'];
  240. }
  241. $order2 = ApplyOrderDetail::whereNotIn('type',[ApplyOrder::type_one,ApplyOrder::type_two,ApplyOrder::type_five])
  242. ->whereIn('apply_order_id', $array2)
  243. ->select('data_id','apply_order_id')
  244. ->get()->toArray();
  245. $order2_map = [];
  246. foreach ($order2 as $value){
  247. $order2_map[$value['apply_order_id']][] = $value['data_id'];
  248. }
  249. $order3 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_five])
  250. ->whereIn('apply_order_id', $array3)
  251. ->select('data_id','apply_order_id')
  252. ->get()->toArray();
  253. $order3_map = [];
  254. foreach ($order3 as $value){
  255. $order3_map[$value['apply_order_id']][] = $value['data_id'];
  256. }
  257. $map1 = DispatchSub::whereIn('id', array_unique(array_column($order1,'data_id')))
  258. ->pluck('out_order_no','id')
  259. ->toArray();
  260. $map2 = Box::whereIn('id', array_unique(array_column($order2,'data_id')))
  261. ->pluck('top_order_no','id')
  262. ->toArray();
  263. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
  264. ->pluck('out_order_no','order_no')
  265. ->toArray();
  266. $map3 = ScrappCount::whereIn('id', array_unique(array_column($order3,'data_id')))
  267. ->pluck('out_order_no','id')
  268. ->toArray();
  269. foreach ($data['data'] as $key => $value){
  270. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  271. $data['data'][$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
  272. $data['data'][$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
  273. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  274. $data['data'][$key]['apply_name'] = $emp[$value['apply_id']] ?? '';
  275. if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  276. $order_tmp = $order1_map[$value['id']] ?? [];
  277. $order_tmp = array_unique($order_tmp);
  278. $sale_order_number = [];
  279. foreach ($order_tmp as $v){
  280. $str = $map1[$v] ?? '';
  281. if(empty($str)) continue;
  282. $sale_order_number[] = $str;
  283. }
  284. $sale_order_number = array_unique($sale_order_number);
  285. $sale_order_number = implode(',',$sale_order_number);
  286. $data['data'][$key]['sale_order_number'] = $sale_order_number;
  287. }elseif ($value['type'] == ApplyOrder::type_five){
  288. $order_tmp = $order3_map[$value['id']] ?? [];
  289. $order_tmp = array_unique($order_tmp);
  290. $sale_order_number = [];
  291. foreach ($order_tmp as $v){
  292. $str = $map3[$v] ?? '';
  293. if(empty($str)) continue;
  294. $sale_order_number[] = $str;
  295. }
  296. $sale_order_number = array_unique($sale_order_number);
  297. $sale_order_number = implode(',',$sale_order_number);
  298. $data['data'][$key]['sale_order_number'] = $sale_order_number;
  299. }else{
  300. $order_tmp = $order2_map[$value['id']] ?? [];
  301. $order_tmp = array_unique($order_tmp);
  302. $sale_order_number = [];
  303. foreach ($order_tmp as $v){
  304. $m2_t = $map2[$v] ?? '';
  305. $m2_t = $map2_fin[$m2_t] ?? "";
  306. if(empty($m2_t)) continue;
  307. $sale_order_number[] = $m2_t;
  308. }
  309. $sale_order_number = array_unique($sale_order_number);
  310. $sale_order_number = implode(',',$sale_order_number);
  311. $data['data'][$key]['sale_order_number'] = $sale_order_number;
  312. }
  313. }
  314. return $data;
  315. }
  316. public function MaterialDetail($data){
  317. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  318. $id = $data['id'];
  319. $detail = ApplyOrder::where('del_time',0)
  320. ->where('id',$id)
  321. ->first();
  322. if(empty($detail)) return [false,'申请单不存在或已被删除'];
  323. $detail = $detail->toArray();
  324. $detail['apply_time'] = $detail['apply_time'] ? date('Y-m-d',$detail['apply_time']) : '';
  325. $detail['apply_title'] = Employee::where('id', $detail['apply_id'])->value('emp_name');
  326. $apply_d = ApplyOrderDetail::where('del_time', 0)
  327. ->where('apply_order_id', $id)
  328. ->get()->toArray();
  329. $apply_id = array_column($apply_d, 'data_id');
  330. $d = [];
  331. if($detail['type'] == ApplyOrder::type_one){
  332. $d = DispatchSub::where('del_time',0)
  333. ->whereIn('id', $apply_id)
  334. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  335. ->get()->toArray();
  336. $args = "";
  337. foreach ($d as $value){
  338. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  339. }
  340. $args = rtrim($args, 'OR ');
  341. $d_no_map = [];
  342. $d_no = DispatchSub::where('del_time',0)
  343. ->whereRaw($args)
  344. ->select('dispatch_no as order_no','order_product_id','crt_time')
  345. ->get()->toArray();
  346. foreach ($d_no as $value){
  347. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  348. 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'];
  349. }else{
  350. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  351. }
  352. }
  353. }elseif ($detail['type'] == ApplyOrder::type_two){
  354. $d = DispatchSub::where('del_time',0)
  355. ->whereIn('id', $apply_id)
  356. ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
  357. ->get()->toArray();
  358. $args = "";
  359. foreach ($d as $value){
  360. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  361. }
  362. $args = rtrim($args, 'OR ');
  363. $d_no_map = [];
  364. $d_no = DispatchSub::where('del_time',0)
  365. ->whereRaw($args)
  366. ->select('dispatch_no as order_no','order_product_id','crt_time')
  367. ->get()->toArray();
  368. foreach ($d_no as $value){
  369. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  370. 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'];
  371. }else{
  372. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  373. }
  374. }
  375. }elseif ($detail['type'] == ApplyOrder::type_three){
  376. $d = Box::where('del_time',0)
  377. ->whereIn('id', $apply_id)
  378. ->select('id', 'order_no')
  379. ->get()->toArray();
  380. }elseif ($detail['type'] == ApplyOrder::type_four){
  381. $d = Box::where('del_time',0)
  382. ->whereIn('id', $apply_id)
  383. ->select('id', 'order_no')
  384. ->get()->toArray();
  385. }elseif ($detail['type'] == ApplyOrder::type_five){ //质检入库申请
  386. $d = ScrappCount::where('del_time',0)
  387. ->whereIn('id', $apply_id)
  388. ->select('id', 'order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  389. ->get()->toArray();
  390. }
  391. $d_tmp = array_column($d,null,'id');
  392. $return = [];
  393. foreach ($apply_d as $t){
  394. $tmp = $d_tmp[$t['data_id']] ?? [];
  395. $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
  396. $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
  397. $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
  398. $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
  399. if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
  400. $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
  401. }else{
  402. $order_no = $tmp['order_no'] ?? "";
  403. }
  404. if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
  405. $product_unit = "吨";
  406. }else{
  407. $product_unit = $t['product_unit'] ?? "";
  408. }
  409. $return[] = [
  410. 'id' => $t['data_id'] ?? 0,
  411. 'quantity' => $t['quantity'] ?? 0,
  412. 'product_no' => $t['product_no'] ?? "",
  413. 'product_title' => $t['product_title'] ?? "",
  414. 'product_size' => $t['product_size'] ?? "",
  415. 'product_unit' => $product_unit,
  416. 'top_product_title' => $top_product_title,
  417. 'top_product_no' => $top_product_no,
  418. 'technology_name' => $technology_name, //颜色
  419. 'wood_name' => $wood_name,
  420. 'order_no' => $order_no,
  421. ];
  422. }
  423. $detail['order_data'] = $return;
  424. return [true, $detail];
  425. }
  426. public function MaterialRule(&$data,$is_add = true){
  427. if($this->isEmpty($data,'apply_id')) return [false,'申请人不能为空'];
  428. if($this->isEmpty($data,'apply_time')) return [false,'申请时间不能为空'];
  429. if($this->isEmpty($data,'storehouse_id')) return [false,'仓库不能为空'];
  430. if($this->isEmpty($data,'type')) return [false,'申请单类型不能为空'];
  431. if(empty($data['order_data'])) return [false, '申请单详细信息不能为空'];
  432. foreach ($data['order_data'] as $value){
  433. if(empty($value['id'])) return [false, '申请单详细信息ID不能为空'];
  434. if($data['type'] != ApplyOrder::type_three){
  435. if(empty($value['quantity'])) return [false, '申请单详细信息数量不能为空'];
  436. }
  437. }
  438. $id = array_unique(array_column($data['order_data'],'id'));
  439. if($data['type'] == ApplyOrder::type_one){
  440. $dispatch = DispatchSub::where('del_time',0)
  441. ->whereIn('id', $id)
  442. ->select('id', 'dispatch_no', 'status')
  443. ->get()->toArray();
  444. if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
  445. }elseif ($data['type'] == ApplyOrder::type_two){
  446. $dispatch = DispatchSub::where('del_time',0)
  447. ->whereIn('id', $id)
  448. ->select('id', 'dispatch_no', 'status')
  449. ->get()->toArray();
  450. if(count($dispatch) != count($id)) return [false, '完工单信息错误,请重新选择'];
  451. }elseif ($data['type'] == ApplyOrder::type_three){
  452. $box = Box::where('del_time',0)
  453. ->whereIn('id', $id)
  454. ->select('id', 'order_no', 'status')
  455. ->get()->toArray();
  456. if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
  457. }elseif ($data['type'] == ApplyOrder::type_four){
  458. $box = Box::where('del_time',0)
  459. ->whereIn('id', $id)
  460. ->select('id', 'order_no', 'status')
  461. ->get()->toArray();
  462. if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
  463. }elseif ($data['type'] == ApplyOrder::type_five){
  464. $scrapp = ScrappCount::where('del_time',0)
  465. ->whereIn('id', $id)
  466. ->select('id', 'order_no', 'status')
  467. ->get()->toArray();
  468. if(count($scrapp) != count($id)) return [false, '质检单信息错误,请重新选择'];
  469. }else{
  470. return [false, '申请单类型错误'];
  471. }
  472. if($is_add){
  473. $data['order_number'] = $this->setOrderNO($data['type']);
  474. if(empty($data['order_number'])) return [false, '申请单唯一标识生成失败'];
  475. }else{
  476. if(empty($data['id'])) return [false, '申请单唯一标识不能为空'];
  477. if(empty($data['order_number'])) return [false, '申请单唯一标识不能为空'];
  478. $apply = ApplyOrder::where('id', $data['id'])->where('del_time', 0)->first();
  479. if(empty($apply)) return [false, '申请单不存在或已被删除'];
  480. if($apply->status != 0) return [false, '申请单已审核,编辑失败'];
  481. }
  482. return [true,''];
  483. }
  484. public function setOrderNO($type = "",$time = 0){
  485. if(empty($time)){
  486. $str = date('Ymd',time());
  487. }else{
  488. $str = date('Ymd',$time);
  489. }
  490. $order_number = ApplyOrder::where('order_number','Like','%'. $str . '%')
  491. ->where('type', $type)
  492. ->max('order_number');
  493. if(empty($order_number)){
  494. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  495. $number = $str . $number;
  496. }else{
  497. $tmp = substr($order_number, -3);
  498. $tmp = $tmp + 1;
  499. //超过999
  500. if(strlen($tmp) > 3) return '';
  501. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  502. $number = $str . $number;
  503. }
  504. return $number;
  505. }
  506. public function createSQ($data, $user, $type = 0, $status = 1,$time = 0){
  507. if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
  508. if($type == ApplyOrder::type_one){
  509. $storehouse_id = "001";
  510. $storehouse_title = "原料仓";
  511. }elseif ($type == ApplyOrder::type_two){
  512. $storehouse_id = "004";
  513. $storehouse_title = "待清洗原材料仓";
  514. }elseif ($type == ApplyOrder::type_three){
  515. $storehouse_id = "002";
  516. $storehouse_title = "产成品仓";
  517. }elseif($type == ApplyOrder::type_four){
  518. $storehouse_id = "003";
  519. $storehouse_title = "包材、辅料仓" ;
  520. }
  521. try{
  522. DB::beginTransaction();
  523. $order_number = $this->setOrderNO($type, $time);
  524. // list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type);
  525. // if(! $status) return [false, $msg];
  526. if(empty($time)) $time = time();
  527. $model = new ApplyOrder();
  528. $model->order_number = $order_number;
  529. $model->apply_id = $user['id'];
  530. $model->apply_time = $time;
  531. $model->storehouse_id = $storehouse_id;
  532. $model->storehouse_title = $storehouse_title;
  533. $model->type = $type;
  534. $model->status = $status; //是否审核
  535. $model->save();
  536. $id = $model->id;
  537. $detail_insert = [];
  538. foreach ($data as $v){
  539. $detail_insert[] = [
  540. 'apply_order_id' => $id,
  541. 'data_id' => $v['id'],
  542. 'quantity' => $v['quantity'] ?? 0,
  543. 'product_no' => $v['product_no'] ?? "",
  544. 'product_title' => $v['product_title'] ?? "",
  545. 'product_size' => $v['product_size'] ?? "",
  546. 'product_unit' => $v['product_unit'] ?? "",
  547. 'technology_name' => $v['technology_name'] ?? "",
  548. 'wood_name' => $v['wood_name'] ?? "",
  549. 'top_product_no' => $v['top_product_no'] ?? "",
  550. 'top_product_title' => $v['top_product_title'] ?? "",
  551. 'type' => $type,
  552. 'storehouse_id' => $storehouse_id,
  553. 'crt_time' => $time,
  554. ];
  555. }
  556. ApplyOrderDetail::insert($detail_insert);
  557. DB::commit();
  558. }catch (\Exception $e){
  559. DB::rollBack();
  560. return [false,$e->getMessage()];
  561. }
  562. return [true, $id];
  563. }
  564. public function createSqFive($data, $user, $type = 0, $status = 1, $time = 0){
  565. if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
  566. try{
  567. DB::beginTransaction();
  568. if(empty($time)) $time = time();
  569. $first = $data[0] ?? [];
  570. $order_number = $this->setOrderNO($type, $time);
  571. $model = new ApplyOrder();
  572. $model->order_number = $order_number;
  573. $model->apply_id = $user['id'];
  574. $model->apply_time = $time;
  575. $model->storehouse_id = $first['storehouse_id'];
  576. $model->storehouse_title = $first['storehouse_title'];
  577. $model->type = $type;
  578. $model->status = $status; //是否审核
  579. $model->save();
  580. $id = $model->id;
  581. $insert = [];
  582. foreach ($data as $value){
  583. $insert[] = [
  584. 'apply_order_id' => $id,
  585. 'data_id' => $value['id'],
  586. 'quantity' => $value['quantity'] ?? 0,
  587. 'product_no' => $value['product_no'] ?? "",
  588. 'product_title' => $value['product_title'] ?? "",
  589. 'product_size' => $value['product_size'] ?? "",
  590. 'product_unit' => $value['product_unit'] ?? "",
  591. 'technology_name' => $value['technology_name'] ?? "",
  592. 'wood_name' => $value['wood_name'] ?? "",
  593. 'top_product_no' => $value['top_product_no'] ?? "",
  594. 'top_product_title' => $value['top_product_title'] ?? "",
  595. 'type' => $type,
  596. 'storehouse_id' => $value['storehouse_id'],
  597. 'crt_time' => $time,
  598. ];
  599. }
  600. ApplyOrderDetail::insert($insert);
  601. DB::commit();
  602. }catch (\Exception $e){
  603. DB::rollBack();
  604. return [false,$e->getMessage()];
  605. }
  606. return [true, ''];
  607. }
  608. //流水
  609. public function createRecord($id){return [true, ''];
  610. $record = ApplyOrderDetail::where('apply_order_id', $id)
  611. ->where('del_time',0)
  612. ->get()->toArray();
  613. if(empty($record)) return [false, '申请单明细数据不存在或已被删除'];
  614. try{
  615. DB::beginTransaction();
  616. $time = time();
  617. $insert = [];
  618. foreach ($record as $value){
  619. if(empty($value['product_no'])) continue;
  620. if($value['type'] == ApplyOrder::type_one){
  621. $number = - $value['quantity'];
  622. }else{
  623. $number = $value['quantity'];
  624. }
  625. $insert[] = [
  626. 'apply_order_id' => $id,
  627. 'product_no' => $value['product_no'],
  628. 'number' => $number,
  629. 'type' => $value['type'],
  630. 'storehouse_id' => $value['storehouse_id'],
  631. 'crt_time' => $time,
  632. ];
  633. }
  634. if(! empty($insert)) InOutRecord::insert($insert);
  635. DB::commit();
  636. }catch (\Exception $e){
  637. DB::rollBack();
  638. return [false,$e->getMessage()];
  639. }
  640. return [true, ''];
  641. }
  642. //报工
  643. public function reportWorkingEdit($data, $user){
  644. list($status,$msg) = $this->reportWorkingRule($data,$user,false);
  645. if(!$status) return [$status,$msg];
  646. DB::beginTransaction();
  647. try{
  648. $time = time();
  649. $model = new ReportWorking();
  650. $model = $model->where('id',$data['id'])->first();
  651. $model->report_time = $data['report_time'];
  652. $model->mark = $data['mark']?? "";
  653. $model->status = $data['auto'] ?? 0;
  654. $model->save();
  655. $id = $model->id;
  656. ReportWorkingDetail::where('del_time',0)->where('report_working_id',$id)->update([
  657. 'del_time' => $time
  658. ]);
  659. //班组下的人
  660. $team_man = [];
  661. $model_t = new EmployeeTeamPermission();
  662. $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();
  663. foreach ($team_array as $v){
  664. $team_man[$v['team_id']][] = $v['employee_id'];
  665. }
  666. $detail_insert = [];
  667. foreach ($data['order_data'] as $v){
  668. $t = $team_man[$v['team_id']] ?? [];
  669. if(! empty($t)){
  670. foreach ($t as $t_v){
  671. $detail_insert[] = [
  672. 'report_working_id' => $id,
  673. 'data_id' => $v['id'],
  674. 'quantity' => $v['quantity'] ?? 0,
  675. 'process_id' => $v['process_id'] ?? 0,
  676. 'finished_id' => $t_v,
  677. 'team_id' => $v['team_id'],
  678. 'crt_time' => $time,
  679. ];
  680. }
  681. }else{
  682. $detail_insert[] = [
  683. 'report_working_id' => $id,
  684. 'data_id' => $v['id'],
  685. 'quantity' => $v['quantity'] ?? 0,
  686. 'process_id' => $v['process_id'] ?? 0,
  687. 'finished_id' => 0,
  688. 'team_id' => $v['team_id'],
  689. 'crt_time' => $time,
  690. ];
  691. }
  692. }
  693. ReportWorkingDetail::insert($detail_insert);
  694. DB::commit();
  695. }catch (\Exception $e){
  696. DB::rollBack();
  697. return [false,$e->getMessage()];
  698. }
  699. return [true,''];
  700. }
  701. public function reportWorkingAdd($data,$user){
  702. list($status,$msg) = $this->reportWorkingRule($data,$user);
  703. if(!$status) return [$status,$msg];
  704. DB::beginTransaction();
  705. try{
  706. $time = time();
  707. $model = new ReportWorking();
  708. $model->order_number = $data['order_number'];
  709. $model->report_time = $data['report_time'];
  710. $model->mark = $data['mark']?? "";
  711. $model->crt_id = $user['id'];
  712. $model->status = $data['auto'] ?? 0;
  713. $model->save();
  714. $id = $model->id;
  715. //班组下的人
  716. $team_man = [];
  717. $model_t = new EmployeeTeamPermission();
  718. $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();
  719. foreach ($team_array as $v){
  720. $team_man[$v['team_id']][] = $v['employee_id'];
  721. }
  722. $detail_insert = [];
  723. foreach ($data['order_data'] as $v){
  724. $t = $team_man[$v['team_id']] ?? [];
  725. if(! empty($t)){
  726. foreach ($t as $t_v){
  727. $detail_insert[] = [
  728. 'report_working_id' => $id,
  729. 'data_id' => $v['id'],
  730. 'quantity' => $v['quantity'] ?? 0,
  731. 'process_id' => $v['process_id'] ?? 0,
  732. 'finished_id' => $t_v,
  733. 'team_id' => $v['team_id'],
  734. 'crt_time' => $time,
  735. ];
  736. }
  737. }else{
  738. $detail_insert[] = [
  739. 'report_working_id' => $id,
  740. 'data_id' => $v['id'],
  741. 'quantity' => $v['quantity'] ?? 0,
  742. 'process_id' => $v['process_id'] ?? 0,
  743. 'finished_id' => 0,
  744. 'team_id' => $v['team_id'],
  745. 'crt_time' => $time,
  746. ];
  747. }
  748. }
  749. ReportWorkingDetail::insert($detail_insert);
  750. DB::commit();
  751. }catch (\Exception $e){
  752. DB::rollBack();
  753. return [false,$e->getMessage()];
  754. }
  755. return [true, ''];
  756. }
  757. public function reportWorkingDel($data){
  758. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  759. $apply = ReportWorking::where('id',$data['id'])->first();
  760. if($apply->del_time > 0) return [false,'报工单不存在或已被删除'];
  761. if($apply->state != ReportWorking::state_zero) return [false, '报工单已审核,删除失败'];
  762. $time = time();
  763. try {
  764. DB::beginTransaction();
  765. $apply->del_time = $time;
  766. $apply->save();
  767. ReportWorkingDetail::where('del_time', 0)->where('report_working_id',$data['id'])->update([
  768. 'del_time'=>time()
  769. ]);
  770. DB::commit();
  771. }catch (\Throwable $exception){
  772. DB::rollBack();
  773. return [false, $exception->getMessage()];
  774. }
  775. return [true,''];
  776. }
  777. public function reportWorkingList($data){
  778. $model = ReportWorking::where('del_time',0)
  779. ->select('*')
  780. ->orderBy('id','desc');
  781. if(isset($data['status'])) $model->where('status', $data['status']);
  782. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  783. if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
  784. if(! empty($data['report_time'][0]) && ! empty($data['report_time'][1])) $model->whereBetween('report_time',[$data['report_time'][0],$data['report_time'][1]]);
  785. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  786. if(! empty($data['dispatch_no1'])) {
  787. $dispatch_id = DispatchSub::where('del_time',0)
  788. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no1'].'%')
  789. ->select('id')
  790. ->get()->toArray();
  791. $dispatch_id = array_column($dispatch_id,'id');
  792. $detail = ReportWorkingDetail::where('del_time',0)
  793. ->whereIn('data_id', $dispatch_id)
  794. ->select('report_working_id')
  795. ->get()->toArray();
  796. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  797. }
  798. if(! empty($data['dispatch_no'])) {
  799. $dispatch_id = DispatchSub::where('del_time',0)
  800. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  801. ->select('id')
  802. ->get()->toArray();
  803. $dispatch_id = array_column($dispatch_id,'id');
  804. $detail = ReportWorkingDetail::where('del_time',0)
  805. ->whereIn('data_id', $dispatch_id)
  806. ->select('report_working_id')
  807. ->get()->toArray();
  808. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  809. }
  810. if(! empty($data['sale_order_number'])){
  811. $id = SaleOrdersProduct::where('del_time',0)
  812. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  813. ->select('id')
  814. ->get()->toArray();
  815. $dispatch_id = DispatchSub::where('del_time',0)
  816. ->whereIn('sale_orders_product_id',array_column($id,'id'))
  817. ->select('id')
  818. ->get()->toArray();
  819. $detail = ReportWorkingDetail::where('del_time',0)
  820. ->whereIn('data_id', $dispatch_id)
  821. ->select('report_working_id')
  822. ->get()->toArray();
  823. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  824. }
  825. $list = $this->limit($model,'',$data);
  826. $list = $this->reportWorkingfillData($list);
  827. return [true,$list];
  828. }
  829. public function reportWorkingfillData($data){
  830. if(empty($data['data'])) return $data;
  831. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  832. ->pluck('emp_name','id')
  833. ->toArray();
  834. $detail = ReportWorkingDetail::where('del_time',0)
  835. ->whereIn('report_working_id', array_column($data['data'],'id'))
  836. ->select('data_id','report_working_id')
  837. ->get()->toArray();
  838. $map = [];
  839. foreach ($detail as $value){
  840. $map[$value['data_id']] = $value['report_working_id'];
  841. }
  842. $dispatch = DispatchSub::whereIn('id',array_column($detail,'data_id'))
  843. ->get()->toArray();
  844. $map1 = [];
  845. $team_map = Team::where("del_time",0)->pluck('title','id')->toArray();
  846. $process_map = Process::where("del_time",0)->pluck('title','id')->toArray();
  847. foreach ($dispatch as $value){
  848. $report_working_id = $map[$value['id']] ?? 0;
  849. $value['team_name'] = $team_map[$value['team_id']] ?? "";
  850. $value['process_name'] = $process_map[$value['process_id']] ?? "";
  851. $map1[$report_working_id][] = $value;
  852. }
  853. foreach ($data['data'] as $key => $value){
  854. $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d',$value['report_time']) : '';
  855. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d',$value['crt_time']) : '';
  856. $data['data'][$key]['status_title'] = ReportWorking::$state_name[$value['status']] ?? "";
  857. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  858. $data['data'][$key]['dispatch'] = $map1[$value['id']] ?? [];
  859. }
  860. return $data;
  861. }
  862. public function reportWorkingDetail($data){
  863. if(empty($data['id']) && empty($data['order_number'])) return [false,'ID与OrderNumber不能为空!'];
  864. $id = $data['id'] ?? 0;
  865. $order_number = $data['order_number'] ?? "";
  866. $detail = ReportWorking::where('del_time',0)
  867. ->when(! empty($id), function ($query) use ($id){
  868. return $query->where('id', $id);
  869. })
  870. ->when(! empty($order_number), function ($query) use ($order_number){
  871. return $query->where('order_number', $order_number);
  872. })
  873. ->first();
  874. if(empty($detail)) return [false,'报工单不存在或已被删除'];
  875. $detail = $detail->toArray();
  876. if(empty($id)) $id = $detail['id'];
  877. $detail['report_time'] = $detail['report_time'] ? date('Y-m-d',$detail['report_time']) : '';
  878. $apply_d = ReportWorkingDetail::where('del_time', 0)
  879. ->where('report_working_id', $id)
  880. ->get()->toArray();
  881. $apply_id = array_column($apply_d, 'data_id');
  882. //派工单
  883. $d = DispatchSub::where('del_time',0)
  884. ->whereIn('id', $apply_id)
  885. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','process_id','product_unit','price','product_size','device_id')
  886. ->get()->toArray();
  887. $d_tmp = array_column($d,null,'id');
  888. $process_map = Process::whereIn('id',array_unique(array_column($d,'process_id')))
  889. ->pluck('title','id')
  890. ->toArray();
  891. $team_map = Team::pluck('title','id')->toArray();
  892. $device_map = Equipment::whereIn('id',array_unique(array_column($d,'device_id')))
  893. ->pluck('title','id')
  894. ->toArray();
  895. $return = [];
  896. foreach ($apply_d as $t){
  897. $tmp = $d_tmp[$t['data_id']] ?? [];
  898. $str = $t['data_id'] . $t['process_id'];
  899. if(! isset($return[$str])){
  900. $return[$str] = [
  901. 'id' => $t['data_id'] ?? 0,
  902. 'quantity' => $t['quantity'] ?? 0,
  903. 'product_no' => $tmp['product_no'] ?? "",
  904. 'product_title' => $tmp['product_title'] ?? "",
  905. 'product_size' => $tmp['product_size'] ?? "",
  906. 'product_unit' => $tmp['product_unit'] ?? "",
  907. 'technology_name' => $tmp['technology_name'] ?? "",
  908. 'wood_name' => $tmp['wood_name'] ?? "",
  909. 'order_no' => $tmp['order_no'] ?? "",
  910. 'process_id' => $tmp['process_id'] ?? 0,
  911. 'process_name' => $process_map[$tmp['process_id']] ?? "",
  912. 'team_id' => $t['team_id'] ?? 0,
  913. 'team_title' => $team_map[$t['team_id']] ?? '',
  914. 'device_id' => $tmp['device_id'] ?? 0,
  915. 'device_title' => $device_map[$tmp['device_id']] ?? '',
  916. ];
  917. }
  918. }
  919. $detail['order_data'] = array_values($return);
  920. return [true, $detail];
  921. }
  922. public function reportWorkingRule(&$data,$user, $is_add = true){
  923. if($this->isEmpty($data,'report_time')) return [false,'报工时间不能为空'];
  924. if(empty($data['order_data'])) return [false, '报工单详细信息不能为空'];
  925. foreach ($data['order_data'] as $value){
  926. if(empty($value['id'])) return [false, '报工单详细信息ID不能为空'];
  927. if(empty($value['quantity'])) return [false, '报工单详细信息数量不能为空'];
  928. if(empty($value['process_id'])) return [false, '报工单详细信息工序不能为空'];
  929. if(empty($value['team_id'])) return [false, '报工单详细信息班组不能为空'];
  930. }
  931. $id = array_unique(array_column($data['order_data'],'id'));
  932. $dispatch = DispatchSub::where('del_time',0)
  933. ->whereIn('id', $id)
  934. ->select('id', 'dispatch_no', 'status', 'dispatch_quantity','order_product_id')
  935. ->get()->toArray();
  936. if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
  937. $count = array_unique(array_column($dispatch,'order_product_id'));
  938. if(count($count) > 1) return [false, '批量报工时请选择同一生产订单下的派工单'];
  939. if($is_add){
  940. $data['order_number'] = $this->setOrderNO2();
  941. if(empty($data['order_number'])) return [false, '报工单唯一标识生成失败'];
  942. }else{
  943. if(empty($data['id'])) return [false, '报工单唯一标识不能为空'];
  944. if(empty($data['order_number'])) return [false, '报工单唯一标识不能为空'];
  945. $apply = ReportWorking::where('id', $data['id'])->where('del_time', 0)->first();
  946. if(empty($apply)) return [false, '报工单不存在或已被删除'];
  947. if($apply->status != 0) return [false, '报工单已审核,编辑失败'];
  948. }
  949. list($status, $msg) = $this->compareQuantity($dispatch, $data);
  950. if(! $status) return [false, $msg];
  951. $em = new EmployeeService();
  952. $data['auto'] = $em->is_auto($user, "wgbg_auto");
  953. return [true,''];
  954. }
  955. public function compareQuantity($dispatch, $data){
  956. $id = $data['id'] ?? 0;
  957. $result = ReportWorkingDetail::where('del_time',0)
  958. ->when(! empty($id), function ($query) use ($id){
  959. return $query->where('report_working_id', '<>', $id);
  960. })
  961. ->where('data_id', array_column($dispatch, 'id'))
  962. ->select('data_id as dispatch_id','quantity')
  963. ->get()
  964. ->toArray();
  965. $map1 = [];
  966. foreach ($result as $value){
  967. if(isset($map1[$value['dispatch_id']])){
  968. $tmp = bcadd($map1[$value['dispatch_id']], $value['quantity'], 3);
  969. $map1[$value['dispatch_id']] = $tmp;
  970. }else{
  971. $map1[$value['dispatch_id']] = $value['quantity'];
  972. }
  973. }
  974. $map2 = array_column($dispatch,null,'id');
  975. foreach ($data['order_data'] as $value){
  976. $tmp = $map2[$value['id']] ?? [];
  977. $total = $tmp['dispatch_quantity'] ?? 0;
  978. $dispatch_no = $tmp['dispatch_no'] ?? "";
  979. $save = $map1[$value['id']] ?? 0;
  980. $save_total = bcadd($value['quantity'], $save, 2);
  981. if($save_total > $total) return [false, "派工单号:" . $dispatch_no . "的报工总数量不能超过" . $total . "吨,目前累计报工" . $save . "吨"];
  982. }
  983. return [true, ''];
  984. }
  985. public function setOrderNO2($time = 0){
  986. if(empty($time)) {
  987. $str = date('Ymd',time());
  988. }else{
  989. $str = date('Ymd', $time);
  990. }
  991. $model = new ReportWorking();
  992. $order_number = $model->where('order_number','Like','%'. $str . '%')
  993. ->max('order_number');
  994. if(empty($order_number)){
  995. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  996. $number = $str . $number;
  997. }else{
  998. $tmp = substr($order_number, -3);
  999. $tmp = $tmp + 1;
  1000. //超过999
  1001. if(strlen($tmp) > 3) return '';
  1002. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  1003. $number = $str . $number;
  1004. }
  1005. return $number;
  1006. }
  1007. public function applyOrderReportList($data){
  1008. if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!'];
  1009. if(empty($data['type'])) return [false, 'TYPE不能为空!'];
  1010. $model = ApplyOrder::where('del_time',0)
  1011. ->where('apply_time',">=",$data['apply_time'][0])
  1012. ->where('apply_time',"<=",$data['apply_time'][1])
  1013. ->where('type',$data['type'])
  1014. ->select('id','order_number','apply_time','mark','status','storehouse_title')
  1015. ->orderBy('id','desc');
  1016. if(isset($data['status'])) $model->where('status', $data['status']);
  1017. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){
  1018. $model->where('crt_time',">=",$data['crt_time'][0]);
  1019. $model->where('crt_time',"<=",$data['crt_time'][1]);
  1020. }
  1021. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  1022. if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
  1023. if(! empty($data['sale_order_number'])){
  1024. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  1025. $dispatch_id = DispatchSub::where('del_time',0)
  1026. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  1027. ->select('id')
  1028. ->get()->toArray();
  1029. $dispatch_id = array_column($dispatch_id,'id');
  1030. $detail = ApplyOrderDetail::where('del_time',0)
  1031. ->where('type', $data['type'])
  1032. ->whereIn('data_id', $dispatch_id)
  1033. ->select('apply_order_id')
  1034. ->get()->toArray();
  1035. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1036. }elseif($data['type'] == ApplyOrder::type_five){
  1037. $zj_id = ScrappCount::where('del_time',0)
  1038. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  1039. ->select('id')
  1040. ->get()->toArray();
  1041. $zj_id = array_column($zj_id,'id');
  1042. $detail = ApplyOrderDetail::where('del_time',0)
  1043. ->where('type', $data['type'])
  1044. ->whereIn('data_id', $zj_id)
  1045. ->select('apply_order_id')
  1046. ->get()->toArray();
  1047. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1048. }else{
  1049. $no = SaleOrdersProduct::where('del_time',0)
  1050. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  1051. ->select('order_no')
  1052. ->get()->toArray();
  1053. $no = array_unique(array_column($no,'order_no'));
  1054. $box_id = Box::where('del_time',0)
  1055. ->whereIn('top_order_no', $no)
  1056. ->select('id')
  1057. ->get()->toArray();
  1058. $box_id = array_column($box_id,'id');
  1059. $detail = ApplyOrderDetail::where('del_time',0)
  1060. ->where('type', $data['type'])
  1061. ->whereIn('data_id', $box_id)
  1062. ->select('apply_order_id')
  1063. ->get()->toArray();
  1064. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1065. }
  1066. }
  1067. if(! empty($data['dispatch_no'])){
  1068. $dispatch_id = DispatchSub::where('del_time',0)
  1069. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  1070. ->select('id')
  1071. ->get()->toArray();
  1072. $dispatch_id = array_column($dispatch_id,'id');
  1073. $detail = ApplyOrderDetail::where('del_time',0)
  1074. ->where('type', $data['type'])
  1075. ->whereIn('data_id', $dispatch_id)
  1076. ->select('apply_order_id')
  1077. ->get()->toArray();
  1078. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  1079. }
  1080. $list = $model->get()->toArray();
  1081. if(empty($list)) return [true, []];
  1082. $apply_id = array_column($list, 'id');
  1083. //明细数据
  1084. $result = ApplyOrderDetail::where('del_time',0)
  1085. ->whereIn('apply_order_id', $apply_id)
  1086. ->get()->toArray();
  1087. if(empty($result)) return [true, []];
  1088. //组织主表需要的数据
  1089. $list_map = $this->makeListMap($list, $result, $data['type']);
  1090. $data_id = array_unique(array_column($result, 'data_id'));
  1091. list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']);
  1092. $return = [];
  1093. $d_tmp = array_column($d,null,'id');
  1094. foreach ($result as $t){
  1095. $tmp = $d_tmp[$t['data_id']] ?? [];
  1096. $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
  1097. $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
  1098. $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
  1099. $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
  1100. if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
  1101. $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
  1102. }else{
  1103. $order_no = $tmp['order_no'] ?? "";
  1104. }
  1105. if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
  1106. $product_unit = "吨";
  1107. }elseif($t['type'] == ApplyOrder::type_four){
  1108. $product_unit = "只";
  1109. }else{
  1110. $product_unit = $t['product_unit'] ?? "";
  1111. }
  1112. $tmp = $list_map[$t['apply_order_id']] ?? [];
  1113. $return[] = [
  1114. 'order_number' => $tmp['order_number'],
  1115. 'sale_order_number' => $tmp['sale_order_number'],
  1116. 'apply_time' => $tmp['apply_time'],
  1117. 'mark' => $tmp['mark'],
  1118. 'status_title' => $tmp['status_title'],
  1119. 'storehouse_title' => $tmp['storehouse_title'],
  1120. 'id' => $t['data_id'] ?? 0,
  1121. // 'main_id' => $t['id'],
  1122. 'quantity' => $t['quantity'] ?? 0,
  1123. 'product_no' => $t['product_no'] ?? "",
  1124. 'product_title' => $t['product_title'] ?? "",
  1125. 'product_size' => $t['product_size'] ?? "",
  1126. 'product_unit' => $product_unit,
  1127. 'top_product_title' => $top_product_title,
  1128. 'top_product_no' => $top_product_no,
  1129. 'technology_name' => $technology_name, //颜色
  1130. 'wood_name' => $wood_name,
  1131. 'order_no' => $order_no,
  1132. ];
  1133. }
  1134. return [true, array_values($return)];
  1135. }
  1136. private function getDetailList($apply_id, $type){
  1137. $d = $d_no_map = [];
  1138. if($type == ApplyOrder::type_one){
  1139. $d = DispatchSub::where('del_time',0)
  1140. ->whereIn('id', $apply_id)
  1141. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  1142. ->get()->toArray();
  1143. $args = "";
  1144. foreach ($d as $value){
  1145. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  1146. }
  1147. $args = rtrim($args, 'OR ');
  1148. $d_no_map = [];
  1149. $d_no = DispatchSub::where('del_time',0)
  1150. ->whereRaw($args)
  1151. ->select('dispatch_no as order_no','order_product_id','crt_time')
  1152. ->get()->toArray();
  1153. foreach ($d_no as $value){
  1154. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  1155. 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'];
  1156. }else{
  1157. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  1158. }
  1159. }
  1160. }elseif ($type == ApplyOrder::type_two){
  1161. $d = DispatchSub::where('del_time',0)
  1162. ->whereIn('id', $apply_id)
  1163. ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
  1164. ->get()->toArray();
  1165. $args = "";
  1166. foreach ($d as $value){
  1167. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  1168. }
  1169. $args = rtrim($args, 'OR ');
  1170. $d_no_map = [];
  1171. $d_no = DispatchSub::where('del_time',0)
  1172. ->whereRaw($args)
  1173. ->select('dispatch_no as order_no','order_product_id','crt_time')
  1174. ->get()->toArray();
  1175. foreach ($d_no as $value){
  1176. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  1177. 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'];
  1178. }else{
  1179. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  1180. }
  1181. }
  1182. }elseif ($type == ApplyOrder::type_three){
  1183. $d = Box::where('del_time',0)
  1184. ->whereIn('id', $apply_id)
  1185. ->select('id', 'order_no')
  1186. ->get()->toArray();
  1187. }elseif ($type == ApplyOrder::type_four){
  1188. $d = Box::where('del_time',0)
  1189. ->whereIn('id', $apply_id)
  1190. ->select('id', 'order_no')
  1191. ->get()->toArray();
  1192. }elseif($type == ApplyOrder::type_five){
  1193. $d = ScrappCount::where('del_time',0)
  1194. ->whereIn('id', $apply_id)
  1195. ->select('id', 'order_number as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  1196. ->get()->toArray();
  1197. }
  1198. return [$d, $d_no_map];
  1199. }
  1200. private function makeListMap($data, $order, $type){
  1201. if(empty($data)) return [];
  1202. //申请单 所有的数据id
  1203. $order_map = $data_id = [];
  1204. foreach ($order as $value){
  1205. $order_map[$value['apply_order_id']][] = $value['data_id'];
  1206. $data_id[] = $value['data_id'];
  1207. }
  1208. if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
  1209. $map1 = DispatchSub::whereIn('id', array_unique($data_id))
  1210. ->pluck('out_order_no','id')
  1211. ->toArray();
  1212. }elseif($type == ApplyOrder::type_five){
  1213. $map3 = ScrappCount::whereIn('id', array_unique($data_id))
  1214. ->pluck('out_order_no','id')
  1215. ->toArray();
  1216. }else{
  1217. $map2 = Box::whereIn('id', array_unique($data_id))
  1218. ->pluck('top_order_no','id')
  1219. ->toArray();
  1220. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
  1221. ->pluck('out_order_no','order_no')
  1222. ->toArray();
  1223. }
  1224. foreach ($data as $key => $value){
  1225. $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
  1226. $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
  1227. $order_tmp = $order_map[$value['id']] ?? [];
  1228. $order_tmp = array_unique($order_tmp);
  1229. $sale_order_number = [];
  1230. if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
  1231. foreach ($order_tmp as $v){
  1232. $str = $map1[$v] ?? '';
  1233. if(empty($str)) continue;
  1234. $sale_order_number[] = $str;
  1235. }
  1236. $sale_order_number = array_unique($sale_order_number);
  1237. $sale_order_number = implode(',',$sale_order_number);
  1238. $data[$key]['sale_order_number'] = $sale_order_number;
  1239. }elseif ($type == ApplyOrder::type_five){
  1240. foreach ($order_tmp as $v){
  1241. $str = $map3[$v] ?? '';
  1242. if(empty($str)) continue;
  1243. $sale_order_number[] = $str;
  1244. }
  1245. $sale_order_number = array_unique($sale_order_number);
  1246. $sale_order_number = implode(',',$sale_order_number);
  1247. $data[$key]['sale_order_number'] = $sale_order_number;
  1248. }else{
  1249. foreach ($order_tmp as $v){
  1250. $m2_t = $map2[$v] ?? '';
  1251. $m2_t = $map2_fin[$m2_t] ?? "";
  1252. if(empty($m2_t)) continue;
  1253. $sale_order_number[] = $m2_t;
  1254. }
  1255. $sale_order_number = array_unique($sale_order_number);
  1256. $sale_order_number = implode(',',$sale_order_number);
  1257. $data[$key]['sale_order_number'] = $sale_order_number;
  1258. }
  1259. }
  1260. return array_column($data,null,'id');
  1261. }
  1262. public function editZj($data, $user){
  1263. list($status, $msg) = $this->editZjRule($data);
  1264. if(! $status) return [false, $msg];
  1265. $order_map = array_column($msg,null,'data_id');
  1266. try{
  1267. DB::beginTransaction();
  1268. $time = time();
  1269. $insert = [];
  1270. $max_id = $this->getDailyId();
  1271. list($status,$msg) = $this->limitingSendRequestBackg("quantity_id_" . $max_id);
  1272. if(! $status) return [false,$msg];
  1273. $get_order_number = $insert_wg = [];
  1274. foreach ($data['data'] as $value){
  1275. $order_t = $order_map[$value['data_id']] ?? [];
  1276. $order = $order_t['dispatch'] ?? [];
  1277. if(empty($value['waste'])){
  1278. $max_id += 1;
  1279. $order_number = date("Ymd") . str_pad($max_id,3,'0',STR_PAD_LEFT);
  1280. $get_order_number[] = $order_number;
  1281. $insert[] = [
  1282. 'sale_orders_product_id' => $order['sale_orders_product_id'],
  1283. 'order_product_id' => $order['order_product_id'],
  1284. 'dispatch_sub_id' => $order['id'],
  1285. 'out_order_no' => $order['out_order_no'],
  1286. 'order_no' => $order['order_no'],
  1287. 'customer_no' => $order['customer_no'],
  1288. 'customer_name' => $order['customer_name'],
  1289. 'product_no' => $order['product_no'],
  1290. 'product_title' => $order['product_title'],
  1291. 'product_size' => $order['product_size'],
  1292. 'product_unit' => $order['product_unit'],
  1293. 'technology_material' => $order['technology_material'],
  1294. 'technology_name' => $order['technology_name'],
  1295. 'wood_name' => $order['wood_name'],
  1296. 'price' => $order['price'],
  1297. 'process_mark' => $order['process_mark'],
  1298. 'table_body_mark' => $order['table_body_mark'],
  1299. 'table_header_mark' => $order['table_header_mark'],
  1300. 'crt_id' => $user['id'],
  1301. 'crt_time' => $time,
  1302. 'scrapp_num' => 0,
  1303. 'scrapp_id' => 0,
  1304. 'team_id' => $data['team_id'],
  1305. 'finished_id' => $user['id'],
  1306. 'equipment_id' => $data['equipment_id'],
  1307. 'order_number' => $order_number,
  1308. 'process_id' => $order['process_id'],
  1309. 'quantity' => $value['num'],
  1310. 'report_id' => $data['id'],
  1311. 'result' => 0,
  1312. 'warehouse_no' => "",
  1313. 'warehouse_title' => "",
  1314. 'daily_id' => $max_id,
  1315. ];
  1316. }else{
  1317. foreach ($value['waste'] as $v){
  1318. $max_id += 1;
  1319. $order_number = date("Ymd") . str_pad($max_id,3,'0',STR_PAD_LEFT);
  1320. $get_order_number[] = $order_number;
  1321. $insert[] = [
  1322. 'sale_orders_product_id' => $order['sale_orders_product_id'],
  1323. 'order_product_id' => $order['order_product_id'],
  1324. 'dispatch_sub_id' => $order['id'],
  1325. 'out_order_no' => $order['out_order_no'],
  1326. 'order_no' => $order['order_no'],
  1327. 'customer_no' => $order['customer_no'],
  1328. 'customer_name' => $order['customer_name'],
  1329. 'product_no' => $order['product_no'],
  1330. 'product_title' => $order['product_title'],
  1331. 'product_size' => $order['product_size'],
  1332. 'product_unit' => $order['product_unit'],
  1333. 'technology_material' => $order['technology_material'],
  1334. 'technology_name' => $order['technology_name'],
  1335. 'wood_name' => $order['wood_name'],
  1336. 'price' => $order['price'],
  1337. 'process_mark' => $order['process_mark'],
  1338. 'table_body_mark' => $order['table_body_mark'],
  1339. 'table_header_mark' => $order['table_header_mark'],
  1340. 'crt_id' => $user['id'],
  1341. 'crt_time' => $time,
  1342. 'scrapp_num' => $v['num'],
  1343. 'scrapp_id' => $v['scrapp_id'],
  1344. 'team_id' => $data['team_id'],
  1345. 'finished_id' => $user['id'],
  1346. 'equipment_id' => $data['equipment_id'],
  1347. 'order_number' => $order_number,
  1348. 'process_id' => $order['process_id'],
  1349. 'quantity' => $value['num'],
  1350. 'report_id' => $data['id'],
  1351. 'result' => $v['result'],
  1352. 'warehouse_no' => $v['warehouse_no'],
  1353. 'warehouse_title' => $v['warehouse_title'],
  1354. 'daily_id' => $max_id,
  1355. ];
  1356. }
  1357. }
  1358. if($value['num'] > 0) $insert_wg[] = [
  1359. 'dispatch_id' => $value['data_id'],
  1360. 'num' => $value['num']
  1361. ];
  1362. }
  1363. //质检单 不良品单 会记录报工单的id
  1364. if(! empty($insert)) ScrappCount::insert($insert);
  1365. //获取新增的单据
  1366. $result = ScrappCount::whereIn('order_number',$get_order_number)
  1367. ->select('scrapp_num as num','id','dispatch_sub_id','warehouse_no','warehouse_title')
  1368. ->get()->toArray();
  1369. //完工入库申请单 半成品
  1370. list($status, $msg) = $this->insertWg($insert_wg, $user);
  1371. if(! $status) return [false, $msg];
  1372. //获取写入的数据 写其它的业务逻辑
  1373. list($status, $msg) = $this->insertOther($result, $user, $time);
  1374. if(! $status) return [false, $msg];
  1375. //反写 根据质检单上的派工单id 反写上游单据
  1376. $this->writeDispatchQuantity(array_column($result,'dispatch_sub_id'));
  1377. DB::commit();
  1378. }catch (\Throwable $exception){
  1379. DB::rollBack();
  1380. return [false, $exception->getMessage() . '|' . $exception->getLine()];
  1381. }
  1382. return [true, ''];
  1383. }
  1384. public function editZjRule($data){
  1385. if(empty($data['id'])) return [false, '报工单ID不能为空'];
  1386. $order = ReportWorkingDetail::where('del_time',0)
  1387. ->where('report_working_id', $data['id'])
  1388. ->get()->toArray();
  1389. if(empty($order)) return [false, "报工单信息不存在或已被删除"];
  1390. $map = [];
  1391. foreach ($order as $value){
  1392. if(! isset($map[$value['data_id']])) $map[$value['data_id']] = $value['quantity'];
  1393. }
  1394. if(empty($data['team_id'])) return [false, '质检班组不能为空'];
  1395. if(empty($data['equipment_id'])) return [false, '设备不能为空'];
  1396. if(empty($data['data'])) return [false, '质检信息不能为空'];
  1397. $data_id = [];
  1398. foreach ($data['data'] as $value){
  1399. if(empty($value['data_id'])) return [false, 'dataID不能为空'];
  1400. if(! isset($value['num'])) return [false, 'num不能为空'];
  1401. $num = 0;
  1402. if(! empty($value['waste'])){
  1403. foreach ($value['waste'] as $v){
  1404. if(empty($v['num'])) return [false, "质检数量不能为空"];
  1405. if(empty($v['scrapp_id'])) return [false, "请选择质检原因"];
  1406. if(empty($v['result'])) return [false, '请选择质检产品处理结果'];
  1407. if($v['result'] == ReportWorking::type_two && (empty($v['warehouse_no']) || empty($v['warehouse_title']))) return [false, '请选择产品去向仓库'];
  1408. $num = bcadd($v['num'], $num, 3);
  1409. }
  1410. }
  1411. $total = bcadd($num, $value['num'],3);
  1412. $total2 = $map[$value['data_id']] ?? 0;
  1413. if($total > $total2) return [false, '总数量不能超过报工数量'];
  1414. $data_id[] = $value['data_id'];
  1415. }
  1416. $dispatch = DispatchSub::whereIn('id',$data_id)->get()->toArray();
  1417. $dispatch_map = array_column($dispatch,null,'id');
  1418. foreach ($order as $key => $value){
  1419. if(! in_array($value['data_id'], $data_id)){
  1420. unset($order[$key]);
  1421. continue;
  1422. }
  1423. $order[$key]['dispatch'] = $dispatch_map[$value['data_id']] ?? [];
  1424. }
  1425. return [true, array_values($order)];
  1426. }
  1427. public function getDailyId($time = 0)
  1428. {
  1429. if(empty($time)) $time = time();
  1430. $today_start = strtotime(date("Y-m-d 00:00:00",$time));
  1431. $today_end = strtotime(date("Y-m-d 23:59:59",$time));
  1432. // 获取今天的最大daily_id
  1433. $maxDailyId = ScrappCount::where('crt_time', '>=', $today_start)
  1434. ->where('crt_time', '<=', $today_end)
  1435. ->max(DB::raw('daily_id + 0'));
  1436. return $maxDailyId ?? 0;
  1437. }
  1438. public function insertWg($all_data, $user){
  1439. if(empty($all_data)) return [true, ''];
  1440. $dispatchList = DispatchSub::whereIn('id', array_column($all_data,'dispatch_id'))->get()->toArray();
  1441. //是否最后一道工序
  1442. $orders = OrdersProduct::whereIn('id', array_unique(array_column($dispatchList, 'order_product_id')))
  1443. ->select('id','process_id')
  1444. ->get()->toArray();
  1445. $map = [];
  1446. foreach ($orders as $value){
  1447. $p = explode(",", $value['process_id']);
  1448. $lastElement = end($p);
  1449. $map[$value['id']] = $lastElement;
  1450. }
  1451. $all_data_map = array_column($all_data,null,'dispatch_id');
  1452. $need_insert = [];
  1453. foreach ($dispatchList as $value){
  1454. $num = $all_data_map[$value['id']] ?? 0;
  1455. if(! $num) continue;
  1456. if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id']){
  1457. $tmp = [
  1458. 'id' => $value['id'],
  1459. 'quantity' => $num,
  1460. 'product_no' => $value['product_no'] ?? "",
  1461. 'product_title' => $value['product_title'] ?? "",
  1462. 'product_size' => $value['product_size'] ?? "",
  1463. 'product_unit' => $value['product_unit'] ?? "",
  1464. ];
  1465. $need_insert[] = $tmp;
  1466. }
  1467. }
  1468. //生成完工入库申请单
  1469. $service = new BoxService();
  1470. list($status, $msg) = $service->createWGSQ($need_insert, $user);
  1471. if(! $status) return [false, $msg];
  1472. return [true, ''];
  1473. }
  1474. public function insertOther($all_data, $user, $time){
  1475. try {
  1476. $result = [];
  1477. foreach ($all_data as $value){
  1478. if($value['result'] > 0) $result[] = $value;
  1479. }
  1480. if(empty($result)) return [true, ''];
  1481. $dispatchList = DispatchSub::whereIn('id', array_column($result,'dispatch_sub_id'))->get()->toArray();
  1482. $dispatch_map = array_column($dispatchList,null,'id');
  1483. //是否自动审核
  1484. $em = new EmployeeService();
  1485. $auto = $em->is_auto($user, "pg_auto");
  1486. $dispatch_no = (new DispatchService())->setOrderNO();
  1487. if(! $dispatch_no) return [false,'返工派工单号生成失败!'];
  1488. $prefix = substr($dispatch_no, 0, -3);// "20250611"
  1489. $startNum = (int)substr($dispatch_no, -3); // 7
  1490. $is_first = true;
  1491. $dispatch_main = $dispatch = $is_need_ll = $is_need_warehouse = [];
  1492. foreach ($result as $value){
  1493. //派工单
  1494. $t = $dispatch_map[$value['dispatch_sub_id']] ?? [];
  1495. if(! $is_first){
  1496. $startNum += 1;
  1497. $number = str_pad($startNum,3,'0',STR_PAD_LEFT);
  1498. $number = $prefix . $number;
  1499. $dispatch_no_t = $number;
  1500. }else{
  1501. $dispatch_no_t = $dispatch_no;
  1502. $is_first = false;
  1503. }
  1504. if($value['result'] == ReportWorking::type_one){
  1505. //重新入生产流程 需要生成返工派工单
  1506. }else{
  1507. //入仓库 那么需要生成质检入库申请单 生成领料申请单 生成返工派工单
  1508. //'id','dispatch_quantity as quantity',"product_no","order_product_id","product_no",'technology_name','crt_time'
  1509. $is_need_ll[] = $dispatch_no_t;
  1510. $is_need_warehouse[] = [
  1511. 'warehouse_no' => $value['warehouse_no'],
  1512. 'warehouse_title' => $value['warehouse_title'],
  1513. ];
  1514. }
  1515. $dispatch_main[] = ['dispatch_no' => $dispatch_no_t,'crt_time' => $time];
  1516. $dispatch[] = [
  1517. 'order_product_id' => $t['order_product_id'],
  1518. 'order_no' => $t['order_no'],
  1519. 'product_no' => $t['product_no'],
  1520. 'product_title' => $t['product_title'],
  1521. 'product_size' => $t['product_size'],
  1522. 'product_unit' => $t['product_unit'],
  1523. 'production_quantity' => $t['production_quantity'],
  1524. 'dispatch_quantity' => $value['num'],
  1525. 'technology_material' => $t['technology_material'],
  1526. 'technology_name' => $t['technology_name'],
  1527. 'wood_name' => $t['wood_name'],
  1528. 'process_mark' => $t['process_mark'],
  1529. 'table_body_mark' => $t['table_body_mark'],
  1530. 'table_header_mark' => $t['table_header_mark'],
  1531. 'crt_time' => $time,
  1532. 'status' => $auto,
  1533. 'dispatch_no' => $dispatch_no_t,
  1534. 'process_id' => $t['process_id'],
  1535. 'dispatch_time_start' => $t['dispatch_time_start'],
  1536. 'dispatch_time_end' => $t['dispatch_time_end'],
  1537. 'crt_id' => $user['id'],
  1538. 'sale_orders_product_id' => $t['sale_orders_product_id'],
  1539. 'out_order_no' => $t['out_order_no'],
  1540. 'out_order_no_time' => $t['out_order_no_time'],
  1541. 'price' => $t['price'],
  1542. 'customer_no' => $t['customer_no'],
  1543. 'customer_name' => $t['customer_name'],
  1544. 'pre_shipment_time' => $t['pre_shipment_time'],
  1545. 'team_id' => $t['team_id'],
  1546. 'device_id' => $t['device_id'],
  1547. 'type' => 1,
  1548. ];
  1549. }
  1550. Dispatch::insert($dispatch_main);
  1551. DispatchSub::insert($dispatch);
  1552. if(! empty($is_need_ll)){
  1553. //获取上一次插入订单的所有数据
  1554. $insert = DispatchSub::whereIn('dispatch_no', $is_need_ll)
  1555. ->where('crt_time',$time)
  1556. ->where('crt_id',$user['id'])
  1557. ->select('id','dispatch_quantity as quantity',"product_no")
  1558. ->get()->toArray();
  1559. //领料申请单
  1560. list($status, $msg) = (new DispatchService())->createSQ($insert, $user);
  1561. if(! $status) return [false, $msg];
  1562. //质检入库申请单 原材料
  1563. list($status, $msg) = $this->createZJSQ($insert, $user, $is_need_warehouse);
  1564. if(! $status) return [false, $msg];
  1565. }
  1566. return [true, ''];
  1567. }catch (\Throwable $exception){
  1568. return [false, $exception->getMessage()];
  1569. }
  1570. }
  1571. public function createZJSQ($insert_data, $user, $warehouse, $time = 0){
  1572. try {
  1573. DB::beginTransaction();
  1574. //是否自动审核质检入库申请单
  1575. $em = new EmployeeService();
  1576. $auto = $em->is_auto($user, "zjsq_auto");
  1577. $return = [];
  1578. $product_no = array_unique(array_column($insert_data,'product_no'));
  1579. //获取原料
  1580. $service = new FyyOrderService();
  1581. list($status, $msg) = $service->getProductDataFromSqlServer(['product_no' => $product_no], $user);
  1582. if($status) $return = $msg;
  1583. //组织原材料写入数据
  1584. $insert = [];
  1585. foreach ($insert_data as $key => $value){
  1586. $quantity = 0;
  1587. if(! empty($value['quantity'])) {
  1588. $quantity = $value['quantity'];
  1589. }elseif (! empty($value['dispatch_quantity'])){
  1590. $quantity = $value['dispatch_quantity'];
  1591. }
  1592. $t = $return[$value['product_no']] ?? [];
  1593. $tmp = [
  1594. 'id' => $value['id'],
  1595. 'quantity' => $quantity,
  1596. 'product_no' => "",
  1597. 'product_title' => "",
  1598. 'product_size' => "",
  1599. 'product_unit' => "",
  1600. 'storehouse_id' => $warehouse[$key]['warehouse_no'] ?? "",
  1601. 'storehouse_title' => $warehouse[$key]['warehouse_title'] ?? "",
  1602. ];
  1603. if(! empty($t)){
  1604. foreach ($t as $v){
  1605. $tmp['product_no'] = $v['product_no'];
  1606. $tmp['product_title'] = $v['product_title'];
  1607. $tmp['product_size'] = $v['product_size'];
  1608. $tmp['product_unit'] = $v['product_unit'];
  1609. $insert[] = $tmp;
  1610. }
  1611. }else{
  1612. $tmp['quantity'] = 0;
  1613. $insert[] = $tmp;
  1614. }
  1615. }
  1616. //生成质检入库申请单
  1617. $service = new ApplyOrderService();
  1618. list($status, $msg) = $service->createSqFive($insert, $user, ApplyOrder::type_five, $auto, $time);
  1619. if(! $status) {
  1620. DB::rollBack();
  1621. return [false, $msg];
  1622. }
  1623. DB::commit();
  1624. }catch (\Throwable $exception){
  1625. DB::rollBack();
  1626. return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
  1627. }
  1628. return [true, ''];
  1629. }
  1630. //反写
  1631. public function writeDispatchQuantity($dispatch_id){
  1632. $result = ScrappCount::where('del_time',0)
  1633. ->whereIn('dispatch_sub_id', $dispatch_id)
  1634. ->select('dispatch_sub_id','quantity','scrapp_num','crt_time','process_id','order_product_id')
  1635. ->get()->toArray();
  1636. //是否最后一道工序
  1637. $orders = OrdersProduct::whereIn('id', array_unique(array_column($result, 'order_product_id')))
  1638. ->select('id','process_id')
  1639. ->get()->toArray();
  1640. $map = [];
  1641. foreach ($orders as $value){
  1642. $p = explode(",", $value['process_id']);
  1643. $lastElement = end($p);
  1644. $map[$value['id']] = $lastElement;
  1645. }
  1646. //汇总这个派工单一共质检良品数量 但是同一时间下的 只取一次
  1647. $update = $flag = $need_update = [];
  1648. foreach ($result as $value){
  1649. if(isset($map[$value['order_product_id']]) && $map[$value['order_product_id']] == $value['process_id'] && ! in_array($value['order_product_id'], $need_update)){
  1650. $need_update[] = [
  1651. 'order_product_id' => $value['order_product_id'],
  1652. 'process_id' => $value['process_id'],
  1653. ];
  1654. }
  1655. $key = $value['dispatch_sub_id'] . $value['crt_time'];
  1656. if(isset($flag[$key])) continue;
  1657. if(isset($update[$value['dispatch_sub_id']])){
  1658. $tmp = bcadd($value['quantity'], $update[$value['dispatch_sub_id']], 3);
  1659. $update[$value['dispatch_sub_id']] = $tmp;
  1660. }else{
  1661. $update[$value['dispatch_sub_id']] = $value['quantity'];
  1662. }
  1663. $flag[$key] = 1;
  1664. }
  1665. unset($flag);
  1666. //更新派工单完工数量----------------------------------------------------
  1667. foreach ($update as $id => $num){
  1668. DispatchSub::where('id', $id)->update([
  1669. 'finished_num' => $num
  1670. ]);
  1671. }
  1672. if(! empty($need_update)){
  1673. //更新生产订单的完工数量--------------------------------------------------
  1674. $list = DispatchSub::where("del_time",0)
  1675. ->when(! empty($need_update), function ($query) use ($need_update) {
  1676. return $query->where(function ($q) use ($need_update) {
  1677. foreach ($need_update as $value) {
  1678. $order_product_id = $value['order_product_id'];
  1679. $process_id = $value['process_id'];
  1680. $q->orWhere(function ($subQ) use ($order_product_id, $process_id) {
  1681. $subQ->where('order_product_id', $order_product_id)
  1682. ->where('process_id', $process_id);
  1683. });
  1684. }
  1685. });
  1686. })
  1687. ->select('order_product_id', 'finished_num', 'sale_orders_product_id')
  1688. ->get()->toArray();
  1689. $map2 = [];
  1690. foreach ($list as $value){
  1691. if(isset($map2[$value['order_product_id']])){
  1692. $tmp = bcadd($map2[$value['order_product_id']], $value['finished_num'], 3);
  1693. $map2[$value['order_product_id']] = $tmp;
  1694. }else{
  1695. $map2[$value['order_product_id']] = $value['finished_num'];
  1696. }
  1697. }
  1698. foreach ($map2 as $id => $num){
  1699. OrdersProduct::where('id', $id)->update([
  1700. 'finished_num' => $num
  1701. ]);
  1702. }
  1703. //更新销售订单完工数量-------------------------------------------------------
  1704. $list2 = OrdersProduct::where('del_time',0)
  1705. ->whereIn('sale_orders_product_id', array_unique(array_column($list, 'sale_orders_product_id')))
  1706. ->select('finished_num','sale_orders_product_id')
  1707. ->get()->toArray();
  1708. $map3 = [];
  1709. foreach ($list2 as $value){
  1710. if(isset($map3[$value['sale_orders_product_id']])){
  1711. $tmp = bcadd($map3[$value['sale_orders_product_id']], $value['finished_num'], 3);
  1712. $map3[$value['sale_orders_product_id']] = $tmp;
  1713. }else{
  1714. $map3[$value['sale_orders_product_id']] = $value['finished_num'];
  1715. }
  1716. }
  1717. foreach ($map3 as $id => $num){
  1718. SaleOrdersProduct::where('id', $id)->update([
  1719. 'finished_num' => $num
  1720. ]);
  1721. }
  1722. }
  1723. }
  1724. public function orderSearchList($data){
  1725. if(empty($data['type'])) return [false, '单据联查类型不能为空'];
  1726. if(empty($data['data_id'])) return [false, '依据单据ID不能为空'];
  1727. //不管进来的是什么单据id 转换为销售订单
  1728. $sales_id = $this->changeIdToOrder($data);
  1729. list($status, $msg) = $this->forSearch($data, $sales_id);
  1730. return [$status, $msg];
  1731. }
  1732. private function changeIdToOrder($data)
  1733. {
  1734. $id = $data['id'];
  1735. $search_id = $result = [];
  1736. if ($data['type'] == 1) {
  1737. $result[] = [
  1738. 'id' => $id
  1739. ];
  1740. } elseif ($data['type'] == 2) {
  1741. $result = OrdersProduct::where('del_time', 0)
  1742. ->where('id', $id)
  1743. ->select('sale_orders_product_id as id')
  1744. ->get()->toArray();
  1745. } elseif ($data['type'] == 3) {
  1746. $result = DispatchSub::where('del_time', 0)
  1747. ->where('id', $data['data_id'])
  1748. ->select('sale_orders_product_id as id')
  1749. ->get()->toArray();
  1750. } elseif ($data['type'] == 4) {
  1751. $detail = ReportWorkingDetail::where('data_id', $id)
  1752. ->where('del_time', 0)
  1753. ->select('data_id')
  1754. ->get()->toArray();
  1755. $result = DispatchSub::where('del_time', 0)
  1756. ->whereIn('id', array_unique(array_column($detail, 'data_id')))
  1757. ->select('sale_orders_product_id as id')
  1758. ->get()->toArray();
  1759. } elseif ($data['type'] == 5) {
  1760. $result = DispatchSub::where('del_time', 0)
  1761. ->where('id', $id)
  1762. ->where('finished_num', '>', 0)
  1763. ->select('sale_orders_product_id as id')
  1764. ->get()->toArray();
  1765. } elseif ($data['type'] == 6) {
  1766. $first = Box::where('del_time', 0)
  1767. ->where('id', $id)
  1768. ->select("top_order_no", 'order_no')
  1769. ->first();
  1770. if (!empty($first)) {
  1771. $first = $first->toArray();
  1772. $box_detail = new BoxDetail(['channel' => $first['top_order_no']]);
  1773. $result = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();
  1774. }
  1775. } elseif ($data['type'] == 7) {
  1776. $result = ScrappCount::where('del_time', 0)
  1777. ->where('id', $id)
  1778. ->select('sale_orders_product_id as id')
  1779. ->get()->toArray();
  1780. } elseif ($data['type'] == 8) {
  1781. $result = ScrappCount::where('del_time', 0)
  1782. ->where('id', $id)
  1783. ->where('scrapp_num', '>', 0)
  1784. ->select('sale_orders_product_id as id')
  1785. ->get()->toArray();
  1786. } elseif ($data['type'] == 9) {
  1787. $detail = ApplyOrderDetail::where('del_time', 0)
  1788. ->where('apply_order_id', $id)
  1789. ->where('type', ApplyOrder::type_one)
  1790. ->select('data_id as dispatch_id')
  1791. ->get()->toArray();
  1792. $result = DispatchSub::where('del_time', 0)
  1793. ->where('id', array_unique(array_column($detail, 'dispatch_id')))
  1794. ->select('sale_orders_product_id as id')
  1795. ->get()->toArray();
  1796. } elseif ($data['type'] == 10) {
  1797. $detail = ApplyOrderDetail::where('del_time', 0)
  1798. ->where('apply_order_id', $id)
  1799. ->where('type', ApplyOrder::type_two)
  1800. ->select('data_id as dispatch_id')
  1801. ->get()->toArray();
  1802. $result = DispatchSub::where('del_time', 0)
  1803. ->where('id', array_unique(array_column($detail, 'dispatch_id')))
  1804. ->select('sale_orders_product_id as id')
  1805. ->get()->toArray();
  1806. } elseif ($data['type'] == 11) {
  1807. $detail = ApplyOrderDetail::where('del_time', 0)
  1808. ->where('apply_order_id', $id)
  1809. ->where('type', ApplyOrder::type_four)
  1810. ->select('data_id as box_id')
  1811. ->get()->toArray();
  1812. $first = Box::where('del_time', 0)
  1813. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  1814. ->select("top_order_no", 'order_no')
  1815. ->get()->toArray();
  1816. if (!empty($first)) {
  1817. foreach ($first as $value) {
  1818. $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
  1819. $t = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();
  1820. $result = array_merge($result, $t);
  1821. }
  1822. }
  1823. } elseif ($data['type'] == 12) {
  1824. $detail = ApplyOrderDetail::where('del_time', 0)
  1825. ->where('apply_order_id', $id)
  1826. ->where('type', ApplyOrder::type_three)
  1827. ->select('data_id as box_id')
  1828. ->get()->toArray();
  1829. $first = Box::where('del_time', 0)
  1830. ->whereIn('id', array_unique(array_column($detail, 'box_id')))
  1831. ->select("top_order_no", 'order_no')
  1832. ->get()->toArray();
  1833. if (!empty($first)) {
  1834. foreach ($first as $value) {
  1835. $box_detail = new BoxDetail(['channel' => $value['top_order_no']]);
  1836. $t = $box_detail->where('order_no', $first['order_no'])->selct('top_id as id')->get()->toArray();
  1837. $result = array_merge($result, $t);
  1838. }
  1839. }
  1840. } elseif ($data['type'] == 13) {
  1841. $detail = ApplyOrderDetail::where('del_time', 0)
  1842. ->where('apply_order_id', $id)
  1843. ->where('type', ApplyOrder::type_five)
  1844. ->select('data_id as zj_id')
  1845. ->get()->toArray();
  1846. $result = ScrappCount::where('del_time', 0)
  1847. ->whereIn('id', array_unique(array_column($detail, 'zj_id')))
  1848. ->select('sale_orders_product_id as id')
  1849. ->get()->toArray();
  1850. }
  1851. foreach ($result as $value) {
  1852. if (!in_array($value['id'], $search_id)) {
  1853. $search_id[] = $value['id'];
  1854. }
  1855. }
  1856. return $search_id;
  1857. }
  1858. private function forSearch($data, $sales_id){
  1859. $result = [];
  1860. //通过销售订单查找所有业务单据
  1861. if($data['for_type'] == 1){
  1862. $result = SaleOrdersProduct::where('del_time',0)
  1863. ->whereIn('id', $sales_id)
  1864. ->select('id', 'out_order_no as order_no')
  1865. ->get()->toArray();
  1866. }elseif($data['for_type'] == 2){
  1867. $result = OrdersProduct::where('del_time',0)
  1868. ->whereIn('sale_orders_product_id', $sales_id)
  1869. ->select('id', 'production_no as order_no')
  1870. ->get()->toArray();
  1871. }elseif($data['for_type'] == 3){
  1872. $result = DispatchSub::where('del_time',0)
  1873. ->whereIn('sale_orders_product_id', $sales_id)
  1874. ->select('id', 'dispatch_no as order_no')
  1875. ->get()->toArray();
  1876. }elseif($data['for_type'] == 4){
  1877. $dispatch = DispatchSub::where('del_time',0)
  1878. ->whereIn('sale_orders_product_id', $sales_id)
  1879. ->select('id')
  1880. ->get()->toArray();
  1881. $detail = ReportWorkingDetail::whereIn('data_id', array_column($dispatch,'id'))
  1882. ->where('del_time',0)
  1883. ->select('data_id')
  1884. ->get()->toArray();
  1885. $result = ReportWorking::where('del_time',0)
  1886. ->whereIn('id', array_unique(array_column($detail,'data_id')))
  1887. ->select('id', 'order_number as order_no')
  1888. ->get()->toArray();
  1889. }elseif($data['for_type'] == 5){
  1890. $result = DispatchSub::where('del_time',0)
  1891. ->whereIn('sale_orders_product_id', $sales_id)
  1892. ->where('finished_num','>',0)
  1893. ->select('id', 'dispatch_no as order_no')
  1894. ->get()->toArray();
  1895. }elseif($data['for_type'] == 6){
  1896. $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
  1897. foreach ($sale as $value){
  1898. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  1899. $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();
  1900. $tmp = Box::where('del_time',0)
  1901. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  1902. ->select("id","order_no")
  1903. ->get()->toArray();
  1904. $result = array_merge($result, $tmp);
  1905. }
  1906. }elseif ($data['for_type'] == 7){
  1907. $result = ScrappCount::where('del_time',0)
  1908. ->whereIn('sale_orders_product_id', $sales_id)
  1909. ->select('id', 'order_number as order_no')
  1910. ->get()->toArray();
  1911. }elseif ($data['for_type'] == 8){
  1912. $result = ScrappCount::where('del_time',0)
  1913. ->whereIn('sale_orders_product_id', $sales_id)
  1914. ->where('scrapp_num','>', 0)
  1915. ->select('id', 'order_number as order_no')
  1916. ->get()->toArray();
  1917. }elseif ($data['for_type'] == 9){
  1918. $dispatch = DispatchSub::where('del_time',0)
  1919. ->whereIn('sale_orders_product_id', $sales_id)
  1920. ->select('id')
  1921. ->get()->toArray();
  1922. $detail = ApplyOrderDetail::where('del_time',0)
  1923. ->whereIn('data_id', array_column($dispatch,'id'))
  1924. ->where('type',ApplyOrder::type_one)
  1925. ->select('apply_order_id')
  1926. ->get()->toArray();
  1927. $result = ApplyOrder::where('del_time',0)
  1928. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  1929. ->select('id','order_number as order_no')
  1930. ->get()->toArray();
  1931. }elseif ($data['for_type'] == 10){
  1932. $dispatch = DispatchSub::where('del_time',0)
  1933. ->whereIn('sale_orders_product_id', $sales_id)
  1934. ->select('id')
  1935. ->get()->toArray();
  1936. $detail = ApplyOrderDetail::where('del_time',0)
  1937. ->whereIn('data_id', array_column($dispatch,'id'))
  1938. ->where('type',ApplyOrder::type_two)
  1939. ->select('apply_order_id')
  1940. ->get()->toArray();
  1941. $result = ApplyOrder::where('del_time',0)
  1942. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  1943. ->select('id','order_number as order_no')
  1944. ->get()->toArray();
  1945. }elseif($data['for_type'] == 11){
  1946. $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
  1947. $ids = [];
  1948. foreach ($sale as $value){
  1949. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  1950. $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();
  1951. $tmp = Box::where('del_time',0)
  1952. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  1953. ->select("id")
  1954. ->get()->toArray();
  1955. $ids = array_merge($ids, $tmp);
  1956. }
  1957. $detail = ApplyOrderDetail::where('del_time',0)
  1958. ->whereIn('data_id', array_column($ids,'id'))
  1959. ->where('type',ApplyOrder::type_four)
  1960. ->select('apply_order_id')
  1961. ->get()->toArray();
  1962. $result = ApplyOrder::where('del_time',0)
  1963. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  1964. ->select('id','order_number as order_no')
  1965. ->get()->toArray();
  1966. }elseif($data['for_type'] == 12){
  1967. $sale = SaleOrdersProduct::whereIn('id', $sales_id)->select('order_no')->get()->toArray();
  1968. $ids = [];
  1969. foreach ($sale as $value){
  1970. $box_detail = new BoxDetail(['channel'=> $value['order_no']]);
  1971. $box_detail_order = $box_detail->where('top_id',$data['data_id'])->selct('order_no')->get()->toArray();
  1972. $tmp = Box::where('del_time',0)
  1973. ->whereIn('order_no',array_unique(array_column($box_detail_order, 'order_no')))
  1974. ->select("id")
  1975. ->get()->toArray();
  1976. $ids = array_merge($ids, $tmp);
  1977. }
  1978. $detail = ApplyOrderDetail::where('del_time',0)
  1979. ->whereIn('data_id', array_column($ids,'id'))
  1980. ->where('type',ApplyOrder::type_three)
  1981. ->select('apply_order_id')
  1982. ->get()->toArray();
  1983. $result = ApplyOrder::where('del_time',0)
  1984. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  1985. ->select('id','order_number as order_no')
  1986. ->get()->toArray();
  1987. }elseif($data['for_type'] == 13){
  1988. $zj_id = ScrappCount::where('del_time',0)
  1989. ->where('scrapp_num','>',0)
  1990. ->whereIn('sale_orders_product_id', $sales_id)
  1991. ->select('id')
  1992. ->get()->toArray();
  1993. $detail = ApplyOrderDetail::where('del_time',0)
  1994. ->whereIn('data_id', array_column($zj_id,'id'))
  1995. ->where('type',ApplyOrder::type_five)
  1996. ->select('apply_order_id')
  1997. ->get()->toArray();
  1998. $result = ApplyOrder::where('del_time',0)
  1999. ->whereIn('id', array_unique(array_column($detail,'apply_order_id')))
  2000. ->select('id','order_number as order_no')
  2001. ->get()->toArray();
  2002. }
  2003. return [true, $result];
  2004. }
  2005. }