dever 3 年之前
父节点
当前提交
deaeb259a9
共有 4 个文件被更改,包括 45 次插入28 次删除
  1. 3 3
      app/goods/database/info.php
  2. 3 3
      app/goods/database/info_sku.php
  3. 1 1
      app/goods/lib/Manage.php
  4. 38 21
      app/shop/src/Buy.php

+ 3 - 3
app/goods/database/info.php

@@ -155,7 +155,7 @@ $config = array
 
         'price'     => array
         (
-            'type'      => 'varchar-100',
+            'type'      => 'decimal-10,2',
             'name'      => '销售价',
             'default'   => '',
             'desc'      => '销售价',
@@ -166,7 +166,7 @@ $config = array
 
         's_price'       => array
         (
-            'type'      => 'varchar-100',
+            'type'      => 'decimal-10,2',
             'name'      => '市场价',
             'default'   => '',
             'desc'      => '市场价',
@@ -177,7 +177,7 @@ $config = array
 
         'f_price'       => array
         (
-            'type'      => 'varchar-100',
+            'type'      => 'decimal-10,2',
             'name'      => '进价',
             'default'   => '',
             'desc'      => '进价',

+ 3 - 3
app/goods/database/info_sku.php

@@ -56,7 +56,7 @@ $config = array
 
         'price'     => array
         (
-            'type'      => 'varchar-100',//decimal-10,2
+            'type'      => 'decimal-10,2',
             'name'      => '销售价',
             'default'   => '',
             'desc'      => '销售价',
@@ -67,7 +67,7 @@ $config = array
 
         's_price'       => array
         (
-            'type'      => 'varchar-100',
+            'type'      => 'decimal-10,2',
             'name'      => '市场价',
             'default'   => '',
             'desc'      => '市场价',
@@ -78,7 +78,7 @@ $config = array
 
         'f_price'       => array
         (
-            'type'      => 'varchar-100',
+            'type'      => 'decimal-10,2',
             'name'      => '进价',
             'default'   => '',
             'desc'      => '进价',

+ 1 - 1
app/goods/lib/Manage.php

@@ -217,7 +217,7 @@ class Manage
                     $data['price'] = $price[$k];
                     $data['s_price'] = $s_price[$k];
                     $data['f_price'] = $f_price[$k];
-                    $data['num'] = $num[$k];
+                    //$data['num'] = $num[$k];
                     if (!$info) {
                         Dever::db('goods/info_sku')->insert($data);
                     } else {

+ 38 - 21
app/shop/src/Buy.php

@@ -79,6 +79,8 @@ class Buy extends Core
             $state = Dever::db('shop/cart')->insert($where);
         } else {
             $where['num'] += $info['num'];
+            # 验证库存
+            $this->total($where['num'], $info['goods_id'], $this->shop_id, $info['sku_id'], 2);
             $where['where_id'] = $info['id'];
             $state = Dever::db('shop/cart')->update($where);
         }
@@ -115,6 +117,8 @@ class Buy extends Core
                 $state = Dever::db('shop/cart')->delete($where);
             } else {
                 $where['num'] = $num;
+                # 验证库存
+                $this->total($where['num'], $info['goods_id'], $this->shop_id, $info['sku_id'], 2);
                 $state = Dever::db('shop/cart')->update($where); 
             }
         }
@@ -272,33 +276,14 @@ class Buy extends Core
         foreach ($goods_id as $k => $v) {
             $s = isset($sku_id[$k]) ? $sku_id[$k] : 0;
             $n = isset($num[$k]) ? $num[$k] : 1;
-            $total = 0;
             $this->data['list'][$k]['goods'] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
 
             # 2是库存不足
             $this->data['list'][$k]['goods']['ku_state'] = 1;
+
             # 验证是否有货
-            if ($s > 0) {
-                $w['goods_id'] = $v;
-                $w['shop_id'] = $this->shop_id;
-                $w['sku_id'] = $s;
-                $sku = Dever::db('shop/goods_sku')->getOne($w);
-
-                if ($sku) {
-                    $total = $sku['num'];
-                }
-            } else {
-                $w['goods_id'] = $v;
-                $w['shop_id'] = $this->shop_id;
-                $goods = Dever::db('shop/goods')->getOne($w);
-                if ($goods) {
-                    $total = $goods['num'];
-                }
-            }
+            $total = $this->total($n, $v, $this->shop_id, $s);
 
-            if ($n > $total) {
-                $n = $total;
-            }
             if ($total <= 0) {
                 $this->data['list'][$k]['goods']['ku_state'] = 2;
             }
@@ -354,4 +339,36 @@ class Buy extends Core
 
         return $pay;
     }
+
+    # 验证库存
+    private function total(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
+    {
+        $total = 0;
+        if ($sku_id > 0) {
+            $w['goods_id'] = $goods_id;
+            $w['shop_id'] = $shop_id;
+            $w['sku_id'] = $sku_id;
+            $sku = Dever::db('shop/goods_sku')->getOne($w);
+
+            if ($sku) {
+                $total = $sku['num'];
+            }
+        } else {
+            $w['goods_id'] = $goods_id;
+            $w['shop_id'] = $shop_id;
+            $goods = Dever::db('shop/goods')->getOne($w);
+            if ($goods) {
+                $total = $goods['num'];
+            }
+        }
+
+        if ($num > $total) {
+            if ($state == 2) {
+                Dever::alert('库存不足');
+            }
+            $num = $total;
+        }
+
+        return $total;
+    }
 }