ConstructionService.php 50 KB

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