ConstructionService.php 17 KB

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