ConstructionService.php 51 KB

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