ConstructionService.php 50 KB

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