ConstructionService.php 18 KB

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