StockSale.php 1.8 KB
<?php

namespace addons\groupon\library\traits;

use addons\groupon\exception\Exception;
use addons\groupon\model\ActivityGoodsSkuPrice;
use addons\groupon\model\Goods;
use addons\groupon\model\GoodsSkuPrice;
use addons\groupon\model\OrderItem;
use addons\groupon\model\ScoreGoodsSkuPrice;

/**
 * 库存销量
 */
trait StockSale
{
    use StockWarning;
    
    // 真实正向 减库存加销量
    public function realForwardStockSale($order) {
        $items = OrderItem::where('order_id', $order['id'])->select();

        foreach ($items as $key => $orderItem) {
            // 增加商品销量
            Goods::where('id', $orderItem->goods_id)->setInc('sales', $orderItem->goods_num);

            $goodsSkuPrice = GoodsSkuPrice::where('id', $orderItem->goods_sku_price_id)->find();
            if ($goodsSkuPrice) {
                $goodsSkuPrice->setDec('stock', $orderItem->goods_num);         // 减少库存
                $goodsSkuPrice->setInc('sales', $orderItem->goods_num);         // 增加销量

                // 库存预警检测
                $this->checkStockWarning($goodsSkuPrice);
            }
        }
    }


    // 真实反向 加库存减销量(支付过的现在没有返库存,暂时没用)
    public function realBackStockSale($order)
    {
        $items = OrderItem::where('order_id', $order['id'])->select();

        foreach ($items as $key => $orderItem) {
            // 返还商品销量
            Goods::where('id', $orderItem->goods_id)->setDec('sales', $orderItem->goods_num);

            // 返还规格库存
            $goodsSkuPrice = GoodsSkuPrice::where('id', $orderItem->goods_sku_price_id)->find();
            if ($goodsSkuPrice) {
                $goodsSkuPrice->setInc('stock', $orderItem->goods_num);
                $goodsSkuPrice->setDec('sales', $orderItem->goods_num);
            }
        }
    }
}