cqpCow 1 年之前
父節點
當前提交
c4f026c75d
共有 3 個文件被更改,包括 138 次插入22 次删除
  1. 33 7
      app/Scopes/DepartmentScope.php
  2. 1 1
      app/Service/EmployeeService.php
  3. 104 14
      app/Service/RangeService.php

+ 33 - 7
app/Scopes/DepartmentScope.php

@@ -20,6 +20,8 @@ class DepartmentScope implements Scope
 
     public function apply(Builder $builder, Model $model)
     {
+        //是否所有部门
+        $is_all_depart = $this->user['is_all_depart'] ?? 0;
         //权限范围内的部门
         $depart_range = $this->user['depart_range'] ?? [];
         //我可见的
@@ -40,14 +42,38 @@ class DepartmentScope implements Scope
             $top_depart_id = $search_depart_id;
         }
 
-        //顶级部门下 权限范围内部门的所有数据
-        $builder->where('top_depart_id', $top_depart_id)
-            ->whereIn('depart_id', $depart_range);
-
-        //加上可见范围 部门 人以后的数据
         $id = [];
-        if($is_see && $is_function_range) $id = RangeService::$range_function($this->user);
-        $builder->orWhereIn('id', $id);
+        //可见范围 以及单据里面填写人员
+        if($is_function_range) $id = RangeService::$range_function($this->user,$this->search);
+
+        if($is_all_depart){
+            //所有部门
+            if(empty($search_depart_id)){
+                if(! $is_see){
+                    //全部
+                    $builder->whereIn('depart_id', $depart_range);
+                }else{
+                    $builder->whereIn('id', $id);
+                }
+            }else{
+                if(! $is_see){
+                    //某个分社
+                    $builder->where('top_depart_id', $top_depart_id)
+                        ->whereIn('depart_id', $depart_range);
+                }else{
+                    $builder->whereIn('id', $id);
+                }
+            }
+        }else{
+            if(! $is_see){
+                //某个分社
+                $builder->where('top_depart_id', $top_depart_id)
+                    ->whereIn('depart_id', $depart_range)
+                    ->orWhereIn('id', $id);
+            }else{
+                $builder->whereIn('id', $id);
+            }
+        }
 
         if(! empty($this->user['is_search_main'])) $builder->orWhere('is_main', '>',0);
     }

+ 1 - 1
app/Service/EmployeeService.php

@@ -1239,7 +1239,7 @@ class EmployeeService extends Service
                             'is_stock' => $set_map[$t] ?? 1,
                         ];
                         $map[$value['depart_id']] = $t;
