Abstract.php 7.13 KB
<?php

/**
 * Created by PhpStorm.
 * User: ziy
 * Date: 14-7-22
 * Time: 下午5:05
 */
abstract class YHMCart_Orders_Abstract extends YHMCart_Orders_Dispatch implements YHMCart_Orders_Interface
{

    protected $orderType = 1;

    protected $uid = 0;

    protected $agreementData = array(
        'agreement_key' => '',
        'buyer_uid' => 0
    );

    /**
     * agreement    协议价
     * ordinary     普通、常规价格
     * @var string
     */
    protected $buyingPatterns = 'ordinary';

    public function __construct($uid, $orderType = 1)
    {
        $this->uid = $uid;
        $this->orderType = $orderType;
    }

    /**
     * 设置默认的字典
     *
     * @var array
     */
    private $orderDictionary = array(
        'want_invoice' => 'N', #是否开发票
        'payment_type' => 1,
        'bank_code' => '',
        'is_payed' => 'N',
        'order_status' => 100,
        'order_remark' => ''
    );

    /**
     * 订单必选字典
     * @var array
     */
    private $orderMandatoryDictionary = array(
        'buyer_uid',
        'seller_uid',
        'store_id',
        'order_amount',
        'last_order_amount',
        'shipping_fee',
        'last_shipping_fee',
        'buying_way',
        'payment_id'
    );


    /**
     * 初始化订单数据
     * @param array $data
     * @return array
     */
    public function initOrdersData(array $data)
    {
        $data['order_type'] = $this->orderType;
        $_orderData = array(
            'buying_way' => $data['buying_way']
        );
        $lastOrderData = array();
        foreach ($this->orderMandatoryDictionary as $key => $val) {
            $_orderData[$val] = '';
            if (isset($data[$val])) {
                $_orderData[$val] = $data[$val];
            }
        }
        foreach ($this->orderDictionary as $odKey => $odVal) {
            if (isset($data[$odKey])) {
                $_orderData[$odKey] = $data[$odKey];
                if (empty($_orderData[$odKey])) {
                    $_orderData[$odKey] = $odVal;
                }
            }
        }
        if (!empty($data['goods_list'])) {
            $goodsData = $this->initOrdersGoods($data['goods_list']);
            if (empty($goodsData)) {
                throw new Exception('订单商品数据不能为空.');
            }
            foreach ($goodsData as $key => $storeGoods) {
                $orderCode = YHMCart_Library_Number::getOrderCode($this->uid);
                $_orderData['order_code'] = $orderCode;
                $_orderData['goods_list'] = $storeGoods['goods_list'];
                $_orderData['order_amount'] = $storeGoods['order_amount'];
                $_orderData['last_order_amount'] = $storeGoods['last_order_amount'];
                $_orderData['seller_uid'] = $storeGoods['seller_uid'];
                $_orderData['store_id'] = $storeGoods['store_id'];
                $lastOrderData[] = $_orderData;
            }
            unset($goodsData);
        }
        return $lastOrderData;
    }

    /**
     * 初始化订单商品
     * @param array $orderGoods
     * @param $order_code
     * @return array
     */
    public function initOrdersGoods(array $orderGoods)
    {
        $_orderGoods = array();
        /**
         * $orderGoods[] = array(
         * 'product_sku' => 2,
         * 'buy_number' => 1
         * );
         * $orderGoods[] = array(
         * 'product_sku' => 3,
         * 'buy_number' => 1
         * );
         */
        foreach ($orderGoods as $key => $goods) {
            if (empty($goods['product_sku'])) {
                continue;
            }
            $stock = YHMProduct_Models_Stock_Client::getStockBySku($goods['product_sku']);
            if (empty($stock)) {
                continue;
            }
            $goodsInfo = YHMProduct_Models_Goods_Client::getOneByProductSkc($stock['product_skc']);
            $lastPrice = empty($goodsInfo['last_price']) ? $goodsInfo['sale_price'] : $goodsInfo['last_price'];
            if ($this->buyingPatterns == 'agreement') {
                $agreementInfo = YHMOrders_Models_Shopping_Client::getShoppingChangePrice($this->agreementData['agreement_key'], $this->agreementData['buyer_uid']);
                $lastPrice = empty($agreementInfo) ? $goodsInfo['sale_price'] : $agreementInfo['agreement_price'];
            }
            $storeGoods = array(
                'store_id' => $goodsInfo['store_id'],
                'seller_uid' => $goodsInfo['uid']
            );
            if (empty($_orderGoods[$goodsInfo['store_id']])) {
                $orderAmount = ($lastPrice * (int)$goods['buy_number']);
                $storeGoods['order_amount'] = $orderAmount;
                $storeGoods['last_order_amount'] = $orderAmount;
            } else {
                $orderAmount = $_orderGoods[$goodsInfo['store_id']]['order_amount'] + ($lastPrice * (int)$goods['buy_number']);
                $lastOrderAmount = $_orderGoods[$goodsInfo['store_id']]['last_order_amount'] + ($lastPrice * (int)$goods['buy_number']);
                $storeGoods['order_amount'] = $orderAmount;
                $storeGoods['last_order_amount'] = $lastOrderAmount;
            }
            $storeGoods['goods_list'][] = array(
                'product_skc' => $stock['product_skc'],
                'product_sku' => $stock['product_sku'],
                'buy_number' => $goods['buy_number'],
                'goods_type' => empty($goods['goods_type']) ? 1 : $goods['goods_type'],
                'quality_level' => $goodsInfo['quality_level'],
                'sale_price' => $goodsInfo['sale_price'],
                'last_price' => $lastPrice,
                'store_id' => $goodsInfo['store_id'],
                'color_id' => $goodsInfo['color_id'],
                'brand_id' => $goodsInfo['brand_id']
            );
            if (empty($_orderGoods[$goodsInfo['store_id']])) {
                $_orderGoods[$goodsInfo['store_id']] = $storeGoods;
            } else {
                $_orderGoods[$goodsInfo['store_id']]['goods_list'] = array_merge($_orderGoods[$goodsInfo['store_id']]['goods_list'], $storeGoods['goods_list']);
                unset($storeGoods['goods_list']);
                $_orderGoods[$goodsInfo['store_id']] = array_merge($_orderGoods[$goodsInfo['store_id']], $storeGoods);
            }
        }
        return $_orderGoods;
    }

    /**
     * 是否需要支付
     * @param array $orderData
     * @return bool|mixed
     */
    public function needPay(array $orderData)
    {
        if ((int)$orderData['last_order_amount'] != 0) {
            return true;
        }
        return false;
    }

    /**
     * 购买方式、形式
     * @param $patterns
     * @return $this
     */
    public function buyingPatterns($patterns)
    {
        $this->buyingPatterns = $patterns;
        return $this;
    }

    /**
     * 设置协议KEY
     * @param array $agreementData
     * @return $this
     */
    public function setAgreement(array $agreementData)
    {
        $this->agreementData = $agreementData;
        return $this;
    }

    /**
     * 支付URL
     * @param $order_code
     * @return string
     */
    static function getPayUrl($order_code)
    {
        return YHMCart_Config::$payUrl . $order_code;
    }
}