Sfoglia il codice sorgente

扣库存,加销量

sorry 5 anni fa
parent
commit
d49a237a2a

+ 4 - 0
biz-hd/goods/classes/GoodsClass.php

@@ -2,6 +2,7 @@
 
 namespace bizHd\goods\classes;
 
+use biz\goods\models\Goods;
 use bizHd\goods\services\GoodsCategoryService;
 use biz\sj\services\MerchantService;
 use common\components\business;
@@ -71,6 +72,9 @@ class GoodsClass extends BaseClass
                 }
             }
             $val['belongCategory'] = $belongCategory;
+            if ($val['stock'] == Goods::FULL_STOCK) {
+                $val['stock'] = '';
+            }
             $list[$key] = $val;
         }
         return $list;

+ 72 - 29
biz-hd/order/classes/OrderClass.php

@@ -2,6 +2,8 @@
 
 namespace bizHd\order\classes;
 
+use biz\goods\classes\GoodsClass;
+use biz\goods\models\Goods;
 use biz\shop\classes\ShopAdminClass;
 use biz\shop\models\Shop;
 use biz\stat\classes\StatOrderCountClass;
@@ -12,6 +14,7 @@ use common\components\orderSn;
 use common\components\util;
 use Yii;
 use bizHd\base\classes\BaseClass;
+use yii\db\Expression;
 
 class OrderClass extends BaseClass
 {
@@ -180,40 +183,57 @@ class OrderClass extends BaseClass
         if (isset($order->status) == false || $order->status != self::ORDER_STATUS_UN_PAY) {
             util::fail('订单不是待付款状态');
         }
-        $order->status = self::ORDER_STATUS_UN_SEND;
-        $order->currentFlow = 2;
-        $order->payWay = $payWay;
-        $order->save();
+        $orderGoods = OrderGoodsClass::getAllByCondition(['orderId' => $order->id], null, '*');
+        if (empty($orderGoods)) {
+            util::fail('没有找到订单商品');
+        }
+        
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+        try {
+            $order->status = self::ORDER_STATUS_UN_SEND;
+            $order->currentFlow = 2;
+            $order->payWay = $payWay;
+            $order->save();
 
-        //下单完成,开始制单
-        $current = time();
-        $orderId = $order->id;
-        $orderSn = $order->orderSn;
-        OrderSendClass::placeOrder(['orderId' => $orderId, 'payTime' => $current, 'orderSn' => $orderSn]);
+            //下单完成,开始制单
+            $current = time();
+            $orderId = $order->id;
+            $orderSn = $order->orderSn;
+            OrderSendClass::placeOrder(['orderId' => $orderId, 'payTime' => $current, 'orderSn' => $orderSn]);
 
-        $shopId = $order->shopId;
-        $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
-        $shop->finishOrder += 1;
-        $shop->unSendOrder += 1;
-        $shop->unPayOrder -= 1;
-        $shop->save();
+            $shopId = $order->shopId;
+            $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
+            $shop->finishOrder += 1;
+            $shop->unSendOrder += 1;
+            $shop->unPayOrder -= 1;
+            $shop->save();
 
-        if (isset($order->shopAdminId) && !empty($order->shopAdminId)) {
-            //员工开单,自取订单,直接完成
-            if ($order->sendType == self::SEND_TYPE_NO) {
-                OrderSendClass::withoutSend($order);
-            }
-        } else {
-            //门店和朋友圈订单并且没有填写收花人的直接将订单置为自取并且是完成状态
-            if (isset($order->fromType) && in_array($order->fromType, [0, 2])) {
-                if (isset($order->receiveMobile) && isset($order->address)) {
-                    if (empty($order->receiveMobile) && empty($order->address)) {
-                        OrderSendClass::withoutSend($order);
+            // 扣库存
+            self::decGoodsStock($orderGoods);
+
+            // 增加销量
+            self::updateGoodsActualSold($orderGoods);
+
+            if (isset($order->shopAdminId) && !empty($order->shopAdminId)) {
+                //员工开单,自取订单,直接完成
+                if ($order->sendType == self::SEND_TYPE_NO) {
+                    OrderSendClass::withoutSend($order);
+                }
+            } else {
+                //门店和朋友圈订单并且没有填写收花人的直接将订单置为自取并且是完成状态
+                if (isset($order->fromType) && in_array($order->fromType, [0, 2])) {
+                    if (isset($order->receiveMobile) && isset($order->address)) {
+                        if (empty($order->receiveMobile) && empty($order->address)) {
+                            OrderSendClass::withoutSend($order);
+                        }
                     }
                 }
-            }
+            };
+            $transaction->commit();
+        } catch (\Exception $exception) {
+            $transaction->rollback();
         }
-
     }
 
     //转化出送到时间 ssh 2020.3.15
@@ -262,4 +282,27 @@ class OrderClass extends BaseClass
         }
     }
 
-}
+    // 扣库存
+    public static function decGoodsStock($orderGoods)
+    {
+        foreach ($orderGoods as $goods) {
+            Goods::updateAll([
+                'stock' => new Expression('`stock` - '. $goods['num'])
+            ], [
+                'and',
+                ['id' => $goods['goodsId']],
+                ['<>', 'stock', Goods::FULL_STOCK]
+            ]);
+        }
+    }
+
+    // 更新商品销量
+    public static function updateGoodsActualSold($orderGoods)
+    {
+        foreach ($orderGoods as $goods) {
+            GoodsClass::updateById($goods['goodsId'], [
+                'actualSold' => new Expression('`actualSold` + '. $goods['num'])
+            ]);
+        }
+    }
+}

+ 3 - 0
biz/goods/models/Goods.php

@@ -6,6 +6,9 @@ use biz\base\models\Base;
 
 class Goods extends Base
 {
+    // 库存充足
+    public const FULL_STOCK = -100000;
+
     public static function tableName()
     {
         return 'xhGoods';