ApplyOrderService.php 101 KB

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