cqp 4 月之前
父節點
當前提交
77880605b1

+ 13 - 0
app/Http/Controllers/Api/CustomerController.php

@@ -33,6 +33,19 @@ class CustomerController extends BaseController
         }
     }
 
+    public function customerImportanceEdit(Request $request)
+    {
+        $service = new CustomerService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerImportanceEdit($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
     public function customerDel(Request $request)
     {
         $service = new CustomerService();

+ 13 - 0
app/Http/Controllers/Api/FollowUpRecordController.php

@@ -71,4 +71,17 @@ class FollowUpRecordController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function followUpRecordBatchAdd(Request $request)
+    {
+        $service = new FollowUpRecordService();
+        $user = $request->userData->toArray();
+        list($status,$data) = $service->followUpRecordBatchAdd($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 26 - 0
app/Http/Controllers/Api/SalesOrderController.php

@@ -122,4 +122,30 @@ class SalesOrderController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function salesOrderBatchDispatchCompany(Request $request)
+    {
+        $service = new SalesOrderService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->salesOrderBatchDispatchCompany($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function salesOrderGetBuildFee(Request $request)
+    {
+        $service = new SalesOrderService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->salesOrderGetBuildFee($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 4 - 0
app/Http/Middleware/OssFileDeal.php

@@ -47,6 +47,10 @@ class OssFileDeal
                     }else{
                         if(! empty($file['new']['origin']) && ! empty($file['new']['img_list'])){
                             $service->createOssUploadBatch($file['new']);
+                        }else{
+                            foreach ($file['new'] as $value){
+                                $service->createOssUploadBatch($value);
+                            }
                         }
                     }
 

+ 6 - 2
app/Model/Construction.php

@@ -9,19 +9,23 @@ class Construction extends UseScopeBaseModel
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
     const Model_type_one = 1; // 到店安装
-    const Model_type_two = 2; // 仅施工
+    const Model_type_two = 2; // 带货安装
+    const Model_type_three = 3; // 带货安装
     public static $model_type = [
         self::Model_type_one,
         self::Model_type_two,
+        self::Model_type_three,
     ];
     public static $model_type_title = [
         self::Model_type_one => '到店安装',
-        self::Model_type_two => '仅施工',
+        self::Model_type_two => '带货安装(包安装)',
+        self::Model_type_three => '带货安装(不包安装)',
     ];
 
     public static $prefix = [
         self::Model_type_one => 'WO0.',
         self::Model_type_two => 'T9SH.',
+        self::Model_type_three => 'T9SH.',
     ];
 
     const State_minus_one = -1;//驳回

+ 2 - 0
app/Model/OrderOperation.php

@@ -44,6 +44,7 @@ class OrderOperation extends Model
     const twenty_eight = 28;
     const twenty_nine = 29;
     const thirty = 30;
+    const thirty_one = 31;
 
     public static $type = [
         self::zero => '系统设置变更 ',
@@ -77,5 +78,6 @@ class OrderOperation extends Model
         self::twenty_eight => '活动包弃审',
         self::twenty_nine => '虚拟采购单确认',
         self::thirty => '虚拟采购单弃审',
+        self::thirty_one => '合同批量门店派单',
     ];
 }

+ 13 - 2
app/Model/SalesOrder.php

@@ -71,7 +71,7 @@ class SalesOrder extends UseScopeBaseModel
 
     const special_status = -100;
     //已审核
-    const search = "(sales_order_type = " . SalesOrder::Order_type_one . " and state > " . SalesOrder::State_one." and dispatch_time_second = 0)";
+    const search = "(sales_order_type = " . SalesOrder::Order_type_one . " and state > " . SalesOrder::State_one. ")";
 
     //审核通过条件  安装件 且 线上订单 补贴订单 的已审核是2  其它是3
     const search1 = "(sales_order_type = " . SalesOrder::Order_type_one . " and ((model_type IN( " . SalesOrder::Model_type_four ." , " . SalesOrder::Model_type_seven .") and state = " . SalesOrder::State_two.") or (model_type Not IN (" . SalesOrder::Model_type_four ." , " . SalesOrder::Model_type_seven .") and state = " . SalesOrder::State_three.")))";
@@ -111,12 +111,23 @@ class SalesOrder extends UseScopeBaseModel
         self::invoice_one => '已发货',
     ];
 
+    const build_minus = -1;//总社
     //分社安装类型
     const build_zero = 0;//到店安装
     const build_one = 1;//仅施工
+    const build_two = 2;//仅施工
     public static $build_state = [
+        self::build_minus => '总社|分社自行安装',
         self::build_zero => '到店安装',
-        self::build_one => '仅施工',
+        self::build_one => '带货安装(包安装)',
+        self::build_two => '带货安装(不包安装)',
+    ];
+
+    //对应施工单的模板
+    public static $build_for_con = [
+        self::build_zero => 1,
+        self::build_one => 2,
+        self::build_two => 3,
     ];
 
     public static $prefix = [

+ 2 - 0
app/Model/SalesOrderInfo.php

@@ -16,6 +16,8 @@ class SalesOrderInfo extends Model
     const type_four = 4; // 文件
     const type_five = 5; // 活动包
     const type_six = 6; // 客户完结图片
+    const type_seven = 7; // 公司完结图片
+    const type_eight = 8; // 公司完结备注
     public static $type = [
         self::type_one,
         self::type_two,

+ 72 - 3
app/Service/CheckService.php

@@ -5,7 +5,6 @@ namespace App\Service;
 use App\Model\Construction;
 use App\Model\ConstructionProductInfo;
 use App\Model\Customer;
-use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\InOutRecord;
 use App\Model\Inventory;
@@ -53,6 +52,7 @@ class CheckService extends Service
     const twl  = 12; //活动包
     const thi  = 13; //虚拟采购单
     const fourteen  = 14; //盘点
+    const fifteen  = 15; //放弃门店派单
 
     //中文对照
     public $map = [
@@ -70,6 +70,7 @@ class CheckService extends Service
         self::twl => '活动包',
         self::thi => '虚拟采购单',
         self::fourteen => '盘点单',
+        self::fifteen => '放弃门店派单',
     ];
 
     //入库操作
@@ -111,6 +112,7 @@ class CheckService extends Service
         self::four => 'settleSalesOrder', // 合同 安装件 合同公司完结
         self::nine => 'settleSalesOrderCustomer', // 合同 安装件 合同客户完结
         self::eve => 'settleSalesOrderConfirm', // 合同 安装件 合同接收
+        self::fifteen => 'settleSalesOrderConfirmBack', // 合同 安装件 合同接收
     ];
 
     //单据操作
@@ -1067,7 +1069,33 @@ class CheckService extends Service
         SalesOrder::where('del_time',0)->where('order_number',$data['order_number'])
             ->update(['state' => SalesOrder::State_seven]);
 
-        return [true, $model->toArray()];
+        $time = time();
+        $insert = [];
+        $new = [];
+        if(! empty($data['img'])){
+            foreach ($data['img'] as $value){
+                $insert[] = [
+                    'sales_order_id' => $model->id,
+                    'data_id' => 0,
+                    'type' => SalesOrderInfo::type_seven,
+                    'file' => $value,
+                    'crt_time' => $time,
+                ];
+                $new[] = $value;
+            }
+        }
+        if(! empty($data['mark'])){
+            $insert[] = [
+                'sales_order_id' => $model->id,
+                'data_id' => $user['id'],
+                'type' => SalesOrderInfo::type_eight,
+                'file' => $data['mark'],
+                'crt_time' => $time,
+            ];
+        }
+        if(! empty($insert)) SalesOrderInfo::insert($insert);
+
+        return [true, ['file' => $new, 'order' => $model->toArray()]];
     }
 
     public function settleSalesOrderCustomer($data,$user){
@@ -1126,6 +1154,40 @@ class CheckService extends Service
         return [true, $model->toArray()];
     }
 
+    public function settleSalesOrderConfirmBack($data,$user){
+        $model = SalesOrder::where('order_number', $data['order_number'])
+            ->where('del_time',0)
+            ->first();
+        if(empty($model)) return [false, '合同不存在或已被删除'];
+        //安装件
+        if($model->state != SalesOrder::State_four) return [false, '请确认合同状态,操作失败'];
+
+        //校验施工
+        $bool = Construction::where('del_time',0)
+            ->where('sales_order_id', $model->id)
+            ->exists();
+        if($bool) return [false,'合同已生成施工单,操作失败'];
+
+        if($model->is_confirm != 1) return [false, '合同未接受派单,操作失败'];
+        $model->is_confirm = 0;
+        $model->save();
+
+        //单据状态还原
+        SalesOrder::where('del_time',0)->where('id', $model->id)->update([
+            'state' => SalesOrder::State_three,
+            'dispatch_time_second' => 0,
+            'dispatch_time_second_id' => 0,
+        ]);
+
+        SeeRange::where('del_time',0)
+            ->where('data_id', $model->id)
+            ->where('data_type',SeeRange::type_seven)
+            ->where('type',SeeRange::data_three)
+            ->update(['del_time' => time()]);
+
+        return [true, $model->toArray()];
+    }
+
     public function confirmSales($data){
         $model = SalesOrder::where('order_number', $data['order_number'])
             ->where('del_time',0)
@@ -1419,11 +1481,18 @@ class CheckService extends Service
             if($function2) {
                 list($bool,$msg) = $this->$function2($data,$user);
                 if($bool) {
-                    $order = $msg;
+                    $file = [];
+                    if(isset($msg['order'])){
+                        $order = $msg['order'];
+                        $file = $msg['file'];
+                    } else{
+                        $order = $msg;
+                    }
 
                     list($bool,$err) = $this->createRecordAndInventoryMy($data,$user,$order);
                     if($bool){
                         DB::commit();
+                        if(! empty($file)) return [true, ['file' => ['new' => $file]]];
                         return [true, ''];
                     }else{
                         DB::rollBack();

+ 45 - 6
app/Service/ConstructionService.php

@@ -315,7 +315,7 @@ class ConstructionService extends Service
         }catch (\Exception $exception){
             DB::rollBack();
             if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
-                return [false, '施工单编号已存在,请重新获取!'];
+                return [false, '网络波动,请重新操作!'];
             }
             return [false,$exception->getMessage()];
         }
@@ -708,6 +708,8 @@ class ConstructionService extends Service
             //总社的订单 需要派单 才能建施工
             if($sale['state'] < SalesOrder::State_four) return [false,'合同未门店派单,不允许新建施工单'];
         }
+        if(empty($data['customer_id'])) return [false, '订单合同暂无客户,请补充完信息再进行施工单操作'];
+
         list($status,$msg) = $this->limitingSendRequestBackgExpire("construction" . $sale['order_number']);
         if(! $status) return [false, $msg];
         if(empty($data['product'])) return [false,'请选择产品'];
@@ -785,10 +787,10 @@ class ConstructionService extends Service
         }
 
         if($is_add){
-            $bool = Construction::where('del_time',0)
-                ->where('order_number',$data['order_number'])
-                ->exists();
-            if($bool) return [false,'工单编号已存在,请重新获取'];
+            $prefix = Construction::$prefix[$data['model_type']];
+            $order_number = OrderNoService::createConstructionOrderNumber($prefix);
+            if(! $order_number) return [false,'工单编号生成失败!'];
+            $data['order_number'] = $order_number;
         }else{
             if(empty($data['id'])) return [false,'ID不能为空'];
             $construction = Construction::where('del_time',0)
@@ -1149,6 +1151,8 @@ class ConstructionService extends Service
                 }
             }
 
+            if(isset($data['is_submit'])) $this->updateSaleOrder($model->id);
+
         }catch (\Throwable $e){
             return [false, $e->getMessage()];
         }
@@ -1156,6 +1160,39 @@ class ConstructionService extends Service
         return [true, ['file' => ['old' => $old, 'new' => $new]]];
     }
 
+    public function updateSaleOrder($id){
+        $d = DeliveryNote::where('id',$id)->first();
+        if(empty($d)) return;
+        $d = $d->toArray();
+
+        if(! empty($d['img2'])) $img[] = $d['img2'];
+        if(! empty($d['img3'])) $img[] = $d['img3'];
+
+        $sale = SalesOrder::where('del_time',0)
+            ->where('order_number',$d['sales_order_number'])
+            ->first();
+        if(empty($sale)) return;
+        $id = $sale->id;
+        if($sale->state < SalesOrder::State_seven){
+            $sale->state = SalesOrder::State_seven;
+            $sale->save();
+        }
+
+        $time = time();
+        $insert = [];
+        if(! empty($img)){
+            foreach ($img as $value){
+                $insert[] = [
+                    'sales_order_id' => $id,
+                    'type' => SalesOrderInfo::type_seven,
+                    'file' => $value,
+                    'crt_time' => $time,
+                ];
+            }
+        }
+        if(! empty($insert)) SalesOrderInfo::insert($insert);
+    }
+
     public function deliveryNoteEditRule(&$data, $user){
         if(empty($data['construction_order_number'])) return [false, '施工单编号不能为空'];
         $construction = Construction::where('del_time',0)
@@ -1177,7 +1214,9 @@ class ConstructionService extends Service
         list($status, $msg) = (new SalesOrderService())->detail(['order_number' => $data['sales_order_number']], $user);
         if(! $status) return [false, $msg];
 
-        if($is_submit && $msg['payment_receipt_list']['not_receipt_amount'] > 0) return [false, '销售订单:' . $data['sales_order_number'] . '还有未回款金额, 交车失败'];
+        if($is_submit && $msg['payment_receipt_list']['not_receipt_amount'] > 0 && $msg['model_type'] == SalesOrder::Model_type_one) {
+            return [false, '销售订单:' . $data['sales_order_number'] . '还有未回款金额, 交车失败'];
+        }
 
         $new = $old = [];
         if(isset($data['id'])) {

+ 72 - 8
app/Service/CustomerService.php

@@ -66,6 +66,7 @@ class CustomerService extends Service
 //            $model->company_short_name = $data['company_short_name'] ?? '';
             $model->state_type = $data['state_type'] ?? 0;
             $model->customer_state = $data['customer_state'] ?? 0;
+            $model->enter_time = $data['enter_time'] ?? 0;
             $model->save();
             $time = time();
 
@@ -232,6 +233,7 @@ class CustomerService extends Service
             $model->top_depart_id = $data['top_depart_id'] ?? 0;
             $model->state_type = $data['state_type'] ?? 0;
             $model->customer_state = $data['customer_state'] ?? 0;
+            $model->enter_time = $data['enter_time'] ?? 0;
             $model->save();
             $time = time();
 
@@ -261,10 +263,10 @@ class CustomerService extends Service
                 }
                 CustomerInfo::insert($insert);
 
-                Customer::where('id',$model->id)->update([
+//                Customer::where('id',$model->id)->update([
 //                    'fp_top_depart_id' => $data['fp_top_depart_id'] ?? 0,
-                    'fp_time' => $time,
-                ]);
+//                    'fp_time' => $time,
+//                ]);
             }
 
             if(! empty($data['employee_two'])){
@@ -601,6 +603,27 @@ class CustomerService extends Service
             $model->whereIn('id',array_column($customer_info,'customer_id'));
         }
         if(! empty($data['no_fp_time'])) $model->where('fp_time', 0);
+        $is_fp = -1;
+        if(isset($data['is_fp'])) {
+            if($data['is_fp']){
+                $model->where('fp_time', '>', 0);
+                $is_fp = 1;
+            } else{
+                $model->where('fp_time', 0);
+                $is_fp = 0;
+            }
+        }
+        if(! empty($data['belong_top_depart_title'])){
+            list($id, $depart_id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
+            if($is_fp < 0){
+                $model->whereIn('id', $id)
+                    ->orWhereIn('top_depart_id', $depart_id);
+            }elseif ($is_fp > 0){
+                $model->whereIn('id', $id);
+            }else{
+                $model->where('top_depart_id', $depart_id);
+            }
+        }
 
         if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
             $model->where('crt_time','>=',$data['wx_crt_time'][0]);
@@ -691,7 +714,7 @@ class CustomerService extends Service
             $model->where('fp_time','<=',$return[1]);
         }
         if(! empty($data['belong_top_depart_title'])){
-            $id = $this->searchCustomerDepart($data['belong_top_depart_title']);
+            list($id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
             $model->whereIn('id', $id);
         }
         if(! empty($data['wx_fp_time'][0]) && ! empty($data['wx_fp_time'][1])) {
@@ -748,6 +771,9 @@ class CustomerService extends Service
         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(empty($data['enter_time'])) return [false, '录入系统日期不能为空'];
+        $data['enter_time'] = $this->changeDateToDate($data['enter_time']);
+
         //所属部门 以及  顶级部门
         if(empty($data['depart_id'])) {
             $data['depart_id'] = $this->getDepart($user);
@@ -774,6 +800,7 @@ class CustomerService extends Service
             if($bool) return [false,'车型名称已存在'];
         }
 
+
         if($is_add){
             $bool = Customer::where('del_time',0)
                 ->where('top_depart_id',$data['top_depart_id'])
@@ -927,8 +954,19 @@ class CustomerService extends Service
             $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
             $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
             $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
-            $data['data'][$key]['top_depart_title'] = $depart[$value['top_depart_id']] ?? "";
-            $data['data'][$key]['fp_top_depart_title'] = $depart2_map[$value['id']] ?? "";
+            $top_depart_title = $depart[$value['top_depart_id']] ?? "";
+            $data['data'][$key]['top_depart_title'] = $top_depart_title;
+            //分配门店
+            $fp_top_depart_title = $depart2_map[$value['id']] ?? "";
+            if($value['fp_time'] > 0) {
+                //分配过所属门店是分配门店
+                $data['data'][$key]['fp_top_depart_title'] = $fp_top_depart_title;
+            }else{
+                //没分配过所属门店是创建门店
+                $data['data'][$key]['fp_top_depart_title'] = $top_depart_title;
+            }
+            $data['data'][$key]['is_dispatch_title'] = $value['fp_time'] ? "已分配或移交" : "未分配或移交";
+
             $data['data'][$key]['xt'] = $xt[$value['id']] ?? "";
         }
 
@@ -1021,7 +1059,7 @@ class CustomerService extends Service
             ->where('title', 'LIKE', '%'. $depart_title .'%')
             ->select('id')->get()->toArray();
         $depart_id = array_column($depart_id, 'id');
-        if(empty($depart_id)) return $customer_id;
+        if(empty($depart_id)) return [$customer_id, $depart_id];
 
         $customer_id = SeeRange::where('del_time',0)
             ->whereIn('param_id', $depart_id)
@@ -1031,7 +1069,7 @@ class CustomerService extends Service
             ->get()->toArray();
         $customer_id = array_column($customer_id, 'customer_id');
 
-        return $customer_id;
+        return [$customer_id, $depart_id];
     }
 
     //抢客户
@@ -1130,4 +1168,30 @@ class CustomerService extends Service
 
         return array_column($customer,'id');
     }
+
+    public function customerImportanceEdit($data,$user){
+        if(empty($data['id'])) return [false, '请选择客户'];
+        $model = Customer::where('id',$data['id'])->first();
+        if(empty($model) || $model->del_time > 0) return [false, '客户不存在或已被删除'];
+        if(! isset($data['importance'])) return [false, '客户重要程度不存在'];
+
+        $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $model->title;
+        $params = $this->getDataFile($data);
+        (new OperationLogService())->setOperationList($params,$user,2);
+
+        try {
+            DB::beginTransaction();
+
+            $model = Customer::where('id',$data['id'])->first();
+            $model->importance = $data['importance'] ?? '';
+            $model->save();
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
 }

+ 251 - 5
app/Service/FollowUpRecordService.php

@@ -4,6 +4,7 @@ namespace App\Service;
 
 use App\Model\BasicType;
 use App\Model\Customer;
+use App\Model\CustomerInfo;
 use App\Model\Employee;
 use App\Model\FollowUpRecord;
 use App\Model\FollowUpRecordFile;
@@ -13,8 +14,8 @@ use Illuminate\Support\Facades\DB;
 class FollowUpRecordService extends Service
 {
     public function followUpRecordEdit($data,$user){return [true, ''];
-        list($status,$msg) = $this->followUpRecordRule($data,false);
-        if(!$status) return [$status,$msg];
+//        list($status,$msg) = $this->followUpRecordRule($data,$user,false);
+//        if(!$status) return [$status,$msg];
 
         try {
             DB::beginTransaction();
@@ -60,7 +61,7 @@ class FollowUpRecordService extends Service
     }
 
     public function followUpRecordAdd($data,$user){
-        list($status,$msg) = $this->followUpRecordRule($data);
+        list($status,$msg) = $this->followUpRecordRule($data, $user);
         if(!$status) return [$status,$msg];
 
         try {
@@ -76,6 +77,7 @@ class FollowUpRecordService extends Service
             $model->is_remind = $data['is_remind'] ?? 0;
             $model->crt_id = $user['id'];
             $model->result = $data['result'] ?? '';
+            $model->customer_contact = $data['customer_contact'] ?? '';
             $model->save();
 
             $time = time();
@@ -96,13 +98,51 @@ class FollowUpRecordService extends Service
                 FollowUpRecordFile::insert($insert);
             }
 
+            if ($data['type'] == FollowUpRecord::type_two){
+                if(! empty($data['customer_id'])){ // 存在客户id 就是编辑
+                    Customer::where('id', $data['customer_id'])->update(['title' => $data['customer_title']]);
+                    if(! empty($data['customer_contact_id'])){
+                        CustomerInfo::where('id', $data['customer_contact_id'])
+                            ->update(['contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact']]);
+                    }else{
+                        CustomerInfo::insert([
+                            'customer_id' => $data['customer_id'],
+                            'contact_type' => $data['contact_type_id'],
+                            'contact_info' => $data['customer_contact'],
+                            'type' => CustomerInfo::type_one,
+                            'crt_time' => $time,
+                        ]);
+                    }
+                }else{
+                    $model = new Customer();
+                    $model->title = $data['customer_title'];
+                    $model->model_type = $data['model_type'];
+                    $model->crt_id = $user['id'];
+                    $model->depart_id = $data['depart_id'] ?? 0;
+                    $model->top_depart_id = $data['top_depart_id'] ?? 0;
+                    $model->save();
+
+                    CustomerInfo::insert([
+                        'customer_id' => $model->id,
+                        'contact_type' => $data['contact_type_id'],
+                        'contact_info' => $data['customer_contact'],
+                        'type' => CustomerInfo::type_one,
+                        'crt_time' => $time,
+                    ]);
+                    $data['customer_id'] = $model->id;
+                }
+
+                SalesOrder::where('id', $data['data_id'])
+                    ->update(['customer_id' => $data['customer_id'], 'customer_contact' => $data['customer_contact']]);
+            }
+
             DB::commit();
         }catch (\Exception $exception){
             DB::rollBack();
             return [false,$exception->getMessage()];
         }
 
-        file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8);
+//        file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8);
 
         return [true, ['file' => ['new' => $new]]];
     }
@@ -168,14 +208,50 @@ class FollowUpRecordService extends Service
         return $data;
     }
 
-    public function followUpRecordRule(&$data,$is_add = true){
+    public function followUpRecordRule(&$data,$user, $is_add = true){
         if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空'];
         if(empty($data['type']) || ! isset(FollowUpRecord::$type[$data['type']])) return [false,'跟进类型不能为空或跟进类型不存在'];
         if($data['type'] == FollowUpRecord::type_one){
             $data['data_title'] = Customer::where('id',$data['data_id'])->value('title');
         }elseif ($data['type'] == FollowUpRecord::type_two){
             $data['data_title'] = SalesOrder::where('id',$data['data_id'])->value('order_number');
+
+            //所属部门 以及  顶级部门
+            if(empty($data['depart_id'])) {
+                $data['depart_id'] = $this->getDepart($user);
+                $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
+            }
+            if(empty($data['customer_title'])) return [false, '客户名称不能为空'];
+            if(empty($data['contact_type_id'])) return [false, '客户联系类型不能为空'];
+            if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
+            $customer_id = $data['customer_id'] ?? 0;
+            if(empty($customer_id)){
+                if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
+                if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
+            }
+
+            $bool = Customer::where('del_time',0)
+                ->when(! empty($customer_id), function ($query) use ($customer_id) {
+                    return $query->where('id', '<>',$customer_id);
+                })
+                ->where('top_depart_id',$data['top_depart_id'])
+                ->where('title', $data['customer_title'])
+                ->exists();
+            if($bool) return [false,'客户名称在该门店下已存在'];
+
+            $boolean = CustomerInfo::from('customer_info as a')
+                ->join('customer as b','b.id','a.customer_id')
+                ->when(! empty($customer_id), function ($query) use ($customer_id) {
+                    return $query->where('b.id', '<>',$customer_id);
+                })
+                ->where('a.del_time',0)
+                ->where('b.del_time',0)
+                ->where('b.top_depart_id',$data['top_depart_id'])
+                ->where('a.contact_info', $data['customer_contact'])
+                ->exists();
+            if($boolean) return [false,'客户联系内容已存在'];
         }
+
         if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空'];
         if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空'];
         if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空'];
@@ -241,4 +317,174 @@ class FollowUpRecordService extends Service
 
         return $record_array;
     }
+
+    public function followUpRecordBatchAdd($data,$user){
+        list($status,$msg) = $this->followUpRecordBatchRule($data, $user);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            $time = time();
+            $insert = [];
+
+            foreach ($data['data_title'] as $value){
+                $insert[] = [
+                    'data_id' => $value['id'],
+                    'data_title' => $value['order_number'],
+                    'type' => $data['type'],
+                    'basic_type_id' => $data['basic_type_id'],
+                    'visit_time' => $data['visit_time'],
+                    'content' => $data['content'],
+                    'is_remind' => $data['is_remind'] ?? 0,
+                    'crt_id' => $user['id'],
+                    'crt_time' => $time,
+                    'customer_contact' => $data['customer_contact'],
+                ];
+            }
+            FollowUpRecord::insert($insert);
+
+            //订单合同id
+            $data_id = array_column($data['data_title'],'id');
+            //获取上一次插入订单的所有id
+            $last_insert_id = FollowUpRecord::whereIn('data_id', $data_id)
+                ->where('crt_time', $time)
+                ->where('crt_id', $user['id'])
+                ->select('id')
+                ->get()->toArray();
+
+            $new = [];
+            if(! empty($data['file'])){
+                $insert = [];
+                foreach ($last_insert_id as $key => $f_id){
+                    foreach ($data['file'] as $value){
+                        //生成oss文件数据
+                        $tmp['origin'] = $value['url'] ?? "";
+                        $tmp['img_list'] = [];
+                        $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $value['url']);
+                        $img = explode('.', $img);
+                        $copy = $img[0] . 'C' . $key . '.' . $img[1];
+                        $copy = FileUploadService::string . FileUploadService::string2 . $copy;
+                        $tmp['img_list'][] = $copy;
+                        if(isset($new[$tmp['origin']])){
+                            $new[$tmp['origin']]['img_list'][] = $copy;
+                        }else{
+                            $new[$tmp['origin']] = $tmp;
+                        }
+
+                        //生成数据库记录数据
+                        $insert[] = [
+                            'follow_up_record_id' => $f_id['id'],
+                            'file' => $copy,
+                            'type' => FollowUpRecordFile::type_one,
+                            'name' => $value['name'],
+                            'crt_time' => $time,
+                        ];
+                    }
+                }
+                FollowUpRecordFile::insert($insert);
+            }
+
+            if ($data['type'] == FollowUpRecord::type_two){
+                if(! empty($data['customer_id'])){ // 存在客户id 就是编辑
+                    Customer::where('id', $data['customer_id'])->update(['title' => $data['customer_title']]);
+                    if(! empty($data['customer_contact_id'])){
+                        CustomerInfo::where('id', $data['customer_contact_id'])
+                            ->update(['contact_type' => $data['contact_type_id'], 'contact_info' => $data['customer_contact']]);
+                    }else{
+                        CustomerInfo::insert([
+                            'customer_id' => $data['customer_id'],
+                            'contact_type' => $data['contact_type_id'],
+                            'contact_info' => $data['customer_contact'],
+                            'type' => CustomerInfo::type_one,
+                            'crt_time' => $time,
+                        ]);
+                    }
+                }else{
+                    $model = new Customer();
+                    $model->title = $data['customer_title'];
+                    $model->model_type = $data['model_type'];
+                    $model->crt_id = $user['id'];
+                    $model->depart_id = $data['depart_id'] ?? 0;
+                    $model->top_depart_id = $data['top_depart_id'] ?? 0;
+                    $model->save();
+
+                    CustomerInfo::insert([
+                        'customer_id' => $model->id,
+                        'contact_type' => $data['contact_type_id'],
+                        'contact_info' => $data['customer_contact'],
+                        'type' => CustomerInfo::type_one,
+                        'crt_time' => $time,
+                    ]);
+                    $data['customer_id'] = $model->id;
+                }
+
+                SalesOrder::whereIn('id', $data_id)
+                    ->update(['customer_id' => $data['customer_id'], 'customer_contact' => $data['customer_contact']]);
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false, $exception->getFile() . $exception->getMessage() . $exception->getLine()];
+        }
+
+        file_put_contents('follow_record.txt',date("Y-m-d H:i:s") . "请求参数:" . json_encode($data) . '操作人:' .$user['id'] . '|' .$user['emp_name'] . PHP_EOL,8);
+
+        return [true, ['is_batch' => true, 'file' => ['new' => array_values($new)]]];
+    }
+
+    public function followUpRecordBatchRule(&$data,$user){
+        if($this->isEmpty($data,'data_id')) return [false,'数据id不能为空'];
+        if(empty($data['type']) || $data['type'] != FollowUpRecord::type_two) return [false,'跟进类型不能为空或跟进类型非订单合同'];
+        $data_id = explode(',', $data['data_id']);
+        $data['data_title'] = SalesOrder::whereIn('id', $data_id)
+            ->select('id','customer_id','order_number')
+            ->get()->toArray();
+        $customerIds = array_unique(array_column($data['data_title'], 'customer_id'));
+        $allEqual = count($customerIds) === 1;
+        if(! $allEqual) return [false, '批量新增订单跟进记录,合同的客户必须一致或客户全部为空'];
+
+        //所属部门 以及  顶级部门
+        if(empty($data['depart_id'])) {
+            $data['depart_id'] = $this->getDepart($user);
+            $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
+        }
+        if(empty($data['customer_title'])) return [false, '客户名称不能为空'];
+        if(empty($data['contact_type_id'])) return [false, '客户联系类型不能为空'];
+        if(empty($data['customer_contact'])) return [false, '客户联系方式不能为空'];
+        $customer_id = $data['customer_id'] ?? 0;
+        if(empty($customer_id)){
+            if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
+            if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
+        }
+
+        $bool = Customer::where('del_time',0)
+            ->when(! empty($customer_id), function ($query) use ($customer_id) {
+                return $query->where('id', '<>',$customer_id);
+            })
+            ->where('top_depart_id',$data['top_depart_id'])
+            ->where('title', $data['customer_title'])
+            ->exists();
+        if($bool) return [false,'客户名称在该门店下已存在'];
+
+        $boolean = CustomerInfo::from('customer_info as a')
+            ->join('customer as b','b.id','a.customer_id')
+            ->when(! empty($customer_id), function ($query) use ($customer_id) {
+                return $query->where('b.id', '<>',$customer_id);
+            })
+            ->where('a.del_time',0)
+            ->where('b.del_time',0)
+            ->where('b.top_depart_id',$data['top_depart_id'])
+            ->where('a.contact_info', $data['customer_contact'])
+            ->exists();
+        if($boolean) return [false,'客户联系方式已存在'];
+
+        if($this->isEmpty($data,'basic_type_id')) return [false,'跟进方式不能为空'];
+        if($this->isEmpty($data,'visit_time')) return [false,'拜访时间不能为空'];
+        if($this->isEmpty($data,'content')) return [false,'跟进内容不能为空'];
+        $data['visit_time'] = $this->changeDateToDateMin($data['visit_time']);
+
+        return [true,''];
+    }
 }

