Example #1
0
 public function actionProcess($objectId, $modelClass, $orderId)
 {
     $package = Package::findOne($objectId);
     $op = new OrderProduct();
     $op->modelClass = $modelClass;
     $op->objectId = $objectId;
     $op->name = $package->name;
     $op->order_id = $orderId;
     $op->mask = $package->mask;
     if ($op->save()) {
         if ($modelClass === 'Package') {
             return $this->createCodeBank($package->duration, $op->id, $package->maxCallOut, $package->minBalance);
         } elseif ($modelClass == 'Topup') {
             return $this->createTopupBank($op->id);
         }
     }
     return false;
 }
Example #2
0
 /**
  * Updates an existing Order model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     //if(!Yii::$app->user->can('updateYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     $query = OrderProduct::find()->where(['order_id' => $id]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model, 'dataProvider' => $dataProvider]);
     }
 }
Example #3
0
 public function actionUpdateTopup()
 {
     $result = \Yii::$app->request->post();
     foreach ($result as $key => $value) {
         Yii::$app->db->createCommand('Update toppupBank set code=:code , used=1 where order_product_id=:key')->bindValue(':code', $value)->bindValue(':key', $key)->execute();
         $codeBank = CodeBank::find()->where(['code' => $value])->one();
         // Get Topup id
         $orderProduct = OrderProduct::findOne($key);
         // Get topup maxCallOut
         $tp = Topup::findOne($orderProduct->objectId);
         $codeBank->updateCounters(['maxCallout' => $tp->maxCallOut]);
     }
     return 1;
 }
Example #4
0
 /**
  * Lists all Comment models.
  * @return mixed
  */
 public function actionIndex()
 {
     $commentedOrders = Comment::find()->where(['user_id' => Yii::$app->user->id])->all();
     $orderIds = ArrayHelper::map($commentedOrders, 'order_id', 'order_id');
     $productIds = ArrayHelper::map($commentedOrders, 'order_id', 'product_id');
     if (count($orderIds) && count($productIds)) {
         $ids = [];
         foreach ($productIds as $orderId => $productId) {
             $orderProduct = OrderProduct::find()->where(['and', 'user_id = ' . Yii::$app->user->id, 'order_id = ' . $orderId, 'product_id = ' . $productId])->one();
             $ids[] = $orderProduct->id;
         }
         $query = OrderProduct::find()->where(['and', 'user_id = ' . Yii::$app->user->id, ['not in', 'id', $ids]]);
     } else {
         $query = OrderProduct::find()->where(['user_id' => Yii::$app->user->id]);
     }
     $query->orderBy(['created_at' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => Yii::$app->params['defaultPageSizeOrder']], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     return $this->render('index', ['models' => $dataProvider->getModels(), 'pagination' => $dataProvider->pagination]);
 }
Example #5
0
 public function actionAjaxStatus($id, $status)
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $model = $this->findModel($id);
     if ($model) {
         $oldStatus = $model->status;
         $model->status = $status;
         $model->save();
         // 记录订单日志
         $orderLog = new OrderLog(['order_id' => $model->id, 'status' => $model->status]);
         $orderLog->save();
         // 如果订单为取消,则恢复对应的库存
         if ($oldStatus > 0 && $status == Order::STATUS_CANCEL) {
             $orderProducts = OrderProduct::find()->where(['order_id' => $model->id])->all();
             foreach ($orderProducts as $product) {
                 Product::updateAllCounters(['stock' => $product->number], ['id' => $product->product_id]);
             }
         }
         return ['status' => 1];
     }
     return ['status' => -1];
 }
Example #6
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getOrderProducts()
 {
     return $this->hasMany(OrderProduct::className(), ['order_id' => 'id']);
 }
Example #7
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getOrderProduct()
 {
     return $this->hasOne(OrderProduct::className(), ['id' => 'order_product_id']);
 }
Example #8
0
 public static function TrialExist()
 {
     return OrderProduct::find()->where(['modelClass' => 'Package', 'mask' => 1, 'created_by' => Yii::$app->user->id])->exists();
 }
