ConstructionService.php 17 KB

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