Эх сурвалжийг харах

发货数量汇总、重量汇总

afa 5 сар өмнө
parent
commit
4c87c644e3

+ 5 - 54
app/api/controller/Inventory.php

@@ -54,8 +54,12 @@ class Inventory extends Base
     public function stocklog(StockLog $stockLog)
     {
         $where = [];
+        $type    = $this->request->post('type/s', '');
         $type_id = $this->request->post('type_id/s', '');
+        $spec_id = $this->request->post('spec/s', '');
+        if(!empty($type))    $where['a.type'] = $type;
         if(!empty($type_id)) $where['a.type_id'] = $type_id;
+        if(!empty($spec_id)) $where['b.title'] = ['like', '%'.$spec_id.'%'];
 
         $list = $stockLog::alias('a')
                 ->leftjoin('stock_config b', 'a.variety_id = b.id')
@@ -67,59 +71,6 @@ class Inventory extends Base
         $this->success('ok', $list);
     }
 
-    //编辑出入库
-    public function stockedit(StockLog $stockLog)
-    {
-        $data = $this->request->post();
-        $result = false;
-        Db::startTrans();
-        try {
-          
-            validate(StockValidate::class)->scene('add')->check($data);
-            
-            $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
-        
-            $result = $stockLog->saveAll($resData);
-            Db::commit();
-        }catch (ValidateException $e) {
-      
-            return $this->error($e->getError());
-        } catch (\Exception $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
-        }
-        if ($result === false) {
-            $this->error(__('没有新增任何数据'));
-        }
-        $this->success();
-    }
-
-
-    //删除出入库
-    public function stockdel(StockLog $stockLog)
-    {
-        $data = $this->request->post();
-        $result = false;
-        Db::startTrans();
-        try {
-          
-            validate(StockValidate::class)->scene('add')->check($data);
-            
-            $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
-        
-            $result = $stockLog->saveAll($resData);
-            Db::commit();
-        }catch (ValidateException $e) {
-      
-            return $this->error($e->getError());
-        } catch (\Exception $e) {
-            Db::rollback();
-            $this->error($e->getMessage());
-        }
-        if ($result === false) {
-            $this->error(__('没有新增任何数据'));
-        }
-        $this->success();
-    }
+   
    
 }

+ 68 - 1
app/api/controller/Shops.php

@@ -13,6 +13,12 @@ class Shops extends Base
 {
     //protected $noNeedLogin = ['*'];
 
+    //发货数量汇总、重量汇总
+    public function getCount(ShopDelivery $shopDelivery)
+    {
+        $result = $shopDelivery->field('sum(num) total_num,sum(weigh) total_weight')->where('user_id', $this->userinfo['id'])->find();
+        $this->success('ok', $result);
+    }
   
     //发货记录
     public function delivery(ShopDelivery $shopDelivery){
@@ -20,16 +26,25 @@ class Shops extends Base
         $plat_id = $this->request->post('plat_id/d', 0); //平台
         $shop_id = $this->request->post('shop_id/d', 0); //店铺
         $variety_id = $this->request->post('variety_id/d', 0); //品种
+        $spec_id = $this->request->post('spec_id/d', 0); //规格
+        $customer = $this->request->post('customer_name/s', ''); //客户
+        $num  = $this->request->post('num/d', 0); //数量
+
         $limit = $this->request->post('limit/d', 15); //条数
         $where['a.user_id'] = $this->userinfo['id'];
         if($plat_id  > 0) $where['a.plat_id']   = $plat_id;
         if($shop_id  > 0) $where['a.shop_id']   = $shop_id;
         if($variety_id  > 0) $where['a.variety_id']= $variety_id;
+        if($spec_id  > 0) $where['a.spec_id']   = $spec_id;
+        if(!empty($customer)) $where['customer_name']   = ['like', '%'.$customer.'%'];
+        if($num  > 0)      $where['a.num']= $num;
+
         $result = $shopDelivery::alias('a')
                 ->leftjoin('shop_list b', 'a.shop_id = b.id') //店铺
                 ->leftjoin('stock_config c', 'a.variety_id = c.id') //品种
                 ->leftjoin('product_config d', 'a.spec_id = d.id') //规格
-                ->field('a.id,a.plat_id,a.shop_id,a.createtime,a.num, b.name shop_name,c.title variety_name,d.title spec_name')
+                ->leftjoin('customer u', 'a.customer_id = u.id') //客户
+                ->field('a.id,a.plat_id,a.shop_id,a.createtime,a.num, b.name shop_name,c.title variety_name,d.title spec_name,u.name customer_name')
                 ->where($where)
                 ->paginate($limit);
         $this->success('ok', $result);
@@ -63,7 +78,59 @@ class Shops extends Base
             $this->error(__('没有新增任何数据'));
         }
         $this->success();
+    }
 
