Exemplo n.º 1
0
 public function actionOrder()
 {
     $order = new Order();
     /** @var ShoppingCart $cart */
     $cart = \Yii::$app->cart;
     /* @var $products Product[] */
     $products = $cart->getPositions();
     if ($order->load(\Yii::$app->request->post()) && $order->validate()) {
         $transaction = $order->getDb()->beginTransaction();
         $order->save(false);
         // part of the transaction;
         foreach ($products as $product) {
             $orderItem = new OrderItem();
             $orderItem->order_id = $order->id;
             $orderItem->product_id = $product->id;
             $orderItem->title = $product->title;
             $orderItem->price = $product->getPrice();
             $orderItem->quantity = $product->getQuantity();
             if (!$orderItem->save(false)) {
                 $transaction->rollBack();
                 Yii::$app->session->setFlash('error', 'Your order cannot be accepted. Please contact us!');
                 $this->goBack();
             }
         }
         $transaction->commit();
         Yii::$app->cart->removeAll();
         Yii::$app->session->addFlash('message', 'Thanks for your order. We\'ll contact you soon!');
         //            $order->sendEmail();
         $this->redirect('/');
     }
     return $this->render('order', ['order' => $order, 'products' => $products, 'total' => $cart->getCost()]);
 }
Exemplo n.º 2
0
 public function actionOrder()
 {
     $order = new Order();
     /* @var $cart ShoppingCart */
     $cart = \Yii::$app->cart;
     /* @var $products Product[] */
     $products = $cart->getPositions();
     $total = $cart->getCost();
     if ($order->load(\Yii::$app->request->post()) && $order->validate()) {
         $transaction = $order->getDb()->beginTransaction();
         $order->save(false);
         foreach ($products as $product) {
             $orderItem = new OrderItem();
             $orderItem->order_id = $order->id;
             $orderItem->title = $product->title;
             $orderItem->price = $product->getPrice();
             $orderItem->product_id = $product->id;
             $orderItem->quantity = $product->getQuantity();
             if (!$orderItem->save(false)) {
                 $transaction->rollBack();
                 \Yii::$app->session->addFlash('error', 'Cannot place your order. Please contact us.');
                 return $this->redirect('catalog/list');
             }
         }
         $transaction->commit();
         \Yii::$app->cart->removeAll();
         \Yii::$app->session->addFlash('success', 'Thanks for your order. We\'ll contact you soon.');
         $order->sendEmail();
         return $this->redirect('catalog/list');
     }
     return $this->render('order', ['order' => $order, 'products' => $products, 'total' => $total]);
 }
Exemplo n.º 3
0
 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]);
     }
 }
 /**
  * Creates a new Order model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Order();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $redirect = yii::$app->request->post('goto') == 'list' ? ['index'] : ['update', 'id' => $model->id];
         return $this->redirect($redirect);
     }
     return $this->render('create', ['model' => $model]);
 }
Exemplo n.º 5
0
 /**
  * Creates a new Order model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Order();
     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 actionCreate()
 {
     $model = new Order();
     $model->send_at = date("Y-m-d H:i:s");
     $model->is_new = 0;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->render('thank');
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 7
0
 /**
  * Creates a new Order 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 Order();
     $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]);
     }
 }
Exemplo n.º 8
0
 public function actionOrder()
 {
     $cartPositions = Yii::$app->cart->getPositions();
     if (!$cartPositions or $cartPositions === null) {
         return $this->redirect(['index']);
     }
     $dataProvider = new ArrayDataProvider(['allModels' => $cartPositions]);
     $model = new Order();
     if ($model->load(Yii::$app->request->post())) {
         $model->status = 1;
         $model->total_cost = Yii::$app->cart->getCost();
         $model->date = date('Y-m-d H:i');
         $model->data = Yii::$app->cart->getSerialized();
         if ($model->validate() && $model->save()) {
             Yii::$app->cart->removeAll();
             return $this->render('orderSuccess', ['model' => $model]);
         }
     }
     return $this->render('order', ['model' => $model, 'dataProvider' => $dataProvider]);
 }
Exemplo n.º 9
0
 public function actionCartFormSubmit()
 {
     if (Yii::$app->request->isPost) {
         $model_form = new Order();
         $model_form->load(Yii::$app->request->post());
         $model_form->scenario = 'submit';
         $model_form->payment_type = Yii::$app->request->post('payment') ? Yii::$app->request->post('payment') : 0;
         //$model_form->validate();
         //vd( $model_form->getErrors());
         $model_form->status = 1;
     }
     if ($model_form->load(Yii::$app->request->post()) && $model_form->validate()) {
         $model_form->save();
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         Yii::$app->session->setFlash('success', 'Благодарим за покупку');
         //Todo сохранить заказы !!!
         $iP = Yii::$app->session->id;
         $model = Cart::getAllByIp($iP);
         if ($model) {
             //vd($model_form->created_at);
             $listItemsInOrder = OrderItems::Add($model, $model_form->id, $model_form->created_at);
             //vd($listItemsInOrder);
         }
         //Todo отправка email !!!
         // Todo отправка ел почты
         //$model_Email = Email::find()->where(['id' => 1])->one();
         // $currEmail = $model_Email->email;
         //vd($model->email);
         Yii::$app->mailer->compose(['html' => '@common/mail/order'], ['order_id' => $model_form->id, 'listItemsInOrder' => $listItemsInOrder])->setFrom('*****@*****.**')->setTo('*****@*****.**')->setSubject('нОвый заказ')->send();
         //Todo Очистить корзину
         Cart::DeleteAllByIp($iP);
         $this->redirect('/site/index');
     } else {
         // либо страница отображается первый раз, либо есть ошибка в данных
         $data = $this->getCommonDate();
         $model_form = new Order();
         $iP = Yii::$app->session->id;
         $model = Cart::getAllByIp($iP);
     }
     return $this->render('cart', ['data' => $data, 'model' => $model, 'model_form' => $model_form]);
 }
Exemplo n.º 10
0
 public function actionCreate()
 {
     $out = ['status' => 'err', 'error' => 'Unknown error'];
     $order = new Order();
     $r = new Request();
     if ($order->load($r->post())) {
         $order->price = $order->price * 100;
         // $user = \Yii::$app->user;
         if (\Yii::$app->user->isGuest) {
             if ($user = User::findByEmail($r->post('SignupForm')['email'])) {
                 $order->user_id = $user->getId();
             } else {
                 $signup = new SignupForm();
                 if ($signup->load($r->post())) {
                     $signup->username = $signup->email;
                     $signup->password = User::MakePass(7);
                     if ($user = $signup->signup()) {
                         $order->user_id = $user->getId();
                     } else {
                         $out['error'] = array_values($signup->errors)[0][0];
                     }
                 } else {
                     //vd($signup->errors,1);
                 }
             }
         } else {
             $order->user_id = \Yii::$app->user->getId();
         }
         if ($order->validate() && $order->save()) {
             //vd([$r->post(),$order->attributes]);
             $out = ['status' => 'ok', 'order' => $order->id, 'user' => $order->user_id, 'sum' => $order->price / 100];
         } else {
             $out['error'] = array_values($order->errors)[0][0];
             //vd($order->errors);
         }
     }
     //vd($out);
     \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     return $out;
     //return BaseJson::encode($out,JSON_PRETTY_PRINT);
     /*vd(['get' => $r->getQueryParams() ,
       'post' => $r->post(),
       'order' => $order],1); //*/
 }
Exemplo n.º 11
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']);
     }
 }