ConstructionService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Construction;
  5. use App\Model\ConstructionInfo;
  6. use App\Model\ConstructionProductInfo;
  7. use App\Model\Customer;
  8. use App\Model\Employee;
  9. use App\Model\SalesOrder;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * 施工订单
  13. */
  14. class ConstructionService extends Service
  15. {
  16. /**
  17. * 施工订单编辑
  18. * @param $data
  19. * @param $user
  20. * @return array
  21. */
  22. public function constructionEdit($data,$user){
  23. list($status,$msg) = $this->constructionRule($data, $user, false);
  24. if(!$status) return [$status,$msg];
  25. try {
  26. DB::beginTransaction();
  27. $model = Construction::where('id', $data['id'])->first();
  28. $model->model_type = $data['model_type'];
  29. $model->order_number = $data['order_number'];
  30. $model->title = $data['title'] ?? '';
  31. $model->customer_id = $data['customer_id'] ?? 0;
  32. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  33. $model->install_method = $data['install_method'] ?? 0;
  34. $model->install_position = $data['install_position'] ?? '';
  35. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  36. $model->construction_fee = $data['construction_fee'] ?? 0;
  37. $model->service_price = $data['service_price'] ?? 0;
  38. $model->construction_time = $data['construction_time'] ?? 0;
  39. $model->handover_time = $data['handover_time'] ?? 0;
  40. $model->urgency = $data['urgency'] ?? 0;
  41. $model->mark = $data['mark'] ?? '';
  42. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  43. $model->address2 = $data['address2'] ?? '';
  44. $model->introduction = $data['introduction'] ?? '';
  45. // $model->depart_id = $data['depart_id'] ?? 0;
  46. // $model->top_depart_id = $data['top_depart_id'] ?? 0;
  47. $model->save();
  48. $time = time();
  49. ConstructionInfo::where('del_time',0)
  50. ->where('construction_id',$data['id'])
  51. ->update(['del_time' => $time]);
  52. ConstructionProductInfo::where('del_time',0)
  53. ->where('construction_id',$data['id'])
  54. ->update(['del_time' => $time]);
  55. if(! empty($data['construction_contact'])){
  56. $insert = [];
  57. foreach ($data['construction_contact'] as $value){
  58. $insert[] = [
  59. 'construction_id' => $model->id,
  60. 'contact_type' => $value['id'],
  61. 'contact_info' => $value['info'],
  62. 'type' => ConstructionInfo::type_one,
  63. 'crt_time' => $time,
  64. ];
  65. }
  66. ConstructionInfo::insert($insert);
  67. }
  68. if(! empty($data['employee_one'])){
  69. $insert = [];
  70. foreach ($data['employee_one'] as $value){
  71. $insert[] = [
  72. 'construction_id' => $model->id,
  73. 'employee_id' => $value,
  74. 'type' => ConstructionInfo::type_two,
  75. 'crt_time' => $time,
  76. ];
  77. }
  78. ConstructionInfo::insert($insert);
  79. }
  80. if(! empty($data['product'])){
  81. $insert = [];
  82. foreach ($data['product'] as $value){
  83. $insert[] = [
  84. 'construction_id' => $model->id,
  85. 'product_id' => $value['product_id'],
  86. 'number' => $value['number'],
  87. 'title' => $value['title'],
  88. 'code' => $value['code'] ?? '',
  89. 'size' => $value['size'] ?? '',
  90. 'unit' => $value['unit'] ?? 0,
  91. 'bar_code' => $value['bar_code'] ?? '',
  92. 'cost' => $value['cost'] ?? 0,
  93. 'depart_price' => $value['depart_price'] ?? 0,
  94. 'retail_price' => $value['retail_price'] ?? 0,
  95. 'mark' => $value['mark'] ?? '',
  96. 'crt_time' => $time,
  97. ];
  98. }
  99. ConstructionProductInfo::insert($insert);
  100. //锁定库存
  101. ProductInventoryService::changeLockNumber($msg[0],$msg[1]);
  102. }
  103. DB::commit();
  104. }catch (\Exception $exception){
  105. DB::rollBack();
  106. return [false,$exception->getMessage()];
  107. }
  108. return [true,''];
  109. }
  110. /**
  111. * 施工订单新增
  112. * @param $data
  113. * @param $user
  114. * @return array
  115. */
  116. public function constructionAdd($data,$user){
  117. list($status,$msg) = $this->constructionRule($data,$user);
  118. if(!$status) return [$status,$msg];
  119. try {
  120. DB::beginTransaction();
  121. $model = new Construction();
  122. $model->model_type = $data['model_type'];
  123. $model->order_number = $data['order_number'];
  124. $model->title = $data['title'] ?? '';
  125. $model->customer_id = $data['customer_id'] ?? 0;
  126. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  127. $model->install_method = $data['install_method'] ?? 0;
  128. $model->install_position = $data['install_position'] ?? '';
  129. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  130. $model->construction_fee = $data['construction_fee'] ?? 0;
  131. $model->service_price = $data['service_price'] ?? 0;
  132. $model->construction_time = $data['construction_time'] ?? 0;
  133. $model->handover_time = $data['handover_time'] ?? 0;
  134. $model->urgency = $data['urgency'] ?? 0;
  135. $model->mark = $data['mark'] ?? '';
  136. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  137. $model->address2 = $data['address2'] ?? '';
  138. $model->introduction = $data['introduction'] ?? '';
  139. $model->crt_id = $user['id'];
  140. $model->depart_id = $data['depart_id'] ?? 0;
  141. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  142. $model->save();
  143. $time = time();
  144. if(! empty($data['construction_contact'])){
  145. $insert = [];
  146. foreach ($data['construction_contact'] as $value){
  147. $insert[] = [
  148. 'construction_id' => $model->id,
  149. 'contact_type' => $value['id'],
  150. 'contact_info' => $value['info'],
  151. 'type' => ConstructionInfo::type_one,
  152. 'crt_time' => $time,
  153. ];
  154. }
  155. ConstructionInfo::insert($insert);
  156. }
  157. if(! empty($data['employee_one'])){
  158. $insert = [];
  159. foreach ($data['employee_one'] as $value){
  160. $insert[] = [
  161. 'construction_id' => $model->id,
  162. 'employee_id' => $value,
  163. 'type' => ConstructionInfo::type_two,
  164. 'crt_time' => $time,
  165. ];
  166. }
  167. ConstructionInfo::insert($insert);
  168. }
  169. if(! empty($data['product'])){
  170. $insert = [];
  171. foreach ($data['product'] as $value){
  172. $insert[] = [
  173. 'construction_id' => $model->id,
  174. 'product_id' => $value['product_id'],
  175. 'number' => $value['number'],
  176. 'title' => $value['title'],
  177. 'code' => $value['code'] ?? '',
  178. 'size' => $value['size'] ?? '',
  179. 'unit' => $value['unit'] ?? 0,
  180. 'bar_code' => $value['bar_code'] ?? '',
  181. 'cost' => $value['cost'] ?? 0,
  182. 'depart_price' => $value['depart_price'] ?? 0,
  183. 'retail_price' => $value['retail_price'] ?? 0,
  184. 'mark' => $value['mark'] ?? '',
  185. 'crt_time' => $time,
  186. ];
  187. }
  188. ConstructionProductInfo::insert($insert);
  189. //锁定库存
  190. ProductInventoryService::changeLockNumber($msg[0]);
  191. }
  192. DB::commit();
  193. }catch (\Exception $exception){
  194. DB::rollBack();
  195. return [false,$exception->getMessage()];
  196. }
  197. return [true,''];
  198. }
  199. /**
  200. * 施工订单删除
  201. * @param $data
  202. * @return array
  203. */
  204. public function constructionDel($data){
  205. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  206. $construction = Construction::where('del_time',0)->where('id',$data['id'])->first();
  207. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  208. $construction = $construction->toArray();
  209. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单状态,操作失败'];
  210. $product_save = $this->getSaveDetail($data['id']);
  211. try {
  212. DB::beginTransaction();
  213. Construction::where('id',$data['id'])->update([
  214. 'del_time'=> time()
  215. ]);
  216. ConstructionInfo::where('del_time',0)
  217. ->where('construction_id',$data['id'])
  218. ->update(['del_time' => time()]);
  219. ConstructionProductInfo::where('del_time',0)
  220. ->where('construction_id',$data['id'])
  221. ->update(['del_time' => time()]);
  222. //锁定库存释放
  223. ProductInventoryService::changeLockNumber([],$product_save);
  224. DB::commit();
  225. }catch (\Exception $exception){
  226. DB::rollBack();
  227. return [false,$exception->getMessage()];
  228. }
  229. return [true,''];
  230. }
  231. /**
  232. * 施工订单详情
  233. * @param $data
  234. * @return array
  235. */
  236. public function constructionDetail($data){
  237. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  238. $construction = Construction::where('del_time',0)
  239. ->where('id',$data['id'])
  240. ->first();
  241. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  242. $construction = $construction->toArray();
  243. $address = '';
  244. if(! empty($construction['address1'])) {
  245. $tmp = json_decode($construction['address1'],true);
  246. $construction['address1'] = $tmp;
  247. $tmp = implode(' ',$tmp);
  248. $tmp .= ' ' . $construction['address2'];
  249. $address = $tmp;
  250. }
  251. $construction['address'] = $address;
  252. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  253. $construction['sales_order_number'] = $sales;
  254. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  255. $construction['customer_title'] = $customer_title;
  256. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  257. $construction['customer_contact_title'] = $emp_title;
  258. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  259. $array = [
  260. $construction['install_method'],
  261. $construction['urgency'],
  262. ];
  263. $basic_map = BasicType::whereIn('id',$array)
  264. ->pluck('title','id')
  265. ->toArray();
  266. $construction = [$construction];
  267. foreach ($construction as $key => $value){
  268. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  269. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  270. }
  271. $construction = $construction[0];
  272. $construction_info = ConstructionInfo::where('del_time',0)
  273. ->where('construction_id',$construction['id'])
  274. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  275. ->get()->toArray();
  276. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  277. ->pluck('emp_name','id')
  278. ->toArray();
  279. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  280. ->pluck('title','id')
  281. ->toArray();
  282. foreach ($construction_info as $value){
  283. if($value['type'] == ConstructionInfo::type_one){
  284. $tmp = [
  285. 'id' => $value['contact_type'],
  286. 'title' => $basic_map2[$value['contact_type']] ?? '',
  287. 'info' => $value['contact_info']
  288. ];
  289. $construction['construction_contact'][] = $tmp;
  290. }elseif ($value['type'] == ConstructionInfo::type_two){
  291. $tmp = [
  292. 'id' => $value['employee_id'],
  293. 'name' => $emp_map[$value['employee_id']] ?? '',
  294. ];
  295. $construction['employee_one'][] = $tmp;
  296. }
  297. }
  298. $p_info = ConstructionProductInfo::where('del_time',0)
  299. ->where('construction_id',$construction['id'])
  300. ->get()->toArray();
  301. foreach ($p_info as $value){
  302. $construction['product'][] = $value;
  303. }
  304. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  305. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  306. return [true, $construction];
  307. }
  308. /**
  309. * 施工订单列表
  310. * @param $data
  311. * @param $user
  312. * @return array
  313. */
  314. public function constructionList($data,$user){
  315. $model = new Construction(['userData' => $user]);
  316. $model = $model->where('del_time',0)
  317. ->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')
  318. ->orderby('id', 'desc');
  319. if($user['id'] != Employee::SPECIAL_ADMIN){
  320. //单据中选择的签订负责协同人
  321. $construction_id = ConstructionInfo::where('del_time',0)
  322. ->where('employee_id',$user['id'])
  323. ->select('construction_id')
  324. ->get()->toArray();
  325. $construction_id = array_unique(array_column($construction_id,'construction_id'));
  326. //可见范围
  327. $model->whereIn('id', $construction_id);
  328. }
  329. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  330. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  331. $list = $this->limit($model,'',$data);
  332. $list = $this->fillData($list);
  333. return [true, $list];
  334. }
  335. /**
  336. * 参数规则
  337. * @param $data
  338. * @param $user
  339. * @param $is_add
  340. * @return array
  341. */
  342. public function constructionRule(&$data, $user, $is_add = true){
  343. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  344. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  345. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  346. if(empty($data['construction_time'])) return [false,'实施日期不能为空'];
  347. $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  348. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  349. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  350. if(empty($sale)) return [false,'合同不存在或已被删除'];
  351. if($sale['state'] < SalesOrder::State_two) return [false,'合同未派单,不允许新建施工单'];
  352. if(empty($data['product'])) return [false,'请选择产品'];
  353. if(! empty($data['construction_fee'])){
  354. $res = $this->checkNumber($data['construction_fee']);
  355. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  356. }
  357. if(! empty($data['service_price'])){
  358. $res = $this->checkNumber($data['service_price']);
  359. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  360. }
  361. if(! empty($data['handover_time'])) {
  362. $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  363. }
  364. if($data['model_type'] == Construction::Model_type_one){
  365. if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  366. if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  367. }else{
  368. if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  369. if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  370. }
  371. //所属部门 以及 顶级部门
  372. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  373. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  374. $product_submit = [];
  375. foreach ($data['product'] as $value){
  376. if(empty($value['number'])) return [false,'产品数量不能为空'];
  377. $res = $this->checkNumber($value['number']);
  378. if(! $res) return [false,'请输入正确的产品数量'];
  379. if(isset($product_submit[$value['product_id']])){
  380. $product_submit[$value['product_id']] += $value['number'];
  381. }else{
  382. $product_submit[$value['product_id']] = $value['number'];
  383. }
  384. }
  385. $id = $data['id'] ?? 0;
  386. $product_save = $this->getSaveDetail($id);
  387. list($status,$msg) = (new ProductInventoryService())->compareStock(array_keys($product_submit), $product_submit, $product_save);
  388. if(! $status) return [false, $msg];
  389. if($is_add){
  390. $bool = Construction::where('del_time',0)->where('order_number',$data['order_number'])->exists();
  391. if($bool) return [false,'工单编号已存在,请重新获取'];
  392. }else{
  393. if(empty($data['id'])) return [false,'ID不能为空'];
  394. }
  395. return [true, [$product_submit, $product_save]];
  396. }
  397. /**
  398. * 数据拼接
  399. * @param $data
  400. * @return array
  401. */
  402. public function fillData($data){
  403. if(empty($data['data'])) return $data;
  404. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency')));
  405. $basic_map = BasicType::whereIn('id',$array)
  406. ->pluck('title','id')
  407. ->toArray();
  408. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  409. ->pluck('emp_name','id')
  410. ->toArray();
  411. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  412. ->pluck('title','id')
  413. ->toArray();
  414. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->pluck('order_number','id')->toArray();
  415. foreach ($data['data'] as $key => $value){
  416. $address = '';
  417. if(! empty($value['address1'])) {
  418. $tmp = json_decode($value['address1'],true);
  419. $tmp = implode(' ',$tmp);
  420. $tmp .= ' ' . $value['address2'];
  421. $address = $tmp;
  422. }
  423. $data['data'][$key]['address'] = $address;
  424. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  425. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  426. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  427. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  428. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  429. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  430. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  431. $data['data'][$key]['sales_order_number'] = $sales[$value['sales_order_id']] ?? '';
  432. }
  433. return $data;
  434. }
  435. /**
  436. * 获取施工单号
  437. * @param $data
  438. * @return array
  439. */
  440. public function constructionGet($data){
  441. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  442. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  443. $prefix = Construction::$prefix[$data['model_type']];
  444. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  445. if(! $order_number) return [false,'工单编号生成失败!'];
  446. return [true,['order_number' => $order_number]];
  447. }
  448. /**
  449. * 获取保存详情
  450. * @param $id
  451. * @return array
  452. */
  453. public function getSaveDetail($id){
  454. $product_save = [];
  455. if(empty($id)) return $product_save;
  456. $sub = ConstructionProductInfo::where('construction_id',$id)
  457. ->where('del_time',0)
  458. ->get()->toArray();
  459. foreach ($sub as $value){
  460. if(isset($product_save[$value['product_id']])){
  461. $product_save[$value['product_id']] += $value['number'];
  462. }else{
  463. $product_save[$value['product_id']] = $value['number'];
  464. }
  465. }
  466. return $product_save;
  467. }
  468. }