ConstructionService.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Construction;
  5. use App\Model\ConstructionFile;
  6. use App\Model\ConstructionInfo;
  7. use App\Model\ConstructionProductInfo;
  8. use App\Model\Customer;
  9. use App\Model\CustomerInfo;
  10. use App\Model\DeliveryNote;
  11. use App\Model\Depart;
  12. use App\Model\Employee;
  13. use App\Model\EmployeeDepartPermission;
  14. use App\Model\Oa;
  15. use App\Model\OaSub;
  16. use App\Model\OaSubEmployee;
  17. use App\Model\OaSubReportEmployee;
  18. use App\Model\OaSubRule;
  19. use App\Model\Product;
  20. use App\Model\SalesOrder;
  21. use App\Model\SalesOrderInfo;
  22. use App\Model\SalesOrderProductInfo;
  23. use App\Model\ScheduleInfo;
  24. use App\Model\SeeRange;
  25. use App\Model\Storehouse;
  26. use Illuminate\Support\Facades\DB;
  27. //use Barryvdh\DomPDF\Facade as PDF;
  28. use Barryvdh\DomPDF\PDF;
  29. /**
  30. * 施工订单
  31. */
  32. class ConstructionService extends Service
  33. {
  34. /**
  35. * 施工订单编辑
  36. * @param $data
  37. * @param $user
  38. * @return array
  39. */
  40. public function constructionEdit($data,$user){
  41. list($status,$msg) = $this->constructionRule($data, $user, false);
  42. if(!$status) return [$status,$msg];
  43. $params = $this->getDataFile($data);
  44. (new OperationLogService())->setOperationList($params,$user,2);
  45. try {
  46. DB::beginTransaction();
  47. $model = Construction::where('id', $data['id'])->first();
  48. $model->model_type = $data['model_type'];
  49. $model->order_number = $data['order_number'];
  50. $model->title = $data['title'] ?? '';
  51. $model->customer_id = $data['customer_id'] ?? 0;
  52. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  53. $model->install_method = $data['install_method'] ?? 0;
  54. $model->install_position = $data['install_position'] ?? 0;
  55. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  56. $model->construction_fee = $data['construction_fee'] ?? 0;
  57. $model->service_price = $data['service_price'] ?? 0;
  58. $model->construction_time = $data['construction_time'] ?? 0;
  59. $model->handover_time = $data['handover_time'] ?? 0;
  60. $model->urgency = $data['urgency'] ?? 0;
  61. $model->mark = $data['mark'] ?? '';
  62. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  63. $model->address2 = $data['address2'] ?? '';
  64. $model->introduction = $data['introduction'] ?? '';
  65. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  66. $model->start_time = $data['start_time'] ?? 0;
  67. $model->end_time = $data['end_time'] ?? 0;
  68. $model->schedule_id = $data['schedule_id'] ?? 0;
  69. $model->day_stamp = $data['day_stamp'] ?? 0;
  70. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  71. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  72. $model->save();
  73. $time = time();
  74. ConstructionInfo::where('del_time',0)
  75. ->where('construction_id',$data['id'])
  76. ->update(['del_time' => $time]);
  77. ConstructionProductInfo::where('del_time',0)
  78. ->where('construction_id',$data['id'])
  79. ->update(['del_time' => $time]);
  80. $old = ConstructionFile::where('del_time',0)
  81. ->where('construction_id',$data['id'])
  82. ->select('file')
  83. ->get()->toArray();
  84. $old = array_column($old,'file');
  85. ConstructionFile::where('del_time',0)
  86. ->where('construction_id',$data['id'])
  87. ->update(['del_time' => $time]);
  88. if(! empty($data['construction_contact'])){
  89. $insert = [];
  90. foreach ($data['construction_contact'] as $value){
  91. $insert[] = [
  92. 'construction_id' => $model->id,
  93. 'contact_type' => $value['id'],
  94. 'contact_info' => $value['info'],
  95. 'type' => ConstructionInfo::type_one,
  96. 'crt_time' => $time,
  97. ];
  98. }
  99. ConstructionInfo::insert($insert);
  100. }
  101. if(! empty($data['employee_one'])){
  102. $insert = [];
  103. foreach ($data['employee_one'] as $value){
  104. $insert[] = [
  105. 'construction_id' => $model->id,
  106. 'employee_id' => $value,
  107. 'type' => ConstructionInfo::type_two,
  108. 'crt_time' => $time,
  109. ];
  110. }
  111. ConstructionInfo::insert($insert);
  112. }
  113. if(! empty($data['product'])){
  114. $insert = [];
  115. foreach ($data['product'] as $value){
  116. $insert[] = [
  117. 'construction_id' => $model->id,
  118. 'product_id' => $value['product_id'],
  119. 'number' => $value['number'],
  120. 'cost' => $value['cost'] ?? 0,
  121. 'retail_price' => $value['retail_price'] ?? 0,
  122. 'mark' => $value['mark'] ?? '',
  123. 'crt_time' => $time,
  124. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  125. 'basic_type_id' => $value['basic_type_id'],
  126. 'price' => $value['price'],
  127. 'final_amount' => $value['final_amount'] ?? 0,
  128. ];
  129. }
  130. ConstructionProductInfo::insert($insert);
  131. //锁定库存
  132. ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1]);
  133. }
  134. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  135. $new = [];
  136. if(! empty($data['file'])){
  137. $insert = [];
  138. foreach ($data['file'] as $value){
  139. $insert[] = [
  140. 'order_number' => $data['order_number'],
  141. 'construction_id' => $data['id'],
  142. 'file' => $value['url'],
  143. 'name' => $value['name'],
  144. 'mark' => $value['mark'] ?? "",
  145. 'crt_time' => $time,
  146. ];
  147. $new[]= $value['url'];
  148. }
  149. ConstructionFile::insert($insert);
  150. }
  151. DB::commit();
  152. }catch (\Exception $exception){
  153. DB::rollBack();
  154. return [false,$exception->getMessage()];
  155. }
  156. $this->delStorageFile($old, $new);
  157. if(! empty($data['check'])) {
  158. list($status,$msg) = (new CheckService())->checkAll([
  159. "id" => $model->id,
  160. "order_number" => $data['order_number'],
  161. "opt_case" => CheckService::five,
  162. "menu_id" => $data['menu_id']
  163. ],$user);
  164. if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  165. }
  166. return [true,''];
  167. }
  168. /**
  169. * 施工订单新增
  170. * @param $data
  171. * @param $user
  172. * @return array
  173. */
  174. public function constructionAdd($data,$user){
  175. list($status,$msg) = $this->constructionRule($data,$user);
  176. if(!$status) return [$status,$msg];
  177. try {
  178. DB::beginTransaction();
  179. $model = new Construction();
  180. $model->model_type = $data['model_type'];
  181. $model->order_number = $data['order_number'];
  182. $model->title = $data['title'] ?? '';
  183. $model->customer_id = $data['customer_id'] ?? 0;
  184. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  185. $model->install_method = $data['install_method'] ?? 0;
  186. $model->install_position = $data['install_position'] ?? 0;
  187. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  188. $model->construction_fee = $data['construction_fee'] ?? 0;
  189. $model->service_price = $data['service_price'] ?? 0;
  190. $model->construction_time = $data['construction_time'] ?? 0;
  191. $model->handover_time = $data['handover_time'] ?? 0;
  192. $model->urgency = $data['urgency'] ?? 0;
  193. $model->mark = $data['mark'] ?? '';
  194. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  195. $model->address2 = $data['address2'] ?? '';
  196. $model->introduction = $data['introduction'] ?? '';
  197. $model->crt_id = $user['id'];
  198. $model->depart_id = $data['depart_id'] ?? 0;
  199. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  200. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  201. $model->start_time = $data['start_time'] ?? 0;
  202. $model->end_time = $data['end_time'] ?? 0;
  203. $model->schedule_id = $data['schedule_id'] ?? 0;
  204. $model->day_stamp = $data['day_stamp'] ?? 0;
  205. $model->day_start_stamp = $data['day_start_stamp'] ?? 0;
  206. $model->day_end_stamp = $data['day_end_stamp'] ?? 0;
  207. $model->save();
  208. $time = time();
  209. if(! empty($data['construction_contact'])){
  210. $insert = [];
  211. foreach ($data['construction_contact'] as $value){
  212. $insert[] = [
  213. 'construction_id' => $model->id,
  214. 'contact_type' => $value['id'],
  215. 'contact_info' => $value['info'],
  216. 'type' => ConstructionInfo::type_one,
  217. 'crt_time' => $time,
  218. ];
  219. }
  220. ConstructionInfo::insert($insert);
  221. }
  222. if(! empty($data['employee_one'])){
  223. $insert = [];
  224. foreach ($data['employee_one'] as $value){
  225. $insert[] = [
  226. 'construction_id' => $model->id,
  227. 'employee_id' => $value,
  228. 'type' => ConstructionInfo::type_two,
  229. 'crt_time' => $time,
  230. ];
  231. }
  232. ConstructionInfo::insert($insert);
  233. }
  234. if(! empty($data['product'])){
  235. $insert = [];
  236. foreach ($data['product'] as $value){
  237. $insert[] = [
  238. 'construction_id' => $model->id,
  239. 'product_id' => $value['product_id'],
  240. 'number' => $value['number'],
  241. 'cost' => $value['cost'] ?? 0,
  242. 'retail_price' => $value['retail_price'] ?? 0,
  243. 'mark' => $value['mark'] ?? '',
  244. 'crt_time' => $time,
  245. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  246. 'basic_type_id' => $value['basic_type_id'],
  247. 'price' => $value['price'],
  248. 'final_amount' => $value['final_amount'] ?? 0,
  249. ];
  250. }
  251. ConstructionProductInfo::insert($insert);
  252. //锁定库存
  253. ProductInventoryService::changeLockNumber($user,$msg[0],[]);
  254. }
  255. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  256. if(! empty($data['file'])){
  257. $insert = [];
  258. foreach ($data['file'] as $value){
  259. $insert[] = [
  260. 'order_number' => $data['order_number'],
  261. 'construction_id' => $model->id,
  262. 'file' => $value['url'],
  263. 'name' => $value['name'],
  264. 'mark' => $value['mark'] ?? "",
  265. 'crt_time' => $time,
  266. ];
  267. }
  268. ConstructionFile::insert($insert);
  269. }
  270. DB::commit();
  271. }catch (\Exception $exception){
  272. DB::rollBack();
  273. return [false,$exception->getMessage()];
  274. }
  275. (new OperationLogService())->setOperationList($data,$user);
  276. if(! empty($data['check'])) {
  277. list($status,$msg) = (new CheckService())->checkAll([
  278. "id" => $model->id,
  279. "order_number" => $data['order_number'],
  280. "opt_case" => CheckService::five,
  281. "menu_id" => $data['menu_id']
  282. ],$user);
  283. if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  284. }
  285. return [true,''];
  286. }
  287. /**
  288. * 施工订单删除
  289. * @param $data
  290. * @return array
  291. */
  292. public function constructionDel($data,$user){
  293. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  294. $construction = Construction::where('del_time',0)->where('id',$data['id'])->first();
  295. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  296. $construction = $construction->toArray();
  297. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单状态,操作失败'];
  298. $product_save = $this->getSaveDetail($data['id']);
  299. try {
  300. DB::beginTransaction();
  301. Construction::where('id',$data['id'])->update([
  302. 'del_time'=> time()
  303. ]);
  304. ConstructionInfo::where('del_time',0)
  305. ->where('construction_id',$data['id'])
  306. ->update(['del_time' => time()]);
  307. $old = ConstructionFile::where('del_time',0)
  308. ->where('construction_id',$data['id'])
  309. ->select('file')
  310. ->get()->toArray();
  311. $old = array_column($old,'file');
  312. ConstructionFile::where('del_time',0)
  313. ->where('construction_id',$data['id'])
  314. ->update(['del_time' => time()]);
  315. ConstructionProductInfo::where('del_time',0)
  316. ->where('construction_id',$data['id'])
  317. ->update(['del_time' => time()]);
  318. (new RangeService())->RangeDelete($data['id'],SeeRange::type_two);
  319. //锁定库存释放
  320. ProductInventoryService::changeLockNumber($user,[],$product_save);
  321. //排班修改
  322. // $schedule = ScheduleInfo::where('del_time',0)
  323. // ->where('schedule_id',$construction['schedule_id'])
  324. // ->where('start_time',$construction['day_start_stamp'])
  325. // ->where('end_time',$construction['day_end_stamp'])
  326. // ->where('is_use','>', ScheduleInfo::not_use)
  327. // ->first();
  328. // if(! empty($schedule)) ScheduleInfo::where('id',$schedule->id)->update(['is_use' => ScheduleInfo::not_use]);
  329. DB::commit();
  330. }catch (\Exception $exception){
  331. DB::rollBack();
  332. return [false,$exception->getMessage()];
  333. }
  334. $this->delStorageFile($old);
  335. return [true,''];
  336. }
  337. /**
  338. * 施工订单详情
  339. * @param $data
  340. * @return array
  341. */
  342. public function detail($data){
  343. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  344. if(! empty($data['id'])){
  345. $construction = Construction::where('del_time',0)
  346. ->where('id',$data['id'])
  347. ->first();
  348. }else{
  349. $construction = Construction::where('del_time',0)
  350. ->where('order_number',$data['order_number'])
  351. ->first();
  352. $data['id'] = empty($construction->id) ? 0 : $construction->id;
  353. }
  354. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  355. $construction = $construction->toArray();
  356. $construction['state_title'] = Construction::$name[$construction['state']] ?? '';
  357. $construction['pq_state_title'] = Construction::$pq_name[$construction['pq_state']] ?? '';
  358. if(! empty($construction['address1'])) {
  359. $tmp = json_decode($construction['address1'],true);
  360. $construction['address1'] = $tmp;
  361. $tmp = implode(' ',$tmp);
  362. $tmp .= ' ' . $construction['address2'];
  363. $address = $tmp;
  364. }else{
  365. $address = $construction['address2'];
  366. }
  367. $construction['address'] = $address;
  368. $start_time = $construction['start_time'] ? date("Y-m-d H:i",$construction['start_time']) : '';
  369. $end_time = $construction['end_time'] ? date("Y-m-d H:i",$construction['end_time']) : '';
  370. $construction['construction_period'] = $start_time . '——' . $end_time;
  371. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  372. $construction['sales_order_number'] = $sales;
  373. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  374. $construction['customer_title'] = $customer_title ?? "";
  375. $info = CustomerInfo::from('customer_info as a')
  376. ->leftJoin('basic_type as b','b.id','a.contact_type')
  377. ->where('a.del_time',0)
  378. ->where('a.customer_id',$construction['customer_id'])
  379. ->where('a.contact_type','>',0)
  380. ->select('b.title','a.contact_info')
  381. ->get()->toArray();
  382. $construction['customer_array'] = $info;
  383. $info = CustomerInfo::from('customer_info as a')
  384. ->leftJoin('basic_type as b','b.id','a.contact_type')
  385. ->where('a.del_time',0)
  386. ->where('a.customer_id',$construction['customer_id'])
  387. ->where('a.contact_type','>',0)
  388. ->select('b.title','a.contact_info')
  389. ->get()->toArray();
  390. $construction['customer_array'] = $info;
  391. $construction['storehouse_title'] = Storehouse::where('id',$construction['storehouse_id'])->value('title');
  392. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  393. $construction['customer_contact_title'] = $emp_title;
  394. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  395. $array = [
  396. $construction['install_method'],
  397. $construction['install_position'],
  398. $construction['urgency'],
  399. ];
  400. $basic_map = BasicType::whereIn('id',$array)
  401. ->pluck('title','id')
  402. ->toArray();
  403. $construction = [$construction];
  404. foreach ($construction as $key => $value){
  405. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  406. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  407. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  408. }
  409. $construction = $construction[0];
  410. $construction['file'] = [];
  411. $file = ConstructionFile::where('del_time',0)
  412. ->where('construction_id',$construction['id'])
  413. ->select('id','construction_id','file','name','mark')
  414. ->get()->toArray();
  415. foreach ($file as $value){
  416. $construction['file'][] = [
  417. 'url' => $value['file'],
  418. 'name' => $value['name'],
  419. 'mark' => $value['mark'],
  420. ];
  421. }
  422. $construction_info = ConstructionInfo::where('del_time',0)
  423. ->where('construction_id',$construction['id'])
  424. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  425. ->get()->toArray();
  426. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  427. ->pluck('emp_name','id')
  428. ->toArray();
  429. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  430. ->pluck('title','id')
  431. ->toArray();
  432. foreach ($construction_info as $value){
  433. if($value['type'] == ConstructionInfo::type_one){
  434. $tmp = [
  435. 'id' => $value['contact_type'],
  436. 'title' => $basic_map2[$value['contact_type']] ?? '',
  437. 'info' => $value['contact_info']
  438. ];
  439. $construction['construction_contact'][] = $tmp;
  440. }elseif ($value['type'] == ConstructionInfo::type_two){
  441. $tmp = [
  442. 'id' => $value['employee_id'],
  443. 'name' => $emp_map[$value['employee_id']] ?? '',
  444. ];
  445. $construction['employee_one'][] = $tmp;
  446. }
  447. }
  448. $p_info = ConstructionProductInfo::where('del_time',0)
  449. ->where('construction_id',$construction['id'])
  450. ->get()->toArray();
  451. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  452. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  453. foreach ($p_info as $value){
  454. $tmp = $map[$value['product_id']] ?? [];
  455. $value['title'] = $tmp['title'] ?? "";
  456. $value['code'] = $tmp['code'] ?? "";
  457. $value['size'] = $tmp['size'] ?? "";
  458. $value['unit'] = $tmp['unit'] ?? "";
  459. $value['bar_code'] = $tmp['bar_code'] ?? "";
  460. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  461. $construction['product'][] = $value;
  462. }
  463. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  464. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  465. //可见范围
  466. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  467. $construction['depart'] = $return[0] ?? [];
  468. $construction['employee'] = $return[1] ?? [];
  469. return [true, $construction];
  470. }
  471. /**
  472. * 施工订单列表
  473. * @param $data
  474. * @param $user
  475. * @return array
  476. */
  477. public function constructionList($data,$user){
  478. $model = Construction::Clear($user,$data);
  479. $model = $model->where('del_time',0)
  480. ->select('title','id','model_type','order_number','customer_id','customer_contact_id','install_method','install_position','sales_order_id','construction_fee','construction_time','handover_time','urgency','crt_id','crt_time','mark','state','address1','address2','introduction','service_price','storehouse_id','start_time','end_time','pq_state','day_start_stamp','day_end_stamp')
  481. ->orderby('id', 'desc');
  482. if(isset($data['state'])) $model->where('state', $data['state']);
  483. if(isset($data['pq_state'])) $model->where('pq_state', $data['pq_state']);
  484. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  485. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  486. if(! empty($data['time_type'])) {
  487. if($data['time_type'] == 1) {
  488. $start = strtotime('today');
  489. $end = strtotime('tomorrow') - 1;
  490. }elseif ($data['time_type'] == 2){
  491. $start = strtotime('this week',strtotime('today'));
  492. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  493. }
  494. if(! empty($start) && ! empty($end)) {
  495. $model->where('crt_time','>=',$start);
  496. $model->where('crt_time','<=',$end);
  497. }
  498. }
  499. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  500. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  501. $model->where('start_time','>=',$return[0]);
  502. $model->where('end_time','<=',$return[1]);
  503. }
  504. if(! empty($data['pq_period'][0]) && ! empty($data['pq_period'][1])) {
  505. $model->where('day_start_stamp','>=',$this->changeDateToDateMin($data['pq_period'][0]));
  506. $model->where('day_end_stamp','<=',$this->changeDateToDateMin($data['pq_period'][1]));
  507. }
  508. if(! empty($data['sale_order'])){
  509. $model2 = SalesOrder::Clear($user,$data);
  510. $sale = $model2->where('del_time',0)
  511. ->where('order_number', 'LIKE', '%'.$data['sale_order'].'%')
  512. ->select('id')
  513. ->get()->toArray();
  514. $model->whereIn('sales_order_id',array_unique(array_column($sale,'id')));
  515. }
  516. if(! empty($data['install_method'])) $model->where('install_method',$data['install_method']);
  517. if(! empty($data['install_position'])) $model->where('install_position',$data['install_position']);
  518. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  519. $list = $this->limit($model,'',$data);
  520. $list = $this->fillData($list);
  521. return [true, $list];
  522. }
  523. /**
  524. * 参数规则
  525. * @param $data
  526. * @param $user
  527. * @param $is_add
  528. * @return array
  529. */
  530. public function constructionRule(&$data, $user, $is_add = true){
  531. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  532. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  533. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  534. if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
  535. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  536. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  537. if(empty($sale)) return [false,'合同不存在或已被删除'];
  538. $sale = $sale->toArray();
  539. if($sale['state'] < SalesOrder::State_two) return [false,'合同未派单,不允许新建施工单'];
  540. list($status,$msg) = $this->limitingSendRequestBackgExpire("construction" . $sale['order_number']);
  541. if(! $status) return [false, $msg];
  542. if(empty($data['product'])) return [false,'请选择产品'];
  543. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  544. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  545. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  546. if(! empty($data['construction_fee'])){
  547. $res = $this->checkNumber($data['construction_fee']);
  548. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  549. }
  550. if(! empty($data['service_price'])){
  551. $res = $this->checkNumber($data['service_price']);
  552. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  553. }
  554. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  555. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  556. if($data['model_type'] == Construction::Model_type_one){
  557. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  558. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  559. }else{
  560. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  561. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  562. }
  563. //所属部门 以及 顶级部门
  564. if(empty($data['depart_id'])) {
  565. $data['depart_id'] = $this->getDepart($user);
  566. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  567. }
  568. $product_submit = $product_id = [];
  569. foreach ($data['product'] as $value){
  570. if(empty($value['number'])) return [false,'产品数量不能为空'];
  571. $res = $this->checkNumber($value['number']);
  572. if(! $res) return [false,'请输入正确的产品数量'];
  573. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  574. if(isset($product_submit[$key])){
  575. $product_submit[$key] += $value['number'];
  576. }else{
  577. $product_submit[$key] = $value['number'];
  578. }
  579. $product_id[] = $value['product_id'];
  580. }
  581. //剩余能施工
  582. $id = $data['id'] ?? 0;
  583. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  584. //比较
  585. foreach ($product_submit as $pro => $number){
  586. $tmp = explode(',',$pro);
  587. $p = $tmp[0];
  588. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  589. $s_number = $s_product[$p];
  590. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据'];
  591. }
  592. $id = $data['id'] ?? 0;
  593. $product_save = $this->getSaveDetail($id);
  594. //是否校验库存
  595. ProductInventoryService::is_check($user,$data);
  596. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  597. if(! $status) return [false, $msg];
  598. $start_time = date("Y-m-d H:i",$data['start_time']);
  599. $end_time = date("Y-m-d H:i",$data['end_time']);
  600. if($is_add){
  601. $bool = Construction::where('del_time',0)
  602. ->where('order_number',$data['order_number'])
  603. ->exists();
  604. if($bool) return [false,'工单编号已存在,请重新获取'];
  605. $bool = Construction::where('del_time',0)
  606. ->where('sales_order_id',$data['sales_order_id'])
  607. ->where('start_time', '<=', $data['end_time'])
  608. ->where('end_time', '>=', $data['start_time'])
  609. ->exists();
  610. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  611. }else{
  612. if(empty($data['id'])) return [false,'ID不能为空'];
  613. $bool = Construction::where('del_time',0)
  614. ->where('id','<>',$data['id'])
  615. ->where('sales_order_id',$data['sales_order_id'])
  616. ->where('start_time', '<=', $data['end_time'])
  617. ->where('end_time', '>=', $data['start_time'])
  618. ->exists();
  619. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  620. }
  621. return [true, [$product_submit, $product_save]];
  622. }
  623. /**
  624. * 数据拼接
  625. * @param $data
  626. * @return array
  627. */
  628. public function fillData($data){
  629. if(empty($data['data'])) return $data;
  630. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  631. $basic_map = BasicType::whereIn('id',$array)
  632. ->pluck('title','id')
  633. ->toArray();
  634. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  635. ->pluck('emp_name','id')
  636. ->toArray();
  637. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  638. ->pluck('title','id')
  639. ->toArray();
  640. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->select('order_number','id','handover_time')->get()->toArray();
  641. $sales_map = [];
  642. foreach ($sales as $value){
  643. $sales_map[$value['id']] = $value;
  644. }
  645. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  646. ->pluck('title','id')
  647. ->toArray();
  648. //分派的总社或分社
  649. $dispatch = $this->getDispatchData($data['data']);
  650. //施工产品
  651. $product_map = $this->getProduct($data['data']);
  652. foreach ($data['data'] as $key => $value){
  653. $product_tmp = $product_map[$value['id']] ?? [];
  654. $data['data'][$key]['product_detail'] = implode(',',$product_tmp);
  655. if(! empty($value['address1'])) {
  656. $tmp = json_decode($value['address1'],true);
  657. $tmp = implode(' ',$tmp);
  658. $tmp .= ' ' . $value['address2'];
  659. $address = $tmp;
  660. }else{
  661. $address = $value['address2'];
  662. }
  663. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  664. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  665. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  666. $data['data'][$key]['address'] = $address;
  667. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  668. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  669. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  670. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  671. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  672. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  673. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  674. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  675. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  676. $tmp_sales = $sales_map[$value['sales_order_id']] ?? [];
  677. // $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  678. $data['data'][$key]['sales_order_number'] = $tmp_sales['order_number'];
  679. $data['data'][$key]['handover_time'] = $value['handover_time'] ? date("Y-m-d") : "";
  680. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  681. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  682. $data['data'][$key]['pq_state_title'] = Construction::$pq_name[$value['pq_state']] ?? '';
  683. $str = "";
  684. $start_time = $value['day_start_stamp'] ? date("Y-m-d H:i",$value['day_start_stamp']) : '';
  685. $end_time = $value['day_end_stamp'] ? date("Y-m-d H:i",$value['day_end_stamp']) : '';
  686. if(! empty($start_time) && ! empty($end_time)) $str = $start_time . '——' . $end_time;
  687. $data['data'][$key]['pq_period'] = $str;
  688. }
  689. return $data;
  690. }
  691. public function getProduct($data){
  692. $search_id = array_column($data,'id');
  693. if(empty($search_id)) return [];
  694. $product = ConstructionProductInfo::where('del_time',0)
  695. ->whereIn('construction_id',$search_id)
  696. ->select('product_id','construction_id')
  697. ->get()->toArray();
  698. $product_map = Product::whereIn('id',array_unique(array_column($product,'product_id')))->pluck('title','id')->toArray();
  699. $return = [];
  700. foreach ($product as $value){
  701. $product_tmp = $product_map[$value['product_id']] ?? "";
  702. if($product_tmp) $return[$value['construction_id']][] = $product_tmp;
  703. }
  704. return $return;
  705. }
  706. public function getDispatchData($data){
  707. $search_id = [];
  708. foreach ($data as $value){
  709. $search_id[] = $value['sales_order_id'];
  710. }
  711. if(empty($search_id)) return [];
  712. $see = SeeRange::where('del_time',0)
  713. ->whereIn('data_id',$search_id)
  714. ->where('data_type',SeeRange::type_seven)
  715. ->where('type',SeeRange::data_three)
  716. ->select('data_id','param_id')
  717. ->get()->toArray();
  718. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  719. ->pluck('title','id')
  720. ->toArray();
  721. $see_array = [];
  722. foreach ($see as $value){
  723. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  724. }
  725. return $see_array;
  726. }
  727. /**
  728. * 获取施工单号
  729. * @param $data
  730. * @return array
  731. */
  732. public function constructionGet($data){
  733. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  734. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  735. $prefix = Construction::$prefix[$data['model_type']];
  736. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  737. if(! $order_number) return [false,'工单编号生成失败!'];
  738. return [true,['order_number' => $order_number]];
  739. }
  740. /**
  741. * 获取保存详情
  742. * @param $id
  743. * @return array
  744. */
  745. public function getSaveDetail($id){
  746. $product_save = [];
  747. if(empty($id)) return $product_save;
  748. $sub = ConstructionProductInfo::where('construction_id',$id)
  749. ->where('del_time',0)
  750. ->get()->toArray();
  751. foreach ($sub as $value){
  752. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  753. if(isset($product_save[$key])){
  754. $product_save[$key] += $value['number'];
  755. }else{
  756. $product_save[$key] = $value['number'];
  757. }
  758. }
  759. return $product_save;
  760. }
  761. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  762. $construction = Construction::where('del_time',0)
  763. ->where('sales_order_id',$sales_order_id)
  764. ->select('id')->get()->toArray();
  765. $construction_id = array_column($construction,'id');
  766. $product_save = [];
  767. $sub = ConstructionProductInfo::where('del_time',0)
  768. ->whereIn('construction_id',$construction_id)
  769. ->when(! empty($id), function ($query) use ($id) {
  770. return $query->where('construction_id', '<>',$id);
  771. })
  772. ->get()->toArray();
  773. foreach ($sub as $value){
  774. if(isset($product_save[$value['product_id']])){
  775. $product_save[$value['product_id']] += $value['number'];
  776. }else{
  777. $product_save[$value['product_id']] = $value['number'];
  778. }
  779. }
  780. $sales_order_product = [];
  781. $sales_product = SalesOrderProductInfo::where('del_time',0)
  782. ->where('sales_order_id',$sales_order_id)
  783. ->get()->toArray();
  784. foreach ($sales_product as $value){
  785. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  786. if(isset($sales_order_product[$value['product_id']])){
  787. $sales_order_product[$value['product_id']] += $value['number'];
  788. }else{
  789. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
  790. }
  791. }
  792. return $sales_order_product;
  793. }
  794. public function deliveryNoteEdit($data,$user){
  795. $id = $user['id'];
  796. if(isset($data['id'])) {
  797. $model = DeliveryNote::where('id',$data['id'])->first();
  798. if($model->img3) return [false,'客户已签字,无法编辑!'];
  799. if(DeliveryNote::where('del_time',0)->where('construction_order_number',$data['construction_order_number'])->where('id','<>',$data['id'])->first()) return [false,'施工单客户确认单已存在!'];
  800. }
  801. else {
  802. $model = new DeliveryNote();
  803. $model->crt_id = $id;
  804. }
  805. try {
  806. $model->upd_id = $id;
  807. $model->construction_order_number = $data['construction_order_number'] ?? '';
  808. $model->start_time = $data['start_time'] ?? '';
  809. $model->end_time = $data['end_time'] ?? '';
  810. $model->vin_no = $data['vin_no'] ?? '';
  811. $model->system = $data['system'] ?? '';
  812. $model->mile = $data['mile'] ?? '';
  813. $model->is_wait = $data['is_wait'] ?? '';
  814. $model->customer_name = $data['customer_name'] ?? '';
  815. $model->customer_mobile = $data['customer_mobile'] ?? '';
  816. $model->sale_man = $data['sale_man'] ?? '';
  817. $model->install_man = $data['install_man'] ?? '';
  818. $model->is_brash = $data['is_brash'] ?? '';
  819. $model->is_chong = $data['is_chong'] ?? '';
  820. $model->service_mark = $data['service_mark'] ?? '';
  821. $model->mark = $data['mark'] ?? '';
  822. $model->break = $data['break'] ? json_encode($data['break']):json_encode([]);
  823. $model->break_mark = $data['break_mark'] ? json_encode($data['break_mark']):json_encode([]);
  824. $model->project_id = $data['project_id'] ? json_encode($data['project_id']):json_encode([]);
  825. $model->other_project_mark = $data['other_project_mark'] ?? '';
  826. $model->img = $data['img'] ?? '';
  827. $model->img1 = $data['img1'] ?? '';
  828. $model->img2 = $data['img2'] ?? '';
  829. $model->img3 = $data['img3'] ?? '';
  830. $model->save();
  831. return [true,'保存成功!'];
  832. }catch (\Throwable $e){
  833. return [true,'保存成功!'];
  834. }
  835. }
  836. public function deliveryNoteList($data)
  837. {
  838. $list = DeliveryNote::where('del_time',0);
  839. $list = $this->limit($list,'*',$data);
  840. return [true,$list];
  841. }
  842. public function deliveryNoteDetail($data){
  843. if(isset($data['id'])) $model = DeliveryNote::where('id',$data['id'])->where('del_time',0)->first();
  844. if(isset($data['construction_order_number'])) $model = DeliveryNote::where('id',$data['construction_order_number'])->where('del_time',0)->first();
  845. if(empty($model)) return [false,'数据不存在!'];
  846. $employee_key_list = Employee::pluck('emp_name','id')->toArray();
  847. $detail = $model->toArray();
  848. $detail['install_man_title'] = $employee_key_list[$detail['install_man']] ?? "";
  849. $detail['sale_man_title'] = $employee_key_list[$detail['sale_man']] ?? "";
  850. $img_list = ConstructionFile::where('order_number',$detail['construction_order_number'])->where('del_time',0)->select('file as url','name','mark')->get()->toArray();
  851. $detail['file'] = $img_list;
  852. return [true,$detail];
  853. }
  854. public function deliveryNoteDel($data,$user){
  855. DeliveryNote::where('id',$data['id'])->update(
  856. [
  857. 'del_time' => time(),
  858. 'upd_id' => $user['id'],
  859. ]
  860. );
  861. return [true,'删除成功!'];
  862. }
  863. public function constructionPdf($data, $user){
  864. if(empty($data['id'])) return [false, '请选择下载的施工单'];
  865. $construction = Construction::where('del_time',0)
  866. ->where('id',$data['id'])
  867. ->first();
  868. if(empty($construction)) return [false, '施工单不存在或已被删除'];
  869. $construction = $construction->toArray();
  870. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  871. $construction['customer_title'] = $customer_title ?? "";
  872. $construction['sales_order_number'] = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number') ?? '';
  873. $construction['handover_time'] = $construction['handover_time'] ? date("Y-m-d H:i:s",$construction['handover_time']): '';
  874. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  875. $construction_info = ConstructionInfo::where('del_time',0)
  876. ->where('construction_id',$construction['id'])
  877. ->where('type',CustomerInfo::type_two)
  878. ->select('id','employee_id')
  879. ->get()->toArray();
  880. $emp_list = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  881. ->select('emp_name','id','mobile')
  882. ->get()->toArray();
  883. $emp_map = [];
  884. foreach ($emp_list as $value){
  885. $emp_map[$value['id']] = [
  886. 'name' => $value['emp_name'],
  887. 'mobile' => $value['mobile'],
  888. ];
  889. }
  890. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  891. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  892. ->select('a.employee_id', 'b.title')
  893. ->whereIn('employee_id',array_column($emp_list,'id'))
  894. ->get()->toArray();
  895. $emp_message_map = [];
  896. foreach ($emp_message as $value){
  897. if(isset($emp_message_map[$value['employee_id']])){
  898. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  899. }else{
  900. $emp_message_map[$value['employee_id']] = $value['title'];
  901. }
  902. }
  903. $crt_name = ($emp_map[$construction['crt_id']]['name'] ?? '') . ' ' . ($emp_message_map[$construction['crt_id']] ?? '') . ' ' . ($emp_map[$construction['crt_id']]['mobile'] ?? '');
  904. $construction['crt_name'] = $crt_name;
  905. $xt_name = "";
  906. foreach ($construction_info as $value){
  907. $xt_name .= ($emp_map[$value['employee_id']]['name'] ?? '') . ' ' . ($emp_message_map[$value['employee_id']] ?? '') . ' ' . ($emp_map[$value['employee_id']]['mobile'] ?? '') . " | ";
  908. }
  909. $xt_name = rtrim($xt_name, " | ");
  910. $construction['xt_name'] = $xt_name;
  911. $p_info = ConstructionProductInfo::where('del_time',0)
  912. ->where('construction_id',$construction['id'])
  913. ->get()->toArray();
  914. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  915. $unit_map = BasicType::whereIn('id',array_column($map,'unit'))
  916. ->pluck('title','id')->toArray();
  917. $product = [];
  918. $count = 0;
  919. foreach ($p_info as $value){
  920. $tmp = $map[$value['product_id']] ?? [];
  921. $value['count'] = $count+1;
  922. $value['title'] = $tmp['title'] ?? "";
  923. $value['code'] = $tmp['code'] ?? "";
  924. $value['size'] = $tmp['size'] ?? "";
  925. $value['unit'] = $unit_map[$tmp['unit']] ?? "";
  926. $product[] = $value;
  927. }
  928. //工单进展
  929. $detail = $this->getOaJz($data,$user);
  930. $order_message = ['order' => $construction, 'product' => $product,'detail'=> $detail];
  931. $pdf = app('dompdf.wrapper')->loadView('pdf.construction', $order_message);
  932. $file_name = time().rand(1000,9999);
  933. $filename = '施工单_' . $file_name.'.' . 'pdf';
  934. $pdf->save(storage_path('app/public/export/' . $filename));
  935. return [true, ['file' => $filename]];
  936. }
  937. public function getOaJz($data,$user)
  938. {
  939. $oa = Oa::where('menu_id', 34)
  940. ->where('del_time',0)
  941. ->where('channel',$user['depart_top'][0]['depart_id'])
  942. ->first();
  943. if(empty($oa)) return [];
  944. $oa = $oa->toArray();
  945. $list = OaSub::where('oa_id', $oa['id'])->where('del_time', 0)->get()->toArray();
  946. $oa_sub_ids = [];
  947. foreach ($list as $v) {
  948. $oa_sub_ids[] = $v['id'];
  949. }
  950. $oaEmployee = OaSubEmployee::wherein('oa_sub_id', $oa_sub_ids)->get()->toArray();
  951. $emp_list = Employee::whereIn('id',array_column($oaEmployee,'employee_id'))
  952. ->select('emp_name','id','mobile')
  953. ->get()->toArray();
  954. $emp_map = [];
  955. foreach ($emp_list as $value){
  956. $emp_map[$value['id']] = [
  957. 'name' => $value['emp_name'],
  958. 'mobile' => $value['mobile'],
  959. ];
  960. }
  961. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  962. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  963. ->whereIn('a.employee_id',array_column($emp_list,'id'))
  964. ->select('a.employee_id', 'b.title')
  965. ->get()->toArray();
  966. $emp_message_map = [];
  967. foreach ($emp_message as $value){
  968. if(isset($emp_message_map[$value['employee_id']])){
  969. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  970. }else{
  971. $emp_message_map[$value['employee_id']] = $value['title'];
  972. }
  973. }
  974. $oaEmployeeKey = [];
  975. foreach ($oaEmployee as $v) {
  976. $str = "";
  977. $tmp = $emp_map[$v['employee_id']] ?? [];
  978. $tmp2 = $emp_message_map[$v['employee_id']] ?? "";
  979. $str = $tmp['name'] . " " . $tmp2 . " " . $tmp['mobile'];
  980. $oaEmployeeKey[$v['oa_sub_id']][] = [
  981. 'id' => $v['employee_id'],
  982. 'emp_name' => $str,
  983. ];
  984. }
  985. $return = [];
  986. foreach ($list as $v) {
  987. $return[$v['sort']][] = [
  988. 'emp_id' => $oaEmployeeKey[$v['id']],
  989. 'index' => $v['h5_key'],
  990. ];
  991. }
  992. $detail = [];
  993. foreach ($return as $v) {
  994. $children = "";
  995. foreach ($v as $vv) {
  996. $children .= implode('|',array_column($vv['emp_id'],'emp_name')) . ',';
  997. }
  998. $detail[] = rtrim($children,',');
  999. }
  1000. return $detail;
  1001. }
  1002. }