|
@@ -0,0 +1,439 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Service;
|
|
|
+
|
|
|
+use App\Model\BasicType;
|
|
|
+use App\Model\Customer;
|
|
|
+use App\Model\CustomerInfo;
|
|
|
+use App\Model\Depart;
|
|
|
+use App\Model\Employee;
|
|
|
+use App\Model\SalesOrder;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+class SalesOrderService extends Service
|
|
|
+{
|
|
|
+ public function salesOrderEdit($data,$user){
|
|
|
+ list($status,$msg) = $this->salesOrderRule($data,false);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = Customer::where('id',$data['id'])->first();
|
|
|
+ $model->title = $data['title'];
|
|
|
+ $model->model_type = $data['model_type'];
|
|
|
+ $model->customer_intention = $data['customer_intention'] ?? 0;
|
|
|
+ $model->customer_from = $data['customer_from'] ?? 0;
|
|
|
+ $model->customer_type = $data['customer_type'] ?? 0;
|
|
|
+ $model->car_type = $data['car_type'] ?? 0;
|
|
|
+ $model->consulting_product = $data['consulting_product'] ?? '';
|
|
|
+ $model->intention_product = $data['intention_product'] ?? 0;
|
|
|
+ $model->progress_stage = $data['progress_stage'] ?? 0;
|
|
|
+ $model->address1 = $data['address1'] ?? '';
|
|
|
+ $model->address2 = $data['address2'] ?? '';
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->importance = $data['importance'] ?? '';
|
|
|
+ $model->company = $data['company'] ?? '';
|
|
|
+ $model->company_short_name = $data['company_short_name'] ?? '';
|
|
|
+ $model->depart_id = $data['depart_id'] ?? 0;
|
|
|
+ $model->state_type = $data['state_type'] ?? 0;
|
|
|
+ $model->customer_state = $data['customer_state'] ?? 0;
|
|
|
+ $model->customer_grade = $data['customer_grade'] ?? 0;
|
|
|
+ $model->save();
|
|
|
+ $time = time();
|
|
|
+
|
|
|
+ CustomerInfo::where('del_time',0)
|
|
|
+ ->where('customer_id',$data['id'])
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ if(! empty($data['customer_contact'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['customer_contact'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'contact_type' => $value['id'],
|
|
|
+ 'contact_info' => $value['info'],
|
|
|
+ 'type' => CustomerInfo::type_one,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['employee_one'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['employee_one'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'employee_id' => $value,
|
|
|
+ 'type' => CustomerInfo::type_two,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['employee_two'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['employee_two'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'employee_id' => $value,
|
|
|
+ 'type' => CustomerInfo::type_three,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['employee_three'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['employee_three'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'employee_id' => $value,
|
|
|
+ 'type' => CustomerInfo::type_four,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['img'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['img'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'file' => $value,
|
|
|
+ 'type' => CustomerInfo::type_five,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['file'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['file'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'file' => $value,
|
|
|
+ 'type' => CustomerInfo::type_six,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function salesOrderAdd($data,$user){
|
|
|
+ list($status,$msg) = $this->salesOrderRule($data);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ $model = new Customer();
|
|
|
+ $model->title = $data['title'];
|
|
|
+ $model->model_type = $data['model_type'];
|
|
|
+ $model->customer_intention = $data['customer_intention'] ?? 0;
|
|
|
+ $model->customer_from = $data['customer_from'] ?? 0;
|
|
|
+ $model->customer_type = $data['customer_type'] ?? 0;
|
|
|
+ $model->car_type = $data['car_type'] ?? 0;
|
|
|
+ $model->consulting_product = $data['consulting_product'] ?? '';
|
|
|
+ $model->intention_product = $data['intention_product'] ?? 0;
|
|
|
+ $model->progress_stage = $data['progress_stage'] ?? 0;
|
|
|
+ $model->address1 = $data['address1'] ?? '';
|
|
|
+ $model->address2 = $data['address2'] ?? '';
|
|
|
+ $model->crt_id = $user['id'];
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->importance = $data['importance'] ?? '';
|
|
|
+ $model->company = $data['company'] ?? '';
|
|
|
+ $model->company_short_name = $data['company_short_name'] ?? '';
|
|
|
+ $model->depart_id = $data['depart_id'] ?? 0;
|
|
|
+ $model->state_type = $data['state_type'] ?? 0;
|
|
|
+ $model->customer_state = $data['customer_state'] ?? 0;
|
|
|
+ $model->customer_grade = $data['customer_grade'] ?? 0;
|
|
|
+ $model->save();
|
|
|
+ $time = time();
|
|
|
+
|
|
|
+ if(! empty($data['customer_contact'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['customer_contact'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'contact_type' => $value['id'],
|
|
|
+ 'contact_info' => $value['info'],
|
|
|
+ 'type' => CustomerInfo::type_one,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['employee_one'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['employee_one'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'employee_id' => $value,
|
|
|
+ 'type' => CustomerInfo::type_two,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['employee_two'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['employee_two'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'employee_id' => $value,
|
|
|
+ 'type' => CustomerInfo::type_three,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['employee_three'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['employee_three'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'employee_id' => $value,
|
|
|
+ 'type' => CustomerInfo::type_four,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['img'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['img'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'file' => $value,
|
|
|
+ 'type' => CustomerInfo::type_five,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(! empty($data['file'])){
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['file'] as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'customer_id' => $model->id,
|
|
|
+ 'file' => $value,
|
|
|
+ 'type' => CustomerInfo::type_six,
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ CustomerInfo::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function salesOrderDel($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
+
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ Customer::whereIn('id',$data['id'])->update([
|
|
|
+ 'del_time'=> time()
|
|
|
+ ]);
|
|
|
+ CustomerInfo::where('del_time',0)
|
|
|
+ ->where('customer_id',$data['id'])
|
|
|
+ ->update(['del_time' => time()]);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function salesOrderDetail($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
+
|
|
|
+ $customer = Customer::where('del_time',0)
|
|
|
+ ->where('id',$data['id'])
|
|
|
+ ->first();
|
|
|
+ if(empty($customer)) return [false,'客户不存在或已被删除'];
|
|
|
+ $customer = $customer->toArray();
|
|
|
+ $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] =[];
|
|
|
+ $array = [
|
|
|
+ $customer['customer_intention'],
|
|
|
+ $customer['customer_from'],
|
|
|
+ $customer['customer_type'],
|
|
|
+ $customer['car_type'],
|
|
|
+ $customer['intention_product'],
|
|
|
+ $customer['progress_stage'],
|
|
|
+ $customer['state_type'],
|
|
|
+ $customer['customer_state'],
|
|
|
+ $customer['customer_grade'],
|
|
|
+ ];
|
|
|
+ $basic_map = BasicType::whereIn('id',$array)
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+ $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value();
|
|
|
+ foreach ($customer as $key => $value){
|
|
|
+ $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
|
|
|
+ $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
|
|
|
+ $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
|
|
|
+ $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
|
|
|
+ $customer[$key]['intention_product_title'] = $basic_map[$value['intention_product']] ?? '';
|
|
|
+ $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
|
|
|
+ $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
|
|
|
+ $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
|
|
|
+ $customer[$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? '';
|
|
|
+ $customer[$key]['depart_title'] = $depart_title ?? '';
|
|
|
+ }
|
|
|
+
|
|
|
+ $customer_info = CustomerInfo::where('del_time',0)
|
|
|
+ ->where('customer_id',$customer['id'])
|
|
|
+ ->select('id','customer_id','contact_type','contact_info','employee_id','file','type')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($customer_info as $value){
|
|
|
+ if($value['type'] == CustomerInfo::type_one){
|
|
|
+ $customer['customer_contact'][] = [
|
|
|
+ 'id' => $value['contact_type'],
|
|
|
+ 'info' => $value['contact_info']
|
|
|
+ ];
|
|
|
+ }elseif ($value['type'] == CustomerInfo::type_two){
|
|
|
+ $customer['employee_one'][] = [
|
|
|
+ $value['employee_id']
|
|
|
+ ];
|
|
|
+ }elseif ($value['type'] == CustomerInfo::type_three){
|
|
|
+ $customer['employee_two'][] = [
|
|
|
+ $value['employee_id']
|
|
|
+ ];
|
|
|
+ }elseif ($value['type'] == CustomerInfo::type_four){
|
|
|
+ $customer['employee_three'][] = [
|
|
|
+ $value['employee_id']
|
|
|
+ ];
|
|
|
+ }elseif ($value['type'] == CustomerInfo::type_five){
|
|
|
+ $customer['img'][] = [
|
|
|
+ $value['employee_id']
|
|
|
+ ];
|
|
|
+ }elseif ($value['type'] == CustomerInfo::type_six){
|
|
|
+ $customer['file'][] = [
|
|
|
+ $value['employee_id']
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $customer];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function salesOrderList($data,$user){
|
|
|
+ $model = Customer::where('del_time',0)
|
|
|
+ ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','customer_grade')
|
|
|
+ ->orderby('id', 'desc')
|
|
|
+ ->where('model_type',$data['model_type']);
|
|
|
+
|
|
|
+ if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
+
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+ $list = $this->fillData($list);
|
|
|
+
|
|
|
+ return [true, $list];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function salesOrderRule($data, $is_add = true){
|
|
|
+ if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
|
|
|
+ if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
|
|
|
+ if(empty($data['title'])) return [false,'客户名称不能为空'];
|
|
|
+ if($data['model_type'] == Customer::Model_type_one){
|
|
|
+ if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
|
|
|
+ if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
|
|
|
+ if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
|
|
|
+ if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
|
|
|
+ if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
|
|
|
+ }else{
|
|
|
+ if(empty($data['car_type'])) return [false,'车型不能为空'];
|
|
|
+ if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if($is_add){
|
|
|
+ $bool = Customer::where('del_time',0)
|
|
|
+ ->where('title',$data['title'])
|
|
|
+ ->where('model_type',$data['model_type'])
|
|
|
+ ->exists();
|
|
|
+ }else{
|
|
|
+ if(empty($data['id'])) return [false,'ID不能为空'];
|
|
|
+ $bool = Customer::where('del_time',0)
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
+ ->where('title',$data['title'])
|
|
|
+ ->where('model_type',$data['model_type'])
|
|
|
+ ->exists();
|
|
|
+ }
|
|
|
+ if($bool) return [false,'客户名称不能重复'];
|
|
|
+
|
|
|
+ return [true, $data];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fillData($data){
|
|
|
+ if(empty($data['data'])) return $data;
|
|
|
+
|
|
|
+ $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_intention'),array_column($data['data'],'customer_from'),array_column($data['data'],'customer_type'),array_column($data['data'],'car_type'),array_column($data['data'],'intention_product'),array_column($data['data'],'progress_stage'),array_column($data['data'],'state_type'),array_column($data['data'],'customer_state'),array_column($data['data'],'customer_grade')));
|
|
|
+ $basic_map = BasicType::whereIn('id',$array)
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+ $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+ $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
|
|
|
+ ->pluck('emp_name','id')
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
|
|
|
+ $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
|
|
|
+ $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
|
|
|
+ $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
|
|
|
+ $data['data'][$key]['intention_product_title'] = $basic_map[$value['intention_product']] ?? '';
|
|
|
+ $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
|
|
|
+ $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
|
|
|
+ $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
|
|
|
+ $data['data'][$key]['customer_grade_title'] = $basic_map[$value['customer_grade']] ?? '';
|
|
|
+ $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
|
|
|
+ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
+ $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function salesOrderGet($data){
|
|
|
+ if(empty($data['model_type'])) return [false,'订单模板类型不能为空'];
|
|
|
+ if(! isset(SalesOrder::$prefix[$data['model_type']])) return [false,'订单模板类型错误'];
|
|
|
+ $prefix = SalesOrder::$prefix[$data['model_type']];
|
|
|
+ $order_number = OrderNoService::createSalesOrderNumber($prefix);
|
|
|
+ if(! $order_number) return [false,'合同编号生成失败!'];
|
|
|
+
|
|
|
+ return [false,$order_number];
|
|
|
+ }
|
|
|
+}
|