ConstructionService.php 52 KB

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