ApplyOrderService.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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_no1'])) {
  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. if(! empty($data['dispatch_no'])) {
  670. $dispatch_id = DispatchSub::where('del_time',0)
  671. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  672. ->select('id')
  673. ->get()->toArray();
  674. $dispatch_id = array_column($dispatch_id,'id');
  675. $detail = ReportWorkingDetail::where('del_time',0)
  676. ->whereIn('data_id', $dispatch_id)
  677. ->select('report_working_id')
  678. ->get()->toArray();
  679. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  680. }
  681. if(! empty($data['sale_order_number'])){
  682. $id = SaleOrdersProduct::where('del_time',0)
  683. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  684. ->select('id')
  685. ->get()->toArray();
  686. $dispatch_id = DispatchSub::where('del_time',0)
  687. ->whereIn('sale_orders_product_id',array_column($id,'id'))
  688. ->select('id')
  689. ->get()->toArray();
  690. $detail = ReportWorkingDetail::where('del_time',0)
  691. ->whereIn('data_id', $dispatch_id)
  692. ->select('report_working_id')
  693. ->get()->toArray();
  694. $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
  695. }
  696. $list = $this->limit($model,'',$data);
  697. $list = $this->reportWorkingfillData($list);
  698. return [true,$list];
  699. }
  700. public function reportWorkingfillData($data){
  701. if(empty($data['data'])) return $data;
  702. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  703. ->pluck('emp_name','id')
  704. ->toArray();
  705. foreach ($data['data'] as $key => $value){
  706. $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d H:i:s',$value['report_time']) : '';
  707. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  708. $data['data'][$key]['status_title'] = ReportWorking::$state_name[$value['status']] ?? "";
  709. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  710. }
  711. return $data;
  712. }
  713. public function reportWorkingDetail($data){
  714. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  715. $id = $data['id'];
  716. $detail = ReportWorking::where('del_time',0)
  717. ->where('id',$id)
  718. ->first();
  719. if(empty($detail)) return [false,'报工单不存在或已被删除'];
  720. $detail = $detail->toArray();
  721. $apply_d = ReportWorkingDetail::where('del_time', 0)
  722. ->where('report_working_id', $id)
  723. ->get()->toArray();
  724. $apply_id = array_column($apply_d, 'data_id');
  725. //派工单
  726. $d = DispatchSub::where('del_time',0)
  727. ->whereIn('id', $apply_id)
  728. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','process_id','product_unit','price','product_size')
  729. ->get()->toArray();
  730. $d_tmp = array_column($d,null,'id');
  731. $process_map = Process::whereIn('id',array_unique(array_column($d,'process_id')))
  732. ->pluck('title','id')
  733. ->toArray();
  734. $team_map = Team::pluck('title','id')->toArray();
  735. $return = [];
  736. foreach ($apply_d as $t){
  737. $tmp = $d_tmp[$t['data_id']] ?? [];
  738. $str = $t['data_id'] . $t['process_id'];
  739. if(! isset($return[$str])){
  740. $return[$str] = [
  741. 'id' => $t['data_id'] ?? 0,
  742. 'quantity' => $t['quantity'] ?? 0,
  743. 'product_no' => $tmp['product_no'] ?? "",
  744. 'product_title' => $tmp['product_title'] ?? "",
  745. 'product_size' => $tmp['product_size'] ?? "",
  746. 'product_unit' => $tmp['product_unit'] ?? "",
  747. 'technology_name' => $tmp['technology_name'] ?? "",
  748. 'wood_name' => $tmp['wood_name'] ?? "",
  749. 'order_no' => $tmp['order_no'] ?? "",
  750. 'process_id' => $tmp['process_id'] ?? 0,
  751. 'process_name' => $process_map[$tmp['process_id']] ?? "",
  752. 'team_id' => $t['team_id'] ?? 0,
  753. 'team_title' => $team_map[$t['team_id']] ?? '',
  754. ];
  755. }
  756. }
  757. $detail['order_data'] = array_values($return);
  758. return [true, $detail];
  759. }
  760. public function reportWorkingRule(&$data,$is_add = true){
  761. if($this->isEmpty($data,'report_time')) return [false,'报工时间不能为空'];
  762. if(empty($data['order_data'])) return [false, '报工单详细信息不能为空'];
  763. foreach ($data['order_data'] as $value){
  764. if(empty($value['id'])) return [false, '报工单详细信息ID不能为空'];
  765. if(empty($value['quantity'])) return [false, '报工单详细信息数量不能为空'];
  766. if(empty($value['process_id'])) return [false, '报工单详细信息工序不能为空'];
  767. if(empty($value['team_id'])) return [false, '报工单详细信息班组不能为空'];
  768. }
  769. $id = array_unique(array_column($data['order_data'],'id'));
  770. $dispatch = DispatchSub::where('del_time',0)
  771. ->whereIn('id', $id)
  772. ->select('id', 'dispatch_no', 'status')
  773. ->get()->toArray();
  774. if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
  775. if($is_add){
  776. $data['order_number'] = $this->setOrderNO2();
  777. if(empty($data['order_number'])) return [false, '报工单唯一标识生成失败'];
  778. }else{
  779. if(empty($data['id'])) return [false, '报工单唯一标识不能为空'];
  780. if(empty($data['order_number'])) return [false, '报工单唯一标识不能为空'];
  781. $apply = ReportWorking::where('id', $data['id'])->where('del_time', 0)->first();
  782. if(empty($apply)) return [false, '报工单不存在或已被删除'];
  783. if($apply->status != 0) return [false, '报工单已审核,编辑失败'];
  784. }
  785. return [true,''];
  786. }
  787. public function setOrderNO2(){
  788. $str = date('Ymd',time());
  789. $model = new ReportWorking();
  790. $order_number = $model->where('order_number','Like','%'. $str . '%')
  791. ->max('order_number');
  792. if(empty($order_number)){
  793. $number = str_pad(1,3,'0',STR_PAD_LEFT);
  794. $number = $str . $number;
  795. }else{
  796. $tmp = substr($order_number, -3);
  797. $tmp = $tmp + 1;
  798. //超过999
  799. if(strlen($tmp) > 3) return '';
  800. $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
  801. $number = $str . $number;
  802. }
  803. return $number;
  804. }
  805. public function applyOrderReportList($data){
  806. if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!'];
  807. if(empty($data['type'])) return [false, 'TYPE不能为空!'];
  808. $model = ApplyOrder::where('del_time',0)
  809. ->where('apply_time',">=",$data['apply_time'][0])
  810. ->where('apply_time',"<=",$data['apply_time'][1])
  811. ->where('type',$data['type'])
  812. ->select('id','order_number','apply_time','mark','status','storehouse_title')
  813. ->orderBy('id','desc');
  814. if(isset($data['status'])) $model->where('status', $data['status']);
  815. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){
  816. $model->where('crt_time',">=",$data['crt_time'][0]);
  817. $model->where('crt_time',"<=",$data['crt_time'][1]);
  818. }
  819. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  820. if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
  821. if(! empty($data['sale_order_number'])){
  822. if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
  823. $dispatch_id = DispatchSub::where('del_time',0)
  824. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  825. ->select('id')
  826. ->get()->toArray();
  827. $dispatch_id = array_column($dispatch_id,'id');
  828. $detail = ApplyOrderDetail::where('del_time',0)
  829. ->where('type', $data['type'])
  830. ->whereIn('data_id', $dispatch_id)
  831. ->select('apply_order_id')
  832. ->get()->toArray();
  833. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  834. }else{
  835. $no = SaleOrdersProduct::where('del_time',0)
  836. ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
  837. ->select('order_no')
  838. ->get()->toArray();
  839. $no = array_unique(array_column($no,'order_no'));
  840. $box_id = Box::where('del_time',0)
  841. ->whereIn('top_order_no', $no)
  842. ->select('id')
  843. ->get()->toArray();
  844. $box_id = array_column($box_id,'id');
  845. $detail = ApplyOrderDetail::where('del_time',0)
  846. ->where('type', $data['type'])
  847. ->whereIn('data_id', $box_id)
  848. ->select('apply_order_id')
  849. ->get()->toArray();
  850. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  851. }
  852. }
  853. if(! empty($data['dispatch_no'])){
  854. $dispatch_id = DispatchSub::where('del_time',0)
  855. ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
  856. ->select('id')
  857. ->get()->toArray();
  858. $dispatch_id = array_column($dispatch_id,'id');
  859. $detail = ApplyOrderDetail::where('del_time',0)
  860. ->where('type', $data['type'])
  861. ->whereIn('data_id', $dispatch_id)
  862. ->select('apply_order_id')
  863. ->get()->toArray();
  864. $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
  865. }
  866. $list = $model->get()->toArray();
  867. if(empty($list)) return [true, []];
  868. $apply_id = array_column($list, 'id');
  869. //明细数据
  870. $result = ApplyOrderDetail::where('del_time',0)
  871. ->whereIn('apply_order_id', $apply_id)
  872. ->get()->toArray();
  873. if(empty($result)) return [true, []];
  874. //组织主表需要的数据
  875. $list_map = $this->makeListMap($list, $result, $data['type']);
  876. $data_id = array_unique(array_column($result, 'data_id'));
  877. list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']);
  878. $return = [];
  879. $d_tmp = array_column($d,null,'id');
  880. foreach ($result as $t){
  881. $tmp = $d_tmp[$t['data_id']] ?? [];
  882. $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
  883. $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
  884. $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
  885. $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
  886. if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
  887. $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
  888. }else{
  889. $order_no = $tmp['order_no'] ?? "";
  890. }
  891. if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
  892. $product_unit = "吨";
  893. }elseif($t['type'] == ApplyOrder::type_four){
  894. $product_unit = "只";
  895. }else{
  896. $product_unit = $t['product_unit'] ?? "";
  897. }
  898. $tmp = $list_map[$t['apply_order_id']] ?? [];
  899. $return[] = [
  900. 'order_number' => $tmp['order_number'],
  901. 'sale_order_number' => $tmp['sale_order_number'],
  902. 'apply_time' => $tmp['apply_time'],
  903. 'mark' => $tmp['mark'],
  904. 'status_title' => $tmp['status_title'],
  905. 'storehouse_title' => $tmp['storehouse_title'],
  906. 'id' => $t['data_id'] ?? 0,
  907. // 'main_id' => $t['id'],
  908. 'quantity' => $t['quantity'] ?? 0,
  909. 'product_no' => $t['product_no'] ?? "",
  910. 'product_title' => $t['product_title'] ?? "",
  911. 'product_size' => $t['product_size'] ?? "",
  912. 'product_unit' => $product_unit,
  913. 'top_product_title' => $top_product_title,
  914. 'top_product_no' => $top_product_no,
  915. 'technology_name' => $technology_name, //颜色
  916. 'wood_name' => $wood_name,
  917. 'order_no' => $order_no,
  918. ];
  919. }
  920. return [true, array_values($return)];
  921. }
  922. private function getDetailList($apply_id, $type){
  923. $d = $d_no_map = [];
  924. if($type == ApplyOrder::type_one){
  925. $d = DispatchSub::where('del_time',0)
  926. ->whereIn('id', $apply_id)
  927. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
  928. ->get()->toArray();
  929. $args = "";
  930. foreach ($d as $value){
  931. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  932. }
  933. $args = rtrim($args, 'OR ');
  934. $d_no_map = [];
  935. $d_no = DispatchSub::where('del_time',0)
  936. ->whereRaw($args)
  937. ->select('dispatch_no as order_no','order_product_id','crt_time')
  938. ->get()->toArray();
  939. foreach ($d_no as $value){
  940. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  941. 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'];
  942. }else{
  943. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  944. }
  945. }
  946. }elseif ($type == ApplyOrder::type_two){
  947. $d = DispatchSub::where('del_time',0)
  948. ->whereIn('id', $apply_id)
  949. ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
  950. ->get()->toArray();
  951. $args = "";
  952. foreach ($d as $value){
  953. $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
  954. }
  955. $args = rtrim($args, 'OR ');
  956. $d_no_map = [];
  957. $d_no = DispatchSub::where('del_time',0)
  958. ->whereRaw($args)
  959. ->select('dispatch_no as order_no','order_product_id','crt_time')
  960. ->get()->toArray();
  961. foreach ($d_no as $value){
  962. if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
  963. 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'];
  964. }else{
  965. $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
  966. }
  967. }
  968. }elseif ($type == ApplyOrder::type_three){
  969. $d = Box::where('del_time',0)
  970. ->whereIn('id', $apply_id)
  971. ->select('id', 'order_no')
  972. ->get()->toArray();
  973. }elseif ($type == ApplyOrder::type_four){
  974. $d = Box::where('del_time',0)
  975. ->whereIn('id', $apply_id)
  976. ->select('id', 'order_no')
  977. ->get()->toArray();
  978. }
  979. return [$d, $d_no_map];
  980. }
  981. private function makeListMap($data, $order, $type){
  982. if(empty($data)) return [];
  983. //申请单 所有的数据id
  984. $order_map = $data_id = [];
  985. foreach ($order as $value){
  986. $order_map[$value['apply_order_id']][] = $value['data_id'];
  987. $data_id[] = $value['data_id'];
  988. }
  989. if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
  990. $map1 = DispatchSub::whereIn('id', array_unique($data_id))
  991. ->pluck('out_order_no','id')
  992. ->toArray();
  993. }else{
  994. $map2 = Box::whereIn('id', array_unique($data_id))
  995. ->pluck('top_order_no','id')
  996. ->toArray();
  997. $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
  998. ->pluck('out_order_no','order_no')
  999. ->toArray();
  1000. }
  1001. foreach ($data as $key => $value){
  1002. $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
  1003. $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
  1004. $order_tmp = $order_map[$value['id']] ?? [];
  1005. $order_tmp = array_unique($order_tmp);
  1006. $sale_order_number = [];
  1007. if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
  1008. foreach ($order_tmp as $v){
  1009. $str = $map1[$v] ?? '';
  1010. if(empty($str)) continue;
  1011. $sale_order_number[] = $str;
  1012. }
  1013. $sale_order_number = array_unique($sale_order_number);
  1014. $sale_order_number = implode(',',$sale_order_number);
  1015. $data[$key]['sale_order_number'] = $sale_order_number;
  1016. }else{
  1017. foreach ($order_tmp as $v){
  1018. $m2_t = $map2[$v] ?? '';
  1019. $m2_t = $map2_fin[$m2_t] ?? "";
  1020. if(empty($m2_t)) continue;
  1021. $sale_order_number[] = $m2_t;
  1022. }
  1023. $sale_order_number = array_unique($sale_order_number);
  1024. $sale_order_number = implode(',',$sale_order_number);
  1025. $data[$key]['sale_order_number'] = $sale_order_number;
  1026. }
  1027. }
  1028. return array_column($data,null,'id');
  1029. }
  1030. }