ConstructionService.php 21 KB

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