ConstructionService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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\SalesOrderInfo;
  11. use App\Model\SalesOrderProductInfo;
  12. use App\Model\Storehouse;
  13. use Illuminate\Support\Facades\DB;
  14. /**
  15. * 施工订单
  16. */
  17. class ConstructionService extends Service
  18. {
  19. /**
  20. * 施工订单编辑
  21. * @param $data
  22. * @param $user
  23. * @return array
  24. */
  25. public function constructionEdit($data,$user){
  26. list($status,$msg) = $this->constructionRule($data, $user, false);
  27. if(!$status) return [$status,$msg];
  28. try {
  29. DB::beginTransaction();
  30. $model = Construction::where('id', $data['id'])->first();
  31. $model->model_type = $data['model_type'];
  32. $model->order_number = $data['order_number'];
  33. $model->title = $data['title'] ?? '';
  34. $model->customer_id = $data['customer_id'] ?? 0;
  35. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  36. $model->install_method = $data['install_method'] ?? 0;
  37. $model->install_position = $data['install_position'] ?? '';
  38. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  39. $model->construction_fee = $data['construction_fee'] ?? 0;
  40. $model->service_price = $data['service_price'] ?? 0;
  41. $model->construction_time = $data['construction_time'] ?? 0;
  42. $model->handover_time = $data['handover_time'] ?? 0;
  43. $model->urgency = $data['urgency'] ?? 0;
  44. $model->mark = $data['mark'] ?? '';
  45. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  46. $model->address2 = $data['address2'] ?? '';
  47. $model->introduction = $data['introduction'] ?? '';
  48. $model->storehouse_id = $data['storehouse_id'] ?? 0;
  49. $model->save();
  50. $time = time();
  51. ConstructionInfo::where('del_time',0)
  52. ->where('construction_id',$data['id'])
  53. ->update(['del_time' => $time]);
  54. ConstructionProductInfo::where('del_time',0)
  55. ->where('construction_id',$data['id'])
  56. ->update(['del_time' => $time]);
  57. if(! empty($data['construction_contact'])){
  58. $insert = [];
  59. foreach ($data['construction_contact'] as $value){
  60. $insert[] = [
  61. 'construction_id' => $model->id,
  62. 'contact_type' => $value['id'],
  63. 'contact_info' => $value['info'],
  64. 'type' => ConstructionInfo::type_one,
  65. 'crt_time' => $time,
  66. ];
  67. }
  68. ConstructionInfo::insert($insert);
  69. }
  70. if(! empty($data['employee_one'])){
  71. $insert = [];
  72. foreach ($data['employee_one'] as $value){
  73. $insert[] = [
  74. 'construction_id' => $model->id,
  75. 'employee_id' => $value,
  76. 'type' => ConstructionInfo::type_two,
  77. 'crt_time' => $time,
  78. ];
  79. }
  80. ConstructionInfo::insert($insert);
  81. }
  82. if(! empty($data['product'])){
  83. $insert = [];
  84. foreach ($data['product'] as $value){
  85. $insert[] = [
  86. 'construction_id' => $model->id,
  87. 'product_id' => $value['product_id'],
  88. 'number' => $value['number'],
  89. 'cost' => $value['cost'] ?? 0,
  90. 'retail_price' => $value['retail_price'] ?? 0,
  91. 'mark' => $value['mark'] ?? '',
  92. 'crt_time' => $time,
  93. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  94. 'basic_type_id' => $value['basic_type_id'],
  95. 'price' => $value['price'],
  96. ];
  97. }
  98. ConstructionProductInfo::insert($insert);
  99. //锁定库存
  100. ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1]);
  101. }
  102. DB::commit();
  103. }catch (\Exception $exception){
  104. DB::rollBack();
  105. return [false,$exception->getMessage()];
  106. }
  107. return [true,''];
  108. }
  109. /**
  110. * 施工订单新增
  111. * @param $data
  112. * @param $user
  113. * @return array
  114. */
  115. public function constructionAdd($data,$user){
  116. list($status,$msg) = $this->constructionRule($data,$user);
  117. if(!$status) return [$status,$msg];
  118. try {
  119. DB::beginTransaction();
  120. $model = new Construction();
  121. $model->model_type = $data['model_type'];
  122. $model->order_number = $data['order_number'];
  123. $model->title = $data['title'] ?? '';
  124. $model->customer_id = $data['customer_id'] ?? 0;
  125. $model->customer_contact_id = $data['customer_contact_id'] ?? 0;
  126. $model->install_method = $data['install_method'] ?? 0;
  127. $model->install_position = $data['install_position'] ?? '';
  128. $model->sales_order_id = $data['sales_order_id'] ?? 0;
  129. $model->construction_fee = $data['construction_fee'] ?? 0;
  130. $model->service_price = $data['service_price'] ?? 0;
  131. $model->construction_time = $data['construction_time'] ?? 0;
  132. $model->handover_time = $data['handover_time'] ?? 0;
  133. $model->urgency = $data['urgency'] ?? 0;
  134. $model->mark = $data['mark'] ?? '';
  135. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  136. $model->address2 = $data['address2'] ?? '';
  137. $model->introduction = $data['introduction'] ?? '';
  138. $model->crt_id = $user['id'];
  139. $model->depart_id = $data['depart_id'] ?? 0;
  140. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  141. $model->storehouse_id = $data['storehouse_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. 'cost' => $value['cost'] ?? 0,
  177. 'retail_price' => $value['retail_price'] ?? 0,
  178. 'mark' => $value['mark'] ?? '',
  179. 'crt_time' => $time,
  180. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  181. 'basic_type_id' => $value['basic_type_id'],
  182. 'price' => $value['price'],
  183. ];
  184. }
  185. ConstructionProductInfo::insert($insert);
  186. //锁定库存
  187. ProductInventoryService::changeLockNumber($user,$msg[0],[]);
  188. }
  189. DB::commit();
  190. }catch (\Exception $exception){
  191. DB::rollBack();
  192. return [false,$exception->getMessage()];
  193. }
  194. return [true,''];
  195. }
  196. /**
  197. * 施工订单删除
  198. * @param $data
  199. * @return array
  200. */
  201. public function constructionDel($data,$user){
  202. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  203. $construction = Construction::where('del_time',0)->where('id',$data['id'])->first();
  204. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  205. $construction = $construction->toArray();
  206. if($construction['state'] > Construction::STATE_ZERO) return [false,'请确认施工单状态,操作失败'];
  207. $product_save = $this->getSaveDetail($data['id']);
  208. try {
  209. DB::beginTransaction();
  210. Construction::where('id',$data['id'])->update([
  211. 'del_time'=> time()
  212. ]);
  213. ConstructionInfo::where('del_time',0)
  214. ->where('construction_id',$data['id'])
  215. ->update(['del_time' => time()]);
  216. ConstructionProductInfo::where('del_time',0)
  217. ->where('construction_id',$data['id'])
  218. ->update(['del_time' => time()]);
  219. //锁定库存释放
  220. ProductInventoryService::changeLockNumber($user,[],$product_save);
  221. DB::commit();
  222. }catch (\Exception $exception){
  223. DB::rollBack();
  224. return [false,$exception->getMessage()];
  225. }
  226. return [true,''];
  227. }
  228. /**
  229. * 施工订单详情
  230. * @param $data
  231. * @return array
  232. */
  233. public function constructionDetail($data){
  234. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  235. $construction = Construction::where('del_time',0)
  236. ->where('id',$data['id'])
  237. ->first();
  238. if(empty($construction)) return [false,'施工订单不存在或已被删除'];
  239. $construction = $construction->toArray();
  240. $address = '';
  241. if(! empty($construction['address1'])) {
  242. $tmp = json_decode($construction['address1'],true);
  243. $construction['address1'] = $tmp;
  244. $tmp = implode(' ',$tmp);
  245. $tmp .= ' ' . $construction['address2'];
  246. $address = $tmp;
  247. }
  248. $construction['address'] = $address;
  249. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  250. $construction['sales_order_number'] = $sales;
  251. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  252. $construction['customer_title'] = $customer_title;
  253. $construction['storehouse_title'] = Storehouse::where('id',$construction['storehouse_id'])->value('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. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  300. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  301. foreach ($p_info as $value){
  302. $tmp = $map[$value['product_id']] ?? [];
  303. $value['title'] = $tmp['title'] ?? "";
  304. $value['code'] = $tmp['code'] ?? "";
  305. $value['size'] = $tmp['size'] ?? "";
  306. $value['unit'] = $tmp['unit'] ?? "";
  307. $value['bar_code'] = $tmp['bar_code'] ?? "";
  308. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  309. $construction['product'][] = $value;
  310. }
  311. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  312. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  313. return [true, $construction];
  314. }
  315. /**
  316. * 施工订单列表
  317. * @param $data
  318. * @param $user
  319. * @return array
  320. */
  321. public function constructionList($data,$user){
  322. $model = new Construction(['userData' => $user, 'search' => $data]);
  323. $model = $model->where('del_time',0)
  324. ->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')
  325. ->orderby('id', 'desc');
  326. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  327. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  328. $list = $this->limit($model,'',$data);
  329. $list = $this->fillData($list);
  330. return [true, $list];
  331. }
  332. /**
  333. * 参数规则
  334. * @param $data
  335. * @param $user
  336. * @param $is_add
  337. * @return array
  338. */
  339. public function constructionRule(&$data, $user, $is_add = true){
  340. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  341. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  342. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  343. if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
  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['construction_time'])) $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  358. if(! empty($data['handover_time'])) $data['handover_time'] = $this->changeDateToDateMin($data['handover_time']);
  359. if($data['model_type'] == Construction::Model_type_one){
  360. if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  361. if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  362. }else{
  363. if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  364. if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  365. }
  366. //所属部门 以及 顶级部门
  367. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  368. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  369. $product_submit = $product_id = [];
  370. foreach ($data['product'] as $value){
  371. if(empty($value['number'])) return [false,'产品数量不能为空'];
  372. $res = $this->checkNumber($value['number']);
  373. if(! $res) return [false,'请输入正确的产品数量'];
  374. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  375. if(isset($product_submit[$key])){
  376. $product_submit[$key] += $value['number'];
  377. }else{
  378. $product_submit[$key] = $value['number'];
  379. }
  380. $product_id[] = $value['product_id'];
  381. }
  382. //剩余能施工
  383. $id = $data['id'] ?? 0;
  384. $s_product = $this->getSaveReturnCompareMessage($id, $data['sales_order_id']);
  385. //比较
  386. foreach ($product_submit as $pro => $number){
  387. $tmp = explode(',',$pro);
  388. $p = $tmp[0];
  389. if(! isset($s_product[$p])) return [false,'施工产品错误,合同中不存在该产品'];
  390. $s_number = $s_product[$p];
  391. if($number > $s_number) return [false,'施工产品数量不能超过合同产品数据'];
  392. }
  393. $id = $data['id'] ?? 0;
  394. $product_save = $this->getSaveDetail($id);
  395. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit, $product_save);
  396. if(! $status) return [false, $msg];
  397. if($is_add){
  398. $bool = Construction::where('del_time',0)->where('order_number',$data['order_number'])->exists();
  399. if($bool) return [false,'工单编号已存在,请重新获取'];
  400. }else{
  401. if(empty($data['id'])) return [false,'ID不能为空'];
  402. }
  403. return [true, [$product_submit, $product_save]];
  404. }
  405. /**
  406. * 数据拼接
  407. * @param $data
  408. * @return array
  409. */
  410. public function fillData($data){
  411. if(empty($data['data'])) return $data;
  412. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency')));
  413. $basic_map = BasicType::whereIn('id',$array)
  414. ->pluck('title','id')
  415. ->toArray();
  416. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  417. ->pluck('emp_name','id')
  418. ->toArray();
  419. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  420. ->pluck('title','id')
  421. ->toArray();
  422. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->pluck('order_number','id')->toArray();
  423. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  424. ->pluck('title','id')
  425. ->toArray();
  426. foreach ($data['data'] as $key => $value){
  427. $address = '';
  428. if(! empty($value['address1'])) {
  429. $tmp = json_decode($value['address1'],true);
  430. $tmp = implode(' ',$tmp);
  431. $tmp .= ' ' . $value['address2'];
  432. $address = $tmp;
  433. }
  434. $data['data'][$key]['address'] = $address;
  435. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  436. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  437. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  438. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  439. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  440. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  441. $data['data'][$key]['state_title'] = Construction::$name[$value['state']] ?? '';
  442. $data['data'][$key]['sales_order_number'] = $sales[$value['sales_order_id']] ?? '';
  443. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  444. }
  445. return $data;
  446. }
  447. /**
  448. * 获取施工单号
  449. * @param $data
  450. * @return array
  451. */
  452. public function constructionGet($data){
  453. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  454. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  455. $prefix = Construction::$prefix[$data['model_type']];
  456. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  457. if(! $order_number) return [false,'工单编号生成失败!'];
  458. return [true,['order_number' => $order_number]];
  459. }
  460. /**
  461. * 获取保存详情
  462. * @param $id
  463. * @return array
  464. */
  465. public function getSaveDetail($id){
  466. $product_save = [];
  467. if(empty($id)) return $product_save;
  468. $sub = ConstructionProductInfo::where('construction_id',$id)
  469. ->where('del_time',0)
  470. ->get()->toArray();
  471. foreach ($sub as $value){
  472. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  473. if(isset($product_save[$key])){
  474. $product_save[$key] += $value['number'];
  475. }else{
  476. $product_save[$key] = $value['number'];
  477. }
  478. }
  479. return $product_save;
  480. }
  481. public function getSaveReturnCompareMessage($id = 0, $sales_order_id = 0){
  482. $product_save = [];
  483. $sub = ConstructionProductInfo::where('del_time',0)
  484. ->when(! empty($id), function ($query) use ($id) {
  485. return $query->where('construction_id', '<>',$id);
  486. })
  487. ->get()->toArray();
  488. foreach ($sub as $value){
  489. if(isset($product_save[$value['product_id']])){
  490. $product_save[$value['product_id']] += $value['number'];
  491. }else{
  492. $product_save[$value['product_id']] = $value['number'];
  493. }
  494. }
  495. $sales_order_product = [];
  496. $sales_product = SalesOrderProductInfo::where('del_time',0)
  497. ->where('sales_order_id',$sales_order_id)
  498. ->get()->toArray();
  499. foreach ($sales_product as $value){
  500. $product_save_tmp = $product_save[$value['product_id']] ?? 0;
  501. if(isset($sales_order_product[$value['product_id']])){
  502. $sales_order_product[$value['product_id']] += $value['number'];
  503. }else{
  504. $sales_order_product[$value['product_id']] = $value['number'] - $product_save_tmp;
  505. }
  506. }
  507. return $sales_order_product;
  508. }
  509. }