예제 #1
0
 public function buyCardAction()
 {
     $this->view->setTitle('Покупка карты');
     $bread = [];
     $bread[] = ['text' => 'Покупка карты'];
     $user = Users\User::$cur;
     if (!empty($_POST) && !empty($_POST['card_id'])) {
         $error = false;
         $card = \Ecommerce\Card::get((int) $_POST['card_id']);
         if (!$card) {
             $error = true;
             Msg::add('Такой карты не существует', 'danger');
         }
         if (!Users\User::$cur->id) {
             $user_id = $this->Users->registration($_POST, true);
             if (!$user_id) {
                 $error = true;
                 $user = null;
             } else {
                 $user = Users\User::get($user_id);
             }
         }
         $userCard = \Ecommerce\Card\Item::get([['card_id', $card->id], ['user_id', $user->id]]);
         if ($userCard) {
             $error = true;
             Msg::add('У вас уже есть такая карта', 'danger');
         }
         $fields = \Ecommerce\UserAdds\Field::getList();
         foreach ($fields as $field) {
             if (empty($_POST['userAdds']['fields'][$field->id]) && $field->required) {
                 $error = 1;
                 Msg::add('Вы не указали: ' . $field->name);
             }
         }
         if (!$error) {
             $cardItem = new \Ecommerce\Card\Item();
             $cardItem->card_id = $card->id;
             $cardItem->user_id = $user->id;
             $cardItem->save();
             $cart = new \Ecommerce\Cart();
             $cart->user_id = $user->user_id;
             $cart->cart_status_id = 2;
             $cart->comment = htmlspecialchars($_POST['comment']);
             $cart->date_status = date('Y-m-d H:i:s');
             $cart->complete_data = date('Y-m-d H:i:s');
             if (!empty($_SESSION['cart']['cart_id'])) {
                 $cart->card_item_id = $cardItem->id;
             }
             $cart->save();
             $this->module->parseFields($_POST['userAdds']['fields'], $cart);
             $extra = new \Ecommerce\Cart\Extra();
             $extra->name = $card->name;
             $extra->price = $card->price;
             $extra->count = 1;
             $extra->cart_id = $cart->id;
             $extra->info = 'card:' . $card->id . '|cardItem:' . $cardItem->id;
             $extra->save();
             Tools::redirect('/ecommerce/cart/success');
         }
     }
     $this->view->page(['data' => compact('bread')]);
 }
예제 #2
0
파일: Ecommerce.php 프로젝트: krvd/cms-Inji
 public function getCurCart($create = true)
 {
     $cart = false;
     if (!empty($_SESSION['cart']['cart_id'])) {
         $cart = Ecommerce\Cart::get((int) $_SESSION['cart']['cart_id']);
     }
     if (!$cart && $create) {
         $cart = new Ecommerce\Cart();
         $cart->cart_status_id = 1;
         $cart->user_id = Users\User::$cur->id;
         $userCard = \Ecommerce\Card\Item::get(\Users\User::$cur->id, 'user_id');
         if ($userCard) {
             $cart->card_item_id = $userCard->id;
         }
         $cart->save();
         $_SESSION['cart']['cart_id'] = $cart->id;
     }
     return $cart;
 }
