Esempio n. 1
0
 public function actionOrder()
 {
     $order = new Orders();
     /* @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->order_id;
             $orderItem->item_id = $product->item_id;
             $orderItem->price = $product->getPrice();
             $orderItem->quantity = $product->getQuantity();
             if (!$orderItem->save(false)) {
                 $transaction->rollBack();
                 Yii::$app->session->addFlash('error', 'Cannot place your order. Please contact us.');
                 // TODO: may need to add Yii::t()
                 return $this->redirect('../');
                 // redirect to web root
             }
         }
         $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('../');
         // redirect to web root
     }
     return $this->render('order', ['order' => $order, 'products' => $products, 'total' => $total]);
 }
Esempio n. 2
0
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $createanotherone = Input::only(['createanother']);
     //2. validate order fields
     $orderInput = Input::only(['specialinstruction', 'approval']);
     $orderValidator = Validator::make($orderInput, $this->rules());
     $errors = [];
     if ($orderValidator->fails()) {
         $errors[] = $orderValidator->errors();
     }
     //order Items
     $orderitemRules = $this->orderitemrules();
     $orderItems = Input::get('orderitems');
     //1. validate order items information
     foreach ($orderItems as $item) {
         $item['estimatedprice'] = preg_replace("/[^0-9\\.]/", "", $item['estimatedprice']);
         $item['estimatedtotal'] = preg_replace("/[^0-9\\.]/", "", $item['estimatedtotal']);
         $item['finalprice'] = preg_replace("/[^0-9\\.]/", "", $item['finalprice']);
         $item['finaltotal'] = preg_replace("/[^0-9\\.]/", "", $item['finaltotal']);
         $orderItemValidator = Validator::make($item, $orderitemRules);
         if ($orderItemValidator->fails()) {
             $errors[] = $orderItemValidator->errors();
         }
     }
     if (count($errors)) {
         dd($errors);
         return Redirect::back()->withInput()->withErrors($errors);
     }
     //save order
     $newOrder = new Orders();
     $newOrder->user_id = $this->user->id;
     $newOrder->specialinstruction = $orderInput['specialinstruction'];
     $newOrder->status = 1;
     $newOrder->save();
     $error = 0;
     // save order items
     foreach ($orderItems as $item) {
         $orderItemObj = new OrderItems(['category_id' => $item['category'], 'vendor_id' => $item['vendor'], 'order_id' => $newOrder->id, 'name' => $item['name'], 'description' => $item['description'], 'quantity' => $item['quantity'], 'estimatedprice' => preg_replace("/[^0-9\\.]/", "", $item['estimatedprice']), 'estimatedtotal' => preg_replace("/[^0-9\\.]/", "", $item['estimatedtotal']), 'fixedprice' => preg_replace("/[^0-9\\.]/", "", $item['finalprice']), 'fixedtotal' => preg_replace("/[^0-9\\.]/", "", $item['finaltotal']), 'status' => 1]);
         if (!$newOrder->orderitems()->save($orderItemObj)) {
             $error++;
         }
         $approval = Approvals::find(5);
         dd($approval->orderItems()->attach($orderItemObj->id));
         if ($orderItemObj->approval()->create(['orderitem_id' => $orderItemObj->id, 'approval_id' => 5, 'approval_user_id' => Input::get('approval')])->save()) {
             $error++;
         }
         //save the approval
     }
     if ($error) {
         //remove the order
         $newOrder->orderItems()->delete();
         $newOrder->delete();
         return Redirect::to('admin/orders')->withErrors(trans('message.msg_order_create_fail'));
     }
     if ($createanotherone) {
         return Redirect::to('admin/orders/create')->withSuccess(trans('message.msg_order_create_success'));
     }
     return Redirect::to('admin/orders')->withSuccess(trans('message.msg_order_create_success'));
 }
Esempio n. 3
0
 /**
  * Creates a new Orders model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Orders();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->ID]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * Creates a new Users model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Orders();
     //        $model = new Users(['scenario' =>'login']);
     //        error_log(print_r($model,1));
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Esempio n. 5
0
 /**
  * input user data
  * @param $id_tour
  * @param $id_booking
  * @return string
  */
 public function actionRegistration($id_tour, $id_booking)
 {
     $model = new Orders();
     if (Yii::$app->request->isPost) {
         $model->load(Yii::$app->request->post());
         $model->id_tour = $id_tour;
         $model->id_booking = $id_booking;
         if ($model->save()) {
             $this->redirect('/site/success');
         }
     }
     return $this->render('registration', compact('model'));
 }
Esempio n. 6
0
 public function actionCreate($date)
 {
     $model = new Orders();
     $clients = new Clients();
     if ($model->load(Yii::$app->request->post())) {
         $client = $clients->findOne($model->clientId);
         $model->date = $date;
         $model->managerName = Yii::$app->user->identity->full_name;
         $model->driverId = $client->driverId;
         $model->save();
         return true;
     }
     return $this->renderAjax('form', ['model' => $model, 'clients' => $clients, 'delivery' => ArrayHelper::map(Delivery::find()->all(), 'id', 'name'), 'action' => 'create']);
 }
Esempio n. 7
0
 public function actionAddnew()
 {
     $orderNow = orders::find()->where(['active' => 1])->one();
     if ($orderNow == null) {
         $post_order = Yii::$app->request->post()['Orders'];
         $order = new Orders($post_order);
         $order->time_begin = date("Y-m-d H:i:s");
         $order->active = 1;
         $order->save();
         $resultAdd = 3;
     } else {
         $resultAdd = 2;
     }
     Yii::$app->response->redirect(array('site/index', 'resultAdd' => $resultAdd));
 }
 /**
  * Creates a new Orders model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($tour_id)
 {
     $model = new Orders();
     $tour_meta = new ToursMeta();
     $tour_meta = $tour_meta->getFields($tour_id);
     $tour = Tours::findOne($tour_id);
     $model->created = date('d.m.Y');
     $model->tour_id = $tour_id;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         foreach ($_POST['Meta'] as $meta => $val) {
             $order_meta = new OrderMeta();
             $order_meta->order_id = $model->id;
             $order_meta->meta_id = $meta;
             $order_meta->meta_val = $val;
             if (!$order_meta->save()) {
                 return $this->render('create', ['model' => $model, 'tour_meta' => $tour_meta, 'tour' => $tour]);
             }
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'tour_meta' => $tour_meta, 'tour' => $tour]);
     }
 }
Esempio n. 9
0
 public function actionOrder()
 {
     if (Yii::$app->session->get('ids')) {
         $model = new OrderForm();
         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             $order = new Orders();
             $order->delivery = $model->delivery;
             $ids = Yii::$app->session->get('ids');
             $id_count = Yii::$app->session->get('id_count');
             $s = "";
             for ($i = 0; $i < count($ids); $i++) {
                 $s .= "({$ids[$i]} [{$id_count[$i]}]), ";
             }
             $order->product_ids = $s;
             $order->price = Yii::$app->session->get('price');
             $order->email = Html::encode($model->email);
             $order->adress = Html::encode($model->adress);
             $order->notice = Html::encode($model->notice);
             $order->name = Html::encode($model->name);
             $order->phone = Html::encode($model->phone);
             $order->date_order = date("Y-m-d H:i:s");
             Yii::$app->mailer->compose()->setFrom('*****@*****.**')->setTo($model->email)->setSubject('Заказ товаров на mobzp.ua')->setTextBody('<article id="articles">
                             	<h2>Вы успешно заказали наушники</h2>
                             	<p>С вами свяжется наш оператор в течении полу часа и уточнит данные для отправки или место передачи товара.</p>
                             	<p>После отправки по новой почте, вам придет смс, что товар отправлен и вы сможете его получить в вашем отделении через несколько дней.</p>
                             	<p>Спасибо, что воспользовались услугами нашего магазина! Мы всегда рады положительным отзывам на страничке товара.</p>
                             </article>')->send();
             $order->save();
             return $this->render('ordersuccess');
         }
         return $this->render('order', ['model' => $model]);
     } else {
         return $this->redirect(Yii::$app->request->referrer);
     }
 }
Esempio n. 10
0
 public function actionAddProducts()
 {
     if (isset($_POST['product_id'])) {
         $product = Products::findOne(['id' => (int) $_POST['product_id']]);
         // забираем продукт по id
         if (empty($product)) {
             // получили ли мы продукт
             die('error on finding product');
         }
         // $this->redirect('/products/shop'); //error
     }
     if (isset($_POST['quantity']) && $_POST['quantity'] > 0) {
         // проверка
         $quantity = $_POST['quantity'];
     } else {
         die('error with quantity');
     }
     // $this->redirect('/products/shop'); //error
     if (empty(Yii::$app->user->identity->id)) {
         die('error with user');
     }
     // $this->redirect('/products/shop'); //error
     $order = Orders::findOne(['user_id' => Yii::$app->user->identity->id, 'confirm' => 0]);
     //найти не подтвержденный заказ
     if (empty($order)) {
         $order = new Orders();
         $order->user_id = Yii::$app->user->identity->id;
         $order->status = 0;
         $order->confirm = 0;
         $order->price = 0;
         $save = $order->save();
         if ($save === false) {
             die('error on creating order');
         }
         //$this->redirect('/products/shop'); //error
     }
     $orderProducts = $order->getProducts();
     //получить список продуктов этого заказа
     $productIdList = [];
     //заносим в массив id
     foreach ($orderProducts as $orderProduct) {
         $productIdList[] = $orderProduct->id;
     }
     if (in_array($product->id, $productIdList)) {
         //если продукт в массиве то обновляем данные, иначе добавляем
         $add = $order->updateProducts($product->id, $quantity);
     } else {
         $add = $order->addProducts($product->id, $quantity);
     }
     if ($add === false) {
         die('error on adding products to order');
     }
     //$this->redirect('/products/shop'); //error
     $order->price = round($order->calculateGrandTotal());
     //считаем полную стоимость заказа
     if ($order->save()) {
         $this->redirect('cart');
     } else {
         die('error on saving order');
     }
     //$this->redirect('/products/shop'); //error
 }