ConstructionService.php 22 KB

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