/** * Creates a new OrderLog model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { //if(!Yii::$app->user->can('createYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth')); $model = new OrderLog(); $model->loadDefaultValues(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
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]; }
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']); } }