Example #9
0
 public function actionCheckout()
 {
     Yii::$app->session['step'] = 2;
     $userId = Yii::$app->user->id;
     $addresses = Address::find()->where(['user_id' => $userId])->all();
     $model = new Order();
     if ($model->load(Yii::$app->request->post())) {
         if (!Yii::$app->request->post('address_id')) {
             return $this->goBack();
         }
         // 使用优惠券
         $feeCouponUser = $feeCouponCode = $feePoint = 0.0;
         $couponId = Yii::$app->request->post('coupon');
         if ($couponId && Yii::$app->request->post('checkbox-coupon')) {
             $couponUser = Coupon::findOne($couponId);
             if (!$couponUser || $couponUser->used_at > 0 || $couponUser->ended_at < time() || $couponUser->user_id != Yii::$app->user->id) {
                 return $this->goBack();
             }
             $feeCouponUser = $couponUser->money;
         }
         // 使用优惠码
         $sn = Yii::$app->request->post('sn');
         if ($sn) {
             $couponCode = Coupon::find()->where(['sn' => $sn])->one();
             if (!$couponCode || $couponCode->used_at > 0 || $couponCode->ended_at < time()) {
                 return $this->goBack();
             }
             $feeCouponCode = $couponCode->money;
         }
         // 使用积分
         $point = Yii::$app->request->post('point');
         if ($point && Yii::$app->request->post('checkbox-point')) {
             if ($point > Yii::$app->user->identity->point) {
                 return $this->goBack();
             }
             $feePoint = intval($point) / 100;
         }
         $address = Address::find()->where(['id' => Yii::$app->request->post('address_id'), 'user_id' => $userId])->one();
         $model->user_id = $userId;
         $model->sn = date('YmdHis') . rand(1000, 9999);
         $model->consignee = $address->consignee;
         $model->country = $address->country;
         $model->province = $address->province;
         $model->city = $address->city;
         $model->district = $address->district;
         $model->address = $address->address;
         $model->zipcode = $address->zipcode;
         $model->phone = $address->phone;
         $model->mobile = $address->mobile;
         $model->email = $address->email ? $address->email : Yii::$app->user->identity->email;
         if ($model->payment_method == Order::PAYMENT_METHOD_COD) {
             $model->payment_status = Order::PAYMENT_STATUS_COD;
         } else {
             $model->payment_status = Order::PAYMENT_STATUS_UNPAID;
         }
         $model->status = $model->payment_status;
         $products = Cart::find()->where(['session_id' => Yii::$app->session->id])->all();
         if (count($products)) {
             foreach ($products as $product) {
                 $model->amount += $product->number * $product->price;
             }
         } else {
             $this->redirect('/cart');
         }
         $model->amount += floatval($model->shipment_fee) - $feeCouponUser - $feeCouponCode - $feePoint;
         if ($model->save()) {
             // insert order_product and clear cart
             foreach ($products as $product) {
                 $orderProduct = new OrderProduct();
                 $orderProduct->order_id = $model->id;
                 $orderProduct->product_id = $product->product_id;
                 $orderProduct->sku = $product->sku;
                 $orderProduct->name = $product->name;
                 $orderProduct->number = $product->number;
                 $orderProduct->market_price = $product->market_price;
                 $orderProduct->price = $product->price;
                 $orderProduct->thumb = $product->thumb;
                 $orderProduct->type = $product->type;
                 $orderProduct->save();
                 // 减少商品的库存
                 Product::updateAllCounters(['stock' => -$product->number], ['id' => $product->product_id]);
             }
             // 生成订单后,清空购物车,设置优惠码,更新积分和积分记录
             Cart::deleteAll(['session_id' => Yii::$app->session->id]);
             if (isset($couponUser) && Yii::$app->request->post('checkbox-coupon')) {
                 $couponUser->used_at = time();
                 $couponUser->order_id = $model->id;
                 $couponUser->save();
             }
             if (isset($couponCode) && Yii::$app->request->post('checkbox-coupon')) {
                 $couponCode->user_id = Yii::$app->user->id;
                 $couponCode->used_at = time();
                 $couponCode->order_id = $model->id;
                 $couponCode->save();
             }
             if (isset($point) && Yii::$app->request->post('checkbox-point')) {
                 $balance = Yii::$app->user->identity->point - $point;
                 User::updateAllCounters(['point' => -$point], ['id' => Yii::$app->user->id]);
                 $pointLog = new PointLog(['user_id' => Yii::$app->user->id, 'type' => PointLog::POINT_TYPE_BUYING, 'point' => -$point, 'balance' => $balance]);
                 $pointLog->save();
             }
             // 记录订单日志
             $orderLog = new OrderLog(['order_id' => $model->id, 'status' => $model->status]);
             $orderLog->save();
             // 不同的付款方式到不同的页面
             if ($model->payment_method == Order::PAYMENT_METHOD_COD) {
                 return $this->redirect(['cart/cod', 'id' => $model->id]);
             } else {
                 return $this->redirect(['cart/pay', 'sn' => $model->sn]);
             }
         }
     }
     $products = Cart::find()->where(['session_id' => Yii::$app->session->id])->all();
     if (!count($products)) {
         return $this->redirect('/cart');
     }
     if (count($addresses)) {
         return $this->render('checkout', ['model' => $model, 'addresses' => $addresses, 'products' => $products]);
     } else {
         return $this->redirect(['cart/address']);
     }
 }