cqpCow há 1 ano atrás
pai
commit
5eb352fd07

+ 25 - 0
app/Model/InOutRecord.php

@@ -2,12 +2,37 @@
 
 namespace App\Model;
 
+use App\Scopes\ProductInventoryScope;
 use Illuminate\Database\Eloquent\Model;
 
 class InOutRecord extends Model
 {
+    protected $guarded = [];
     protected $table = "in_out_record"; //指定表
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
+
+    public static $user = [];
+    public static $search = [];
+    public static $is_search = false;
+    const range_function = '';
+
+    public function __construct(array $attributes = [])
+    {
+        if(! empty($attributes['userData'])) {
+            self::$user = $attributes['userData'];
+            self::$search = $attributes['search'] ?? [];
+            self::$user['range_function'] = self::range_function;
+            self::$is_search = true;
+        }
+        parent::__construct($attributes);
+    }
+
+    protected static function boot(){
+        parent::boot();
+        if(self::$is_search){
+            static::addGlobalScope(new ProductInventoryScope(self::$user, self::$search));
+        }
+    }
 }

+ 25 - 0
app/Model/ProductInventory.php

@@ -2,12 +2,37 @@
 
 namespace App\Model;
 
+use App\Scopes\ProductInventoryScope;
 use Illuminate\Database\Eloquent\Model;
 
 class ProductInventory extends Model
 {
+    protected $guarded = [];
     protected $table = "product_inventory"; //指定表
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = null;
     protected $dateFormat = 'U';
+
+    public static $user = [];
+    public static $search = [];
+    public static $is_search = false;
+    const range_function = '';
+
+    public function __construct(array $attributes = [])
+    {
+        if(! empty($attributes['userData'])) {
+            self::$user = $attributes['userData'];
+            self::$search = $attributes['search'] ?? [];
+            self::$user['range_function'] = self::range_function;
+            self::$is_search = true;
+        }
+        parent::__construct($attributes);
+    }
+
+    protected static function boot(){
+        parent::boot();
+        if(self::$is_search){
+            static::addGlobalScope(new ProductInventoryScope(self::$user, self::$search));
+        }
+    }
 }

+ 1 - 1
app/Scopes/DepartmentScope.php

@@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Scope;
 
-//判断所属部门和顶级部门
+//判断所属部门和顶级部门(公司)
 class DepartmentScope implements Scope
 {
     public $user = [];

+ 52 - 0
app/Scopes/ProductInventoryScope.php

@@ -0,0 +1,52 @@
+<?php
+namespace App\Scopes;
+
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Scope;
+
+//现存量 库存台账
+class ProductInventoryScope implements Scope
+{
+    public $user = [];
+    public $search = [];
+
+    public function __construct($user = [],$search = [])
+    {
+        $this->user = $user;
+        $this->search = $search;
+    }
+
+    public function apply(Builder $builder, Model $model)
+    {
+        //是否所有部门
+        $is_all_depart = $this->user['is_all_depart'] ?? 0;
+        //权限范围内的部门
+        $depart_range = $this->user['depart_range'] ?? [];
+
+        //顶级部门
+        $search_depart_id = $this->search['top_depart_id'] ?? 0;
+        if(empty($search_depart_id)){
+            //默认进来 自身顶级公司
+            $top_depart_id = $this->user['depart_top'][0] ?? [];
+            $top_depart_id = $top_depart_id['depart_id'] ?? 0;
+        }else{
+            //查询 顶级公司
+            $top_depart_id = $search_depart_id;
+        }
+
+        if($is_all_depart){
+            //所有部门
+            if(empty($search_depart_id)){
+                //全部
+                $builder->whereIn('a.top_depart_id', $depart_range);
+            }else{
+                //查看某个分社
+                $builder->where('a.top_depart_id', $top_depart_id);
+            }
+        }else{
+            //某个分社全部
+            $builder->where('a.top_depart_id', $top_depart_id);
+        }
+    }
+}

+ 0 - 38
app/Scopes/SalesOrderScope.php

@@ -1,38 +0,0 @@
-<?php
-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;
-
-class SalesOrderScope implements Scope
-{
-    public $user = [];
-    public $search = [];
-
-    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) {
-            $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);
-                });
-            });
-        }
-    }
-}

+ 1 - 1
app/Scopes/TopDepartmentScope.php

@@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Scope;
 
