ConstructionService.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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['state'] >= SalesOrder::State_seven) return [false,'请确认合同状态,新建施工单失败'];
  544. if($sale['top_depart_id'] == $user['head']['id']){
  545. //总社的订单 需要派单 才能建施工
  546. if($sale['state'] < SalesOrder::State_four) return [false,'合同未门店派单,不允许新建施工单'];
  547. }
  548. list($status,$msg) = $this->limitingSendRequestBackgExpire("construction" . $sale['order_number']);
  549. if(! $status) return [false, $msg];
  550. if(empty($data['product'])) return [false,'请选择产品'];
  551. if(empty($data['construction_period'][0]) || empty($data['construction_period'][1])) return [false,'请填写施工时间范围'];
  552. $data['start_time'] = $this->changeDateToDateMin($data['construction_period'][0]);
  553. $data['end_time'] = $this->changeDateToDateMin($data['construction_period'][1]);
  554. if(! empty($data['construction_fee'])){
  555. $res = $this->checkNumber($data['construction_fee']);
  556. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  557. }
  558. if(! empty($data['service_price'])){
  559. $res = $this->checkNumber($data['service_price']);
  560. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  561. }
  562. if(! empty($data['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  563. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  564. if($data['model_type'] == Construction::Model_type_one){
  565. // if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  566. // if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  567. }else{
  568. // if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  569. // if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  570. }
  571. //所属部门 以及 顶级部门
  572. if(empty($data['depart_id'])) {
  573. $data['depart_id'] = $this->getDepart($user);
  574. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  575. }
  576. $product_submit = $product_id = [];
  577. foreach ($data['product'] as $value){
  578. if(empty($value['number'])) return [false,'产品数量不能为空'];
  579. $res = $this->checkNumber($value['number']);
  580. if(! $res) return [false,'请输入正确的产品数量'];
  581. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  582. if(isset($product_submit[$key])){
  583. $product_submit[$key] += $value['number'];
  584. }else{
  585. $product_submit[$key] = $value['number'];
  586. }
  587. $product_id[] = $value['product_id'];
  588. }
  589. //剩余能施工
  590. $id = $data['id'] ?? 0;
  591. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  592. //比较
  593. foreach ($product_submit as $pro => $number){
  594. $tmp = explode(',',$pro);
  595. $p = $tmp[0];
  596. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  597. $s_number = $s_product[$p];
  598. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据'];
  599. }
  600. $id = $data['id'] ?? 0;
  601. $product_save = $this->getSaveDetail($id);
  602. //是否校验库存
  603. ProductInventoryService::is_check($user,$data);
  604. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  605. if(! $status) return [false, $msg];
  606. $start_time = date("Y-m-d H:i",$data['start_time']);
  607. $end_time = date("Y-m-d H:i",$data['end_time']);
  608. if($is_add){
  609. $bool = Construction::where('del_time',0)
  610. ->where('order_number',$data['order_number'])
  611. ->exists();
  612. if($bool) return [false,'工单编号已存在,请重新获取'];
  613. $bool = Construction::where('del_time',0)
  614. ->where('sales_order_id',$data['sales_order_id'])
  615. ->where('start_time', '<=', $data['end_time'])
  616. ->where('end_time', '>=', $data['start_time'])
  617. ->exists();
  618. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  619. }else{
  620. if(empty($data['id'])) return [false,'ID不能为空'];
  621. $bool = Construction::where('del_time',0)
  622. ->where('id','<>',$data['id'])
  623. ->where('sales_order_id',$data['sales_order_id'])
  624. ->where('start_time', '<=', $data['end_time'])
  625. ->where('end_time', '>=', $data['start_time'])
  626. ->exists();
  627. if($bool) return [false,'合同:' . $sale['order_number'] . '在' . $start_time . '——' . $end_time . '已下施工单'];
  628. }
  629. return [true, [$product_submit, $product_save]];
  630. }
  631. /**
  632. * 数据拼接
  633. * @param $data
  634. * @return array
  635. */
  636. public function fillData($data){
  637. if(empty($data['data'])) return $data;
  638. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency'),array_column($data['data'],'install_position')));
  639. $basic_map = BasicType::whereIn('id',$array)
  640. ->pluck('title','id')
  641. ->toArray();
  642. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  643. ->pluck('emp_name','id')
  644. ->toArray();
  645. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  646. ->pluck('title','id')
  647. ->toArray();
  648. $sales_map = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->pluck('order_number','id')->toArray();
  649. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  650. ->pluck('title','id')
  651. ->toArray();
  652. //分派的总社或分社
  653. $dispatch = $this->getDispatchData($data['data']);
  654. //施工产品
  655. $product_map = $this->getProduct($data['data']);
  656. $address_map = config('address');
  657. foreach ($data['data'] as $key => $value){
  658. $address_str = [];
  659. $product_tmp = $product_map[$value['id']] ?? [];
  660. $data['data'][$key]['product_detail'] = implode(',',$product_tmp);
  661. if(! empty($value['address1'])) {
  662. $tmp = json_decode($value['address1'],true);
  663. $this->findLabelsByValue($address_map,$tmp,$address_str);
  664. $tmp = implode(' ',$address_str);
  665. $tmp .= ' ' . $value['address2'];
  666. $address = $tmp;
  667. }else{
  668. $address = $value['address2'];
  669. }
  670. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  671. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  672. $data['data'][$key]['construction_period'] = $start_time . '——' . $end_time;
  673. $data['data'][$key]['address'] = $address;
  674. $data['data'][$key]['model_type_title'] = Construction::$model_type_title[$value['model_type']] ?? '';
  675. $data['data'][$key]['install_position_title'] = $basic_map[$value['install_position']] ?? '';
  676. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  677. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  678. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  679. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  680. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  681. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  682. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  683. $tmp_sales = $sales_map[$value['sales_order_id']] ?? "";
  684. // $tmp_sales_time = $tmp_sales['handover_time'] ? date("Y-m-d") : "";
  685. $data['data'][$key]['sales_order_number'] = $tmp_sales;
  686. $data['data'][$key]['handover_time'] = $value['handover_time'] ? date("Y-m-d",$value['handover_time']) : "";
  687. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  688. $data['data'][$key]['dispatch_company'] = $dispatch[$value['sales_order_id']] ?? '';
  689. // $data['data'][$key]['pq_state_title'] = Construction::$pq_name[$value['pq_state']] ?? '';
  690. // $str = "";
  691. // $start_time = $value['day_start_stamp'] ? date("Y-m-d H:i",$value['day_start_stamp']) : '';
  692. // $end_time = $value['day_end_stamp'] ? date("Y-m-d H:i",$value['day_end_stamp']) : '';
  693. // if(! empty($start_time) && ! empty($end_time)) $str = $start_time . '——' . $end_time;
  694. // $data['data'][$key]['pq_period'] = $str;
  695. }
  696. return $data;
  697. }
  698. public function getProduct($data){
  699. $search_id = array_column($data,'id');
  700. if(empty($search_id)) return [];
  701. $product = ConstructionProductInfo::where('del_time',0)
  702. ->whereIn('construction_id',$search_id)
  703. ->select('product_id','construction_id')
  704. ->get()->toArray();
  705. $product_map = Product::whereIn('id',array_unique(array_column($product,'product_id')))->pluck('title','id')->toArray();
  706. $return = [];
  707. foreach ($product as $value){
  708. $product_tmp = $product_map[$value['product_id']] ?? "";
  709. if($product_tmp) $return[$value['construction_id']][] = $product_tmp;
  710. }
  711. return $return;
  712. }
  713. public function getDispatchData($data){
  714. $search_id = [];
  715. foreach ($data as $value){
  716. $search_id[] = $value['sales_order_id'];
  717. }
  718. if(empty($search_id)) return [];
  719. $see = SeeRange::where('del_time',0)
  720. ->whereIn('data_id',$search_id)
  721. ->where('data_type',SeeRange::type_seven)
  722. ->where('type',SeeRange::data_three)
  723. ->select('data_id','param_id')
  724. ->get()->toArray();
  725. $map = Depart::whereIn('id',array_unique(array_column($see,'param_id')))
  726. ->pluck('title','id')
  727. ->toArray();
  728. $see_array = [];
  729. foreach ($see as $value){
  730. $see_array[$value['data_id']] = $map[$value['param_id']] ?? "";
  731. }
  732. return $see_array;
  733. }
  734. /**
  735. * 获取施工单号
  736. * @param $data
  737. * @return array
  738. */
  739. public function constructionGet($data){
  740. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  741. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  742. $prefix = Construction::$prefix[$data['model_type']];
  743. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  744. if(! $order_number) return [false,'工单编号生成失败!'];
  745. return [true,['order_number' => $order_number]];
  746. }
  747. /**
  748. * 获取保存详情
  749. * @param $id
  750. * @return array
  751. */
  752. public function getSaveDetail($id){
  753. $product_save = [];
  754. if(empty($id)) return $product_save;
  755. $sub = ConstructionProductInfo::where('construction_id',$id)
  756. ->where('del_time',0)
  757. ->get()->toArray();
  758. foreach ($sub as $value){
  759. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  760. if(isset($product_save[$key])){
  761. $product_save[$key] += $value['number'];
  762. }else{
  763. $product_save[$key] = $value['number'];
  764. }
  765. }
  766. return $product_save;
  767. }
  768. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  769. $construction = Construction::where('del_time',0)
  770. ->where('sales_order_id',$sales_order_id)
  771. ->select('id')->get()->toArray();
  772. $construction_id = array_column($construction,'id');
  773. $product_save = [];
  774. $sub = ConstructionProductInfo::where('del_time',0)
  775. ->whereIn('construction_id',$construction_id)
  776. ->when(! empty($id), function ($query) use ($id) {
  777. return $query->where('construction_id', '<>',$id);
  778. })
  779. ->get()->toArray();
  780. foreach ($sub as $value){
  781. if(isset($product_save[$value['product_id']])){
  782. $product_save[$value['product_id']] += $value['number'];
  783. }else{
  784. $product_save[$value['product_id']] = $value['number'];
  785. }
  786. }
  787. $sales_order_product = [];
  788. $sales_product = SalesOrderProductInfo::where('del_time',0)
  789. ->where('sales_order_id',$sales_order_id)
  790. ->get()->toArray();
  791. foreach ($sales_product as $value){
  792. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  793. if(isset($sales_order_product[$value['product_id']])){
  794. $sales_order_product[$value['product_id']] += $value['number'];
  795. }else{
  796. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
  797. }
  798. }
  799. return $sales_order_product;
  800. }
  801. public function deliveryNoteEdit($data,$user){
  802. $id = $user['id'];
  803. if(isset($data['id'])) {
  804. $model = DeliveryNote::where('id',$data['id'])->first();
  805. if($model->img3) return [false,'客户已签字,无法编辑!'];
  806. if(DeliveryNote::where('del_time',0)->where('construction_order_number',$data['construction_order_number'])->where('id','<>',$data['id'])->first()) return [false,'施工单客户确认单已存在!'];
  807. }
  808. else {
  809. $model = new DeliveryNote();
  810. $model->crt_id = $id;
  811. }
  812. try {
  813. $model->upd_id = $id;
  814. $model->construction_order_number = $data['construction_order_number'] ?? '';
  815. $model->start_time = $data['start_time'] ?? '';
  816. $model->end_time = $data['end_time'] ?? '';
  817. $model->vin_no = $data['vin_no'] ?? '';
  818. $model->system = $data['system'] ?? '';
  819. $model->mile = $data['mile'] ?? '';
  820. $model->is_wait = $data['is_wait'] ?? '';
  821. $model->customer_name = $data['customer_name'] ?? '';
  822. $model->customer_mobile = $data['customer_mobile'] ?? '';
  823. $model->sale_man = $data['sale_man'] ?? '';
  824. $model->install_man = $data['install_man'] ?? '';
  825. $model->is_brash = $data['is_brash'] ?? '';
  826. $model->is_chong = $data['is_chong'] ?? '';
  827. $model->service_mark = $data['service_mark'] ?? '';
  828. $model->mark = $data['mark'] ?? '';
  829. $model->break = $data['break'] ? json_encode($data['break']):json_encode([]);
  830. $model->break_mark = $data['break_mark'] ? json_encode($data['break_mark']):json_encode([]);
  831. $model->project_id = $data['project_id'] ? json_encode($data['project_id']):json_encode([]);
  832. $model->other_project_mark = $data['other_project_mark'] ?? '';
  833. $model->img = $data['img'] ?? '';
  834. $model->img1 = $data['img1'] ?? '';
  835. $model->img2 = $data['img2'] ?? '';
  836. $model->img3 = $data['img3'] ?? '';
  837. $model->save();
  838. return [true,'保存成功!'];
  839. }catch (\Throwable $e){
  840. return [true,'保存成功!'];
  841. }
  842. }
  843. public function deliveryNoteList($data)
  844. {
  845. $list = DeliveryNote::where('del_time',0);
  846. $list = $this->limit($list,'*',$data);
  847. return [true,$list];
  848. }
  849. public function deliveryNoteDetail($data){
  850. if(isset($data['id'])) $model = DeliveryNote::where('id',$data['id'])->where('del_time',0)->first();
  851. if(isset($data['construction_order_number'])) $model = DeliveryNote::where('id',$data['construction_order_number'])->where('del_time',0)->first();
  852. if(empty($model)) return [false,'数据不存在!'];
  853. $employee_key_list = Employee::pluck('emp_name','id')->toArray();
  854. $detail = $model->toArray();
  855. $detail['install_man_title'] = $employee_key_list[$detail['install_man']] ?? "";
  856. $detail['sale_man_title'] = $employee_key_list[$detail['sale_man']] ?? "";
  857. $img_list = ConstructionFile::where('order_number',$detail['construction_order_number'])->where('del_time',0)->select('file as url','name','mark')->get()->toArray();
  858. $detail['file'] = $img_list;
  859. return [true,$detail];
  860. }
  861. public function deliveryNoteDel($data,$user){
  862. DeliveryNote::where('id',$data['id'])->update(
  863. [
  864. 'del_time' => time(),
  865. 'upd_id' => $user['id'],
  866. ]
  867. );
  868. return [true,'删除成功!'];
  869. }
  870. public function constructionPdf($data, $user){
  871. if(empty($data['id'])) return [false, '请选择下载的施工单'];
  872. $construction = Construction::where('del_time',0)
  873. ->where('id',$data['id'])
  874. ->first();
  875. if(empty($construction)) return [false, '施工单不存在或已被删除'];
  876. $construction = $construction->toArray();
  877. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  878. $construction['customer_title'] = $customer_title ?? "";
  879. $construction['sales_order_number'] = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number') ?? '';
  880. $construction['handover_time'] = $construction['handover_time'] ? date("Y-m-d H:i:s",$construction['handover_time']): '';
  881. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  882. $construction_info = ConstructionInfo::where('del_time',0)
  883. ->where('construction_id',$construction['id'])
  884. ->where('type',CustomerInfo::type_two)
  885. ->select('id','employee_id')
  886. ->get()->toArray();
  887. $emp_list = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  888. ->select('emp_name','id','mobile')
  889. ->get()->toArray();
  890. $emp_map = [];
  891. foreach ($emp_list as $value){
  892. $emp_map[$value['id']] = [
  893. 'name' => $value['emp_name'],
  894. 'mobile' => $value['mobile'],
  895. ];
  896. }
  897. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  898. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  899. ->select('a.employee_id', 'b.title')
  900. ->whereIn('employee_id',array_column($emp_list,'id'))
  901. ->get()->toArray();
  902. $emp_message_map = [];
  903. foreach ($emp_message as $value){
  904. if(isset($emp_message_map[$value['employee_id']])){
  905. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  906. }else{
  907. $emp_message_map[$value['employee_id']] = $value['title'];
  908. }
  909. }
  910. $crt_name = ($emp_map[$construction['crt_id']]['name'] ?? '') . ' ' . ($emp_message_map[$construction['crt_id']] ?? '') . ' ' . ($emp_map[$construction['crt_id']]['mobile'] ?? '');
  911. $construction['crt_name'] = $crt_name;
  912. $xt_name = "";
  913. foreach ($construction_info as $value){
  914. $xt_name .= ($emp_map[$value['employee_id']]['name'] ?? '') . ' ' . ($emp_message_map[$value['employee_id']] ?? '') . ' ' . ($emp_map[$value['employee_id']]['mobile'] ?? '') . " | ";
  915. }
  916. $xt_name = rtrim($xt_name, " | ");
  917. $construction['xt_name'] = $xt_name;
  918. $p_info = ConstructionProductInfo::where('del_time',0)
  919. ->where('construction_id',$construction['id'])
  920. ->get()->toArray();
  921. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  922. $unit_map = BasicType::whereIn('id',array_column($map,'unit'))
  923. ->pluck('title','id')->toArray();
  924. $product = [];
  925. $count = 0;
  926. foreach ($p_info as $value){
  927. $tmp = $map[$value['product_id']] ?? [];
  928. $value['count'] = $count+1;
  929. $value['title'] = $tmp['title'] ?? "";
  930. $value['code'] = $tmp['code'] ?? "";
  931. $value['size'] = $tmp['size'] ?? "";
  932. $value['unit'] = $unit_map[$tmp['unit']] ?? "";
  933. $product[] = $value;
  934. }
  935. //工单进展
  936. $detail = $this->getOaJz($data,$user);
  937. $order_message = ['order' => $construction, 'product' => $product,'detail'=> $detail];
  938. $pdf = app('dompdf.wrapper')->loadView('pdf.construction', $order_message);
  939. $file_name = time().rand(1000,9999);
  940. $filename = '施工单_' . $file_name.'.' . 'pdf';
  941. $pdf->save(storage_path('app/public/export/' . $filename));
  942. return [true, ['file' => $filename]];
  943. }
  944. public function getOaJz($data,$user)
  945. {
  946. $oa = Oa::where('menu_id', 34)
  947. ->where('del_time',0)
  948. ->where('channel',$user['depart_top'][0]['depart_id'])
  949. ->first();
  950. if(empty($oa)) return [];
  951. $oa = $oa->toArray();
  952. $list = OaSub::where('oa_id', $oa['id'])->where('del_time', 0)->get()->toArray();
  953. $oa_sub_ids = [];
  954. foreach ($list as $v) {
  955. $oa_sub_ids[] = $v['id'];
  956. }
  957. $oaEmployee = OaSubEmployee::wherein('oa_sub_id', $oa_sub_ids)->get()->toArray();
  958. $emp_list = Employee::whereIn('id',array_column($oaEmployee,'employee_id'))
  959. ->select('emp_name','id','mobile')
  960. ->get()->toArray();
  961. $emp_map = [];
  962. foreach ($emp_list as $value){
  963. $emp_map[$value['id']] = [
  964. 'name' => $value['emp_name'],
  965. 'mobile' => $value['mobile'],
  966. ];
  967. }
  968. $emp_message = EmployeeDepartPermission::from('employee_depart_permission as a')
  969. ->leftJoin('depart as b', 'b.id', 'a.depart_id')
  970. ->whereIn('a.employee_id',array_column($emp_list,'id'))
  971. ->select('a.employee_id', 'b.title')
  972. ->get()->toArray();
  973. $emp_message_map = [];
  974. foreach ($emp_message as $value){
  975. if(isset($emp_message_map[$value['employee_id']])){
  976. $emp_message_map[$value['employee_id']] .= ',' . $value['title'];
  977. }else{
  978. $emp_message_map[$value['employee_id']] = $value['title'];
  979. }
  980. }
  981. $oaEmployeeKey = [];
  982. foreach ($oaEmployee as $v) {
  983. $str = "";
  984. $tmp = $emp_map[$v['employee_id']] ?? [];
  985. $tmp2 = $emp_message_map[$v['employee_id']] ?? "";
  986. $str = $tmp['name'] . " " . $tmp2 . " " . $tmp['mobile'];
  987. $oaEmployeeKey[$v['oa_sub_id']][] = [
  988. 'id' => $v['employee_id'],
  989. 'emp_name' => $str,
  990. ];
  991. }
  992. $return = [];
  993. foreach ($list as $v) {
  994. $return[$v['sort']][] = [
  995. 'emp_id' => $oaEmployeeKey[$v['id']],
  996. 'index' => $v['h5_key'],
  997. ];
  998. }
  999. $detail = [];
  1000. foreach ($return as $v) {
  1001. $children = "";
  1002. foreach ($v as $vv) {
  1003. $children .= implode('|',array_column($vv['emp_id'],'emp_name')) . ',';
  1004. }
  1005. $detail[] = rtrim($children,',');
  1006. }
  1007. return $detail;
  1008. }
  1009. }