Browse Source

现存量

cqpCow 1 năm trước cách đây
mục cha
commit
825ec995a2

+ 21 - 0
app/Model/Customer.php

@@ -2,10 +2,12 @@
 
 namespace App\Model;
 
+use App\Scopes\DepartmentScope;
 use Illuminate\Database\Eloquent\Model;
 
 class Customer extends Model
 {
+    protected $fillable = ['userData'];
     protected $table = "customer"; //指定表
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
@@ -16,4 +18,23 @@ class Customer extends Model
         self::Model_type_one,
         self::Model_type_two
     ];
+
+    public static $user = [];
+    public static $is_search = false;
+
+    public function __construct(array $attributes = [])
+    {
+        if(! empty($attributes['userData'])) {
+            self::$user = $attributes['userData'];
+            self::$is_search = true;
+        }
+        parent::__construct($attributes);
+    }
+
+    protected static function boot(){
+        parent::boot();
+        if(self::$is_search){
+            static::addGlobalScope(new DepartmentScope(self::$user));
+        }
+    }
 }

+ 6 - 4
app/Scopes/DepartmentScope.php

@@ -20,10 +20,12 @@ class DepartmentScope implements Scope
         if($this->user['id'] != Employee::SPECIAL_ADMIN) {
             $depart_id = $this->user['depart_range'];
             $user_id = $this->user['id'];
-            $builder->where(function ($query) use ($depart_id,$user_id){
-                $query->whereIn('depart_id', $depart_id)
-                    ->orWhereIn('top_depart_id',$depart_id)
-                    ->orWhere('crt_id', $user_id);
+            $builder->orwhere(function ($query) use ($depart_id,$user_id){
+                $query->where(function ($query_sub) use ($depart_id,$user_id){
+                    $query_sub->whereIn('depart_id', $depart_id)
+                        ->orWhereIn('top_depart_id',$depart_id)
+                        ->orWhere('crt_id', $user_id);
+                });
             });
         }
     }

+ 14 - 8
app/Scopes/SalesOrderScope.php

@@ -2,7 +2,9 @@
 namespace App\Scopes;
 
 use App\Model\Employee;
+use App\Model\SalesOrder;
 use App\Model\SalesOrderInfo;
+use App\Service\SalesOrderService;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Scope;
@@ -10,22 +12,26 @@ use Illuminate\Database\Eloquent\Scope;
 class SalesOrderScope implements Scope
 {
     public $user = [];
+    public $search = [];
 
-    public function __construct($user = [])
+    public function __construct($user = [],$search = [])
     {
         $this->user = $user;
+        $this->search = $search;
     }
 
     public function apply(Builder $builder, Model $model)
     {
         if($this->user['id'] != Employee::SPECIAL_ADMIN) {
-            $sales_order_id = SalesOrderInfo::where('del_time',0)
-                ->where('employee_id',$this->user['id'])
-                ->select('sales_order_id')
-                ->get()->toArray();
-            $sales_order_id = array_unique(array_column($sales_order_id,'sales_order_id'));
-            $builder->orWhere(function ($query) use ($sales_order_id){
-                $query->whereIn('id',$sales_order_id);
+            $depart_id = $this->user['depart_range'];
+            $user_id = $this->user['id'];
+            $data = $this->search;
+            $builder->orwhere(function ($query) use ($depart_id,$user_id,$data){
+                $query->where(function ($query_sub) use ($depart_id,$user_id){
+                    $query_sub->whereIn('depart_id', $depart_id)
+                        ->orWhereIn('top_depart_id',$depart_id)
+                        ->orWhere('crt_id', $user_id);
+                });
             });
         }
     }

+ 6 - 3
app/Service/CustomerService.php

@@ -416,19 +416,22 @@ class CustomerService extends Service
      * @return array
      */
     public function customerList($data,$user){
-        $model = Customer::where('del_time',0)
+        $model = new Customer(['userData' => $user]);
+        $model = $model->where('del_time',0)
             ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','customer_grade','pond_state')
             ->orderby('id', 'desc');
 
         //getALL传入后无视设置范围
         if(empty($data['getAll']) && $user['id'] != Employee::SPECIAL_ADMIN) {
+            // 销售人员/负责人 3协同人 可以看见
             $user_id = $user['id'];
             $customer_id = CustomerInfo::where('del_time',0)
                 ->where(function ($query) use($user_id) {
                     $query->where('employee_id',$user_id);
                 })->select('customer_id')->get()
                 ->toArray();
-            $model->whereIn('id',array_unique(array_column($customer_id,'customer_id')));
+            $emp_id = array_unique(array_column($customer_id,'customer_id'));
+            $model->WhereIn('id', $emp_id);
         }
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
         if(! empty($data['time_type'])) {
@@ -489,7 +492,7 @@ class CustomerService extends Service
         }
         if($bool) return [false,'客户名称不能重复'];
 
-        return [true, $data];
+        return [true, ''];
     }
 
     /**

+ 2 - 2
app/Service/SalesOrderService.php

@@ -440,7 +440,6 @@ class SalesOrderService extends Service
                 ->select('sales_order_id')
                 ->get()->toArray();
             $sales_order_id = array_unique(array_column($sales_order_id,'sales_order_id'));
-
             //指派后
             $user_id = $user['id'];
             $depart_id = $user['depart_range'];
@@ -451,7 +450,8 @@ class SalesOrderService extends Service
                 })->select('sales_order_id')->get()
                 ->toArray();
             $sales_order_id = array_unique(array_merge_recursive($sales_order_id,array_column($sales_order_id2,'sales_order_id')));
-            $model->whereIn('id',$sales_order_id);
+            //可见范围
+            $model->whereIn('id', $sales_order_id);
         }
         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'].'%');