ConstructionService.php 45 KB

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