ConstructionService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. $product_save = $this->getSaveDetail($data['id']);
  207. try {
  208. DB::beginTransaction();
  209. Construction::where('id',$data['id'])->update([
  210. 'del_time'=> time()
  211. ]);
  212. ConstructionInfo::where('del_time',0)
  213. ->where('construction_id',$data['id'])
  214. ->update(['del_time' => time()]);
  215. ConstructionProductInfo::where('del_time',0)
  216. ->where('construction_id',$data['id'])
  217. ->update(['del_time' => time()]);
  218. //锁定库存释放
  219. ProductInventoryService::changeLockNumber([],$product_save);
  220. DB::commit();
  221. }catch (\Exception $exception){
  222. DB::rollBack();
  223. return [false,$exception->getMessage()];
  224. }
  225. return [true,''];
  226. }
  227. /**
  228. * 施工订单详情
  229. * @param $data
  230. * @return array
  231. */
  232. public function constructionDetail($data){
  233. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  234. $construction = Construction::where('del_time',0)
  235. ->where('id',$data['id'])
  236. ->first();
  237. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  238. $construction = $construction->toArray();
  239. $address = '';
  240. if(! empty($construction['address1'])) {
  241. $tmp = json_decode($construction['address1'],true);
  242. $construction['address1'] = $tmp;
  243. $tmp = implode(' ',$tmp);
  244. $tmp .= ' ' . $construction['address2'];
  245. $address = $tmp;
  246. }
  247. $construction['address'] = $address;
  248. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  249. $construction['sales_order_number'] = $sales;
  250. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  251. $construction['customer_title'] = $customer_title;
  252. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  253. $construction['customer_contact_title'] = $emp_title;
  254. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  255. $array = [
  256. $construction['install_method'],
  257. $construction['urgency'],
  258. ];
  259. $basic_map = BasicType::whereIn('id',$array)
  260. ->pluck('title','id')
  261. ->toArray();
  262. $construction = [$construction];
  263. foreach ($construction as $key => $value){
  264. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  265. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  266. }
  267. $construction = $construction[0];
  268. $construction_info = ConstructionInfo::where('del_time',0)
  269. ->where('construction_id',$construction['id'])
  270. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  271. ->get()->toArray();
  272. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  273. ->pluck('emp_name','id')
  274. ->toArray();
  275. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  276. ->pluck('title','id')
  277. ->toArray();
  278. foreach ($construction_info as $value){
  279. if($value['type'] == ConstructionInfo::type_one){
  280. $tmp = [
  281. 'id' => $value['contact_type'],
  282. 'title' => $basic_map2[$value['contact_type']] ?? '',
  283. 'info' => $value['contact_info']
  284. ];
  285. $construction['construction_contact'][] = $tmp;
  286. }elseif ($value['type'] == ConstructionInfo::type_two){
  287. $tmp = [
  288. 'id' => $value['employee_id'],
  289. 'name' => $emp_map[$value['employee_id']] ?? '',
  290. ];
  291. $construction['employee_one'][] = $tmp;
  292. }
  293. }
  294. $p_info = ConstructionProductInfo::where('del_time',0)
  295. ->where('construction_id',$construction['id'])
  296. ->get()->toArray();
  297. foreach ($p_info as $value){
  298. $construction['product'][] = $value;
  299. }
  300. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  301. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  302. return [true, $construction];
  303. }
  304. /**
  305. * 施工订单列表
  306. * @param $data
  307. * @param $user
  308. * @return array
  309. */
  310. public function constructionList($data,$user){
  311. $model = new Construction(['userData' => $user]);
  312. $model = $model->where('del_time',0)
  313. ->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')
  314. ->orderby('id', 'desc');
  315. if($user['id'] != Employee::SPECIAL_ADMIN){
  316. //单据中选择的签订负责协同人
  317. $construction_id = ConstructionInfo::where('del_time',0)
  318. ->where('employee_id',$user['id'])
  319. ->select('construction_id')
  320. ->get()->toArray();
  321. $construction_id = array_unique(array_column($construction_id,'construction_id'));
  322. //可见范围
  323. $model->whereIn('id', $construction_id);
  324. }
  325. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  326. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  327. $list = $this->limit($model,'',$data);
  328. $list = $this->fillData($list);
  329. return [true, $list];
  330. }
  331. /**
  332. * 参数规则
  333. * @param $data
  334. * @param $user
  335. * @param $is_add
  336. * @return array
  337. */
  338. public function constructionRule(&$data, $user, $is_add = true){
  339. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  340. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  341. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  342. if(empty($data['construction_time'])) return [false,'实施日期不能为空'];
  343. $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  344. if(empty($data['sales_order_id'])) return [false,'请选择合同'];
  345. $sale = SalesOrder::where('del_time',0)->where('id',$data['sales_order_id'])->first();
  346. if(empty($sale)) return [false,'合同不存在或已被删除'];
  347. if($sale['state'] < SalesOrder::State_two) return [false,'合同未派单,不允许新建施工单'];
  348. if(empty($data['product'])) return [false,'请选择产品'];
  349. if(! empty($data['construction_fee'])){
  350. $res = $this->checkNumber($data['construction_fee']);
  351. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  352. }
  353. if(! empty($data['service_price'])){
  354. $res = $this->checkNumber($data['service_price']);
  355. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  356. }
  357. if(! empty($data['handover_time'])) {
  358. $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  359. }
  360. if($data['model_type'] == Construction::Model_type_one){
  361. if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  362. if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  363. }else{
  364. if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  365. if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  366. }
  367. //所属部门 以及 顶级部门
  368. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  369. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  370. $product_submit = $product_save = [];
  371. foreach ($data['product'] as $value){
  372. if(empty($value['number'])) return [false,'产品数量不能为空'];
  373. $res = $this->checkNumber($value['number']);
  374. if(! $res) return [false,'请输入正确的产品数量'];
  375. if(isset($product_submit[$value['product_id']])){
  376. $product_submit[$value['product_id']] += $value['number'];
  377. }else{
  378. $product_submit[$value['product_id']] = $value['number'];
  379. }
  380. }
  381. $order_number = "";
  382. if(! $is_add) $order_number = $data['order_number'];
  383. list($status,$msg) = (new ProductInventoryService())->compareStock(array_keys($product_submit), [
  384. 'order_number' => $order_number,
  385. 'product' => $product_submit
  386. ]);
  387. if(! $status) return [false, $msg];
  388. if($is_add){
  389. $bool = Construction::where('del_time',0)->where('order_number',$data['order_number'])->exists();
  390. if($bool) return [false,'工单编号已存在,请重新获取'];
  391. }else{
  392. if(empty($data['id'])) return [false,'ID不能为空'];
  393. $product_save = $this->getSaveDetail($data['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. foreach ($data['data'] as $key => $value){
  415. $address = '';
  416. if(! empty($value['address1'])) {
  417. $tmp = json_decode($value['address1'],true);
  418. $tmp = implode(' ',$tmp);
  419. $tmp .= ' ' . $value['address2'];
  420. $address = $tmp;
  421. }
  422. $data['data'][$key]['address'] = $address;
  423. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  424. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  425. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  426. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  427. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  428. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  429. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  430. }
  431. return $data;
  432. }
  433. /**
  434. * 获取施工单号
  435. * @param $data
  436. * @return array
  437. */
  438. public function constructionGet($data){
  439. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  440. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  441. $prefix = Construction::$prefix[$data['model_type']];
  442. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  443. if(! $order_number) return [false,'工单编号生成失败!'];
  444. return [true,['order_number' => $order_number]];
  445. }
  446. /**
  447. * 获取保存详情
  448. * @param $id
  449. * @return array
  450. */
  451. public function getSaveDetail($id){
  452. $product_save = [];
  453. $sub = ConstructionProductInfo::where('construction_id',$id)
  454. ->where('del_time',0)
  455. ->get()->toArray();
  456. foreach ($sub as $value){
  457. if(isset($product_save[$value['product_id']])){
  458. $product_save[$value['product_id']] += $value['number'];
  459. }else{
  460. $product_save[$value['product_id']] = $value['number'];
  461. }
  462. }
  463. return $product_save;
  464. }
  465. }