Inheritance: extends yii\db\ActiveRecord
 /**
  * 创建订单
  * 
  * @param string $runValidation
  * @throws \Exception
  * @return boolean
  */
 public function create($runValidation = true)
 {
     if ($runValidation && !$this->validate()) {
         return false;
     }
     if ($this->_store->status === Store::STATUS_REST) {
         throw new \Exception('该店铺休息中…');
     }
     $volume = Yii::$app->user->identity->getCartGoodsRealVolume($this->_store->id);
     if ($this->_store->has_least && $this->_store->least_val > $volume) {
         throw new \Exception('购物车商品未满起送价!');
     }
     if (empty($this->_cartGoodsList)) {
         throw new \Exception('当前购物车为空!');
     }
     foreach ($this->_cartGoodsList as $cartGoods) {
         if ($cartGoods->isExpired) {
             throw new \Exception('商品“' . $cartGoods->goods->name . '”已失效!请您删除该商品然后继续。');
         }
         if ($cartGoods->isTooMuch) {
             throw new \Exception('商品“' . $cartGoods->goods->name . '”数量已超出库存数量!请返回购物车中修改。');
         }
     }
     $transaction = Yii::$app->db->beginTransaction();
     try {
         $order = new Order();
         $order->generateOrderSn();
         $order->user_id = Yii::$app->user->id;
         $order->store_id = $this->_store->id;
         $order->school_id = $this->_store->school_id;
         $order->status = $this->payment === Order::PAYMENT_OFFLINE ? Order::STATUS_UNSHIPPED : Order::STATUS_UNPAID;
         $order->payment = $this->payment;
         $order->fee = $volume;
         $order->preferential = $this->preferential;
         $order->down_val = null;
         $order->gift_val = null;
         $order->new_down_val = null;
         $order->book_time = $this->bookTime == 0 ? null : $this->bookTime;
         $order->remark = $this->remark;
         $order->cancelled_msg = null;
         // 判断优惠类型
         switch ($this->preferential) {
             case Order::PREFERENTIAL_DOWN:
                 if ($this->_store->has_down && $order->fee >= $this->_store->down_upper) {
                     $order->real_fee = bcsub($order->fee, $this->_store->down_val, 2);
                     $order->down_val = $this->_store->down_val;
                 }
                 break;
             case Order::PREFERENTIAL_GIFT:
                 if ($this->_store->has_gift && $order->fee >= $this->_store->gift_upper) {
                     $order->real_fee = $order->fee;
                     $order->gift_val = $this->_store->gift_val;
                 }
                 break;
             case Order::PREFERENTIAL_NONE:
                 $order->real_fee = $order->fee;
                 break;
             default:
                 throw new \Exception('优惠选择错误!');
         }
         // 新用户立减优惠
         if (Yii::$app->params['enableNewDown'] && $this->newDown && $order->fee >= Yii::$app->params['newDownUpper'] && Yii::$app->user->identity->has_new_down) {
             $order->new_down_val = Yii::$app->params['newDownVal'];
             $order->real_fee = bcsub($order->real_fee, $order->new_down_val, 2);
             Yii::$app->user->identity->has_new_down = 0;
             if (!Yii::$app->user->identity->save(false)) {
                 throw new \Exception('用户错误!');
             }
             if ($order->real_fee < 0) {
                 $order->real_fee = 0;
                 $order->status = ORDER::STATUS_UNSHIPPED;
             }
         }
         if (!$order->save(false)) {
             throw new \Exception('订单错误!');
         }
         $this->_order = $order;
         $address = OrderAddress::createDuplicate($this->addressId);
         $address->order_id = $order->id;
         if (!$address->save(false)) {
             throw new \Exception('收货地址错误!');
         }
         foreach ($this->_cartGoodsList as $cartGoods) {
             $goods = OrderGoods::createDuplicate($cartGoods->goods_id);
             $goods->order_id = $order->id;
             $goods->count = $cartGoods->count;
             if (!$goods->save(false)) {
                 throw new \Exception('订单商品错误!');
             }
             if (!$cartGoods->goods->moveSurplus(-$goods->count, "创建订单:{$order->order_sn}。")) {
                 throw new \Exception('商品错误!');
             }
         }
         Yii::$app->user->identity->clearCartGoods($this->_store->id);
         $transaction->commit();
         return true;
     } catch (\Exception $e) {
         $transaction->rollBack();
         throw $e;
     }
 }
Beispiel #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getAddress()
 {
     return $this->hasOne(OrderAddress::className(), ['order_id' => 'id']);
 }