-                        if(! empty($tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
+                        if(! empty($t_tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
                     }
                 }
             }

+ 104 - 14
app/Service/RangeService.php

@@ -10,11 +10,12 @@ use App\Model\SalesOrderInfo;
 use App\Model\SalesOrderRange;
 use App\Model\SportsBagRange;
 use App\Model\SupplierInfo;
+use Illuminate\Support\Facades\DB;
 
 class RangeService extends Service
 {
     //获取客户可见数据
-    public static function customerRange($user){
+    public static function customerRange($user,$search){
         $user_id = $user['id'];
         $depart_id = $user['depart_range'];
         $type = implode(',',CustomerInfo::$see_man);
@@ -27,11 +28,24 @@ class RangeService extends Service
             ->where(function ($query) use($str) {
                 $query->whereRaw($str);
             })->select('customer_id')->get()->toArray();
-        return array_unique(array_column($customer_id,'customer_id'));
+
+        $return_id = array_unique(array_column($customer_id,'customer_id'));
+        if(! empty($search['top_depart_id'])){
+            $id = DB::table('customer')
+                ->where('del_time',0)
+                ->where('top_depart_id',$search['top_depart_id'])
+                ->select('id')->get()->toArray();
+            $id = array_column($id,'id');
+            foreach ($return_id as $key => $value){
+                if(! in_array($value,$id)) unset($return_id[$key]);
+            }
+        }
+
+        return $return_id;
     }
 
     //获取供应商可见数据
-    public static function supplierRange($user){
+    public static function supplierRange($user,$search){
         $user_id = $user['id'];
         $depart_id = $user['depart_range'];
         $type = SupplierInfo::type_two;
@@ -44,11 +58,24 @@ class RangeService extends Service
             ->where(function ($query) use($str) {
                 $query->whereRaw($str);
             })->select('supplier_id')->get()->toArray();
-        return array_unique(array_column($supplier_id,'supplier_id'));
+
+        $return_id = array_unique(array_column($supplier_id,'supplier_id'));
+        if(! empty($search['top_depart_id'])){
+            $id = DB::table('supplier')
+                ->where('del_time',0)
+                ->where('top_depart_id',$search['top_depart_id'])
+                ->select('id')->get()->toArray();
+            $id = array_column($id,'id');
+            foreach ($return_id as $key => $value){
+                if(! in_array($value,$id)) unset($return_id[$key]);
+            }
+        }
+
+        return $return_id;
     }
 
     //获取产品可见数据
-    public static function productRange($user){
+    public static function productRange($user,$search){
         $user_id = $user['id'];
         $depart_id = $user['depart_range'];
         $product_id = ProductRange::where('del_time',0)
@@ -57,11 +84,24 @@ class RangeService extends Service
                     ->orWhereIn('depart_id', $depart_id);
             })->select('product_id')->get()
             ->toArray();
-        return array_unique(array_column($product_id,'product_id'));
+
+        $return_id = array_unique(array_column($product_id,'product_id'));
+        if(! empty($search['top_depart_id'])){
+            $id = DB::table('product')
+                ->where('del_time',0)
+                ->where('top_depart_id',$search['top_depart_id'])
+                ->select('id')->get()->toArray();
+            $id = array_column($id,'id');
+            foreach ($return_id as $key => $value){
+                if(! in_array($value,$id)) unset($return_id[$key]);
+            }
+        }
+
+        return $return_id;
     }
 
     //获取合同可见数据
-    public static function salesOrderRange($user){
+    public static function salesOrderRange($user,$search){
         //单据中选择的签订负责协同人
         $sales_order_id = SalesOrderInfo::where('del_time',0)
             ->whereIn('type',SalesOrderInfo::$man)
@@ -78,21 +118,45 @@ class RangeService extends Service
                     ->orWhereIn('depart_id', $depart_id);
             })->select('sales_order_id')->get()
             ->toArray();
-        return array_unique(array_merge_recursive($sales_order_id,array_column($sales_order_id2,'sales_order_id')));
+        $return_id = array_unique(array_merge_recursive($sales_order_id,array_column($sales_order_id2,'sales_order_id')));
+        if(! empty($search['top_depart_id'])){
+            $id = DB::table('sales_order')
+                ->where('del_time',0)
+                ->where('top_depart_id',$search['top_depart_id'])
+                ->select('id')->get()->toArray();
+            $id = array_column($id,'id');
+            foreach ($return_id as $key => $value){
+                if(! in_array($value,$id)) unset($return_id[$key]);
+            }
+        }
+
+        return $return_id;
     }
 
     //获取施工单可见数据
-    public static function constructionRange($user){
+    public static function constructionRange($user,$search){
         //单据中选择的签订负责协同人
         $construction_id = ConstructionInfo::where('del_time',0)
             ->where('employee_id',$user['id'])
             ->select('construction_id')
             ->get()->toArray();
-        return array_unique(array_column($construction_id,'construction_id'));
+        $return_id = array_unique(array_column($construction_id,'construction_id'));
+        if(! empty($search['top_depart_id'])){
+            $id = DB::table('construction')
+                ->where('del_time',0)
+                ->where('top_depart_id',$search['top_depart_id'])
+                ->select('id')->get()->toArray();
+            $id = array_column($id,'id');
+            foreach ($return_id as $key => $value){
+                if(! in_array($value,$id)) unset($return_id[$key]);
+            }
+        }
+
+        return $return_id;
     }
 
     //获取退换货单可见数据
-    public static function returnExchangeOrderRange($user){
+    public static function returnExchangeOrderRange($user,$search){
         $user_id = $user['id'];
         $depart_id = $user['depart_range'];
         $return_exchange_id = ReturnExchangeOrderRange::where('del_time',0)
@@ -101,11 +165,24 @@ class RangeService extends Service
                     ->orWhereIn('depart_id', $depart_id);
             })->select('return_exchange_id')->get()
             ->toArray();
-        return array_unique(array_column($return_exchange_id,'return_exchange_id'));
+
+        $return_id = array_unique(array_column($return_exchange_id,'return_exchange_id'));
+        if(! empty($search['top_depart_id'])){
+            $id = DB::table('return_exchange_order')
+                ->where('del_time',0)
+                ->where('top_depart_id',$search['top_depart_id'])
+                ->select('id')->get()->toArray();
+            $id = array_column($id,'id');
+            foreach ($return_id as $key => $value){
+                if(! in_array($value,$id)) unset($return_id[$key]);
+            }
+        }
+
+        return $return_id;
     }
 
     //获取活动包可见数据
-    public static function sportsBagRange($user){
+    public static function sportsBagRange($user,$search){
         $user_id = $user['id'];
         $depart_id = $user['depart_range'];
         $return_exchange_id = SportsBagRange::where('del_time',0)
@@ -114,6 +191,19 @@ class RangeService extends Service
                     ->orWhereIn('depart_id', $depart_id);
             })->select('sports_bag_id')->get()
             ->toArray();
-        return array_unique(array_column($return_exchange_id,'sports_bag_id'));
+
+        $return_id = array_unique(array_column($return_exchange_id,'sports_bag_id'));
+        if(! empty($search['top_depart_id'])){
+            $id = DB::table('sports_bag')
+                ->where('del_time',0)
+                ->where('top_depart_id',$search['top_depart_id'])
+                ->select('id')->get()->toArray();
+            $id = array_column($id,'id');
+            foreach ($return_id as $key => $value){
+                if(! in_array($value,$id)) unset($return_id[$key]);
+            }
+        }
+
+        return $return_id;
     }
 }