-//只判断顶级id
+//只判断顶级id (公司)
 class TopDepartmentScope implements Scope
 {
     public $user = [];

+ 17 - 36
app/Service/ProductInventoryService.php

@@ -179,7 +179,8 @@ class ProductInventoryService extends Service
 
     //现存量
     public function productInventoryList($data,$user){
-        $model = ProductInventory::from('product_inventory as a')
+        $model = new ProductInventory(['userData' => $user, 'search' => $data]);
+        $model = $model->from('product_inventory as a')
             ->join('product as b','b.id','a.product_id')
             ->select('a.product_id','a.id','a.number','b.title','b.code','b.product_category_id','b.unit','b.bar_code','a.crt_time','a.storehouse_id')
             ->orderby('a.crt_time', 'desc');
@@ -237,16 +238,19 @@ class ProductInventoryService extends Service
 
     //库存台账
     public function productInventoryStockList($data,$user){
-        $model = InOutRecord::from('in_out_record as a')
+        if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false,'时间必须选择'];
+
+        $model = new InOutRecord(['userData' => $user, 'search' => $data]);
+        $model = $model->from('in_out_record as a')
             ->where('a.del_time',0)
             ->join('product as b','b.id','a.product_id')
-            ->select('a.id','a.product_id','a.number','a.order_type','a.crt_time','b.title','b.code','b.product_category_id','b.unit',DB::raw('SUM(CASE WHEN number < 0 THEN number ELSE 0 END) as out_number'),DB::raw('SUM(CASE WHEN number >= 0 THEN number ELSE 0 END) as in_number'),'a.order_number','a.crt_time','a.storehouse_id')
-            ->groupby('a.product_id','a.order_number')
+            ->select('a.id','a.product_id','a.number','a.order_type','a.crt_time','b.title','b.code','b.product_category_id','b.unit',DB::raw('SUM(CASE WHEN a.number < 0 THEN a.number ELSE 0 END) as out_number'),DB::raw('SUM(CASE WHEN a.number >= 0 THEN a.number ELSE 0 END) as in_number'),'a.order_number','a.crt_time','a.storehouse_id','a.top_depart_id')
+            ->groupby('a.product_id','a.top_depart_id','a.order_number')
             ->orderBy('a.crt_time','asc');
 
-        if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false,'时间必须选择'];
         $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
         $model->whereBetween('a.crt_time',[$return[0],$return[1]]);
+
         if(! empty($data['product_name'])){
             $product_name = $data['product_name'];
             $id = Product::where('del_time',0)
@@ -277,15 +281,16 @@ class ProductInventoryService extends Service
     public function fillData($data,$time_arr){
         if (empty($data)) return $data;
 
-        $start_data = InOutRecord::whereIn('product_id',array_column($data,'product_id'))
+        $start_data = InOutRecord::withoutGlobalScopes()
+            ->whereIn('product_id',array_unique(array_column($data,'product_id')))
             ->where('crt_time','<',$time_arr[0])
             ->where('del_time',0)
-            ->select('product_id','number',DB::raw('sum(number) as number'))
-            ->groupby('product_id')
+            ->select('product_id','number',DB::raw('sum(number) as number'),'top_depart_id')
+            ->groupby('product_id','top_depart_id')
             ->get()->toArray();
         $start_map = [];
         foreach ($start_data as $value){
-            $start_map[$value['product_id']] = $value['number'];
+            $start_map[$value['product_id'].$value['top_depart_id']] = $value['number'];
         }
 
         $category = ProductCategory::whereIn('id',array_unique(array_column($data,'product_category_id')))
@@ -298,11 +303,11 @@ class ProductInventoryService extends Service
 
         $roll_tmp = [];
         $order_type = $this->getOrderType();
-        $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
+        $storehouse = Storehouse::whereIn('id',array_unique(array_column($data,'storehouse_id')))
             ->pluck('title','id')
             ->toArray();
         foreach ($data as $key => $value){
-            $keys = $value['product_id'];
+            $keys = $value['product_id'] . $value['top_depart_id'];
             if(! isset($roll_tmp[$keys])){
                 if(isset($start_map[$keys])){
                     $data[$key]['start_number'] = $start_map[$keys];
@@ -324,7 +329,7 @@ class ProductInventoryService extends Service
             $data[$key]['order_name'] = $order_type[$value['order_type']] ?? '';
             $data[$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d",$value['crt_time']):'';
             $data[$key]['unit_title'] = $basic_map[$value['unit']] ?? '';
-            $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
+            $data[$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
         }
 
         return $data;
@@ -364,30 +369,6 @@ class ProductInventoryService extends Service
         return [true, $list];
     }
 
-    //系统设置列表
-    public function productInventorySetList1($data, $user){
-        $model = new ProductInventorySet();
-        $model = $model->where('del_time',0)
-            ->select('id','top_depart_id','param_one','param_two');
-
-        if(empty($data['top_depart_id'])){
-            //默认进来 只显示自己公司下的 自己权限范围下的部门数据
-            $top_depart_id = $user['depart_top'][0] ?? [];
-            $top_depart_id = $top_depart_id['depart_id'] ?? 0;
-        }else{
-            $top_depart_id = $data['top_depart_id'];
-        }
-        $model->where('top_depart_id', $top_depart_id);
-        $list = $model->get()->toArray();
-
-        $map = Depart::whereIn('id',array_column($list,'top_depart_id'))->pluck('title','id')->toArray();
-        foreach ($list as $key => $value){
-            $list[$key]['top_depart_title'] = $map[$value['top_depart_id']] ?? "";
-        }
-
-        return [true, $list];
-    }
-
     //库存设置
     public function productInventorySet($data, $user){
         if(empty($data['id'])) return [false, 'ID不能为空'];

+ 2 - 2
routes/api.php

@@ -194,10 +194,10 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
 
     //现存量
     $route->any('productInventoryList', 'Api\ProductInventoryController@productInventoryList');
-    //收发存汇总
-    $route->any('productInAndOutList', 'Api\ProductInventoryController@productInAndOutList');
     //库存台账
     $route->any('productInventoryStockList', 'Api\ProductInventoryController@productInventoryStockList');
+    //收发存汇总
+    $route->any('productInAndOutList', 'Api\ProductInventoryController@productInAndOutList');
     //设置列表
     $route->any('productInventorySetList', 'Api\ProductInventoryController@productInventorySetList');
     //设置开关