ApplyOrderService.php 97 KB

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