ApplyOrderService.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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\DispatchSub;
  8. use App\Model\Employee;
  9. use App\Model\EmployeeTeamPermission;
  10. use App\Model\InOutRecord;
  11. use App\Model\Process;
  12. use App\Model\ReportWorking;
  13. use App\Model\ReportWorkingDetail;
  14. use App\Model\SaleOrdersProduct;
  15. use App\Model\Team;
  16. use Illuminate\Support\Facades\DB;
  17. class ApplyOrderService extends Service
  18. {
  19. public function MaterialEdit($data, $user){
  20. list($status,$msg) = $this->MaterialRule($data,false);
  21. if(!$status) return [$status,$msg];
  22. DB::beginTransaction();
  23. try{
  24. $time = time();
  25. $model = new ApplyOrder();
  26. $model = $model->where('id',$data['id'])->first();
  27. $model->apply_id = $data['apply_id'];
  28. $model->apply_time = $data['apply_time'] ;
  29. $model->mark = $data['mark'] ?? "";
  30. $model->storehouse_id = $data['storehouse_id'];
  31. $model->storehouse_title = $data['storehouse_title'] ?? "";
  32. $model->type = $data['type'];
  33. $model->save();
  34. $id = $model->id;
  35. ApplyOrderDetail::where('del_time',0)->where('apply_order_id',$id)->update([
  36. 'del_time' => $time
  37. ]);
  38. $detail_insert = [];
  39. foreach ($data['order_data'] as $v){
  40. $detail_insert[] = [
  41. 'apply_order_id' => $id,
  42. 'data_id' => $v['id'],
  43. 'quantity' => $v['quantity'] ?? 0,
  44. 'product_no' => $v['product_no'] ?? "",
  45. 'product_title' => $v['product_title'] ?? "",
  46. 'product_size' => $v['product_size'] ?? "",
  47. 'product_unit' => $v['product_unit'] ?? "",
  48. 'technology_name' => $v['technology_name'] ?? "", //颜色
  49. 'top_product_title' => $v['top_product_title'] ?? "",
  50. 'top_product_no' => $v['top_product_title'] ?? "",
  51. 'type' => $data['type'],
  52. 'storehouse_id' => $data['storehouse_id'],
  53. 'crt_time' => $time,
  54. ];
  55. }
  56. ApplyOrderDetail::insert($detail_insert);
  57. DB::commit();
  58. }catch (\Exception $e){
  59. DB::rollBack();
  60. return [false,$e->getMessage()];
  61. }
  62. return [true,''];
  63. }
  64. public function MaterialAdd($data,$user){
  65. list($status,$msg) = $this->MaterialRule($data);
  66. if(!$status) return [$status,$msg];
  67. DB::beginTransaction();
  68. try{
  69. $time = time();
  70. $model = new ApplyOrder();
  71. $model->order_number = $data['order_number'];
  72. $model->apply_id = $data['apply_id'];
  73. $model->apply_time = $data['apply_time'] ;
  74. $model->mark = $data['mark'] ?? "";
  75. $model->storehouse_id = $data['storehouse_id'];
  76. $model->storehouse_title = $data['storehouse_title'] ?? "";
  77. $model->type = $data['type'];
  78. $model->crt_id = $user['id'];
  79. $model->save();
  80. $id = $model->id;
  81. $detail_insert = [];
  82. foreach ($data['order_data'] as $v){
  83. $detail_insert[] = [
  84. 'apply_order_id' => $id,
  85. 'data_id' => $v['id'],
  86. 'quantity' => $v['quantity'] ?? 0,
  87. 'product_no' => $v['product_no'] ?? "",
  88. 'product_title' => $v['product_title'] ?? "",
  89. 'product_size' => $v['product_size'] ?? "",
  90. 'product_unit' => $v['product_unit'] ?? "",
  91. 'technology_name' => $v['technology_name'] ?? "", //颜色
  92. 'top_product_title' => $v['top_product_title'] ?? "",
  93. 'top_product_no' => $v['top_product_title'] ?? "",
  94. 'type' => $data['type'],
  95. 'storehouse_id' => $data['storehouse_id'],
  96. 'crt_time' => $time,
  97. ];
  98. }
  99. ApplyOrderDetail::insert($detail_insert);
  100. DB::commit();
  101. }catch (\Exception $e){
  102. DB::rollBack();
  103. return [false,$e->getMessage()];
  104. }
  105. return [true, ''];
  106. }
  107. public function MaterialDel($data){
  108. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  109. $apply = ApplyOrder::where('id',$data['id'])->first();
  110. if($apply->del_time > 0) return [false,'申请单不存在或已被删除'];
  111. if($apply->state != ApplyOrder::state_zero) return [false, '申请单已审核,删除失败'];
  112. $time = time();
  113. try {
  114. DB::beginTransaction();
  115. $apply->del_time = $time;
  116. $apply->save();
  117. ApplyOrderDetail::where('del_time', 0)->where('apply_order_id',$data['id'])->update([
  118. 'del_time'=>time()
  119. ]);
  120. DB::commit();
  121. }catch (\Throwable $exception){
  122. DB::rollBack();
  123. return [false, $exception->getMessage()];
  124. }
  125. return [true,''];
  126. }
  127. public function MaterialList($data){
  128. $model = ApplyOrder::where('del_time',0)
  129. ->select('*')
  130. ->orderBy('id','desc');
  131. if(isset($data['status'])) $model->where('status', $data['status']);
  132. if(! empty($data['type'])) $model->where('type', $data['type']);
  133. if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
  134. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  135. if(! empty($data['apply_id'])) $model->where('apply_id', $data['apply_id']);
  136. if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
  137. if(! empty($data['apply_time'][0]) && ! empty($data['apply_time'][1])) $model->whereBetween('apply_time',[$data['apply_time'][0],$data['apply_time'][1]]);
  138. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  139. if(! empty($data['dispatch_no'])){
  140. $dispatch_id = DispatchSub::where('del_time',0)
  141. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  142. ->select('id')
  143. ->get()->toArray();
  144. $dispatch_id = array_column($dispatch_id,'id');
  145. $detail = ApplyOrderDetail::where('del_time',0)
  146. ->where('type', $data['type'])
  147. ->whereIn('data_id', $dispatch_id)
  148. ->select('apply_order_id')
  149. ->get()->toArray();
  150. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  151. }
  152. if(! empty($data['box_no'])){
  153. $box_id = Box::where('del_time',0)
  154. ->where('order_no', 'LIKE', '%'.$data['box_no'].'%')
  155. ->select('id')
  156. ->get()->toArray();
  157. $box_id = array_column($box_id,'id');
  158. $detail = ApplyOrderDetail::where('del_time',0)
  159. ->where('type', $data['type'])
  160. ->whereIn('data_id', $box_id)
  161. ->select('apply_order_id')
  162. ->get()->toArray();
  163. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  164. }
  165. if(! empty($data['sale_order_number'])){
  166. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  167. $dispatch_id = DispatchSub::where('del_time',0)
  168. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  169. ->select('id')
  170. ->get()->toArray();
  171. $dispatch_id = array_column($dispatch_id,'id');
  172. $detail = ApplyOrderDetail::where('del_time',0)
  173. ->where('type', $data['type'])
  174. ->whereIn('data_id', $dispatch_id)
  175. ->select('apply_order_id')
  176. ->get()->toArray();
  177. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  178. }else{
  179. $no = SaleOrdersProduct::where('del_time',0)
  180. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  181. ->select('order_no')
  182. ->get()->toArray();
  183. $no = array_unique(array_column($no,'order_no'));
  184. $box_id = Box::where('del_time',0)
  185. ->whereIn('top_order_no', $no)
  186. ->select('id')
  187. ->get()->toArray();
  188. $box_id = array_column($box_id,'id');
  189. $detail = ApplyOrderDetail::where('del_time',0)
  190. ->where('type', $data['type'])
  191. ->whereIn('data_id', $box_id)
  192. ->select('apply_order_id')
  193. ->get()->toArray();
  194. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  195. }
  196. }
  197. $list = $this->limit($model,'',$data);
  198. $list = $this->fillData($list);
  199. return [true,$list];
  200. }
  201. public function fillData($data){
  202. if(empty($data['data'])) return $data;
  203. $emp = Employee::whereIn('id',array_merge_recursive(array_unique(array_column($data['data'],'crt_id')), array_unique(array_column($data['data'],'apply_id'))))
  204. ->pluck('emp_name','id')
  205. ->toArray();
  206. $array1 = $array2 = [];
  207. foreach ($data['data'] as $value){
  208. if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  209. $array1[] = $value['id'];
  210. }else{
  211. $array2[] = $value['id'];
  212. }
  213. }
  214. $order1 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])
  215. ->whereIn('apply_order_id', $array1)
  216. ->select('data_id','apply_order_id')
  217. ->get()->toArray();
  218. $order1_map = [];
  219. foreach ($order1 as $value){
  220. $order1_map[$value['apply_order_id']][] = $value['data_id'];
  221. }
  222. $order2 = ApplyOrderDetail::whereNotIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])
  223. ->whereIn('apply_order_id', $array2)
  224. ->select('data_id','apply_order_id')
  225. ->get()->toArray();
  226. $order2_map = [];
  227. foreach ($order2 as $value){
  228. $order2_map[$value['apply_order_id']][] = $value['data_id'];
  229. }
  230. $map1 = DispatchSub::whereIn('id', array_unique(array_column($order1,'data_id')))
  231. ->pluck('out_order_no','id')
  232. ->toArray();
  233. $map2 = Box::whereIn('id', array_unique(array_column($order2,'data_id')))
  234. ->pluck('top_order_no','id')
  235. ->toArray();
  236. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
  237. ->pluck('out_order_no','order_no')
  238. ->toArray();
  239. foreach ($data['data'] as $key => $value){
  240. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  241. $data['data'][$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
  242. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  243. $data['data'][$key]['apply_name'] = $emp[$value['apply_id']] ?? '';
  244. if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  245. $order_tmp = $order1_map[$value['id']] ?? [];
  246. $order_tmp = array_unique($order_tmp);
  247. $sale_order_number = [];
  248. foreach ($order_tmp as $v){
  249. $str = $map1[$v] ?? '';
  250. if(empty($str)) continue;
  251. $sale_order_number[] = $str;
  252. }
  253. $sale_order_number = array_unique($sale_order_number);
  254. $sale_order_number = implode(',',$sale_order_number);
  255. $data['data'][$key]['sale_order_number'] = $sale_order_number;
  256. }else{
  257. $order_tmp = $order2_map[$value['id']] ?? [];
  258. $order_tmp = array_unique($order_tmp);
  259. $sale_order_number = [];
  260. foreach ($order_tmp as $v){
  261. $m2_t = $map2[$v] ?? '';
  262. $m2_t = $map2_fin[$m2_t] ?? "";
  263. if(empty($m2_t)) continue;
  264. $sale_order_number[] = $m2_t;
  265. }
  266. $sale_order_number = array_unique($sale_order_number);
  267. $sale_order_number = implode(',',$sale_order_number);
  268. $data['data'][$key]['sale_order_number'] = $sale_order_number;
  269. }
  270. }
  271. return $data;
  272. }
  273. public function MaterialDetail($data){
  274. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  275. $id = $data['id'];
  276. $detail = ApplyOrder::where('del_time',0)
  277. ->where('id',$id)
  278. ->first();
  279. if(empty($detail)) return [false,'申请单不存在或已被删除'];
  280. $detail = $detail->toArray();
  281. $detail['apply_title'] = Employee::where('id', $detail['apply_id'])->value('emp_name');
  282. $apply_d = ApplyOrderDetail::where('del_time', 0)
  283. ->where('apply_order_id', $id)
  284. ->get()->toArray();
  285. $apply_id = array_column($apply_d, 'data_id');
  286. $d = [];
  287. if($detail['type'] == ApplyOrder::type_one){
  288. $d = DispatchSub::where('del_time',0)
  289. ->whereIn('id', $apply_id)
  290. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  291. ->get()->toArray();
  292. $args = "";
  293. foreach ($d as $value){
  294. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  295. }
  296. $args = rtrim($args, 'OR ');
  297. $d_no_map = [];
  298. $d_no = DispatchSub::where('del_time',0)
  299. ->whereRaw($args)
  300. ->select('dispatch_no as order_no','order_product_id','crt_time')
  301. ->get()->toArray();
  302. foreach ($d_no as $value){
  303. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  304. 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'];
  305. }else{
  306. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  307. }
  308. }
  309. }elseif ($detail['type'] == ApplyOrder::type_two){
  310. $d = DispatchSub::where('del_time',0)
  311. ->whereIn('id', $apply_id)
  312. ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
  313. ->get()->toArray();
  314. $args = "";
  315. foreach ($d as $value){
  316. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  317. }
  318. $args = rtrim($args, 'OR ');
  319. $d_no_map = [];
  320. $d_no = DispatchSub::where('del_time',0)
  321. ->whereRaw($args)
  322. ->select('dispatch_no as order_no','order_product_id','crt_time')
  323. ->get()->toArray();
  324. foreach ($d_no as $value){
  325. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  326. 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'];
  327. }else{
  328. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  329. }
  330. }
  331. }elseif ($detail['type'] == ApplyOrder::type_three){
  332. $d = Box::where('del_time',0)
  333. ->whereIn('id', $apply_id)
  334. ->select('id', 'order_no')
  335. ->get()->toArray();
  336. }elseif ($detail['type'] == ApplyOrder::type_four){
  337. $d = Box::where('del_time',0)
  338. ->whereIn('id', $apply_id)
  339. ->select('id', 'order_no')
  340. ->get()->toArray();
  341. }
  342. $d_tmp = array_column($d,null,'id');
  343. $return = [];
  344. foreach ($apply_d as $t){
  345. $tmp = $d_tmp[$t['data_id']] ?? [];
  346. $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
  347. $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
  348. $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
  349. $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
  350. if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
  351. $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
  352. }else{
  353. $order_no = $tmp['order_no'] ?? "";
  354. }
  355. if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
  356. $product_unit = "吨";
  357. }else{
  358. $product_unit = $t['product_unit'] ?? "";
  359. }
  360. $return[] = [
  361. 'id' => $t['data_id'] ?? 0,
  362. 'quantity' => $t['quantity'] ?? 0,
  363. 'product_no' => $t['product_no'] ?? "",
  364. 'product_title' => $t['product_title'] ?? "",
  365. 'product_size' => $t['product_size'] ?? "",
  366. 'product_unit' => $product_unit,
  367. 'top_product_title' => $top_product_title,
  368. 'top_product_no' => $top_product_no,
  369. 'technology_name' => $technology_name, //颜色
  370. 'wood_name' => $wood_name,
  371. 'order_no' => $order_no,
  372. ];
  373. }
  374. $detail['order_data'] = $return;
  375. return [true, $detail];
  376. }
  377. public function MaterialRule(&$data,$is_add = true){
  378. if($this->isEmpty($data,'apply_id')) return [false,'申请人不能为空'];
  379. if($this->isEmpty($data,'apply_time')) return [false,'申请时间不能为空'];
  380. if($this->isEmpty($data,'storehouse_id')) return [false,'仓库不能为空'];
  381. if($this->isEmpty($data,'type')) return [false,'申请单类型不能为空'];
  382. if(empty($data['order_data'])) return [false, '申请单详细信息不能为空'];
  383. foreach ($data['order_data'] as $value){
  384. if(empty($value['id'])) return [false, '申请单详细信息ID不能为空'];
  385. if($data['type'] != ApplyOrder::type_three){
  386. if(empty($value['quantity'])) return [false, '申请单详细信息数量不能为空'];
  387. }
  388. }
  389. $id = array_unique(array_column($data['order_data'],'id'));
  390. if($data['type'] == ApplyOrder::type_one){
  391. $dispatch = DispatchSub::where('del_time',0)
  392. ->whereIn('id', $id)
  393. ->select('id', 'dispatch_no', 'status')
  394. ->get()->toArray();
  395. if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
  396. }elseif ($data['type'] == ApplyOrder::type_two){
  397. $dispatch = DispatchSub::where('del_time',0)
  398. ->whereIn('id', $id)
  399. ->select('id', 'dispatch_no', 'status')
  400. ->get()->toArray();
  401. if(count($dispatch) != count($id)) return [false, '完工单信息错误,请重新选择'];
  402. }elseif ($data['type'] == ApplyOrder::type_three){
  403. $box = Box::where('del_time',0)
  404. ->whereIn('id', $id)
  405. ->select('id', 'order_no', 'status')
  406. ->get()->toArray();
  407. if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
  408. }elseif ($data['type'] == ApplyOrder::type_four){
  409. $box = Box::where('del_time',0)
  410. ->whereIn('id', $id)
  411. ->select('id', 'order_no', 'status')
  412. ->get()->toArray();
  413. if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
  414. }else{
  415. return [false, '申请单类型错误'];
  416. }
  417. if($is_add){
  418. $data['order_number'] = $this->setOrderNO($data['type']);
  419. if(empty($data['order_number'])) return [false, '申请单唯一标识生成失败'];
  420. }else{
  421. if(empty($data['id'])) return [false, '申请单唯一标识不能为空'];
  422. if(empty($data['order_number'])) return [false, '申请单唯一标识不能为空'];
  423. $apply = ApplyOrder::where('id', $data['id'])->where('del_time', 0)->first();
  424. if(empty($apply)) return [false, '申请单不存在或已被删除'];
  425. if($apply->status != 0) return [false, '申请单已审核,编辑失败'];
  426. }
  427. return [true,''];
  428. }
  429. public function setOrderNO($type = ""){
  430. $str = date('Ymd',time());
  431. $order_number = ApplyOrder::where('order_number','Like','%'. $str . '%')
  432. ->where('type', $type)
  433. ->max('order_number');
  434. if(empty($order_number)){
  435. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  436. $number = $str . $number;
  437. }else{
  438. $tmp = substr($order_number, -3);
  439. $tmp = $tmp + 1;
  440. //超过999
  441. if(strlen($tmp) > 3) return '';
  442. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  443. $number = $str . $number;
  444. }
  445. return $number;
  446. }
  447. public function createSQ($data, $user, $type = 0, $status = 1){
  448. if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
  449. if($type == ApplyOrder::type_one){
  450. $storehouse_id = "001";
  451. $storehouse_title = "原料仓";
  452. }elseif ($type == ApplyOrder::type_two){
  453. $storehouse_id = "004";
  454. $storehouse_title = "待清洗原材料仓";
  455. }elseif ($type == ApplyOrder::type_three){
  456. $storehouse_id = "002";
  457. $storehouse_title = "产成品仓";
  458. }else{
  459. $storehouse_id = "003";
  460. $storehouse_title = "包材、辅料仓" ;
  461. }
  462. try{
  463. DB::beginTransaction();
  464. $order_number = $this->setOrderNO($type);
  465. // list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type);
  466. // if(! $status) return [false, $msg];
  467. $time = time();
  468. $model = new ApplyOrder();
  469. $model->order_number = $order_number;
  470. $model->apply_id = $user['id'];
  471. $model->apply_time = time();
  472. $model->storehouse_id = $storehouse_id;
  473. $model->storehouse_title = $storehouse_title;
  474. $model->type = $type;
  475. $model->status = $status; //是否审核
  476. $model->save();
  477. $id = $model->id;
  478. $detail_insert = [];
  479. foreach ($data as $v){
  480. $detail_insert[] = [
  481. 'apply_order_id' => $id,
  482. 'data_id' => $v['id'],
  483. 'quantity' => $v['quantity'] ?? 0,
  484. 'product_no' => $v['product_no'] ?? "",
  485. 'product_title' => $v['product_title'] ?? "",
  486. 'product_size' => $v['product_size'] ?? "",
  487. 'product_unit' => $v['product_unit'] ?? "",
  488. 'technology_name' => $v['technology_name'] ?? "",
  489. 'wood_name' => $v['wood_name'] ?? "",
  490. 'top_product_no' => $v['top_product_no'] ?? "",
  491. 'top_product_title' => $v['top_product_title'] ?? "",
  492. 'type' => $type,
  493. 'storehouse_id' => $storehouse_id,
  494. 'crt_time' => $time,
  495. ];
  496. }
  497. ApplyOrderDetail::insert($detail_insert);
  498. DB::commit();
  499. }catch (\Exception $e){
  500. DB::rollBack();
  501. return [false,$e->getMessage()];
  502. }
  503. return [true, $id];
  504. }
  505. //流水
  506. public function createRecord($id){
  507. $record = ApplyOrderDetail::where('apply_order_id', $id)
  508. ->where('del_time',0)
  509. ->get()->toArray();
  510. if(empty($record)) return [false, '申请单明细数据不存在或已被删除'];
  511. try{
  512. DB::beginTransaction();
  513. $time = time();
  514. $insert = [];
  515. foreach ($record as $value){
  516. if(empty($value['product_no'])) continue;
  517. if($value['type'] == ApplyOrder::type_one){
  518. $number = - $value['quantity'];
  519. }else{
  520. $number = $value['quantity'];
  521. }
  522. $insert[] = [
  523. 'apply_order_id' => $id,
  524. 'product_no' => $value['product_no'],
  525. 'number' => $number,
  526. 'type' => $value['type'],
  527. 'storehouse_id' => $value['storehouse_id'],
  528. 'crt_time' => $time,
  529. ];
  530. }
  531. if(! empty($insert)) InOutRecord::insert($insert);
  532. DB::commit();
  533. }catch (\Exception $e){
  534. DB::rollBack();
  535. return [false,$e->getMessage()];
  536. }
  537. return [true, ''];
  538. }
  539. //报工
  540. public function reportWorkingEdit($data, $user){
  541. list($status,$msg) = $this->reportWorkingRule($data,false);
  542. if(!$status) return [$status,$msg];
  543. DB::beginTransaction();
  544. try{
  545. $time = time();
  546. $model = new ReportWorking();
  547. $model = $model->where('id',$data['id'])->first();
  548. $model->report_time = $data['report_time'];
  549. $model->mark = $data['mark']?? "";
  550. $model->save();
  551. $id = $model->id;
  552. ReportWorkingDetail::where('del_time',0)->where('report_working_id',$id)->update([
  553. 'del_time' => $time
  554. ]);
  555. //班组下的人
  556. $team_man = [];
  557. $model_t = new EmployeeTeamPermission();
  558. $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();
  559. foreach ($team_array as $v){
  560. $team_man[$v['team_id']][] = $v['employee_id'];
  561. }
  562. $detail_insert = [];
  563. foreach ($data['order_data'] as $v){
  564. $t = $team_man[$v['team_id']] ?? [];
  565. foreach ($t as $t_v){
  566. $detail_insert[] = [
  567. 'report_working_id' => $id,
  568. 'data_id' => $v['id'],
  569. 'quantity' => $v['quantity'] ?? 0,
  570. 'process_id' => $v['process_id'] ?? 0,
  571. 'finished_id' => $t_v,
  572. 'team_id' => $v['team_id'],
  573. 'crt_time' => $time,
  574. ];
  575. }
  576. }
  577. ReportWorkingDetail::insert($detail_insert);
  578. DB::commit();
  579. }catch (\Exception $e){
  580. DB::rollBack();
  581. return [false,$e->getMessage()];
  582. }
  583. return [true,''];
  584. }
  585. public function reportWorkingAdd($data,$user){
  586. list($status,$msg) = $this->reportWorkingRule($data);
  587. if(!$status) return [$status,$msg];
  588. DB::beginTransaction();
  589. try{
  590. $time = time();
  591. $model = new ReportWorking();
  592. $model->order_number = $data['order_number'];
  593. $model->report_time = $data['report_time'];
  594. $model->mark = $data['mark']?? "";
  595. $model->crt_id = $user['id'];
  596. $model->save();
  597. $id = $model->id;
  598. //班组下的人
  599. $team_man = [];
  600. $model_t = new EmployeeTeamPermission();
  601. $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();
  602. foreach ($team_array as $v){
  603. $team_man[$v['team_id']][] = $v['employee_id'];
  604. }
  605. $detail_insert = [];
  606. foreach ($data['order_data'] as $v){
  607. $t = $team_man[$v['team_id']] ?? [];
  608. foreach ($t as $t_v){
  609. $detail_insert[] = [
  610. 'report_working_id' => $id,
  611. 'data_id' => $v['id'],
  612. 'quantity' => $v['quantity'] ?? 0,
  613. 'process_id' => $v['process_id'] ?? 0,
  614. 'finished_id' => $t_v,
  615. 'team_id' => $v['team_id'],
  616. 'crt_time' => $time,
  617. ];
  618. }
  619. }
  620. ReportWorkingDetail::insert($detail_insert);
  621. DB::commit();
  622. }catch (\Exception $e){
  623. DB::rollBack();
  624. return [false,$e->getMessage()];
  625. }
  626. return [true, ''];
  627. }
  628. public function reportWorkingDel($data){
  629. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  630. $apply = ReportWorking::where('id',$data['id'])->first();
  631. if($apply->del_time > 0) return [false,'报工单不存在或已被删除'];
  632. if($apply->state != ReportWorking::state_zero) return [false, '报工单已审核,删除失败'];
  633. $time = time();
  634. try {
  635. DB::beginTransaction();
  636. $apply->del_time = $time;
  637. $apply->save();
  638. ReportWorkingDetail::where('del_time', 0)->where('report_working_id',$data['id'])->update([
  639. 'del_time'=>time()
  640. ]);
  641. DB::commit();
  642. }catch (\Throwable $exception){
  643. DB::rollBack();
  644. return [false, $exception->getMessage()];
  645. }
  646. return [true,''];
  647. }
  648. public function reportWorkingList($data){
  649. $model = ReportWorking::where('del_time',0)
  650. ->select('*')
  651. ->orderBy('id','desc');
  652. if(isset($data['status'])) $model->where('status', $data['status']);
  653. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  654. if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
  655. if(! empty($data['report_time'][0]) && ! empty($data['report_time'][1])) $model->whereBetween('report_time',[$data['report_time'][0],$data['report_time'][1]]);
  656. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  657. if(! empty($data['dispatch_no'])) {
  658. $dispatch_id = DispatchSub::where('del_time',0)
  659. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  660. ->select('id')
  661. ->get()->toArray();
  662. $dispatch_id = array_column($dispatch_id,'id');
  663. $detail = ReportWorkingDetail::where('del_time',0)
  664. ->whereIn('data_id', $dispatch_id)
  665. ->select('report_working_id')
  666. ->get()->toArray();
  667. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  668. }
  669. $list = $this->limit($model,'',$data);
  670. $list = $this->reportWorkingfillData($list);
  671. return [true,$list];
  672. }
  673. public function reportWorkingfillData($data){
  674. if(empty($data['data'])) return $data;
  675. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  676. ->pluck('emp_name','id')
  677. ->toArray();
  678. foreach ($data['data'] as $key => $value){
  679. $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d H:i:s',$value['report_time']) : '';
  680. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  681. $data['data'][$key]['status_title'] = ReportWorking::$state_name[$value['status']] ?? "";
  682. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  683. }
  684. return $data;
  685. }
  686. public function reportWorkingDetail($data){
  687. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  688. $id = $data['id'];
  689. $detail = ReportWorking::where('del_time',0)
  690. ->where('id',$id)
  691. ->first();
  692. if(empty($detail)) return [false,'报工单不存在或已被删除'];
  693. $detail = $detail->toArray();
  694. $apply_d = ReportWorkingDetail::where('del_time', 0)
  695. ->where('report_working_id', $id)
  696. ->get()->toArray();
  697. $apply_id = array_column($apply_d, 'data_id');
  698. //派工单
  699. $d = DispatchSub::where('del_time',0)
  700. ->whereIn('id', $apply_id)
  701. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','process_id','product_unit','price','product_size')
  702. ->get()->toArray();
  703. $d_tmp = array_column($d,null,'id');
  704. $process_map = Process::whereIn('id',array_unique(array_column($d,'process_id')))
  705. ->pluck('title','id')
  706. ->toArray();
  707. $team_map = Team::pluck('title','id')->toArray();
  708. $return = [];
  709. foreach ($apply_d as $t){
  710. $tmp = $d_tmp[$t['data_id']] ?? [];
  711. $str = $t['data_id'] . $t['process_id'];
  712. if(! isset($return[$str])){
  713. $return[$str] = [
  714. 'id' => $t['data_id'] ?? 0,
  715. 'quantity' => $t['quantity'] ?? 0,
  716. 'product_no' => $tmp['product_no'] ?? "",
  717. 'product_title' => $tmp['product_title'] ?? "",
  718. 'product_size' => $tmp['product_size'] ?? "",
  719. 'product_unit' => $tmp['product_unit'] ?? "",
  720. 'technology_name' => $tmp['technology_name'] ?? "",
  721. 'wood_name' => $tmp['wood_name'] ?? "",
  722. 'order_no' => $tmp['order_no'] ?? "",
  723. 'process_id' => $tmp['process_id'] ?? 0,
  724. 'process_name' => $process_map[$tmp['process_id']] ?? "",
  725. 'team_id' => $t['team_id'] ?? 0,
  726. 'team_title' => $team_map[$t['team_id']] ?? '',
  727. ];
  728. }
  729. }
  730. $detail['order_data'] = array_values($return);
  731. return [true, $detail];
  732. }
  733. public function reportWorkingRule(&$data,$is_add = true){
  734. if($this->isEmpty($data,'report_time')) return [false,'报工时间不能为空'];
  735. if(empty($data['order_data'])) return [false, '报工单详细信息不能为空'];
  736. foreach ($data['order_data'] as $value){
  737. if(empty($value['id'])) return [false, '报工单详细信息ID不能为空'];
  738. if(empty($value['quantity'])) return [false, '报工单详细信息数量不能为空'];
  739. if(empty($value['process_id'])) return [false, '报工单详细信息工序不能为空'];
  740. if(empty($value['team_id'])) return [false, '报工单详细信息班组不能为空'];
  741. }
  742. $id = array_unique(array_column($data['order_data'],'id'));
  743. $dispatch = DispatchSub::where('del_time',0)
  744. ->whereIn('id', $id)
  745. ->select('id', 'dispatch_no', 'status')
  746. ->get()->toArray();
  747. if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
  748. if($is_add){
  749. $data['order_number'] = $this->setOrderNO2();
  750. if(empty($data['order_number'])) return [false, '报工单唯一标识生成失败'];
  751. }else{
  752. if(empty($data['id'])) return [false, '报工单唯一标识不能为空'];
  753. if(empty($data['order_number'])) return [false, '报工单唯一标识不能为空'];
  754. $apply = ReportWorking::where('id', $data['id'])->where('del_time', 0)->first();
  755. if(empty($apply)) return [false, '报工单不存在或已被删除'];
  756. if($apply->status != 0) return [false, '报工单已审核,编辑失败'];
  757. }
  758. return [true,''];
  759. }
  760. public function setOrderNO2(){
  761. $str = date('Ymd',time());
  762. $model = new ReportWorking();
  763. $order_number = $model->where('order_number','Like','%'. $str . '%')
  764. ->max('order_number');
  765. if(empty($order_number)){
  766. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  767. $number = $str . $number;
  768. }else{
  769. $tmp = substr($order_number, -3);
  770. $tmp = $tmp + 1;
  771. //超过999
  772. if(strlen($tmp) > 3) return '';
  773. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  774. $number = $str . $number;
  775. }
  776. return $number;
  777. }
  778. public function applyOrderReportList($data){
  779. if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!'];
  780. if(empty($data['type'])) return [false, 'TYPE不能为空!'];
  781. $model = ApplyOrder::where('del_time',0)
  782. ->where('apply_time',">=",$data['apply_time'][0])
  783. ->where('apply_time',"<=",$data['apply_time'][1])
  784. ->where('type',$data['type'])
  785. ->select('id','order_number','apply_time','mark','status')
  786. ->orderBy('id','desc');
  787. if(isset($data['status'])) $model->where('status', $data['status']);
  788. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){
  789. $model->where('crt_time',">=",$data['crt_time'][0]);
  790. $model->where('crt_time',"<=",$data['crt_time'][1]);
  791. }
  792. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  793. if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
  794. if(! empty($data['sale_order_number'])){
  795. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  796. $dispatch_id = DispatchSub::where('del_time',0)
  797. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  798. ->select('id')
  799. ->get()->toArray();
  800. $dispatch_id = array_column($dispatch_id,'id');
  801. $detail = ApplyOrderDetail::where('del_time',0)
  802. ->where('type', $data['type'])
  803. ->whereIn('data_id', $dispatch_id)
  804. ->select('apply_order_id')
  805. ->get()->toArray();
  806. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  807. }else{
  808. $no = SaleOrdersProduct::where('del_time',0)
  809. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  810. ->select('order_no')
  811. ->get()->toArray();
  812. $no = array_unique(array_column($no,'order_no'));
  813. $box_id = Box::where('del_time',0)
  814. ->whereIn('top_order_no', $no)
  815. ->select('id')
  816. ->get()->toArray();
  817. $box_id = array_column($box_id,'id');
  818. $detail = ApplyOrderDetail::where('del_time',0)
  819. ->where('type', $data['type'])
  820. ->whereIn('data_id', $box_id)
  821. ->select('apply_order_id')
  822. ->get()->toArray();
  823. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  824. }
  825. }
  826. if(! empty($data['dispatch_no'])){
  827. $dispatch_id = DispatchSub::where('del_time',0)
  828. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  829. ->select('id')
  830. ->get()->toArray();
  831. $dispatch_id = array_column($dispatch_id,'id');
  832. $detail = ApplyOrderDetail::where('del_time',0)
  833. ->where('type', $data['type'])
  834. ->whereIn('data_id', $dispatch_id)
  835. ->select('apply_order_id')
  836. ->get()->toArray();
  837. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  838. }
  839. $list = $model->get()->toArray();
  840. if(empty($list)) return [true, []];
  841. $apply_id = array_column($list, 'id');
  842. //明细数据
  843. $result = ApplyOrderDetail::where('del_time',0)
  844. ->whereIn('apply_order_id', $apply_id)
  845. ->get()->toArray();
  846. if(empty($result)) return [true, []];
  847. //组织主表需要的数据
  848. $list_map = $this->makeListMap($list, $result, $data['type']);
  849. $data_id = array_unique(array_column($result, 'data_id'));
  850. list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']);
  851. $return = [];
  852. $d_tmp = array_column($d,null,'id');
  853. foreach ($result as $t){
  854. $tmp = $d_tmp[$t['data_id']] ?? [];
  855. $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
  856. $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
  857. $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
  858. $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
  859. if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
  860. $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
  861. }else{
  862. $order_no = $tmp['order_no'] ?? "";
  863. }
  864. if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
  865. $product_unit = "吨";
  866. }elseif($t['type'] == ApplyOrder::type_four){
  867. $product_unit = "只";
  868. }else{
  869. $product_unit = $t['product_unit'] ?? "";
  870. }
  871. $tmp = $list_map[$t['apply_order_id']] ?? [];
  872. $return[] = [
  873. 'order_number' => $tmp['order_number'],
  874. 'sale_order_number' => $tmp['sale_order_number'],
  875. 'apply_time' => $tmp['apply_time'],
  876. 'mark' => $tmp['mark'],
  877. 'status_title' => $tmp['status_title'],
  878. 'id' => $t['data_id'] ?? 0,
  879. // 'main_id' => $t['id'],
  880. 'quantity' => $t['quantity'] ?? 0,
  881. 'product_no' => $t['product_no'] ?? "",
  882. 'product_title' => $t['product_title'] ?? "",
  883. 'product_size' => $t['product_size'] ?? "",
  884. 'product_unit' => $product_unit,
  885. 'top_product_title' => $top_product_title,
  886. 'top_product_no' => $top_product_no,
  887. 'technology_name' => $technology_name, //颜色
  888. 'wood_name' => $wood_name,
  889. 'order_no' => $order_no,
  890. ];
  891. }
  892. return [true, array_values($return)];
  893. }
  894. private function getDetailList($apply_id, $type){
  895. $d = $d_no_map = [];
  896. if($type == ApplyOrder::type_one){
  897. $d = DispatchSub::where('del_time',0)
  898. ->whereIn('id', $apply_id)
  899. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  900. ->get()->toArray();
  901. $args = "";
  902. foreach ($d as $value){
  903. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  904. }
  905. $args = rtrim($args, 'OR ');
  906. $d_no_map = [];
  907. $d_no = DispatchSub::where('del_time',0)
  908. ->whereRaw($args)
  909. ->select('dispatch_no as order_no','order_product_id','crt_time')
  910. ->get()->toArray();
  911. foreach ($d_no as $value){
  912. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  913. 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'];
  914. }else{
  915. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  916. }
  917. }
  918. }elseif ($type == ApplyOrder::type_two){
  919. $d = DispatchSub::where('del_time',0)
  920. ->whereIn('id', $apply_id)
  921. ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
  922. ->get()->toArray();
  923. $args = "";
  924. foreach ($d as $value){
  925. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  926. }
  927. $args = rtrim($args, 'OR ');
  928. $d_no_map = [];
  929. $d_no = DispatchSub::where('del_time',0)
  930. ->whereRaw($args)
  931. ->select('dispatch_no as order_no','order_product_id','crt_time')
  932. ->get()->toArray();
  933. foreach ($d_no as $value){
  934. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  935. 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'];
  936. }else{
  937. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  938. }
  939. }
  940. }elseif ($type == ApplyOrder::type_three){
  941. $d = Box::where('del_time',0)
  942. ->whereIn('id', $apply_id)
  943. ->select('id', 'order_no')
  944. ->get()->toArray();
  945. }elseif ($type == ApplyOrder::type_four){
  946. $d = Box::where('del_time',0)
  947. ->whereIn('id', $apply_id)
  948. ->select('id', 'order_no')
  949. ->get()->toArray();
  950. }
  951. return [$d, $d_no_map];
  952. }
  953. private function makeListMap($data, $order, $type){
  954. if(empty($data)) return [];
  955. //申请单 所有的数据id
  956. $order_map = $data_id = [];
  957. foreach ($order as $value){
  958. $order_map[$value['apply_order_id']][] = $value['data_id'];
  959. $data_id[] = $value['data_id'];
  960. }
  961. if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
  962. $map1 = DispatchSub::whereIn('id', array_unique($data_id))
  963. ->pluck('out_order_no','id')
  964. ->toArray();
  965. }else{
  966. $map2 = Box::whereIn('id', array_unique($data_id))
  967. ->pluck('top_order_no','id')
  968. ->toArray();
  969. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
  970. ->pluck('out_order_no','order_no')
  971. ->toArray();
  972. }
  973. foreach ($data as $key => $value){
  974. $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
  975. $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
  976. $order_tmp = $order_map[$value['id']] ?? [];
  977. $order_tmp = array_unique($order_tmp);
  978. $sale_order_number = [];
  979. if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
  980. foreach ($order_tmp as $v){
  981. $str = $map1[$v] ?? '';
  982. if(empty($str)) continue;
  983. $sale_order_number[] = $str;
  984. }
  985. $sale_order_number = array_unique($sale_order_number);
  986. $sale_order_number = implode(',',$sale_order_number);
  987. $data[$key]['sale_order_number'] = $sale_order_number;
  988. }else{
  989. foreach ($order_tmp as $v){
  990. $m2_t = $map2[$v] ?? '';
  991. $m2_t = $map2_fin[$m2_t] ?? "";
  992. if(empty($m2_t)) continue;
  993. $sale_order_number[] = $m2_t;
  994. }
  995. $sale_order_number = array_unique($sale_order_number);
  996. $sale_order_number = implode(',',$sale_order_number);
  997. $data[$key]['sale_order_number'] = $sale_order_number;
  998. }
  999. }
  1000. return array_column($data,null,'id');
  1001. }
  1002. }