ConstructionService.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. $address_map = config('address');
  359. $address_str = [];
  360. if(! empty($construction['address1'])) {
  361. $tmp = json_decode($construction['address1'],true);
  362. $construction['address1'] = $tmp;
  363. $this->findLabelsByValue($address_map,$tmp,$address_str);
  364. $tmp = implode(' ',$address_str);
  365. $tmp .= ' ' . $construction['address2'];
  366. $address = $tmp;
  367. }else{
  368. $address = $construction['address2'];
  369. }
  370. $construction['address'] = $address;
  371. $start_time = $construction['start_time'] ? date("Y-m-d H:i",$construction['start_time']) : '';
  372. $end_time = $construction['end_time'] ? date("Y-m-d H:i",$construction['end_time']) : '';
  373. $construction['construction_period'] = $start_time . '——' . $end_time;
  374. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  375. $construction['sales_order_number'] = $sales;
  376. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  377. $construction['customer_title'] = $customer_title ?? "";
  378. $info = CustomerInfo::from('customer_info as a')
  379. ->leftJoin('basic_type as b','b.id','a.contact_type')
  380. ->where('a.del_time',0)
  381. ->where('a.customer_id',$construction['customer_id'])
  382. ->where('a.contact_type','>',0)
  383. ->select('b.title','a.contact_info')
  384. ->get()->toArray();
  385. $construction['customer_array'] = $info;
  386. $info = CustomerInfo::from('customer_info as a')
  387. ->leftJoin('basic_type as b','b.id','a.contact_type')
  388. ->where('a.del_time',0)
  389. ->where('a.customer_id',$construction['customer_id'])
  390. ->where('a.contact_type','>',0)
  391. ->select('b.title','a.contact_info')
  392. ->get()->toArray();
  393. $construction['customer_array'] = $info;
  394. $construction['storehouse_title'] = Storehouse::where('id',$construction['storehouse_id'])->value('title');
  395. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  396. $construction['customer_contact_title'] = $emp_title;
  397. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  398. $array = [
  399. $construction['install_method'],
  400. $construction['install_position'],
  401. $construction['urgency'],
  402. ];
  403. $basic_map = BasicType::whereIn('id',$array)
  404. ->pluck('title','id')
  405. ->toArray();
  406. $construction = [$construction];
  407. foreach ($construction as $key => $value){
  408. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  409. $construction[$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  410. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  411. }
  412. $construction = $construction[0];
  413. $construction['file'] = [];
  414. $file = ConstructionFile::where('del_time',0)
  415. ->where('construction_id',$construction['id'])
  416. ->select('id','construction_id','file','name','mark')
  417. ->get()->toArray();
  418. foreach ($file as $value){
  419. $construction['file'][] = [
  420. 'url' => $value['file'],
  421. 'name' => $value['name'],
  422. 'mark' => $value['mark'],
  423. ];
  424. }
  425. $construction_info = ConstructionInfo::where('del_time',0)
  426. ->where('construction_id',$construction['id'])
  427. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  428. ->get()->toArray();
  429. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  430. ->pluck('emp_name','id')
  431. ->toArray();
  432. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  433. ->pluck('title','id')
  434. ->toArray();
  435. foreach ($construction_info as $value){
  436. if($value['type'] == ConstructionInfo::type_one){
  437. $tmp = [
  438. 'id' => $value['contact_type'],
  439. 'title' => $basic_map2[$value['contact_type']] ?? '',
  440. 'info' => $value['contact_info']
  441. ];
  442. $construction['construction_contact'][] = $tmp;
  443. }elseif ($value['type'] == ConstructionInfo::type_two){
  444. $tmp = [
  445. 'id' => $value['employee_id'],
  446. 'name' => $emp_map[$value['employee_id']] ?? '',
  447. ];
  448. $construction['employee_one'][] = $tmp;
  449. }
  450. }
  451. $p_info = ConstructionProductInfo::where('del_time',0)
  452. ->where('construction_id',$construction['id'])
  453. ->get()->toArray();
  454. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  455. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  456. foreach ($p_info as $value){
  457. $tmp = $map[$value['product_id']] ?? [];
  458. $value['title'] = $tmp['title'] ?? "";
  459. $value['code'] = $tmp['code'] ?? "";
  460. $value['size'] = $tmp['size'] ?? "";
  461. $value['unit'] = $tmp['unit'] ?? "";
  462. $value['bar_code'] = $tmp['bar_code'] ?? "";
  463. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  464. $construction['product'][] = $value;
  465. }
  466. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  467. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  468. //可见范围
  469. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_two);
  470. $construction['depart'] = $return[0] ?? [];
  471. $construction['employee'] = $return[1] ?? [];
  472. return [true, $construction];
  473. }
  474. /**
  475. * 施工订单列表
  476. * @param $data
  477. * @param $user
  478. * @return array
  479. */
  480. public function constructionList($data,$user){
  481. $model = Construction::Clear($user,$data);
  482. $model = $model->where('del_time',0)
  483. ->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')
  484. ->orderby('id', 'desc');
  485. if(isset($data['state'])) $model->where('state', $data['state']);
  486. if(isset($data['pq_state'])) $model->where('pq_state', $data['pq_state']);
  487. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  488. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  489. if(! empty($data['time_type'])) {
  490. if($data['time_type'] == 1) {
  491. $start = strtotime('today');
  492. $end = strtotime('tomorrow') - 1;
  493. }elseif ($data['time_type'] == 2){
  494. $start = strtotime('this week',strtotime('today'));
  495. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  496. }
  497. if(! empty($start) && ! empty($end)) {
  498. $model->where('crt_time','>=',$start);
  499. $model->where('crt_time','<=',$end);
  500. }
  501. }
  502. if(! empty($data['construction_period'][0]) && ! empty($data['construction_period'][1])) {
  503. $return = $this->changeDateToTimeStampAboutRange($data['construction_period']);
  504. $model->where('start_time','>=',$return[0]);
  505. $model->where('end_time','<=',$return[1]);
  506. }
  507. if(! empty($data['pq_period'][0]) && ! empty($data['pq_period'][1])) {
  508. $model->where('day_start_stamp','>=',$this->changeDateToDateMin($data['pq_period'][0]));
  509. $model->where('day_end_stamp','<=',$this->changeDateToDateMin($data['pq_period'][1]));
  510. }
  511. if(! empty($data['sale_order'])){
  512. $model2 = SalesOrder::Clear($user,$data);
  513. $sale = $model2->where('del_time',0)
  514. ->where('order_number', 'LIKE', '%'.$data['sale_order'].'%')
  515. ->select('id')
  516. ->get()->toArray();
  517. $model->whereIn('sales_order_id',array_unique(array_column($sale,'id')));
  518. }
  519. if(! empty($data['install_method'])) $model->where('install_method',$data['install_method']);
  520. if(! empty($data['install_position'])) $model->where('install_position',$data['install_position']);
  521. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  522. $list = $this->limit($model,'',$data);
  523. $list = $this->fillData($list);
  524. return [true, $list];
  525. }
  526. /**
  527. * 参数规则
  528. * @param $data
  529. * @param $user
  530. * @param $is_add
  531. * @return array
  532. */
  533. public function constructionRule(&$data, $user, $is_add = true){
  534. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  535. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  536. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  537. if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
  538. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  539. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  540. if(empty($sale)) return [false,'合同不存在或已被删除'];
  541. $sale = $sale->toArray();
  542. if($sale['state'] < SalesOrder::State_two) return [false,'合同未通过确认,不允许新建施工单'];
  543. if($sale['top_depart_id'] == $user['head']['id']){
  544. //总社的订单 需要派单 才能建施工
  545. if($sale['state'] < SalesOrder::State_four) return [false,'合同未门店派单,不允许新建施工单'];
  546. }
  547. list($status,$msg) = $this->limitingSendRequestBackgExpire("construction" . $sale['order_number']);
  548. if(! $status) return [false, $msg];
  549. if(empty($data['product'])) return [false,'请选择产品'];
  550. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  551. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  552. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  553. if(! empty($data['construction_fee'])){
  554. $res = $this->checkNumber($data['construction_fee']);
  555. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  556. }
  557. if(! empty($data['service_price'])){
  558. $res = $this->checkNumber($data['service_price']);
  559. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  560. }
  561. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  562. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  563. if($data['model_type'] == Construction::Model_type_one){
  564. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  565. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  566. }else{
  567. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  568. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  569. }
  570. //所属部门 以及 顶级部门
  571. if(empty($data['depart_id'])) {
  572. $data['depart_id'] = $this->getDepart($user);
  573. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  574. }
  575. $product_submit = $product_id = [];
  576. foreach ($data['product'] as $value){
  577. if(empty($value['number'])) return [false,'产品数量不能为空'];
  578. $res = $this->checkNumber($value['number']);
  579. if(! $res) return [false,'请输入正确的产品数量'];
  580. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  581. if(isset($product_submit[$key])){
  582. $product_submit[$key] += $value['number'];
  583. }else{
  584. $product_submit[$key] = $value['number'];
  585. }
  586. $product_id[] = $value['product_id'];
  587. }
  588. //剩余能施工
  589. $id = $data['id'] ?? 0;
  590. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  591. //比较
  592. foreach ($product_submit as $pro => $number){
  593. $tmp = explode(',',$pro);
  594. $p = $tmp[0];
  595. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  596. $s_number = $s_product[$p];
  597. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据'];
  598. }
  599. $id = $data['id'] ?? 0;
  600. $product_save = $this->getSaveDetail($id);
  601. //是否校验库存
  602. ProductInventoryService::is_check($user,$data);
  603. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  604. if(! $status) return [false, $msg];
  605. $start_time = date("Y-m-d H:i",$data['start_time']);
  606. $end_time = date("Y-m-d H:i",$data['end_time']);
  607. if($is_add){
  608. $bool = Construction::where('del_time',0)
  609. ->where('order_number',$data['order_number'])
  610. ->exists();
  611. if($bool) return [false,'工单编号已存在,请重新获取'];
  612. $bool = Construction::where('del_time',0)
  613. ->where('sales_order_id',$data['sales_order_id'])
  614. ->where('start_time', '<=', $data['end_time'])
  615. ->where('end_time', '>=', $data['start_time'])
  616. ->exists();
  617. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  618. }else{
  619. if(empty($data['id'])) return [false,'ID不能为空'];
  620. $bool = Construction::where('del_time',0)
  621. ->where('id','<>',$data['id'])
  622. ->where('sales_order_id',$data['sales_order_id'])
  623. ->where('start_time', '<=', $data['end_time'])
  624. ->where('end_time', '>=', $data['start_time'])
  625. ->exists();
  626. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  627. }
  628. return [true, [$product_submit, $product_save]];
  629. }
  630. /**
  631. * 数据拼接
  632. * @param $data
  633. * @return array
  634. */
  635. public function fillData($data){
  636. if(empty($data['data'])) return $data;
  637. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  638. $basic_map = BasicType::whereIn('id',$array)
  639. ->pluck('title','id')
  640. ->toArray();
  641. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  642. ->pluck('emp_name','id')
  643. ->toArray();
  644. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  645. ->pluck('title','id')
  646. ->toArray();
  647. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->select('order_number','id','handover_time')->get()->toArray();
  648. $sales_map = [];
  649. foreach ($sales as $value){
  650. $sales_map[$value['id']] = $value;
  651. }
  652. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  653. ->pluck('title','id')
  654. ->toArray();
  655. //分派的总社或分社
  656. $dispatch = $this->getDispatchData($data['data']);
  657. //施工产品
  658. $product_map = $this->getProduct($data['data']);
  659. $address_map = config('address');
  660. foreach ($data['data'] as $key => $value){
  661. $address_str = [];
  662. $product_tmp = $product_map[$value['id']] ?? [];
  663. $data['data'][$key]['product_detail'] = implode(',',$product_tmp);
  664. if(! empty($value['address1'])) {
  665. $tmp = json_decode($value['address1'],true);
  666. $this->findLabelsByValue($address_map,$tmp,$address_str);
  667. $tmp = implode(' ',$address_str);
  668. $tmp .= ' ' . $value['address2'];
  669. $address = $tmp;
  670. }else{
  671. $address = $value['address2'];
  672. }
  673. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  674. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  675. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  676. $data['data'][$key]['address'] = $address;
  677. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  678. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  679. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  680. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  681. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  682. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  683. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  684. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  685. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  686. $tmp_sales = $sales_map[$value['sales_order_id']] ?? [];
  687. // $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  688. $data['data'][$key]['sales_order_number'] = $tmp_sales['order_number'];
  689. $data['data'][$key]['handover_time'] = $value['handover_time'] ? date("Y-m-d") : "";
  690. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  691. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  692. // $data['data'][$key]['pq_state_title'] = Construction::$pq_name[$value['pq_state']] ?? '';
  693. // $str = "";
  694. // $start_time = $value['day_start_stamp'] ? date("Y-m-d H:i",$value['day_start_stamp']) : '';
  695. // $end_time = $value['day_end_stamp'] ? date("Y-m-d H:i",$value['day_end_stamp']) : '';
  696. // if(! empty($start_time) && ! empty($end_time)) $str = $start_time . '——' . $end_time;
  697. // $data['data'][$key]['pq_period'] = $str;
  698. }
  699. return $data;
  700. }
  701. public function getProduct($data){
  702. $search_id = array_column($data,'id');
  703. if(empty($search_id)) return [];
  704. $product = ConstructionProductInfo::where('del_time',0)
  705. ->whereIn('construction_id',$search_id)
  706. ->select('product_id','construction_id')
  707. ->get()->toArray();
  708. $product_map = Product::whereIn('id',array_unique(array_column($product,'product_id')))->pluck('title','id')->toArray();
  709. $return = [];
  710. foreach ($product as $value){
  711. $product_tmp = $product_map[$value['product_id']] ?? "";
  712. if($product_tmp) $return[$value['construction_id']][] = $product_tmp;
  713. }
  714. return $return;
  715. }
  716. public function getDispatchData($data){
  717. $search_id = [];
  718. foreach ($data as $value){
  719. $search_id[] = $value['sales_order_id'];
  720. }
  721. if(empty($search_id)) return [];
  722. $see = SeeRange::where('del_time',0)
  723. ->whereIn('data_id',$search_id)
  724. ->where('data_type',SeeRange::type_seven)
  725. ->where('type',SeeRange::data_three)
  726. ->select('data_id','param_id')
  727. ->get()->toArray();
  728. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  729. ->pluck('title','id')
  730. ->toArray();
  731. $see_array = [];
  732. foreach ($see as $value){
  733. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  734. }
  735. return $see_array;
  736. }
  737. /**
  738. * 获取施工单号
  739. * @param $data
  740. * @return array
  741. */
  742. public function constructionGet($data){
  743. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  744. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  745. $prefix = Construction::$prefix[$data['model_type']];
  746. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  747. if(! $order_number) return [false,'工单编号生成失败!'];
  748. return [true,['order_number' => $order_number]];
  749. }
  750. /**
  751. * 获取保存详情
  752. * @param $id
  753. * @return array
  754. */
  755. public function getSaveDetail($id){
  756. $product_save = [];
  757. if(empty($id)) return $product_save;
  758. $sub = ConstructionProductInfo::where('construction_id',$id)
  759. ->where('del_time',0)
  760. ->get()->toArray();
  761. foreach ($sub as $value){
  762. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  763. if(isset($product_save[$key])){
  764. $product_save[$key] += $value['number'];
  765. }else{
  766. $product_save[$key] = $value['number'];
  767. }
  768. }
  769. return $product_save;
  770. }
  771. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  772. $construction = Construction::where('del_time',0)
  773. ->where('sales_order_id',$sales_order_id)
  774. ->select('id')->get()->toArray();
  775. $construction_id = array_column($construction,'id');
  776. $product_save = [];
  777. $sub = ConstructionProductInfo::where('del_time',0)
  778. ->whereIn('construction_id',$construction_id)
  779. ->when(! empty($id), function ($query) use ($id) {
  780. return $query->where('construction_id', '<>',$id);
  781. })
  782. ->get()->toArray();
  783. foreach ($sub as $value){
  784. if(isset($product_save[$value['product_id']])){
  785. $product_save[$value['product_id']] += $value['number'];
  786. }else{
  787. $product_save[$value['product_id']] = $value['number'];
  788. }
  789. }
  790. $sales_order_product = [];
  791. $sales_product = SalesOrderProductInfo::where('del_time',0)
  792. ->where('sales_order_id',$sales_order_id)
  793. ->get()->toArray();
  794. foreach ($sales_product as $value){
  795. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  796. if(isset($sales_order_product[$value['product_id']])){
  797. $sales_order_product[$value['product_id']] += $value['number'];
  798. }else{
  799. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
  800. }
  801. }
  802. return $sales_order_product;
  803. }
  804. public function deliveryNoteEdit($data,$user){
  805. $id = $user['id'];
  806. if(isset($data['id'])) {
  807. $model = DeliveryNote::where('id',$data['id'])->first();
  808. if($model->img3) return [false,'客户已签字,无法编辑!'];
  809. if(DeliveryNote::where('del_time',0)->where('construction_order_number',$data['construction_order_number'])->where('id','<>',$data['id'])->first()) return [false,'施工单客户确认单已存在!'];
  810. }
  811. else {
  812. $model = new DeliveryNote();
  813. $model->crt_id = $id;
  814. }
  815. try {
  816. $model->upd_id = $id;
  817. $model->construction_order_number = $data['construction_order_number'] ?? '';
  818. $model->start_time = $data['start_time'] ?? '';
  819. $model->end_time = $data['end_time'] ?? '';
  820. $model->vin_no = $data['vin_no'] ?? '';
  821. $model->system = $data['system'] ?? '';
  822. $model->mile = $data['mile'] ?? '';
  823. $model->is_wait = $data['is_wait'] ?? '';
  824. $model->customer_name = $data['customer_name'] ?? '';
  825. $model->customer_mobile = $data['customer_mobile'] ?? '';
  826. $model->sale_man = $data['sale_man'] ?? '';
  827. $model->install_man = $data['install_man'] ?? '';
  828. $model->is_brash = $data['is_brash'] ?? '';
  829. $model->is_chong = $data['is_chong'] ?? '';
  830. $model->service_mark = $data['service_mark'] ?? '';
  831. $model->mark = $data['mark'] ?? '';
  832. $model->break = $data['break'] ? json_encode($data['break']):json_encode([]);
  833. $model->break_mark = $data['break_mark'] ? json_encode($data['break_mark']):json_encode([]);
  834. $model->project_id = $data['project_id'] ? json_encode($data['project_id']):json_encode([]);
  835. $model->other_project_mark = $data['other_project_mark'] ?? '';
  836. $model->img = $data['img'] ?? '';
  837. $model->img1 = $data['img1'] ?? '';
  838. $model->img2 = $data['img2'] ?? '';
  839. $model->img3 = $data['img3'] ?? '';
  840. $model->save();
  841. return [true,'保存成功!'];
  842. }catch (\Throwable $e){
  843. return [true,'保存成功!'];
  844. }
  845. }
  846. public function deliveryNoteList($data)
  847. {
  848. $list = DeliveryNote::where('del_time',0);
  849. $list = $this->limit($list,'*',$data);
  850. return [true,$list];
  851. }
  852. public function deliveryNoteDetail($data){
  853. if(isset($data['id'])) $model = DeliveryNote::where('id',$data['id'])->where('del_time',0)->first();
  854. if(isset($data['construction_order_number'])) $model = DeliveryNote::where('id',$data['construction_order_number'])->where('del_time',0)->first();
  855. if(empty($model)) return [false,'数据不存在!'];
  856. $employee_key_list = Employee::pluck('emp_name','id')->toArray();
  857. $detail = $model->toArray();
  858. $detail['install_man_title'] = $employee_key_list[$detail['install_man']] ?? "";
  859. $detail['sale_man_title'] = $employee_key_list[$detail['sale_man']] ?? "";
  860. $img_list = ConstructionFile::where('order_number',$detail['construction_order_number'])->where('del_time',0)->select('file as url','name','mark')->get()->toArray();
  861. $detail['file'] = $img_list;
  862. return [true,$detail];
  863. }
  864. public function deliveryNoteDel($data,$user){
  865. DeliveryNote::where('id',$data['id'])->update(
  866. [
  867. 'del_time' => time(),
  868. 'upd_id' => $user['id'],
  869. ]
  870. );
  871. return [true,'删除成功!'];
  872. }
  873. public function constructionPdf($data, $user){
  874. if(empty($data['id'])) return [false, '请选择下载的施工单'];
  875. $construction = Construction::where('del_time',0)
  876. ->where('id',$data['id'])
  877. ->first();
  878. if(empty($construction)) return [false, '施工单不存在或已被删除'];
  879. $construction = $construction->toArray();
  880. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  881. $construction['customer_title'] = $customer_title ?? "";
  882. $construction['sales_order_number'] = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number') ?? '';
  883. $construction['handover_time'] = $construction['handover_time'] ? date("Y-m-d H:i:s",$construction['handover_time']): '';
  884. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  885. $construction_info = ConstructionInfo::where('del_time',0)
  886. ->where('construction_id',$construction['id'])
  887. ->where('type',CustomerInfo::type_two)
  888. ->select('id','employee_id')
  889. ->get()->toArray();
  890. $emp_list = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  891. ->select('emp_name','id','mobile')
  892. ->get()->toArray();
  893. $emp_map = [];
  894. foreach ($emp_list as $value){
  895. $emp_map[$value['id']] = [
  896. 'name' => $value['emp_name'],
  897. 'mobile' => $value['mobile'],
  898. ];
  899. }
  900. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  901. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  902. ->select('a.employee_id', 'b.title')
  903. ->whereIn('employee_id',array_column($emp_list,'id'))
  904. ->get()->toArray();
  905. $emp_message_map = [];
  906. foreach ($emp_message as $value){
  907. if(isset($emp_message_map[$value['employee_id']])){
  908. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  909. }else{
  910. $emp_message_map[$value['employee_id']] = $value['title'];
  911. }
  912. }
  913. $crt_name = ($emp_map[$construction['crt_id']]['name'] ?? '') . ' ' . ($emp_message_map[$construction['crt_id']] ?? '') . ' ' . ($emp_map[$construction['crt_id']]['mobile'] ?? '');
  914. $construction['crt_name'] = $crt_name;
  915. $xt_name = "";
  916. foreach ($construction_info as $value){
  917. $xt_name .= ($emp_map[$value['employee_id']]['name'] ?? '') . ' ' . ($emp_message_map[$value['employee_id']] ?? '') . ' ' . ($emp_map[$value['employee_id']]['mobile'] ?? '') . " | ";
  918. }
  919. $xt_name = rtrim($xt_name, " | ");
  920. $construction['xt_name'] = $xt_name;
  921. $p_info = ConstructionProductInfo::where('del_time',0)
  922. ->where('construction_id',$construction['id'])
  923. ->get()->toArray();
  924. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  925. $unit_map = BasicType::whereIn('id',array_column($map,'unit'))
  926. ->pluck('title','id')->toArray();
  927. $product = [];
  928. $count = 0;
  929. foreach ($p_info as $value){
  930. $tmp = $map[$value['product_id']] ?? [];
  931. $value['count'] = $count+1;
  932. $value['title'] = $tmp['title'] ?? "";
  933. $value['code'] = $tmp['code'] ?? "";
  934. $value['size'] = $tmp['size'] ?? "";
  935. $value['unit'] = $unit_map[$tmp['unit']] ?? "";
  936. $product[] = $value;
  937. }
  938. //工单进展
  939. $detail = $this->getOaJz($data,$user);
  940. $order_message = ['order' => $construction, 'product' => $product,'detail'=> $detail];
  941. $pdf = app('dompdf.wrapper')->loadView('pdf.construction', $order_message);
  942. $file_name = time().rand(1000,9999);
  943. $filename = '施工单_' . $file_name.'.' . 'pdf';
  944. $pdf->save(storage_path('app/public/export/' . $filename));
  945. return [true, ['file' => $filename]];
  946. }
  947. public function getOaJz($data,$user)
  948. {
  949. $oa = Oa::where('menu_id', 34)
  950. ->where('del_time',0)
  951. ->where('channel',$user['depart_top'][0]['depart_id'])
  952. ->first();
  953. if(empty($oa)) return [];
  954. $oa = $oa->toArray();
  955. $list = OaSub::where('oa_id', $oa['id'])->where('del_time', 0)->get()->toArray();
  956. $oa_sub_ids = [];
  957. foreach ($list as $v) {
  958. $oa_sub_ids[] = $v['id'];
  959. }
  960. $oaEmployee = OaSubEmployee::wherein('oa_sub_id', $oa_sub_ids)->get()->toArray();
  961. $emp_list = Employee::whereIn('id',array_column($oaEmployee,'employee_id'))
  962. ->select('emp_name','id','mobile')
  963. ->get()->toArray();
  964. $emp_map = [];
  965. foreach ($emp_list as $value){
  966. $emp_map[$value['id']] = [
  967. 'name' => $value['emp_name'],
  968. 'mobile' => $value['mobile'],
  969. ];
  970. }
  971. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  972. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  973. ->whereIn('a.employee_id',array_column($emp_list,'id'))
  974. ->select('a.employee_id', 'b.title')
  975. ->get()->toArray();
  976. $emp_message_map = [];
  977. foreach ($emp_message as $value){
  978. if(isset($emp_message_map[$value['employee_id']])){
  979. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  980. }else{
  981. $emp_message_map[$value['employee_id']] = $value['title'];
  982. }
  983. }
  984. $oaEmployeeKey = [];
  985. foreach ($oaEmployee as $v) {
  986. $str = "";
  987. $tmp = $emp_map[$v['employee_id']] ?? [];
  988. $tmp2 = $emp_message_map[$v['employee_id']] ?? "";
  989. $str = $tmp['name'] . " " . $tmp2 . " " . $tmp['mobile'];
  990. $oaEmployeeKey[$v['oa_sub_id']][] = [
  991. 'id' => $v['employee_id'],
  992. 'emp_name' => $str,
  993. ];
  994. }
  995. $return = [];
  996. foreach ($list as $v) {
  997. $return[$v['sort']][] = [
  998. 'emp_id' => $oaEmployeeKey[$v['id']],
  999. 'index' => $v['h5_key'],
  1000. ];
  1001. }
  1002. $detail = [];
  1003. foreach ($return as $v) {
  1004. $children = "";
  1005. foreach ($v as $vv) {
  1006. $children .= implode('|',array_column($vv['emp_id'],'emp_name')) . ',';
  1007. }
  1008. $detail[] = rtrim($children,',');
  1009. }
  1010. return $detail;
  1011. }
  1012. }