ApplyOrderService.php 98 KB

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