ConstructionService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. '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'] ? json_encode($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['retail_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. $address = '';
  194. if(! empty($construction['address1'])) {
  195. $tmp = json_decode($construction['address1'],true);
  196. $construction['address1'] = $tmp;
  197. $tmp = implode(' ',$tmp);
  198. $tmp .= ' ' . $construction['address2'];
  199. $address = $tmp;
  200. }
  201. $construction['address'] = $address;
  202. $sales = SalesOrder::where('id',$construction['sales_order_id'])->value('order_number');
  203. $construction['sales_order_number'] = $sales;
  204. $customer_title = Customer::where('id',$construction['customer_id'])->value('title');
  205. $construction['customer_title'] = $customer_title;
  206. $emp_title = Employee::where('id',$construction['customer_contact_id'])->value('emp_name');
  207. $construction['customer_contact_title'] = $emp_title;
  208. $construction['employee_one'] = $construction['construction_contact'] = $construction['product'] = [];
  209. $array = [
  210. $construction['install_method'],
  211. $construction['urgency'],
  212. ];
  213. $basic_map = BasicType::whereIn('id',$array)
  214. ->pluck('title','id')
  215. ->toArray();
  216. $construction = [$construction];
  217. foreach ($construction as $key => $value){
  218. $construction[$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  219. $construction[$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  220. }
  221. $construction = $construction[0];
  222. $construction_info = ConstructionInfo::where('del_time',0)
  223. ->where('construction_id',$construction['id'])
  224. ->select('id','construction_id','employee_id','type','contact_type','contact_info')
  225. ->get()->toArray();
  226. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$construction['crt_id']],array_column($construction_info,'employee_id'))))
  227. ->pluck('emp_name','id')
  228. ->toArray();
  229. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($construction_info,'contact_type')))
  230. ->pluck('title','id')
  231. ->toArray();
  232. foreach ($construction_info as $value){
  233. if($value['type'] == ConstructionInfo::type_one){
  234. $tmp = [
  235. 'id' => $value['contact_type'],
  236. 'title' => $basic_map2[$value['contact_type']] ?? '',
  237. 'info' => $value['contact_info']
  238. ];
  239. $construction['customer_contact'][] = $tmp;
  240. }elseif ($value['type'] == ConstructionInfo::type_two){
  241. $tmp = [
  242. 'id' => $value['employee_id'],
  243. 'name' => $emp_map[$value['employee_id']],
  244. ];
  245. $construction['employee_one'][] = $tmp;
  246. }
  247. }
  248. $p_info = ConstructionProductInfo::where('del_time',0)
  249. ->where('construction_id',$construction['id'])
  250. ->select('id','construction_id','product_id','mark','price','number')
  251. ->get()->toArray();
  252. foreach ($p_info as $value){
  253. $construction['product'][] = [
  254. 'product_id' => $value['product_id'],
  255. 'mark' => $value['mark'],
  256. 'retail_price' => $value['price'],
  257. 'number' => $value['number'],
  258. ];
  259. }
  260. $construction['crt_name'] = $emp_map[$construction['crt_id']] ?? '';
  261. $construction['crt_time'] = $construction['crt_time'] ? date("Y-m-d H:i:s",$construction['crt_time']): '';
  262. return [true, $construction];
  263. }
  264. public function constructionList($data,$user){
  265. $model = new Construction(['userData' => $user]);
  266. $model = $model->where('del_time',0)
  267. ->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')
  268. ->orderby('id', 'desc')
  269. ->where('model_type',$data['model_type']);
  270. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  271. $list = $this->limit($model,'',$data);
  272. $list = $this->fillData($list);
  273. return [true, $list];
  274. }
  275. public function constructionRule(&$data, $user, $is_add = true){
  276. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  277. if(! in_array($data['model_type'],Construction::$model_type)) return [false,'工单模板类型错误'];
  278. if(empty($data['order_number'])) return [false,'工单编号不能为空'];
  279. if(empty($data['construction_time'])) return [false,'实施日期不能为空'];
  280. $data['construction_time'] = $this->changeDateToDateMin($data['construction_time']);
  281. if(empty($data['product'])) return [false,'请选择产品'];
  282. foreach ($data['product'] as $value){
  283. if(empty($value['number'])) return [false,'产品数量不能为空'];
  284. $res = $this->checkNumber($value['number']);
  285. if(! $res) return [false,'请输入正确的产品数量'];
  286. if(empty($value['retail_price'])) return [false,'产品价格不能为空'];
  287. $res = $this->checkNumber($value['retail_price']);
  288. if(! $res) return [false,'产品价格请输入不超过两位小数并且大于0的数值'];
  289. }
  290. if(! empty($data['construction_fee'])){
  291. $res = $this->checkNumber($data['construction_fee']);
  292. if(! $res) return [false,'施工费用请输入不超过两位小数并且大于0的数值'];
  293. }
  294. if(! empty($data['service_price'])){
  295. $res = $this->checkNumber($data['service_price']);
  296. if(! $res) return [false,'服务价格请输入不超过两位小数并且大于0的数值'];
  297. }
  298. if($data['model_type'] == Construction::Model_type_one){
  299. if(empty($data['install_method'])) return [false,'安装方式不能为空'];
  300. if(empty($data['install_position'])) return [false,'安装地点不能为空'];
  301. }else{
  302. if(empty($data['construction_contact'])) return [false,'联系方式不能为空'];
  303. if(empty($data['address1']) || empty($data['address2'])) return [false,'地址不能为空'];
  304. }
  305. //所属部门 以及 顶级部门
  306. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  307. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  308. if($is_add){
  309. $bool = Construction::where('del_time',0)->where('order_number',$data['order_number'])->exists();
  310. if($bool) return [false,'工单编号已存在,请重新获取'];
  311. }else{
  312. if(empty($data['id'])) return [false,'ID不能为空'];
  313. }
  314. return [true, $data];
  315. }
  316. public function fillData($data){
  317. if(empty($data['data'])) return $data;
  318. $array = array_unique(array_merge_recursive(array_column($data['data'],'install_method'),array_column($data['data'],'urgency')));
  319. $basic_map = BasicType::whereIn('id',$array)
  320. ->pluck('title','id')
  321. ->toArray();
  322. $emp = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'customer_contact_id'))))
  323. ->pluck('emp_name','id')
  324. ->toArray();
  325. $customer = Customer::whereIn('id',array_unique(array_column($data['data'],'customer_id')))
  326. ->pluck('title','id')
  327. ->toArray();
  328. foreach ($data['data'] as $key => $value){
  329. $address = '';
  330. if(! empty($value['address1'])) {
  331. $tmp = json_decode($value['address1'],true);
  332. $tmp = implode(' ',$tmp);
  333. $tmp .= ' ' . $value['address2'];
  334. $address = $tmp;
  335. }
  336. $data['data'][$key]['address'] = $address;
  337. $data['data'][$key]['install_method_title'] = $basic_map[$value['install_method']] ?? '';
  338. $data['data'][$key]['urgency_title'] = $basic_map[$value['urgency']] ?? '';
  339. $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
  340. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  341. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  342. $data['data'][$key]['customer_contact_title'] = $emp[$value['customer_contact_id']] ?? '';
  343. }
  344. return $data;
  345. }
  346. public function constructionGet($data){
  347. if(empty($data['model_type'])) return [false,'工单模板类型不能为空'];
  348. if(! isset(Construction::$prefix[$data['model_type']])) return [false,'工单模板类型错误'];
  349. $prefix = Construction::$prefix[$data['model_type']];
  350. $order_number = OrderNoService::createConstructionOrderNumber($prefix);
  351. if(! $order_number) return [false,'工单编号生成失败!'];
  352. return [true,['order_number' => $order_number]];
  353. }
  354. }