예제 #3
0
 public function indexAction()
 {
     $cart = '';
     $deliverys = \Ecommerce\Delivery::getList(['order' => ['weight', 'ASC']]);
     $payTypes = \Ecommerce\PayType::getList(['order' => ['weight', 'ASC']]);
     if (!empty($_SESSION['cart']['cart_id'])) {
         $cart = Ecommerce\Cart::get($_SESSION['cart']['cart_id']);
         if (!empty($_POST)) {
             $error = false;
             if (!Users\User::$cur->id) {
                 $user_id = $this->Users->registration($_POST, true);
                 if (!$user_id) {
                     $error = true;
                 } else {
                     $user = Users\User::get($user_id);
                 }
             } else {
                 $user = Users\User::$cur;
             }
             $ids = [];
             if (!empty($_POST['cartItems'])) {
                 foreach ($_POST['cartItems'] as $cartItemId => $cartItemCont) {
                     $cartItem = \Ecommerce\Cart\Item::get((int) $cartItemId);
                     if (!$cartItem) {
                         continue;
                     }
                     if ($cartItem->cart_id != $cart->id) {
                         continue;
                     }
                     $count = (double) $cartItemCont;
                     if ($count < 0.001) {
                         $count = 1;
                     }
                     $cartItem->count = $count;
                     $cartItem->save();
                     $ids[] = $cartItemId;
                 }
             }
             foreach ($cart->cartItems as $cartItem) {
                 if (!in_array($cartItem->id, $ids)) {
                     $cartItem->delete();
                 }
             }
             $cart = Ecommerce\Cart::get($cart->id);
             if (!$cart->cartItems) {
                 Tools::redirect('/ecommerce', 'Ваша корзина пуста');
             }
             if (empty($this->module->config['sell_over_warehouse'])) {
                 foreach ($cart->cartItems as $cartitem) {
                     $warecount = $cartitem->price->offer->warehouseCount($cart->id);
                     if ($cartitem->count > $warecount) {
                         $error = true;
                         Msg::add('Вы заказали <b>' . $cartitem->item->name . '</b> больше чем есть на складе. на складе: <b>' . $warecount . '</b>', 'danger');
                     }
                 }
             }
             if ($deliverys && empty($deliverys[$_POST['delivery']])) {
                 $error = 1;
                 Msg::add('Выберите способ доставки');
             } elseif ($deliverys && !empty($deliverys[$_POST['delivery']])) {
                 $cart->delivery_id = $_POST['delivery'];
                 foreach ($deliverys[$cart->delivery_id]->fields as $field) {
                     if (empty($_POST['deliveryFields'][$field->id]) && $field->required) {
                         $error = 1;
                         Msg::add('Вы не указали: ' . $field->name);
                     }
                 }
             }
             if ($payTypes && empty($payTypes[$_POST['payType']])) {
                 $error = 1;
                 Msg::add('Выберите способ оплаты');
             } elseif ($payTypes && !empty($payTypes[$_POST['payType']])) {
                 $payType = $payTypes[$_POST['payType']];
                 $cart->paytype_id = $payType->id;
             } else {
                 $payType = null;
             }
             foreach (\Ecommerce\UserAdds\Field::getList() as $field) {
                 if (empty($_POST['userAdds']['fields'][$field->id]) && $field->required) {
                     $error = 1;
                     Msg::add('Вы не указали: ' . $field->name);
                 }
             }
             if (!empty($_POST['discounts']['card_item_id'])) {
                 $userCard = \Ecommerce\Card\Item::get((int) $_POST['discounts']['card_item_id']);
                 if (!$userCard) {
                     $error = true;
                     Msg::add('Такой карты не существует');
                 } elseif ($userCard->user_id != $user->id) {
                     $error = true;
                     Msg::add('Это не ваша карта');
                 } else {
                     $cart->card_item_id = $userCard->id;
                 }
             }
             $cart->save();
             if (!$error && !empty($_POST['action']) && ($_POST['action'] = 'order')) {
                 $cart->user_id = $user->user_id;
                 $this->module->parseFields($_POST['userAdds']['fields'], $cart);
                 if ($payTypes && !empty($payTypes[$cart->paytype_id]) && !empty($_POST['deliveryFields'])) {
                     $this->module->parseDeliveryFields($_POST['deliveryFields'], $cart, $deliverys[$cart->delivery_id]->fields);
                 }
                 $cart->cart_status_id = 2;
                 $cart->comment = htmlspecialchars($_POST['comment']);
                 $cart->date_status = date('Y-m-d H:i:s');
                 $cart->complete_data = date('Y-m-d H:i:s');
                 $cart->warehouse_block = 1;
                 $cart->save();
                 $cart = \Ecommerce\Cart::get($cart->id);
                 foreach ($cart->cartItems as $cartItem) {
                     $cartItem->discount = $cartItem->discount();
                     $cartItem->final_price = $cartItem->price->price - $cartItem->discount;
                     $cartItem->save();
                 }
                 $cart = \Ecommerce\Cart::get($cart->id);
                 if (!empty(\App::$cur->ecommerce->config['notify_mail'])) {
                     $text = 'Перейдите в админ панель чтобы просмотреть новый заказ <a href = "http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/admin/ecommerce/Cart">Админ панель</a>';
                     $title = 'Новый заказ в интернет магазине на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME);
                     \Tools::sendMail('noreply@' . INJI_DOMAIN_NAME, \App::$cur->ecommerce->config['notify_mail'], $title, $text);
                 }
                 if ($this->notifications) {
                     $notification = new Notifications\Notification();
                     $notification->name = 'Новый заказ в интернет магазине на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME);
                     $notification->text = 'Перейдите в админ панель чтобы просмотреть новый заказ';
                     $notification->chanel_id = $this->notifications->getChanel('Ecommerce-orders')->id;
                     $notification->save();
                 }
                 $handlers = $this->ecommerce->getSnippets('payTypeHandler');
                 $redirect = ['/ecommerce/cart/success'];
                 if ($payType && !empty($handlers[$payType->handler]['handler'])) {
                     $newRedirect = $handlers[$payType->handler]['handler']($cart);
                     if (!empty($newRedirect)) {
                         $redirect = $newRedirect;
                     }
                 }
                 unset($_SESSION['cart']['cart_id']);
                 call_user_func_array(['Tools', 'redirect'], $redirect);
             }
         }
     }
     $this->view->setTitle('Корзина');
     $bread = [];
     $bread[] = ['text' => 'Каталог', 'href' => '/ecommerce'];
     $bread[] = ['text' => 'Корзина', 'href' => '/ecommerce/cart'];
     $this->view->page(['data' => compact('cart', 'items', 'deliverys', 'payTypes', 'packItem', 'bread')]);
 }