afa 5 сар өмнө
parent
commit
72d3c82cab

+ 10 - 0
app/api/controller/Inventory.php

@@ -67,6 +67,16 @@ class Inventory extends Base
         $this->success('ok', $list);
     }
 
+    //首页数据
+    public function index(StockLog $stockLog)
+    {
+        //分组统计品种
+        $data= $stockLog->where('user_id', $this->userinfo['id'])
+                        ->field('type,count(id) as count')
+                        ->group('type')
+                        ->select();
+        $this->success('ok', $data);
+    }
 
    
 }

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

@@ -21,7 +21,6 @@ Route::group('user', function () {
     //规格
     Route::rule('specs','shops/getSpec','POST');
 
-
     //添加记录
     Route::rule('create','shops/create','POST');
 
@@ -37,6 +36,11 @@ Route::group('user', function () {
     //库存记录
     Route::rule('stocklog','inventory/stocklog','POST');
 
+    //首页数据
+    Route::rule('index','inventory/index','POST');
+    //库存统计
+    Route::rule('statistics','inventory/statistics','POST');
+
   })->middleware(AllowCrossDomain::class);
 
 

+ 7 - 5
app/api/service/StockService.php

@@ -12,7 +12,6 @@ use app\common\model\StockDetail;
 class StockService {
 
    
-       
       
       //出入库:  id 库存的id
       public static function setGoOutStock(int $uid, array $data): array
@@ -22,16 +21,19 @@ class StockService {
             {
                   if(count($item) != 3 || empty(floatval($item['num'])))  throw new \Exception('参数有误!');
                   $weight = ($data['type']== '2') ? -$item['num']: $item['num'];
+                
+                  //根据品种扣除库存
+                  $stock = StockDetail::setStockConfigNum((int)$item['variety_id'], (string)$weight, $data['type_id']);
+
+                  //记录数据
                   $result[] = [
                         'user_id'         => $uid, 
                         'type_id'         => $data['type_id'],
                         'type'            => $data['type'],
                         'variety_id'      => $item['variety_id'],
-                        'change'          => $item['num']
+                        'change'          => $item['num'],
+                        'after'           => $stock
                   ];
-              
-                  //根据品种扣除库存
-                  StockDetail::setStockConfigNum((int)$item['variety_id'], (string)$weight, $data['type_id']);
             }
             return $result;
       }

+ 5 - 3
app/common/model/StockDetail.php

@@ -30,13 +30,15 @@ class StockDetail extends BaseModel
       public static function setStockConfigNum(int $key, string $num, string $type)
       {     
             $row = StockDetail::where('key', $key)->where('type_id', $type)->find();
+            $stock = $num; //库存
             if(empty($row)){
-              
-                  return self::create(['key' => $key,'num' => $num, 'type_id'=> $type]);
+                  self::create(['key' => $key,'num' => $num, 'type_id'=> $type]);
             }else{
+                  $stock = $row->num;
                   $row->num = bcadd($row->num, $num, 2);
-                  return $row->save()?true:false;
+                  $row->save()?true:false;
             }
+            return $stock;
       }