cqpCow 1 yıl önce
ebeveyn
işleme
ec5d5ffde1

+ 14 - 0
app/Http/Controllers/Api/EmployeeController.php

@@ -195,6 +195,20 @@ class EmployeeController extends BaseController
 
     }
 
+    public function departSet(Request $request)
+    {
+        $service = new EmployeeService();
+        $user = $request->userData->toArray();
+        list($status,$data) = $service->departSet($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
+
     public function teamEdit(Request $request)
     {
         $service = new EmployeeService();

+ 1 - 1
app/Http/Controllers/Api/LoginController.php

@@ -20,6 +20,6 @@ class LoginController extends BaseController
         //生成token
         $jwtToken = TokenService::getToken($return['id']);
 
-        return $this->json_return(200,'', ['token' => $jwtToken, 'emp_name'=>$return['name']]);
+        return $this->json_return(200,'', ['token' => $jwtToken, 'emp_name'=>$return['name'],'is_main'=>$return['is_main']]);
     }
 }

+ 1 - 0
app/Model/BasicType.php

@@ -33,5 +33,6 @@ class BasicType extends Model
         19 => '紧急程度',
         20 => '产品单位',
         21 => '跟进方式',
+        22 => '产品价格名'
     ];
 }

+ 13 - 0
app/Model/DepartPriceName.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class DepartPriceName extends Model
+{
+    protected $table = "depart_price_name"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 21 - 0
app/Model/Product.php

@@ -2,10 +2,12 @@
 
 namespace App\Model;
 
+use App\Scopes\DepartmentScope;
 use Illuminate\Database\Eloquent\Model;
 
 class Product extends Model
 {
+    protected $fillable = ['userData'];
     protected $table = "product"; //指定表
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
@@ -18,4 +20,23 @@ class Product extends Model
         self::State_one => '上架',
         self::State_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));
+        }
+    }
 }

