ApplyOrderService.php 97 KB

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