+ 14 - 2
app/Service/ImportService.php

@@ -286,6 +286,7 @@ class ImportService extends Service
 
         //第一次表格数据校验 非空  已经过滤数据
         $array_clean = $contact_info = [];
+        $now = strtotime(date("Y-m-d",time()));
         foreach ($array as $key => $value){
             $rowData = array_filter($value);
             if (empty($rowData)) {
@@ -296,10 +297,21 @@ class ImportService extends Service
                 foreach ($value as $k => $v){
                     $value[$k] = trim($v);
                 }
-                if(! isset(Customer::dk[$value[0]]))  return [false, '客户模板填写错误'];
+                if(! isset(Customer::dk[$value[0]])) return [false, '客户模板填写错误'];
                 $value[0] = Customer::dk[$value[0]];
                 if(in_array($value[1],$array_clean)) return [false, '客户名称不能重复'];
                 $array_clean[] = $value[1];
+
+                if(! empty($value[15])){
+                    if(! strtotime($value[15])){
+                        return [false, '录入日期请填写正确的日期'];
+                    }else{
+                        $value[15] = strtotime($value[15]);
+                    }
+                } else{
+                    $value[15] = $now;
+                }
+
                 $array[$key] = $value;
 
                 if(! empty($value[13])){
@@ -383,6 +395,7 @@ class ImportService extends Service
                 'crt_id' => $user['id'],
                 'crt_time' => $time,
                 'upd_time' => $time,
+                'enter_time' => $value['15'],
             ];
             $tmp['model_type'] = $value['0'];
             $tmp['consulting_product'] = $value['5'];
@@ -437,7 +450,6 @@ class ImportService extends Service
 //            }
 
             $insert[] = $tmp;
-
             $insert_detail[] = [
                 'customer_id' => 0,
                 'contact_type' => $contact_id,

+ 38 - 1
app/Service/OperationLogService.php

@@ -11,6 +11,8 @@ use App\Model\FollowUpRecordFile;
 use App\Model\OperationLog;
 use App\Model\OperationLogDetail;
 use App\Model\OrderOperation;
+use App\Model\SalesOrder;
+use App\Model\SalesOrderInfo;
 use App\Model\SysMenu;
 use Illuminate\Support\Facades\DB;
 
@@ -129,13 +131,48 @@ class OperationLogService extends Service
             $log_list3[$key]['type'] = 5;//状态类
             $log_list3[$key]['basic_type_title'] = $basic_type_map[$value['basic_type_id']] ?? "";
             $log_list3[$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s', $value['crt_time']) : "";
-            $log_list3[$key]['visit_time'] = $value['visit_time'] ? date('Y-m-d H:i:s', $value['visit_time']) : "";
+            $log_list3[$key]['visit_time'] = $value['visit_time'] ? date('Y-m-d', $value['visit_time']) : "";
             $log_list3[$key]['user_name'] = $employee_key_list[$value['user_id']] ?? "";
             $tmp = $log_list3_detail_map[$value['id']] ?? [];
             $log_list3[$key]['data'] = $tmp;
         }
         $list_final = array_merge($list_final, $log_list3);
 
+        if(isset($data['sales_order'])){
+            $id = SalesOrder::where('order_number', $data['order_number'])->value('id');
+            $detail = SalesOrderInfo::where('del_time',0)
+                ->where('sales_order_id', $id)
+                ->whereIn('type',[SalesOrderInfo::type_seven,SalesOrderInfo::type_eight])
+                ->get()->toArray();
+            $emp = Employee::whereIn('id', array_unique(array_column($detail,'data_id')))->pluck('emp_name','id')->toArray();
+            $file = [];
+            foreach ($detail as $value){
+                if($value['type'] == SalesOrderInfo::type_seven && $value['file']){
+                    $file_tmp = $fileUploadService->getFileShow($value['file']);
+                    $file[$value['crt_time']]['file'][] = $file_tmp;
+                }
+
+                if($value['type'] == SalesOrderInfo::type_eight){
+                    $file[$value['crt_time']]['user_name'] = $emp[$value['data_id']] ?? "";
+                    $file[$value['crt_time']]['mark'] = $value['file'];
+                }
+            }
+
+            $log_list4 = [];
+            foreach ($file as $key => $value){
+                $log_list4[] = [
+                    'type' => 6,
+                    'title' => '公司完结',
+                    'user_name' => $value['user_name'],
+                    'mark' => $value['mark'] ?? "",
+                    'crt_time' => $key ? date('Y-m-d H:i:s', $key) : "",
+                    'data' => $value['file'] ?? [],
+                ];
+            }
+
+            if(! empty($log_list4)) $list_final = array_merge($list_final, $log_list4);
+        }
+
         //时间排序
         usort($list_final, function($a, $b) {
             return strtotime($b['crt_time']) - strtotime($a['crt_time']);

+ 71 - 0
app/Service/ProductService.php

@@ -597,6 +597,14 @@ class ProductService extends Service
             ->orderby('product_attribute', 'desc')
             ->orderby('id', 'desc');
 
+        if(! empty($data['title_t'])) {
+            // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
+            $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
+            // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
+            $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
+                ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
+                ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
+        }
         if(! empty($data['title'])) {
             // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
             $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
@@ -823,6 +831,10 @@ class ProductService extends Service
             }unset($stock);
         }
 
+        //分社价格
+        $product_title = $product_fs = [];
+        if(! empty($search['fs_price_get'])) list($product_title , $product_fs) = $this->getProductPriceDetail(['id' => $product_id], $user);
+
         foreach ($data['data'] as $key => $value){
             $tmp = [];
             if(isset($detail_map[$value['id']])){
@@ -842,6 +854,7 @@ class ProductService extends Service
                 }
             }
             $data['data'][$key]['product_price'] = $tmp;
+            $data['data'][$key]['product_fs_price'] = array_values($product_fs[$value['id']] ?? []);
             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
             $data['data'][$key]['is_use_title'] = Product::$is_use[$value['is_use']] ?? "";
             $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
@@ -863,6 +876,8 @@ class ProductService extends Service
             $data['data'][$key]['cost_show'] = $price;
         }
 
+        $data['product_title'] = $product_title;
+
         return $data;
     }
 
@@ -1074,4 +1089,60 @@ class ProductService extends Service
 
         return $return;
     }
+
+    //获取产品分社价格
+    public function getProductPriceDetail($data, $user){
+        $data['top_depart_id'] = $user['head']['id'] ?? 0;
+        $model = BasicType::TopClear($user,$data);
+        $basic = $model->where('del_time',0)
+            ->where('type',22)
+            ->select('title','id','type')
+            ->orderby('id', 'asc')->get()->toArray();
+
+        $detail = ProductPriceDetail::where('del_time',0)
+            ->whereIn('product_id',$data['id'])
+            ->select('product_id','basic_type_id','price')
+            ->get()->toArray();
+        $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'id'))))
+            ->pluck('title','id')
+            ->toArray();
+        $top_depart = $user['depart_top'][0] ?? [];
+        $product_price = [];
+
+        //特殊功能按钮
+        $special_button = $user['special_button'] ?? [];
+
+        $return_title = [];
+
+        //所有金额
+        foreach ($basic as $value){
+            $show = 0;
+            if(in_array(RoleMenuButton::special_one,$special_button)){
+                $show = 1;
+            }else{
+                if($top_depart['basic_type_id'] == $value['id']) $show = 1;
+            }
+            $return_title[] = [
+                'basic_type_id' => $value['id'],
+                'basic_type_title' => $title_map[$value['id']] ?? '',
+                'is_show' => $show,
+            ];
+            $product_price[$value['id']] = [
+                'basic_type_id' => $value['id'],
+                'price' => 0,
+            ];
+        }
+
+        $return = [];
+        foreach ($data['id'] as $value){
+            $return[$value] = $product_price;
+        }
+
+        //展示金额
+        foreach ($detail as $value){
+            if(isset($return[$value['product_id']][$value['basic_type_id']])) $return[$value['product_id']][$value['basic_type_id']]['price'] = $value['price'];
+        }
+
+        return [$return_title,$return];
+    }
 }

