cqpCow 1 gadu atpakaļ
vecāks
revīzija
ab041eacfd

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

@@ -114,6 +114,19 @@ class ProductController extends BaseController
         }
     }
 
+    public function productList2(Request $request)
+    {
+        $service = new ProductService();
+        $user = $request->userData->toArray();
+        list($status,$data) = $service->productList2($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
     public function productDetail(Request $request)
     {
         $service = new ProductService();

+ 6 - 0
app/Model/ProductActivity.php

@@ -9,4 +9,10 @@ class ProductActivity extends UseScopeBaseModel
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
     const range_function = '';
+    const type_one = 1;
+    const type_two = 2;
+    public static $type_name = [
+        self::type_one => '零售活动',
+        self::type_two => '供应商活动',
+    ];
 }

+ 7 - 0
app/Service/CustomerService.php

@@ -317,6 +317,13 @@ class CustomerService extends Service
         $customer = $customer->toArray();
         $product = Product::where('id',$customer['intention_product'])->value('title');
         $customer['intention_product_title'] = $product;
+        $customer['product_category'] = [];
+        if(! empty($customer['intention_product'])){
+            $pro = (new ProductService())->getProductDetail([$customer['intention_product']]);
+            $product_category = $pro[$customer['intention_product']]['product_category'] ?? '';
+            $customer['product_category'] = $product_category ? json_decode($product_category,true) : [];
+        }
+
         $address = '';
         if(! empty($customer['address1'])) {
             $tmp = json_decode($customer['address1'],true);

+ 31 - 14
app/Service/ProductActivityService.php

@@ -19,6 +19,7 @@ class ProductActivityService extends Service
             DB::beginTransaction();
 
             $model = ProductActivity::where('id',$data['id'])->first();
+            $model->type = $data['type'] ?? 0;
             $model->title = $data['title'] ?? "";
             $model->start_time = $data['start_time'] ?? 0;
             $model->end_time = $data['end_time'] ?? 0;
@@ -64,6 +65,7 @@ class ProductActivityService extends Service
             DB::beginTransaction();
 
             $model = new ProductActivity();
+            $model->type = $data['type'] ?? 0;
             $model->title = $data['title'] ?? "";
             $model->start_time = $data['start_time'] ?? 0;
             $model->end_time = $data['end_time'] ?? 0;
@@ -166,7 +168,7 @@ class ProductActivityService extends Service
     public function productList($data,$user){
         $model = ProductActivity::TopClear($user,$data);
         $model = $model->where('del_time',0)
-            ->select('id','title','crt_id','mark','start_time','end_time','crt_time')
+            ->select('id','title','crt_id','mark','start_time','end_time','crt_time','type')
             ->orderby('id', 'desc');
 
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
@@ -188,26 +190,38 @@ class ProductActivityService extends Service
     }
 
     public function productRule(&$data, $user, $is_add = true){
-        if(empty($data['title'])) return [false,'活动名称名称不能为空'];
+        if(empty($data['type'])) return [false,'活动分类不能为空'];
+        $type_name = ProductActivity::$type_name[$data['type']] ?? "";
+        if(empty($type_name)) return [false,'活动分类不存在'];
+        if(empty($data['title'])) return [false,'活动名称不能为空'];
         if(empty($data['activity_time'][0]) || empty($data['activity_time'][1])) return [false,'请填写活动时间范围'];
         $data['start_time'] = $this->changeDateToDateMin($data['activity_time'][0]);
         $data['end_time'] = $this->changeDateToDateMin($data['activity_time'][1]);
+        $return = [];
         if($is_add){
-            $product = ProductActivityPrice::where('del_time',0)
-                ->where('start_time', '<=', $data['end_time'])
-                ->where('end_time', '>=', $data['start_time'])
-                ->select('product_id')
+            $product = ProductActivityPrice::from('product_activity_price as a')
+                ->join('product_activity as b','b.id','a.product_activity_id')
+                ->where('a.del_time',0)
+                ->where('a.start_time', '<=', $data['end_time'])
+                ->where('a.end_time', '>=', $data['start_time'])
+                ->select('a.product_id','b.type')
                 ->get()->toArray();
-            $product = array_column($product,'product_id');
+            foreach ($product as $value){
+                $return[] = $value['product_id'] . $value['type'];
+            }
         }else{
             if(empty($data['id'])) return [false,'ID不能为空'];
-            $product = ProductActivityPrice::where('del_time',0)
-                ->where('product_activity_id','<>',$data['id'])
-                ->where('start_time', '<=', $data['end_time'])
-                ->where('end_time', '>=', $data['start_time'])
-                ->select('product_id')
+            $product = ProductActivityPrice::from('product_activity_price as a')
+                ->join('product_activity as b','b.id','a.product_activity_id')
+                ->where('a.product_activity_id','<>',$data['id'])
+                ->where('a.del_time',0)
+                ->where('a.start_time', '<=', $data['end_time'])
+                ->where('a.end_time', '>=', $data['start_time'])
+                ->select('a.product_id','b.type')
                 ->get()->toArray();
-            $product = array_column($product,'product_id');
+            foreach ($product as $value){
+                $return[] = $value['product_id'] . $value['type'];
+            }
         }
         if(! empty($data['product'])){
             $start_time = date("Y-m-d H:i",$data['start_time']);
@@ -220,7 +234,9 @@ class ProductActivityService extends Service
                 ->toArray();
             foreach ($data['product'] as $value){
                 $pro = $map2[$value['product_id']] ?? "";
-                if(in_array($value['product_id'], $product)) return [false,'产品:' . $pro . '在' . $start_time . '——' . $end_time . '已设置活动价格'];
+                if(in_array($value['product_id'] . $data['type'], $return)) {
+                    return [false,'产品:' . $pro . '在' . $start_time . '——' . $end_time . '已设置'. $type_name .'价格'];
+                }
                 if(! empty($value['price'])) {
                     $tmp = $map[$value['basic_type_id']] ?? '';
                     $res = $this->checkNumber($value['price']);
@@ -249,6 +265,7 @@ class ProductActivityService extends Service
             $data['data'][$key]['activity_time'] = $start_time . '——' . $end_time;
             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
             $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
+            $data['data'][$key]['type_title'] = ProductActivity::$type_name[$value['type']] ?? '';
         }
 
         return $data;

+ 61 - 10
app/Service/ProductService.php

@@ -5,6 +5,7 @@ namespace App\Service;
 use App\Model\BasicType;
 use App\Model\Employee;
 use App\Model\Product;
+use App\Model\ProductActivity;
 use App\Model\ProductActivityPrice;
 use App\Model\ProductCategory;
 use App\Model\ProductInfo;
@@ -181,6 +182,7 @@ class ProductService extends Service
 
             $model = Product::where('id',$data['id'])->first();
             $model->product_category_id = $data['product_category_id'] ?? 0;
+            $model->product_category = $data['product_category'] ?? '';
             $model->title = $data['title'];
             $model->code = $data['code'] ?? '';
             $model->sn_code = $data['sn_code'] ?? '';
@@ -278,6 +280,7 @@ class ProductService extends Service
 
             $model = new Product();
             $model->product_category_id = $data['product_category_id'] ?? 0;
+            $model->product_category = $data['product_category'] ?? '';
             $model->title = $data['title'];
             $model->code = $data['code'] ?? '';
             $model->sn_code = $data['sn_code'] ?? '';
@@ -402,6 +405,7 @@ class ProductService extends Service
             ->first();
         if(empty($customer)) return [false,'产品不存在或已被删除'];
         $customer = $customer->toArray();
+        $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
         $category = ProductCategory::where('id',$customer['product_category_id'])
             ->value('title');
         $customer['product_category_title'] = $category;
@@ -491,6 +495,34 @@ class ProductService extends Service
         return [true, $list];
     }
 
+    public function productList2($data,$user){
+        $model = ProductCategory::TopClear($user,$data);
+        $model = $model->where('del_time',0)
+            ->select('title','id','parent_id')
+            ->orderby('id','desc');
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+
+        $list = $model->get()->toArray();
+        if(! empty($list)) {
+            $productList = Product::where('del_time',0)
+                ->whereIn('product_category_id',array_column($list,'id'))
+                ->select('id','product_category_id','title','code','sn_code')
+                ->get()->toArray();
+            $productMap = [];
+            foreach ($productList as $value){
+                $productMap[$value['product_category_id']][] = $value;
+            }
+            foreach ($list as $key => $value){
+                if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
+            }
+
+            $list = $this->makeTree(0,$list);
+            $list = $this->set_sort_circle($list);
+        }
+
+        return [200, $list];
+    }
+
     /**
      * 产品参数规则
      * @param $data
@@ -520,6 +552,7 @@ class ProductService extends Service
                 }
             }
         }
+        if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
 
         //所属部门 以及 顶级部门
         if(empty($data['depart_id'])) {
@@ -616,26 +649,44 @@ class ProductService extends Service
 
     //获取产品使用价格
     public function getProductPrice($product_id = [], $type = 1){
-        //type 1 采购  2 合同
+        //type 1 采购    2 合同 和 活动包
 
         $detail_map = [];
+        $time = time();
         if($type == 1) {
-            //分社价
+            //供应商活动价格
+            $activity = ProductActivityPrice::from('product_activity_price as a')
+                ->join('product_activity as b','b.id','a.product_activity_id')
+                ->where('a.del_time',0)
+                ->whereIn('a.product_id',$product_id)
+                ->where('a.start_time','<=',$time)
+                ->where('a.end_time','>=',$time)
+                ->where('b.type',ProductActivity::type_two)
+                ->select('a.product_id','a.basic_type_id','a.price')
+                ->get()->toArray();
+            foreach ($activity as $value){
+                $detail_map[$value['product_id']][] = $value;
+            }
+            //分社价 没有供应商活动价格使用分社价格
             $detail = ProductPriceDetail::where('del_time',0)
                 ->whereIn('product_id',$product_id)
                 ->select('product_id','basic_type_id','price')
                 ->get()->toArray();
             foreach ($detail as $value){
-                $detail_map[$value['product_id']][] = $value;
+                if(! isset($detail_map[$value['product_id']])){
+                    $detail_map[$value['product_id']][] = $value;
+                }
             }
         }else{
-            //活动价格
-            $time = time();
-            $activity = ProductActivityPrice::where('del_time',0)
-                ->whereIn('product_id',$product_id)
-                ->where('start_time','<=',$time)
-                ->where('end_time','>=',$time)
-                ->select('product_id','basic_type_id','price')
+            //零售活动价格
+            $activity = ProductActivityPrice::from('product_activity_price as a')
+                ->join('product_activity as b','b.id','a.product_activity_id')
+                ->where('a.del_time',0)
+                ->whereIn('a.product_id',$product_id)
+                ->where('a.start_time','<=',$time)
+                ->where('a.end_time','>=',$time)
+                ->where('b.type',ProductActivity::type_one)
+                ->select('a.product_id','a.basic_type_id','a.price')
                 ->get()->toArray();
             foreach ($activity as $value){
                 $detail_map[$value['product_id']][] = $value;

+ 1 - 0
routes/api.php

@@ -122,6 +122,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('productAdd', 'Api\ProductController@productAdd');
     $route->any('productDel', 'Api\ProductController@productDel');
     $route->any('productDetail', 'Api\ProductController@productDetail');
+    $route->any('productList2', 'Api\ProductController@productList2');
 
     //采购单
     $route->any('purchaseOrderList', 'Api\PurchaseOrderController@purchaseOrderList');