ConstructionService.php 20 KB

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