ApplyOrderService.php 101 KB

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