+ 13 - 0
app/Model/ProductPriceDetail.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class ProductPriceDetail extends Model
+{
+    protected $table = "product_price_detail"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 100 - 2
app/Service/EmployeeService.php

@@ -3,6 +3,7 @@
 namespace App\Service;
 
 use App\Model\Depart;
+use App\Model\DepartPriceName;
 use App\Model\Employee;
 use App\Model\EmployeeDepartPermission;
 use App\Model\EmployeeManagerDepart;
@@ -521,7 +522,7 @@ class EmployeeService extends Service
      */
     public function departList($data,$user){
         $model = Depart::where('del_time',0)
-            ->select('title','id','code','parent_id','is_main')
+            ->select('title','id','code','parent_id','is_main','grade')
             ->orderby('code', 'asc');
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
         if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
@@ -536,6 +537,24 @@ class EmployeeService extends Service
         return [200,['data' => $list,'tree' => $list_tree]];
     }
 
+    public function fillDepartList($list){
+        if(empty($list)) return $list;
+
+        $name = DepartPriceName::where('del_time',0)
+            ->whereIn('depart_id', array_column($list,'id'))
+            ->select('depart_id','title','id')
+            ->get()->toArray();
+        $map = array_column($name,null,'depart_id');
+
+        foreach ($list as $key => $value){
+            if(empty($value['parent_id'])){
+                $list[$key]['dapart_set'] = $map[$value['id']] ?? [];
+            }
+        }
+
+        return $list;
+    }
+
     /**
      * 部门参数规则
      * @param $data
@@ -583,6 +602,7 @@ class EmployeeService extends Service
 
         foreach ($data['data'] as $key => $value){
             if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
+            if(empty($value['grade'])) $data['data'][$key]['grade'] = 0;
 
             $data['data'][$key]['upd_time'] = time();
 
@@ -622,6 +642,39 @@ class EmployeeService extends Service
         return $bool;
     }
 
+    public function departSet($data,$user){
+        if(empty($data['id'])) return [false,'请选择部门'];
+        if(empty($data['grade']) || ! is_numeric($data['grade'])) return [false,'请输入分社等级'];
+
+        try {
+            DB::beginTransaction();
+            Depart::where('id',$data['id'])->update(['grade' => $data['grade']]);
+
+            $time = time();
+            DepartPriceName::where('del_time',0)->where('depart_id',$data['id'])->update([
+                'del_time' => $time
+            ]);
+            if(! empty($data['title'])){
+                $insert = [];
+                foreach ($data['title'] as $value){
+                    $insert[] = [
+                        'depart_id' => $data['id'],
+                        'title' => $value,
+                        'crt_time' => $time,
+                    ];
+                }
+                DepartPriceName::insert($insert);
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
     /**
      * 班组编辑
      * @param $data
@@ -856,7 +909,8 @@ class EmployeeService extends Service
         if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
         if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
 
-        return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account']]];
+        $is_main = EmployeeService::isMain($res['id']);
+        return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'is_main' => $is_main]];
     }
 
     /**
@@ -1070,6 +1124,50 @@ class EmployeeService extends Service
         return [$depart, $top, $map, array_unique($rule)];
     }
 
+    //判断是否总公司
+    public static function isMain($employee_id){
+        $is_main = 0;
+        if(empty($employee_id)) return $is_main;
+
+        //admin账号
+        if($employee_id == Employee::SPECIAL_ADMIN) {
+            $is_main = 1;
+            return $is_main;
+        }
+
+        //自己绑定的部门 启用的部门
+        $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
+            ->join('depart as b','b.id','a.depart_id')
+            ->where('a.employee_id',$employee_id)
+            ->where('b.is_use',Depart::IS_UES)
+            ->select('a.depart_id','b.is_main','b.parent_id')
+            ->orderBy('a.depart_id','asc')
+            ->get()->toArray();
+
+        if(! empty($depart)){
+            $list = Depart::where('del_time',0)->get()->toArray();
+            $depart_map = array_column($list,null,'id');
+            foreach ($depart as $value){
+                if($value['parent_id'] == 0){//顶级
+                    $tmp['depart_id'] = $value['depart_id'];
+                    $tmp['is_main'] = $value['is_main'];
+                    if(! empty($tmp['is_main']) && ! $is_main) $is_main = 1;
+                }else{
+                    $t = self::getTopParentId($value['depart_id'],$list);
+                    if($t && isset($depart_map[$t])) {
+                        $tmp['depart_id'] = $depart_map[$t]['id'];
+                        $tmp['is_main'] = $depart_map[$t]['is_main'];
+                        $top[] = $tmp;
+                        $map[$value['depart_id']] = $t;
+                        if(! empty($tmp['is_main']) && ! $is_main) $is_main = 1;
+                    }
+                }
+            }
+        }
+
+        return $is_main;
+    }
+
     /**
      * 获取顶级id
      * @param $id

+ 63 - 7
app/Service/ProductService.php

@@ -10,6 +10,7 @@ use App\Model\ProductCategory;
 use App\Model\ProductInfo;
 use App\Model\ProductIntroduction;
 use App\Model\ProductInventory;
+use App\Model\ProductPriceDetail;
 use App\Model\ProductRange;
 use Illuminate\Support\Facades\DB;
 
@@ -159,7 +160,7 @@ class ProductService extends Service
      * @return array
      */
     public function productEdit($data,$user){
-        list($status,$msg) = $this->productRule($data,false);
+        list($status,$msg) = $this->productRule($data, $user, false);
         if(!$status) return [$status,$msg];
 
         try {
@@ -253,6 +254,23 @@ class ProductService extends Service
                 ProductRange::insert($insert);
             }
 
+            ProductPriceDetail::where('del_time',0)
+                ->where('product_id',$data['id'])
+                ->update(['del_time' => $time]);
+
+            if(! empty($data['product_price'])){
+                $insert = [];
+                foreach ($data['product_price'] as $value){
+                    $insert[] = [
+                        'product_id' => $model->id,
+                        'basic_type_id' => $value['basic_type_id'],
+                        'grade' => $value['grade'],
+                        'crt_time' => $time,
+                    ];
+                }
+                ProductPriceDetail::insert($insert);
+            }
+
             DB::commit();
         }catch (\Exception $exception){
             DB::rollBack();
@@ -269,7 +287,7 @@ class ProductService extends Service
      * @return array
      */
     public function productAdd($data,$user){
-        list($status,$msg) = $this->productRule($data);
+        list($status,$msg) = $this->productRule($data, $user);
         if(!$status) return [$status,$msg];
 
         try {
@@ -288,6 +306,8 @@ class ProductService extends Service
             $model->mark = $data['mark'] ?? '';
             $model->state = $data['state'] ?? 0;
             $model->crt_id = $user['id'];
+            $model->depart_id = $data['depart_id'] ?? 0;
+            $model->top_depart_id = $data['top_depart_id'] ?? 0;
             $model->save();
 
             $time = time();
@@ -353,6 +373,19 @@ class ProductService extends Service
                 ProductRange::insert($insert);
             }
 
+            if(! empty($data['product_price'])){
+                $insert = [];
+                foreach ($data['product_price'] as $value){
+                    $insert[] = [
+                        'product_id' => $model->id,
+                        'basic_type_id' => $value['basic_type_id'],
+                        'grade' => $value['grade'],
+                        'crt_time' => $time,
+                    ];
+                }
+                ProductPriceDetail::insert($insert);
+            }
+
             DB::commit();
         }catch (\Exception $exception){
             DB::rollBack();
@@ -388,6 +421,10 @@ class ProductService extends Service
                 ->where('product_id',$data['id'])
                 ->update(['del_time' => $time]);
 
+            ProductPriceDetail::where('del_time',0)
+                ->where('product_id',$data['id'])
+                ->update(['del_time' => $time]);
+
             DB::commit();
         }catch (\Exception $exception){
             DB::rollBack();
@@ -418,13 +455,27 @@ class ProductService extends Service
         $in = ProductIntroduction::where('del_time',0)
             ->where('product_id',$data['id'])
             ->first();
-        if($in) $customer['introduction'] = $in->introduction;
+        if(! empty($in)) $customer['introduction'] = $in->introduction;
+
+        $detail = ProductPriceDetail::where('del_time',0)
+            ->where('product_id',$data['id'])
+            ->select('product_id','basic_type_id','grade')
+            ->get()->toArray();
+        $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
+            ->pluck('title','id')
+            ->toArray();
+        $customer['product_price'] = [];
+        foreach ($detail as $value){
+            $customer['product_price'][] = [
+                'basic_type_id' => $value['basic_type_id'],
+                'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
+                'grade' => $value['grade'],
+            ];
+        }
 
         //单位
         $title = BasicType::where('id',$customer['unit'])->value('title');
         $customer['unit_name'] = $title;
-        //库存
-        $customer['product_inventory'] = (new ProductInventoryService())->getRealStock([$data['id']]);
 
         $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
         $customer_info = ProductInfo::where('del_time',0)
@@ -481,7 +532,8 @@ class ProductService extends Service
      * @return array
      */
     public function productList($data,$user){
-        $model = Product::where('del_time',0)
+        $model = new Product(['userData' => $user]);
+        $model = $model->where('del_time',0)
             ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','depart_price','state','crt_id','crt_time','mark')
             ->orderby('id', 'desc');
 
@@ -512,7 +564,7 @@ class ProductService extends Service
      * @param $is_add
      * @return array
      */
-    public function productRule($data, $is_add = true){
+    public function productRule(&$data, $user, $is_add = true){
         if(empty($data['title'])) return [false,'产品名称不能为空'];
         if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
         if(empty($data['code'])) return [false,'产品编码不能为空'];
@@ -526,6 +578,10 @@ class ProductService extends Service
         $res = $this->checkNumber($data['retail_price']);
         if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值'];
 
+        //所属部门 以及 顶级部门
+        if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
+        $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
+
         if($is_add){
             $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
                 ->where('del_time',0)