Inheritance: extends yii\db\ActiveRecord
 public function actionOrder()
 {
     $session = Yii::$app->getSession();
     $cart = $session->has('cart') ? $session->get('cart') : [];
     if (!$cart) {
         return $this->redirect(['index']);
     }
     $model = new Order();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         if (is_array($cart)) {
             foreach ($cart as $one_goods) {
                 $order_goods = new OrderGoods();
                 $order_goods->goods_id = $one_goods['id'];
                 $order_goods->count = $one_goods['count'];
                 $order_goods->order_id = $model->id;
                 $order_goods->save();
             }
         }
         Yii::$app->session->remove('cart');
         return $this->redirect(['index']);
     } else {
         $goods_id = ArrayHelper::getColumn($cart, 'id');
         $goods = Goods::find()->where(['id' => $goods_id])->asArray()->indexBy('id')->all();
         $amount = 0;
         foreach ($cart as $one_goods) {
             $amount += $one_goods['count'] * $goods[$one_goods['id']]['price'];
         }
         $model->price = $amount;
         return $this->render('order', ['cart' => $cart, 'model' => $model, 'amount' => $amount]);
     }
 }
 /**
  * 创建订单
  * 
  * @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;
     }
 }
Exemple #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getOrderGoods()
 {
     return $this->hasMany(OrderGoods::className(), ['order_id' => 'id']);
 }
 /**
  * Displays a single Order model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $goods = OrderGoods::find()->where(['order_id' => $id])->all();
     return $this->render('view', ['model' => $this->findModel($id), 'goods' => $goods]);
 }
Exemple #5
0
 /**
  * 获取用户某个时间段购买某个商品的数量
  * 
  * @param string $goodsId
  * @param string $timeStart
  * @param string $timeEnd
  * @return integer
  */
 public function getOrderGoodsCount($goodsId, $timeStart = null, $timeEnd = null)
 {
     $sql = 'SELECT sum(t0.count) FROM ' . OrderGoods::tableName() . ' AS t0 LEFT JOIN ' . Order::tableName() . ' AS t1 ON t0.order_id = t1.id WHERE t1.user_id=:user_id AND t0.goods_id=:goods_id';
     $params = [':user_id' => $this->id, ':goods_id' => $goodsId];
     if ($timeStart !== null) {
         $sql .= ' AND t1.created_at >= :time_start';
         $params[':time_start'] = $timeStart;
     }
     if ($timeEnd !== null) {
         $sql .= ' AND t1.created_at < :time_end';
         $params[':time_end'] = $timeEnd;
     }
     $count = Yii::$app->db->createCommand($sql, $params)->queryScalar();
     return (int) $count;
 }