ConstructionService.php 52 KB

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