+ 233 - 0
app/Service/RangeService.php

@@ -10,6 +10,9 @@ use App\Model\CustomerInfo;
 use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\InvoiceOrder;
+use App\Model\OaOrder;
+use App\Model\OaOrderSub;
+use App\Model\OaOrderSubEmployee;
 use App\Model\PaymentReceipt;
 use App\Model\PaymentReceiptInfo;
 use App\Model\Product;
@@ -614,8 +617,19 @@ class RangeService extends Service
     public static function sportsBagCheck($user,$search){
         $args = "";
         if($search['is_check'] == 1) {
+            list($status, $id) = self::getWaitForSportsCheck($user,$search);
+
             //待审核
             $args = "(state IN (" . implode(",", SportsBag::$wait_check) ."))";
+
+            if($status) {
+                $check_2 = implode(',', array_diff(SportsBag::$wait_check, [SportsBag::STATE_ONE]));
+                if(empty($id)) {
+                    $args = "(state IN (" . implode(",", SportsBag::$wait_check) .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }else{
+                    $args = "(state IN (" . implode(",", SportsBag::$wait_check) .") and (id IN (" . implode(",", $id) .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }
+            }
         }elseif($search['is_check'] == 2){
             //已审
             $args = "(state = ". SportsBag::STATE_TWO . ")";
@@ -624,11 +638,37 @@ class RangeService extends Service
         return $args;
     }
 
+    private static function getWaitForSportsCheck($user, $search){
+        if(empty($search['wait_for_me'])) return [false, []];
+
+        //获取待审核
+        $args = "(state = " . SportsBag::STATE_ONE . ")";
+        $data = SportsBag::where('del_time',0)
+            ->whereRaw($args)
+            ->select('order_number','id')
+            ->get()->toArray();
+        if(empty($data)) return [true, []];
+
+        list($status,$msg) = self::getWaitCommon($data,$user);
+        return [$status, $msg];
+    }
+
     public static function paymentReceiptCheck($user,$search){
         $args = "";
         if($search['is_check'] == 1) {
+            list($status, $id) = self::getWaitForPaymentCheck($user,$search);
+
             //待审核
             $args = "(state IN (" . implode(",", PaymentReceipt::$wait_check) ."))";
+
+            if($status) {
+                $check_2 = implode(',', array_diff(SportsBag::$wait_check, [PaymentReceipt::STATE_ONE]));
+                if(empty($id)) {
+                    $args = "(state IN (" . implode(",", PaymentReceipt::$wait_check) .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }else{
+                    $args = "(state IN (" . implode(",", PaymentReceipt::$wait_check) .") and (id IN (" . implode(",", $id) .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }
+            }
         }elseif($search['is_check'] == 2){
             //已审
             $args = "(state = ". PaymentReceipt::STATE_TWO . ")";
@@ -637,11 +677,37 @@ class RangeService extends Service
         return $args;
     }
 
+    private static function getWaitForPaymentCheck($user, $search){
+        if(empty($search['wait_for_me'])) return [false, []];
+
+        //获取待审核
+        $args = "(state = " . PaymentReceipt::STATE_ONE . ")";
+        $data = PaymentReceipt::where('del_time',0)
+            ->whereRaw($args)
+            ->select('order_number','id')
+            ->get()->toArray();
+        if(empty($data)) return [true, []];
+
+        list($status,$msg) = self::getWaitCommon($data,$user);
+        return [$status, $msg];
+    }
+
     public static function salesOrderCheck($user,$search){
         $args = "";
         if($search['is_check'] == 1) {
+            list($status, $id) = self::getWaitForSalesCheck($user,$search);
+
             //待审核
             $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . implode(",", SalesOrder::$wait_check) ."))";
+
+            if($status) {
+                $check_2 = implode(',', array_diff(SalesOrder::$wait_check, [SalesOrder::State_one]));
+                if(empty($id)) {
+                    $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . implode(",", SalesOrder::$wait_check) .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }else{
+                    $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state IN (" . implode(",", SalesOrder::$wait_check) .") and (id IN (" . implode(",", $id) .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }
+            }
         }elseif($search['is_check'] == 2){
             //已审 线上订单的已审核是2  其它是 3
             $args = SalesOrder::search;
@@ -650,11 +716,37 @@ class RangeService extends Service
         return $args;
     }
 
+    private static function getWaitForSalesCheck($user, $search){
+        if(empty($search['wait_for_me'])) return [false, []];
+
+        //获取待审核合同
+        $args = "(sales_order_type = " . SalesOrder::Order_type_one . " and state = " . SalesOrder::State_one . ")";
+        $data = SalesOrder::where('del_time',0)
+            ->whereRaw($args)
+            ->select('order_number','id')
+            ->get()->toArray();
+        if(empty($data)) return [true, []];
+
+        list($status,$msg) = self::getWaitCommon($data,$user);
+        return [$status, $msg];
+    }
+
     public static function invoiceCheck($user,$search){
         $args = "";
         if($search['is_check'] == 1) {
+            list($status, $id) = self::getWaitForInvoiceCheck($user,$search);
+
             //待审核
             $args = "(state IN (" . implode(",", InvoiceOrder::$wait_check) ."))";
+
+            if($status) {
+                $check_2 = implode(',', array_diff(SportsBag::$wait_check, [InvoiceOrder::STATE_ONE]));
+                if(empty($id)) {
+                    $args = "(state IN (" . implode(",", InvoiceOrder::$wait_check) .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }else{
+                    $args = "(state IN (" . implode(",", InvoiceOrder::$wait_check) .") and (id IN (" . implode(",", $id) .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }
+            }
         }elseif($search['is_check'] == 2){
             //已审
             $args = "(state = ". InvoiceOrder::STATE_TWO . ")";
@@ -663,11 +755,37 @@ class RangeService extends Service
         return $args;
     }
 
+    private static function getWaitForInvoiceCheck($user, $search){
+        if(empty($search['wait_for_me'])) return [false, []];
+
+        //获取待审核数据
+        $args = "(state = " . InvoiceOrder::STATE_ONE . ")";
+        $data = InvoiceOrder::where('del_time',0)
+            ->whereRaw($args)
+            ->select('order_number','id')
+            ->get()->toArray();
+        if(empty($data)) return [true, []];
+
+        list($status,$msg) = self::getWaitCommon($data,$user);
+        return [$status, $msg];
+    }
+
     public static function returnExchangeOrderCheck($user,$search){
         $args = "";
         if($search['is_check'] == 1) {
+            list($status, $id) = self::getWaitForReturnExchangeCheck($user,$search);
+
             //待审核
             $args = "(state IN (" . implode(",", ReturnExchangeOrder::$wait_check) ."))";
+
+            if($status) {
+                $check_2 = implode(',', array_diff(SportsBag::$wait_check, [ReturnExchangeOrder::State_one]));
+                if(empty($id)) {
+                    $args = "(state IN (" . implode(",", ReturnExchangeOrder::$wait_check) .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }else{
+                    $args = "(state IN (" . implode(",", ReturnExchangeOrder::$wait_check) .") and (id IN (" . implode(",", $id) .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }
+            }
         }elseif($search['is_check'] == 2){
             //已审
             $args = "(state = ". ReturnExchangeOrder::State_two . ")";
@@ -676,11 +794,37 @@ class RangeService extends Service
         return $args;
     }
 
+    private static function getWaitForReturnExchangeCheck($user, $search){
+        if(empty($search['wait_for_me'])) return [false, []];
+
+        //获取待审核数据
+        $args = "(state = " . ReturnExchangeOrder::State_one . ")";
+        $data = ReturnExchangeOrder::where('del_time',0)
+            ->whereRaw($args)
+            ->select('order_number','id')
+            ->get()->toArray();
+        if(empty($data)) return [true, []];
+
+        list($status,$msg) = self::getWaitCommon($data,$user);
+        return [$status, $msg];
+    }
+
     public static function constructionCheck($user,$search){
         $args = "";
         if($search['is_check'] == 1) {
+            list($status, $id) = self::getWaitForConstructionCheck($user,$search);
+
             //待审核
             $args = "(state IN (" . implode(",", Construction::$wait_check) ."))";
+
+            if($status) {
+                $check_2 = implode(',', array_diff(SportsBag::$wait_check, [Construction::STATE_ONE]));
+                if(empty($id)) {
+                    $args = "(state IN (" . implode(",", Construction::$wait_check) .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }else{
+                    $args = "(state IN (" . implode(",", Construction::$wait_check) .") and (id IN (" . implode(",", $id) .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }
+            }
         }elseif($search['is_check'] == 2){
             //已审
             $args = "(state >= ". Construction::STATE_TWO . ")";
@@ -689,11 +833,37 @@ class RangeService extends Service
         return $args;
     }
 
+    private static function getWaitForConstructionCheck($user, $search){
+        if(empty($search['wait_for_me'])) return [false, []];
+
+        //获取待审核数据
+        $args = "(state = " . Construction::STATE_ONE . ")";
+        $data = Construction::where('del_time',0)
+            ->whereRaw($args)
+            ->select('order_number','id')
+            ->get()->toArray();
+        if(empty($data)) return [true, []];
+
+        list($status,$msg) = self::getWaitCommon($data,$user);
+        return [$status, $msg];
+    }
+
     public static function purchaseCheck($user,$search){
         $args = "";
         if($search['is_check'] == 1) {
+            list($status, $id) = self::getWaitForPurchaseCheck($user,$search);
+
             //待审核
             $args = "(state IN (" . implode(",", PurchaseOrder::$wait_check) ."))";
+
+            if($status) {
+                $check_2 = implode(',', array_diff(SportsBag::$wait_check, [PurchaseOrder::STATE_ONE]));
+                if(empty($id)) {
+                    $args = "(state IN (" . implode(",", PurchaseOrder::$wait_check) .") and (1=0 or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }else{
+                    $args = "(state IN (" . implode(",", PurchaseOrder::$wait_check) .") and (id IN (" . implode(",", $id) .") or (crt_id = " . $user['id'] ." and state IN (" . $check_2 ."))))";
+                }
+            }
         }elseif($search['is_check'] == 2){
             //已审
             $args = "(state >= ". PurchaseOrder::STATE_TWO . ")";
@@ -701,5 +871,68 @@ class RangeService extends Service
 
         return $args;
     }
+
+    private static function getWaitForPurchaseCheck($user, $search){
+        if(empty($search['wait_for_me'])) return [false, []];
+
+        //获取待审核数据
+        $args = "(state = " . PurchaseOrder::STATE_ONE . ")";
+        $data = PurchaseOrder::where('del_time',0)
+            ->whereRaw($args)
+            ->select('order_number','id')
+            ->get()->toArray();
+        if(empty($data)) return [true, []];
+
+        list($status,$msg) = self::getWaitCommon($data,$user);
+        return [$status, $msg];
+    }
+
+    private static function getWaitCommon($data,$user){
+        $data_map = array_column($data,'id','order_number');
+        //查找对应审批流数据
+        $orderNoGroups = OaOrder::whereIn('order_no', array_column($data,'order_number'))
+            ->get()
+            ->groupBy('order_no');
+        $maxIds = $orderNoGroups->map(function ($group) {
+            return $group->max('id');
+        });
+        $map = $maxIds->toArray();
+        if(empty($map)) return [true, []];
+        $oa_order_id = array_values($map);
+        $map2 = array_flip($map);
+        unset($map);
+
+        //获取审批流下的人
+        $list = OaOrderSub::whereIn('oa_order_id', $oa_order_id)
+            ->whereIn('state',[0,1])
+            ->select('id','state','oa_order_id')
+            ->orderBy('id', 'desc')
+            ->get()->toArray();
+        $subEmployeeList = OaOrderSubEmployee::whereIn('oa_order_id', $oa_order_id)
+            ->where('employee_id',$user['id'])
+            ->get()->toArray();
+        if(empty($subEmployeeList)) return [true, []];
+
+        //每条数据对应的人
+        $emp_id_key_list = [];
+        foreach ($subEmployeeList as $v) {
+            $emp_id_key_list[$v['oa_order_sub_id']][] = $v['employee_id'];
+        }
+        unset($subEmployeeList);
+
+        $flag = $id = [];
+        foreach ($list as $v) {
+            //不存在单号或者已存在单号返回数据
+            if(empty($v['oa_order_id']) || isset($flag[$v['oa_order_id']])) continue;
+            $emp_tmp = $emp_id_key_list[$v['id']] ?? [];
+            $flag[$v['oa_order_id']] = $emp_tmp;
+            $order_number = $map2[$v['oa_order_id']] ?? "";
+            $sales_id = $data_map[$order_number] ?? 0;
+            if(in_array($user['id'], $emp_tmp)) $id[] = $sales_id;
+        }
+        unset($flag);
+
+        return [true, $id];
+    }
     //全部 待审 已审核 -----------------------------------------------
 }

+ 444 - 38
app/Service/SalesOrderService.php

@@ -570,7 +570,25 @@ class SalesOrderService extends Service
 
         $customer = Customer::where('id',$sales['customer_id'])->value('title');
         $sales['customer_title'] = $customer ?? '';
+        $customer_contact_id = $contact_type_id = 0;
+        $customer_contact = CustomerInfo::where('del_time',0)
+            ->where('customer_id',$sales['customer_id'])
+            ->where('contact_info', $sales['customer_contact'])
+            ->where('type',CustomerInfo::type_one)
+            ->first();
+        if(! empty($customer_contact)){
+            $customer_contact = $customer_contact->toArray();
+            $customer_contact_id = $customer_contact['id'];
+            $contact_type_id = $customer_contact['contact_type'];
+        }
+        $sales['customer_contact_id'] = $customer_contact_id;
+        $sales['contact_type_id'] = $contact_type_id;
+        $sales['contact_type_title'] = BasicType::where('id',$contact_type_id)->value('title');
+
         $sales['sales_order_type_title'] = SalesOrder::$order_type[$sales['sales_order_type']] ?? '';
+
+        $sales['type_title'] = SalesOrder::$build_state[$sales['type']] ?? '';
+        $sales['is_dispatch'] = $sales['dispatch_time_second'] ? 1 : 0;
         $sales['activity_product'] = $sales['file'] = $sales['employee_one'] = $sales['employee_two'] = $sales['employee_three'] = $sales['product'] = $sales['dispatch_depart_one'] = $sales['dispatch_employee'] = $sales['dispatch_depart_two'] = [];
         $array = [
             $sales['plat_type'],
@@ -746,10 +764,32 @@ class SalesOrderService extends Service
     public function salesOrderCommon($data,$user){
         $model = SalesOrder::Clear($user,$data);
         $model = $model->where('del_time',0)
-            ->select('id','sales_order_type','model_type','order_number','selling_price','vin_no','car_type','order_type','customer_id','sign_time','crt_id','crt_time','mark','product_total','rate','construction_time','handover_time','other_fee','discount_fee','contract_fee','pay_way','car_type','year','mileage','color','original_set','processing','state','invoice_state','plat_type','plat_order','install_method','install_position','customer_contact','is_confirm','dispatch_time_second','contact_order_no')
-            ->orderby('id', 'desc')
-            ->orderby('dispatch_time_second','desc');
+            ->select('id','sales_order_type','model_type','order_number','selling_price','vin_no','car_type','order_type','customer_id','sign_time','crt_id','crt_time','mark','product_total','rate','construction_time','handover_time','other_fee','discount_fee','contract_fee','pay_way','car_type','year','mileage','color','original_set','processing','state','invoice_state','plat_type','plat_order','install_method','install_position','customer_contact','is_confirm','dispatch_time_second','contact_order_no','type');
 
+        if(! empty($data['order_list'])){
+            if($data['order_list'] == 1){
+                $model = $model->orderby('id','desc');
+                //订单合同 安装件合同 状态未指派总社分社的 或者 快递件合同
+//                $model->where('dispatch_time_second', 0);
+//                $model->where('state', '<=', SalesOrder::State_three);
+            }elseif($data['order_list'] == 2){
+                $model = $model->orderby('dispatch_time_second','desc');
+                //派单合同 安装件合同 状态已指派总社分社的
+                $model = $model->where('dispatch_time_second', '>', 0);
+//                $model->where('state', '>=', SalesOrder::State_four);
+            }
+        }else{
+            $model = $model->orderby('id', 'desc');
+        }
+        if(isset($data['is_dispatch'])) {
+            if($data['is_dispatch'] == 0){
+                //订单合同 安装件合同 状态未指派总社分社的 或者 快递件合同
+                $model->where('dispatch_time_second', 0);
+            }else{
+                //派单合同 安装件合同 状态已指派总社分社的
+                $model->where('dispatch_time_second', '>', 0);
+            }
+        }
         if(! empty($data['order_number'])) $model->where('order_number','LIKE', '%'.$data['order_number'].'%');
         if(! empty($data['sales_order_type'])) $model->where('sales_order_type',$data['sales_order_type']);
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
@@ -779,17 +819,6 @@ class SalesOrderService extends Service
 //            $model->where('state','<',SalesOrder::State_seven);
         }
         if(! empty($data['sales_order_id'])) $model->where('id',$data['sales_order_id']);
-        if(! empty($data['order_list'])){
-            if($data['order_list'] == 1){
-                //订单合同 安装件合同 状态未指派总社分社的 或者 快递件合同
-                $model->where('dispatch_time_second', 0);
-//                $model->where('state', '<=', SalesOrder::State_three);
-            }elseif($data['order_list'] == 2){
-                //派单合同 安装件合同 状态已指派总社分社的
-                $model->where('dispatch_time_second', '>', 0);
-//                $model->where('state', '>=', SalesOrder::State_four);
-            }
-        }
         if(isset($data['state'])) {
             if($data['state'] == SalesOrder::special_status){
                 $model->whereRaw(SalesOrder::search1);
@@ -1126,7 +1155,13 @@ class SalesOrderService extends Service
         //跟进记录
         $record_array = (new FollowUpRecordService())->getVisitDataOfTime($data_id, FollowUpRecord::type_two);
 
+        //获取客户的信息
+        $customer_id = array_column($data['data'],'customer_id');
+        $customer_map = $this->getCustomer($customer_id);
+
         foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['construction_model_type'] = SalesOrder::$build_for_con[$value['type']] ?? 1;
+            $data['data'][$key]['type_title'] = SalesOrder::$build_state[$value['type']] ?? '';
             $data['data'][$key]['plat_type_title'] = $basic_map[$value['plat_type']] ?? '';
             $data['data'][$key]['sales_order_type_title'] = SalesOrder::$order_type[$value['sales_order_type']] ?? '';
             $data['data'][$key]['model_type_title'] = SalesOrder::$model_type_title[$value['model_type']] ?? '';
@@ -1136,10 +1171,16 @@ class SalesOrderService extends Service
             $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
             $data['data'][$key]['pay_way_title'] = $basic_map[$value['pay_way']] ?? '';
             $data['data'][$key]['customer_title'] = $customer[$value['customer_id']] ?? '';
+            $c_tmp = $customer_map[$value['customer_id'] . $value['customer_contact']] ?? [];
+            $data['data'][$key]['customer_contact_id'] = $c_tmp['customer_contact_id'] ?? '';
+            $data['data'][$key]['contact_type_id'] = $c_tmp['contact_type_id'] ?? '';
+            $data['data'][$key]['contact_type_title'] = $c_tmp['contact_type_title'] ?? '';
             $data['data'][$key]['sign_time'] = $value['sign_time'] ? date('Y-m-d',$value['sign_time']) : '';
             $data['data'][$key]['construction_time'] = $value['construction_time'] ? date('Y-m-d H:i:s',$value['construction_time']) : '';
             $data['data'][$key]['handover_time'] = $value['handover_time'] ? date('Y-m-d H:i:s',$value['handover_time']) : '';
             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['is_dispatch'] = $value['dispatch_time_second'] ? 1 : 0;
+            $data['data'][$key]['is_dispatch_title'] = $value['dispatch_time_second'] ? "已派单" : "未派单";
             $data['data'][$key]['dispatch_time_second_time'] = $value['dispatch_time_second'] ? date('Y-m-d H:i:s',$value['dispatch_time_second']) : '';
             $crt_name = $emp[$value['crt_id']] ?? '';
             $data['data'][$key]['crt_name'] = $crt_name;
@@ -1202,6 +1243,33 @@ class SalesOrderService extends Service
         return $data;
     }
 
+    public function getCustomer($customer_id){
+        $customer_contact_map = [];
+        $contact_type_id = [];
+        $customer_contact = CustomerInfo::where('del_time',0)
+            ->whereIn('customer_id', $customer_id)
+            ->where('type',CustomerInfo::type_one)
+            ->select('customer_id','contact_info','contact_type','id')
+            ->get()->toArray();
+        if(! empty($customer_contact)){
+            foreach ($customer_contact as $value){
+                if(! empty($value['customer_id']) && ! empty($value['contact_info'])){
+                    $customer_contact_map[$value['customer_id'] . $value['contact_info']] = [
+                        'customer_contact_id' => $value['id'],
+                        'contact_type_id' => $value['contact_type'],
+                    ];
+                    $contact_type_id[] = $value['contact_type'];
+                }
+            }
+        }
+        $contact_type_title = BasicType::whereIn('id', $contact_type_id)->pluck('title','id')->toArray();
+        foreach ($customer_contact_map as $key => $value){
+            $customer_contact_map[$key]['contact_type_title'] = $contact_type_title[$value['contact_type_id']] ?? "";
+        }
+
+        return $customer_contact_map;
+    }
+
     public function countData($column = "", $data, $user){
         if(empty($column) || empty($data['from_wx'])) return [0, 0];
 
@@ -1361,7 +1429,7 @@ class SalesOrderService extends Service
      * @return array
      */
     public function salesOrderDispatchCompany($data,$user){
-        list($status,$msg) = $this->salesOrderDispatchCompanyRule($data);
+        list($status,$msg) = $this->salesOrderDispatchCompanyRule($data,$user);
         if(! $status) return [false,$msg];
 
         try {
@@ -1371,7 +1439,7 @@ class SalesOrderService extends Service
                 'state' => SalesOrder::State_four,
                 'dispatch_time_second' => $time,
                 'dispatch_time_second_id' => $user['id'],
-                'type' => $data['type'] ?? 0
+                'type' => $data['type']
             ]);
 
             SeeRange::where('del_time',0)
@@ -1404,6 +1472,23 @@ class SalesOrderService extends Service
                 $model->save();
             }
 
+            if(isset($data['notify_id'])){
+                SalesOrder::where('del_time',0)->where('id',$msg['id'])->update([
+                    'dispatch_time_first' => $time, // 第一次派单时间也记录
+                ]);
+
+                if(! empty($data['notify_id'])){ //门店负责人不为空
+                    $insert[] = [
+                        'data_id' => $msg['id'],
+                        'data_type' => SeeRange::type_seven,
+                        'param_id' => $data['notify_id'],
+                        'type' => SeeRange::data_two,
+                        'crt_time' => $time,
+                    ];
+                    SeeRange::insert($insert);
+                }
+            }
+
             (new OrderOperationService())->add([
                 'order_number' => $msg['order_number'],
                 'msg' => OrderOperation::$type[OrderOperation::two],
@@ -1452,16 +1537,30 @@ class SalesOrderService extends Service
      * @param $data
      * @return array
      */
-    public function salesOrderDispatchCompanyRule(&$data){
+    public function salesOrderDispatchCompanyRule(&$data,$user){
         if(empty($data['id'])) return [false,'请选择合同派单'];
+        if(empty($data['dispatch_depart_two'])) return [false, '请指派总社或分社'];
+        $depart_id = $data['dispatch_depart_two'][0];
+
+        if(! isset($data['other_fee_1'])) return [false,'分社安装金额不存在'];
+        $res = $this->checkNumber($data['other_fee_1']);
+        if(! isset($data['type']) || ! isset(SalesOrder::$build_state[$data['type']])) return [false, '合同安装类型不存在'];
+        if($data['type'] <= 0) $data['type'] = SalesOrder::build_zero;
+        if(! $res) return [false,'价格请输入不超过两位小数并且大于等于0的数值'];
+
         $sale = SalesOrder::where('del_time',0)
             ->where('id',$data['id'])
             ->first();
         if(empty($sale)) return [false,'合同不存在或已被删除'];
         $sale = $sale->toArray();
         if($sale['sales_order_type'] != SalesOrder::Order_type_one) return [false,'非安装件合同,操作失败'];
-        if($sale['state'] < SalesOrder::State_three) return [false,'请确认合同状态,操作失败'];
         if($sale['state'] > SalesOrder::State_four) return [false,'请确认合同状态,操作失败'];
+        if($sale['state'] < SalesOrder::State_two) return [false, '合同未审核通过,操作失败'];
+        if($sale['state'] < SalesOrder::State_three) {
+            //如果没有派销售过 找到门店负责人
+            $data['notify_id'] = Depart::where('id', $depart_id)->value('notify_id');
+        }
+
         $product = SalesOrderProductInfo::where('del_time',0)
             ->where('sales_order_id',$data['id'])
             ->select('product_id','number')
@@ -1471,18 +1570,16 @@ class SalesOrderService extends Service
         $bool = Construction::where('del_time',0)
             ->where('sales_order_id',$data['id'])
             ->exists();
-        if($bool) return [false,'合同已下施工,操作失败'];
+        if($bool) return [false,'合同已生成施工单,操作失败'];
 
-        if(empty($data['dispatch_depart_two'])) return [false, '请指派总社或分社'];
         $product_id = array_unique(array_column($product,'product_id'));
         $return = RangeService::productNotSeeRange($product_id);
 
         //产品信息
         $product_array = Product::whereIn('id',$product_id)->select('title','id','build_fee')->get()->toArray();
-        $map = $map2 = [];
+        $map = [];
         foreach ($product_array as $value){
             $map[$value['id']] = $value['title'];
-            $map2[$value['id']] = $value['build_fee'];
         }
         if(! empty($return)){
             foreach ($data['dispatch_depart_two'] as $value){
@@ -1493,27 +1590,12 @@ class SalesOrderService extends Service
             }
         }
 
-        //到店安装 原逻辑
-        if(empty($data['type'])){
-            if(empty($data['other_fee_1'])) return [false,'价格不能为空'];
-            $res = $this->checkNumber($data['other_fee_1']);
-            if(! $res) return [false,'到店安装价格请输入不超过两位小数并且大于0的数值'];
-        }else{
-            $other_fee_1 = 0;
-            foreach ($product as $value){
-                $build_fee = $map2[$value['product_id']] ?? 0;
-                $fee = bcmul($value['number'],$build_fee,2);
-                $other_fee_1 = bcadd($other_fee_1,$fee,2);
-            }
-            $data['other_fee_1'] = $other_fee_1;
-        }
-
         return [true, $sale];
     }
 
     public function salesOrderListsalesOrderWxList($data,$user){
         $model = SalesOrder::where('del_time',0)
-            ->select('id','sales_order_type','model_type','order_number','selling_price','vin_no','car_type','order_type','customer_id','sign_time','crt_id','crt_time','mark','product_total','rate','construction_time','handover_time','other_fee','discount_fee','contract_fee','pay_way','car_type','year','mileage','color','original_set','processing','state','invoice_state','plat_type','plat_order','install_method','install_position','customer_contact','is_confirm','dispatch_time_second','contact_order_no')
+            ->select('id','sales_order_type','model_type','order_number','selling_price','vin_no','car_type','order_type','customer_id','sign_time','crt_id','crt_time','mark','product_total','rate','construction_time','handover_time','other_fee','discount_fee','contract_fee','pay_way','car_type','year','mileage','color','original_set','processing','state','invoice_state','plat_type','plat_order','install_method','install_position','customer_contact','is_confirm','dispatch_time_second','contact_order_no','type')
             ->orderby('id', 'desc')
             ->orderby('dispatch_time_second','desc');
 
@@ -1687,4 +1769,328 @@ class SalesOrderService extends Service
 
         return [true, ''];
     }
+
+    /**
+     * 批量推送公司
+     * @param $data
+     * @return array
+     */
+    public function salesOrderBatchDispatchCompany($data,$user){
+        list($status,$msg) = $this->salesOrderBatchDispatchCompanyRule($data,$user);
+        if(! $status) return [false,$msg];
+
+        try {
+            $time = time();
+            DB::beginTransaction();
+
+            $sales = $msg;
+            $sales_id = array_column($sales, 'id');
+
+            SeeRange::where('del_time',0)
+                ->whereIn('data_id', $sales_id)
+                ->where('data_type',SeeRange::type_seven)
+                ->where('type',SeeRange::data_three)
+                ->update(['del_time' => $time]);
+            SalesOrderOtherFee::where('del_time',0)
+                ->whereIn('sales_order_id',$sales_id)
+                ->update(['del_time' => $time]);
+            if(! empty($data['dispatch_depart_two'])){
+                $insert = [];
+                foreach ($data['dispatch_depart_two'] as $value){
+                    foreach ($sales_id as $v){
+                        $insert[] = [
+                            'data_id' => $v,
+                            'data_type' => SeeRange::type_seven,
+                            'param_id' => $value,
+                            'type' => SeeRange::data_three,
+                            'crt_time' => $time,
+                        ];
+                    }
+                }
+                SeeRange::insert($insert);
+            }
+
+            $other_fee = $notify_id = [];
+            foreach ($sales as $value){
+                SalesOrder::where('del_time',0)->where('id', $value['id'])->update([
+                    'state' => SalesOrder::State_four,
+                    'dispatch_time_second' => $time,
+                    'dispatch_time_second_id' => $user['id'],
+                    'type' => $value['type']
+                ]);
+
+                $other_fee[] = [
+                    'sales_order_id' => $value['id'],
+                    'other_fee_1' => $value['other_fee_1'],
+                    'type' => $value['type'],
+                ];
+
+                if(! empty($value['notify_id'])){
+                    //门店负责人不为空
+                    $notify_id[] = [
+                        'data_id' => $value['id'],
+                        'data_type' => SeeRange::type_seven,
+                        'param_id' => $value['notify_id'],
+                        'type' => SeeRange::data_two,
+                        'crt_time' => $time,
+                    ];
+                }
+
+                if(isset($value['notify_id']) && empty($value['dispatch_time_first'])){
+                    SalesOrder::where('del_time',0)->where('id',$value['id'])->update([
+                        'dispatch_time_first' => $time, // 第一次派单时间记录
+                    ]);
+                }
+            }
+            if(! empty($other_fee)) SalesOrderOtherFee::insert($other_fee);
+            if(! empty($notify_id)) SeeRange::insert($notify_id);
+
+            (new OrderOperationService())->batchAdd([
+                'order_number' => array_column($sales, 'order_number'),
+                'msg' => OrderOperation::$type[OrderOperation::thirty_one],
+                'type' => OrderOperation::thirty_one
+            ],$user);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        $depart_id = $data['dispatch_depart_two'][0] ?? 0;
+        $notify_id = Depart::where('id',$depart_id)->value('notify_id');
+        $crt_id = array_column($sales,'crt_id');
+        if($notify_id) $crt_id[] = $notify_id;
+        $emp_name = Employee::whereIn('id',$crt_id)->pluck('emp_name','id')->toArray();
+        foreach ($sales as $value){
+            if(! empty($depart_id) && $depart_id != $value['top_depart_id']){
+                //发送消息
+                if(! empty($notify_id)){
+                    $send_data[] = [
+                        'employee_id' => $notify_id,
+                        'type' => 1,
+                        'state' => 1,
+                        'menu_id' => 37,
+                        'order_number' => $value['order_number'],
+                        'tmp_data' => [
+                            $value['order_number'],
+                            $emp_name[$value['crt_id']] ?? "",
+                            $emp_name[$notify_id] ?? "" . "(派单通知)",
+                            date('Y-m-d H:i:s'),
+                        ],
+                    ];
+                    (new OaService())->sendWxOaCheckMessage($send_data);
+                }
+            }else{
+                //直接确认
+                SalesOrder::where('del_time',0)->where('id',$value['id'])->update([
+                    'is_confirm' => 1,
+                ]);
+            }
+        }
+
+        return [true,''];
+    }
+
+    /**
+     * 批量推送公司规则
+     * @param $data
+     * @return array
+     */
+    public function salesOrderBatchDispatchCompanyRule(&$data,$user){
+        //是总社还是分社
+        if(empty($data['dispatch_depart_two'])) return [false, '请指派总社或分社'];
+        $depart_id = $data['dispatch_depart_two'][0];
+
+        //前端传参
+        if(empty($data['data'])) return [false,'请选择合同派单'];
+
+        $other_fee_1_map = [];
+        foreach ($data['data'] as $value){
+            if(empty($value['id'])) return [false, '合同ID不能为空'];
+            if(! isset($value['type']) || ! isset(SalesOrder::$build_state[$value['type']])) return [false, '合同安装类型不存在'];
+            $type = $value['type'];
+            if($type <= 0) $type = SalesOrder::build_zero;
+            if(! isset($value['other_fee_1'])) return [false, '合同安装金额不存在'];
+            $res = $this->checkNumber($value['other_fee_1']);
+            if(! $res) return [false,'金额请输入不超过两位小数并且大于等于0的数值'];
+            $other_fee_1_map[$value['id']] = [
+                'other_fee_1' => $value['other_fee_1'],
+                'type' => $type,
+            ];
+        }
+
+        //合同信息
+        $sale_id = array_column($data['data'], 'id');
+        $sale = SalesOrder::where('del_time',0)
+            ->whereIn('id', $sale_id)
+            ->get()->toArray();
+        if(empty($sale)) return [false,'合同不存在或已被删除'];
+        $notify_id = Depart::where('id', $depart_id)->value('notify_id');
+
+        //组织合同数据
+        foreach ($sale as $key => $value){
+            if($value['del_time'] > 0) return [false,'合同:' . $value['order_number'] . '不存在或已被删除'];
+            if($value['sales_order_type'] != SalesOrder::Order_type_one) return [false,'非安装件合同,操作失败'];
+            if($value['state'] > SalesOrder::State_four) return [false,'请确认合同状态,操作失败'];
+            if($value['state'] < SalesOrder::State_three) {
+                //如果没有派销售过 找到门店负责人
+                $sale[$key]['notify_id'] = $notify_id;
+            }
+
+            //合同派单信息
+            $other_fee_1 = $other_fee_1_map[$value['id']] ?? [];
+            $sale[$key]['other_fee_1'] = $other_fee_1['other_fee_1'];
+            $sale[$key]['type'] = $other_fee_1['type'];
+        }
+
+        //校验产品
+        $p_id_map = array_column($sale, 'order_number', 'id');
+        $product = SalesOrderProductInfo::where('del_time',0)
+            ->whereIn('sales_order_id', $sale_id)
+            ->select('product_id','sales_order_id')
+            ->get()->toArray();
+        if(empty($product)) return [false,'合同产品不能为空'];
+        $p_map = array_column($product, null, 'sales_order_id');
+        foreach ($data['data'] as $value){
+            if(empty($p_map[$value['id']])){
+                $order_number = $p_id_map[$value['id']] ?? "";
+                return [false, '合同' . $order_number . '产品不能为空'];
+            }
+        }
+
+        //校验施工
+        $bool = Construction::where('del_time',0)
+            ->whereIn('sales_order_id', $sale_id)
+            ->exists();
+        if($bool) return [false,'合同已生成施工单,操作失败'];
+
+        //产品不可见
+        $product_id = array_unique(array_column($product,'product_id'));
+        $return = RangeService::productNotSeeRange($product_id);
+
+        //产品信息
+        $product_array = Product::whereIn('id',$product_id)->select('title','id','build_fee')->get()->toArray();
+        $map = [];
+        foreach ($product_array as $value){
+            $map[$value['id']] = $value['title'];
+        }
+        if(! empty($return)){
+            foreach ($data['dispatch_depart_two'] as $value){
+                foreach ($return as $key => $values){
+                    $tmp = $map[$key] ?? "";
+                    if(in_array($value, $values)) return [false,'产品'. $tmp .'对于该总社或分社不可见'];
+                }
+            }
+        }
+
+        return [true, $sale];
+    }
+
+    /**
+     * 获取安装价格
+     * @param $data
+     * @param $user
+     * @return array
+     */
+    public function salesOrderGetBuildFee($data,$user){
+        if(empty($data['id'])) return [false,'请选择合同派单'];
+        if(empty($data['top_depart_id'])) return [false,'请选择派单分社'];
+        $depart = Depart::where('del_time',0)->where('id', $data['top_depart_id'])->first();
+        if(empty($depart)) return [false, '分社不存在或已被删除'];
+        $depart = $depart->toArray();
+
+        $sale = SalesOrder::where('del_time',0)
+            ->whereIn('id', $data['id'])
+            ->get()->toArray();
+        if(empty($sale)) return [false,'合同不存在或已被删除'];
+        $product = SalesOrderProductInfo::where('del_time',0)
+            ->whereIn('sales_order_id',$data['id'])
+            ->select('product_id','number','sales_order_id')
+            ->get()->toArray();
+        if(empty($product)) return [false,'合同产品不能为空'];
+        $product_array = Product::whereIn('id',array_unique(array_column($product, 'product_id')))
+            ->select('title','id','build_fee')
+            ->get()->toArray();
+        $map2 = [];
+        foreach ($product_array as $value){
+            $map2[$value['id']] = $value['build_fee'];
+        }
+        // 实时 带货安装包安装
+        $count = [];
+        $other_fee_1 = 0;
+        foreach ($product as $value){
+            $build_fee = $map2[$value['product_id']] ?? 0;
+            $fee = bcmul($value['number'],$build_fee,2);
+            $other_fee_1 = bcadd($other_fee_1,$fee,2);
+
+            if(isset($count[$value['sales_order_id']])){
+                $tmp = bcadd($count[$value['sales_order_id']], $other_fee_1,2);
+                $count[$value['sales_order_id']] = $tmp;
+            }else{
+                $count[$value['sales_order_id']] = $other_fee_1;
+            }
+        }
+
+        $product_map = array_column($product,null,'sales_order_id');
+
+        $return_map = [];
+        foreach ($sale as $value){
+            if(! isset($product_map[$value['id']])) return [false, "合同:" . $value['order_number'] . '下没有产品信息'];
+            foreach (SalesOrder::$build_state as $key => $value_v){
+                if($key == SalesOrder::build_minus) continue;
+                if($key == SalesOrder::build_zero){
+                    $rate = bcdiv($depart['rate'], 100,2);
+                    $other_fee_1 = bcmul($value['contract_fee'], $rate,2);
+                }elseif($key == SalesOrder::build_one){
+                    $other_fee_1 = $count[$value['id']] ?? "0.00";
+                }else{
+                    $other_fee_1 = "0.00";
+                }
+                $return_map[$value['id']][$key] = [
+                    'id' => $value['id'],
+                    'order_number' => $value['order_number'],
+                    'type' => $key,
+                    'other_fee_1' => $other_fee_1,
+                    'is_choose' => 0,
+                ];
+            }
+        }
+
+        // 已保存的
+        $save = SalesOrderOtherFee::where('del_time',0)
+            ->whereIn('sales_order_id', $data['id'])
+            ->pluck('other_fee_1','sales_order_id')
+            ->toArray();
+
+        foreach ($sale as $value){// 填充已保存的
+            if(isset($save[$value['id']][$value['type']])){
+                $other_fee_1 = $save[$value['id']];
+                $return_map[$value['id']][$value['type']]['other_fee_1'] = $other_fee_1;
+                $return_map[$value['id']][$value['type']]['is_choose'] = 1;
+            }
+        }
+
+        $return = [];
+        foreach ($return_map as $value){
+            foreach ($value as $v){
+                $tmp = [
+                    'type' => $v['type'],
+                    'other_fee_1' => $v['other_fee_1'],
+                    'is_choose' => $v['is_choose'],
+                ];
+                if(isset($return[$v['id']])){
+                    $return[$v['id']]['data'][] = $tmp;
+                }else{
+                    $return[$v['id']] = [
+                        'id' => $v['id'],
+                        'order_number' => $v['order_number'],
+                        'data' => [$tmp]
+                    ];
+                }
+            }
+        }
+
+        return [true, array_values($return)];
+    }
 }

+ 4 - 0
config/excel/customerTable.php

@@ -60,6 +60,10 @@ return [
         'key' =>'man_fz_number',
         'value' =>'负责人工号',
     ],
+    [
+        'key' =>'enter_time',
+        'value' => '录入时间',
+    ],
 //    [
 //        'key' =>'man_xt',
 //        'value' =>'协同人',

+ 4 - 4
config/header/34.php

@@ -10,10 +10,10 @@ return [
         'key' => 'order_number',
         'value' => '工单编号',
     ],
-    [
-        'key' => 'title',
-        'value' => '工单来源',
-    ],
+//    [
+//        'key' => 'title',
+//        'value' => '工单来源',
+//    ],
     [
         'key' => 'sales_order_number',
         'value' => '关联合同',

+ 4 - 0
config/oa.php

@@ -492,6 +492,10 @@ return [
                 'title' => '状态类型',
             ],
             [
+                'key' => 'importance',
+                'title' => '重要程度',
+            ],
+            [
                 'key' => 'crt_time',
                 'title' => '创建时间',
             ],

+ 5 - 1
routes/api.php

@@ -105,7 +105,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('storehouseDel', 'Api\StorehouseController@del');
 
     //单据状态变动
-    $route->any('checkAll', 'Api\CheckController@checkAll');
+    $route->any('checkAll', 'Api\CheckController@checkAll')->middleware('OssFileDeal');
 
     //基础类型
     $route->any('basicTypeList', 'Api\BasicTypeController@basicTypeList');
@@ -120,6 +120,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('followUpRecordAdd', 'Api\FollowUpRecordController@followUpRecordAdd')->middleware('OssFileDeal');
     $route->any('followUpRecordDetail', 'Api\FollowUpRecordController@followUpRecordDetail');
     $route->any('followUpRecordDel', 'Api\FollowUpRecordController@followUpRecordDel')->middleware('OssFileDeal');
+    $route->any('followUpRecordBatchAdd', 'Api\FollowUpRecordController@followUpRecordBatchAdd')->middleware('OssFileDeal');
 
     //客户
     $route->any('customerList', 'Api\CustomerController@customerList');
@@ -129,6 +130,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('customerDel', 'Api\CustomerController@customerDel')->middleware('OssFileDeal');
     $route->any('customerDetail', 'Api\CustomerController@customerDetail');
     $route->any('customerGrabbing', 'Api\CustomerController@customerGrabbing');
+    $route->any('customerImportanceEdit', 'Api\CustomerController@customerImportanceEdit');
 
     //供应商
     $route->any('supplierList', 'Api\SupplierController@customerList');
@@ -202,6 +204,8 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('salesOrderDispatchSale', 'Api\SalesOrderController@salesOrderDispatchSale');
     //派单总部或分社
     $route->any('salesOrderDispatchCompany', 'Api\SalesOrderController@salesOrderDispatchCompany');
+    $route->any('salesOrderBatchDispatchCompany', 'Api\SalesOrderController@salesOrderBatchDispatchCompany');
+    $route->any('salesOrderGetBuildFee', 'Api\SalesOrderController@salesOrderGetBuildFee');
 
     //施工单
     $route->any('constructionGet', 'Api\ConstructionController@constructionGet');

+ 2 - 1
routes/wx.php

@@ -28,7 +28,7 @@ Route::group(['middleware'=> ['checkWx']],function ($route){
     //文件上传统一方法
     $route->any('uploadFile', 'Api\FileUploadController@uploadFile');
     $route->any('saleOrderList', 'Api\WxController@saleOrderList');
-    $route->any('checkAll', 'Api\CheckController@checkAll');
+    $route->any('checkAll', 'Api\CheckController@checkAll')->middleware('OssFileDeal');
 
     //可见范围
     $route->any('seeRange', 'Api\RangeController@seeRange');
@@ -49,6 +49,7 @@ Route::group(['middleware'=> ['checkWx']],function ($route){
     $route->any('salesOrderAdd', 'Api\SalesOrderController@salesOrderAdd');
     $route->any('salesOrderDel', 'Api\SalesOrderController@salesOrderDel');
     $route->any('salesOrderDispatchCompany', 'Api\SalesOrderController@salesOrderDispatchCompany');
+    $route->any('salesOrderGetBuildFee', 'Api\SalesOrderController@salesOrderGetBuildFee');
 
     $route->any('constructionOperation', 'Api\ConstructionController@constructionOperation');
     $route->any('constructionGet', 'Api\ConstructionController@constructionGet');