+    //编辑录入
+    public function deliveryedit(SpecService $specService)
+    {
+        $data = $this->request->post();
+        $result = false;
+        Db::startTrans();
+        try {
+          
+            validate(ShopValidate::class)->scene('edit')->check($data);
+              
+            $result = $specService::getDeliveryEdit($this->userinfo['id'], $data);
+    
+            Db::commit();
+        }catch (ValidateException $e) {
+      
+            return $this->error($e->getError());
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($result === false) {
+            $this->error(__('没有新增任何数据'));
+        }
+        $this->success();
+    }
+
+
+    //删除录入
+    public function deliverydel(ShopDelivery $shopDelivery, SpecService $specService)
+    {
+        $data = $this->request->post();
+        $result = false;
+        Db::startTrans();
+        try {
+          
+            validate(ShopValidate::class)->scene('del')->check($data);
+            
+            $result = $specService::getDeliveryDel($this->userinfo['id'], $data);
+        
+            Db::commit();
+        }catch (ValidateException $e) {
+      
+            return $this->error($e->getError());
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($result === false) {
+            $this->error(__('没有新增任何数据'));
+        }
+        $this->success();
     }
 
  

+ 7 - 5
app/api/route/route.php

@@ -27,7 +27,12 @@ Route::group('user', function () {
     Route::rule('create','shops/create','POST');
     //发货记录
     Route::rule('delivery','shops/delivery','POST');
-
+    //编辑录入
+    Route::rule('deliveryedit','shops/deliveryedit','POST');
+    //删除
+    Route::rule('deliverydel','shops/deliverydel','POST');
+    //发货数量汇总、重量汇总
+    Route::rule('getCount','shops/getCount','POST');
 
     //出入库数据
     Route::rule('stockList','inventory/stockList','POST');
@@ -35,10 +40,7 @@ Route::group('user', function () {
     Route::rule('stock','inventory/stock','POST');
     //库存记录
     Route::rule('stocklog','inventory/stocklog','POST');
-    //编辑录入
-    Route::rule('stockedit','inventory/stockedit','POST');
-    //删除
-    Route::rule('stockdel','inventory/stockdel','POST');
+
 
    
   })->middleware(AllowCrossDomain::class);

+ 51 - 0
app/api/service/SpecService.php

@@ -6,6 +6,7 @@ use app\common\model\ShopList;
 use app\common\model\StockConfig;
 use app\common\model\ProductConfig;
 use app\common\model\StockDetail;
+use app\common\model\ShopDelivery;
 /**
  * 录入发货
  */
@@ -69,9 +70,59 @@ class SpecService{
             return $result;
       }
 
+      public static function getDeliveryEdit(int $user_id, array $data)
+      {
+            //查询发货记录
+            $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
+            if(!$delivery) throw new \Exception('参数有误!');
 
+            //判断是否当天
+           if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
+    
+            //修改库存
+            $specs = ProductConfig::where('id', $delivery->spec_id)->field('weight,box_id')->find();
+            if(!$specs)  throw new \Exception('参数有误!');
+
+            //根据品种扣除库存
+            $weight = bcmul((string)$specs->weight, $data['num'], 2); //总重
+
+            //扣除品种
+            StockDetail::setStockConfigNum($delivery->variety_id, bcsub($delivery->weigh, $weight, 2), StockConfig::VarietyName);
+
+            //根据规格扣除包装箱
+            StockDetail::setStockConfigNum($specs->box_id, bcsub((string)$delivery->num, $data['num'], 2), StockConfig::PackingBox);
+
+            //修改数量
+            $price  = bcmul($delivery->total_price, (string)$delivery->num, 2); //单价
+            $delivery->weigh = $weight; //总重
+            $delivery->num   = $data['num'];
+            $delivery->total_price = bcmul($price, $data['num'], 2);
+            return $delivery->save();
+      }
    
 
+      //删除
+      public static function getDeliveryDel(int $user_id, array $data)
+      {
+            //查询发货记录
+            $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
+            if(!$delivery) throw new \Exception('参数有误!');
 
+            //判断是否当天
+           if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
+    
+            //修改库存
+            $box_id = ProductConfig::where('id', $delivery->spec_id)->value('box_id');
+            if(!$box_id)  throw new \Exception('参数有误!');
+
+            //扣除品种
+            StockDetail::setStockConfigNum($delivery->variety_id, $delivery->weigh, StockConfig::VarietyName);
+
+            //根据规格扣除包装箱
+            StockDetail::setStockConfigNum($box_id, (string)$delivery->num, StockConfig::PackingBox);
+
+            //删除
+            return $delivery->delete();
+      }
    
 }

+ 8 - 3
app/api/validate/Shop.php

@@ -8,19 +8,24 @@ class Shop extends Validate
     protected $rule =   [
         'plat_id'  => 'require|number',
         'shop_id'  => 'require|number',
-        'variety' => 'require',    
+        'variety'  => 'require',
+        'ids'      => 'require',
+        'num'      => 'require|gt:0',
     ];
     
     protected $message  =   [
         'plat_id'      => '参数有误',
         'shop_id'     => '参数有误',
-        'variety'     => '参数有误',    
+        'variety'     => '参数有误',  
+        'ids'     => '参数有误', 
+        'num'     => '参数有误',   
     ];
 
 
  
     protected $scene = [
         'add'  =>  ['plat_id','shop_id','variety'],
-        'goods'  =>  ['plat_id','shop_id','variety'],
+        'edit' =>  ['ids','num'],
+        'del'  =>  ['ids'],
     ]; 
 }

+ 0 - 6
app/common/model/ShopDelivery.php

@@ -18,12 +18,6 @@ class ShopDelivery Extends Model
     ];
 
 
-
-    
-
-
-
-
     public static function onAfterInsert($data)
     {
         $data->weigh=1000-$data->id;