diff --git a/admin-dev/tabs/AdminCarts.php b/admin-dev/tabs/AdminCarts.php deleted file mode 100644 index 9bfdd32a8..000000000 --- a/admin-dev/tabs/AdminCarts.php +++ /dev/null @@ -1,336 +0,0 @@ - -* @copyright 2007-2011 PrestaShop SA -* @version Release: $Revision: 7310 $ -* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) -* International Registered Trademark & Property of PrestaShop SA -*/ - -include_once(_PS_ADMIN_DIR_.'/../classes/AdminTab.php'); - -class AdminCarts extends AdminTab -{ - public function __construct() - { - $this->table = 'cart'; - $this->className = 'Cart'; - $this->lang = false; - $this->edit = false; - $this->view = true; - $this->delete = true; - - $this->_select = 'CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) `customer`, a.id_cart total, ca.name carrier, o.id_order '; - $this->_join = 'LEFT JOIN '._DB_PREFIX_.'customer c ON (c.id_customer = a.id_customer) - LEFT JOIN '._DB_PREFIX_.'currency cu ON (cu.id_currency = a.id_currency) - LEFT JOIN '._DB_PREFIX_.'carrier ca ON (ca.id_carrier = a.id_carrier) - LEFT JOIN '._DB_PREFIX_.'orders o ON (o.id_cart = a.id_cart) '; - - $this->fieldsDisplay = array( - 'id_cart' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), - 'id_order' => array('title' => $this->l('ID Order'), 'align' => 'center', 'width' => 25), - 'customer' => array('title' => $this->l('Customer'), 'width' => 80, 'filter_key' => 'c!lastname'), - 'total' => array('title' => $this->l('Total'), 'callback' => 'getOrderTotalUsingTaxCalculationMethod', 'orderby' => false, 'search' => false, 'width' => 50, 'align' => 'right', 'prefix' => '', 'suffix' => '', 'currency' => true), - 'carrier' => array('title' => $this->l('Carrier'), 'width' => 25, 'align' => 'center', 'callback' => 'replaceZeroByShopName', 'filter_key' => 'ca!name'), - 'date_add' => array('title' => $this->l('Date'), 'width' => 90, 'align' => 'right', 'type' => 'datetime', 'filter_key' => 'a!date_add')); - $this->shopLinkType = 'shop'; - - parent::__construct(); - } - - public function viewDetails() - { - if (!($cart = $this->loadObject(true))) - return; - $customer = new Customer($cart->id_customer); - $customerStats = $customer->getStats(); - $products = $cart->getProducts(); - $customizedDatas = Product::getAllCustomizedDatas((int)($cart->id)); - Product::addCustomizationPrice($products, $customizedDatas); - $summary = $cart->getSummaryDetails(); - $discounts = $cart->getDiscounts(); - $currency = new Currency($cart->id_currency); - - // display cart header - echo '

'.(($customer->id) ? $customer->firstname.' '.$customer->lastname : $this->l('Guest')).' - '.$this->l('Cart #').sprintf('%06d', $cart->id).' '.$this->l('from').' '.$cart->date_upd.'

'; - - /* Display customer information */ - echo ' -
-
-
- '.$this->l('Customer information').' - '; - if ($customer->id) - echo ' - '.$customer->firstname.' '.$customer->lastname.' ('.$this->l('#').$customer->id.')
- ('.$customer->email.')

- '.$this->l('Account registered:').' '.Tools::displayDate($customer->date_add, $this->context->language->id, true).'
- '.$this->l('Valid orders placed:').' '.$customerStats['nb_orders'].'
- '.$this->l('Total paid since registration:').' '.Tools::displayPrice($customerStats['total_orders'], $currency, false).'
'; - else - echo $this->l('Guest not registered').''; - echo '
'; - echo ' -
-
'; - - /* Display order information */ - $id_order = (int)(Order::getOrderByCartId($cart->id)); - $order = new Order($id_order); - - if ($order->getTaxCalculationMethod() == PS_TAX_EXC) - { - $total_products = $summary['total_products']; - $total_discount = $summary['total_discounts_tax_exc']; - $total_wrapping = $summary['total_wrapping_tax_exc']; - $total_price = $summary['total_price_without_tax']; - $total_shipping = $summary['total_shipping_tax_exc']; - } else { - $total_products = $summary['total_products_wt']; - $total_discount = $summary['total_discounts']; - $total_wrapping = $summary['total_wrapping']; - $total_price = $summary['total_price']; - $total_shipping = $summary['total_shipping']; - } - - echo ' -
- '.$this->l('Order information').' - '; - if ($order->id) - echo ' - '.$this->l('Order #').sprintf('%06d', $order->id).' -

- '.$this->l('Made on:').' '.Tools::displayDate($order->date_add, $this->context->language->id, true).'



'; - else - echo $this->l('No order created from this cart').''; - echo '
'; - echo ' -
'; - - - // List of products - echo ' -
-
- '.$this->l('Products').''.$this->l('Cart summary').' -
- - - - - - - - - '; - $tokenCatalog = Tools::getAdminToken('AdminCatalog'.(int)(Tab::getIdFromClassName('AdminCatalog')).(int)$this->context->employee->id); - foreach ($products as $k => $product) - { - if ($order->getTaxCalculationMethod() == PS_TAX_EXC) - { - $product_price = $product['price']; - $product_total = $product['total']; - } else { - $product_price = $product['price_wt']; - $product_total = $product['total_wt']; - } - - - $image = array(); - if (isset($product['id_product_attribute']) AND (int)($product['id_product_attribute'])) - $image = Db::getInstance()->getRow(' - SELECT id_image - FROM '._DB_PREFIX_.'product_attribute_image - WHERE id_product_attribute = '.(int)($product['id_product_attribute'])); - if (!isset($image['id_image'])) - $image = Db::getInstance()->getRow(' - SELECT id_image - FROM '._DB_PREFIX_.'image - WHERE id_product = '.(int)($product['id_product']).' AND cover = 1'); - - $productObj = new Product($product['id_product']); - - /* Customization display */ - $this->displayCustomizedDatas($customizedDatas, $product, $currency, $image, $tokenCatalog, $stock); - if ($product['cart_quantity'] > $product['customizationQuantityTotal']) - { - $imageProduct = new Image($image['id_image']); - echo ' - - - - - - - - '; - } - } - echo ' - - - - '; - - if ($summary['total_discounts'] != 0) - echo ' - - - - '; - if ($summary['total_wrapping'] > 0) - echo ' - - - - '; - if ($cart->getOrderTotal(true, Cart::ONLY_SHIPPING) > 0) - echo ' - - - - '; - echo ' - - - - -
 '.$this->l('Product').''.$this->l('UP').''.$this->l('Qty').''.$this->l('Stock').''.$this->l('Total').'
'.(isset($image['id_image']) ? cacheImage(_PS_IMG_DIR_.'p/'.$imageProduct->getExistingImgPath().'.jpg', - 'product_mini_'.(int)($product['id_product']).(isset($product['id_product_attribute']) ? '_'.(int)($product['id_product_attribute']) : '').'.jpg', 45, 'jpg') : '--').' - '.$product['name'].'
- '.($product['reference'] ? $this->l('Ref:').' '.$product['reference'] : '') - .(($product['reference'] AND $product['supplier_reference']) ? ' / '.$product['supplier_reference'] : '') - .'
'.Tools::displayPrice($product_price, $currency, false).''.((int)($product['cart_quantity']) - $product['customizationQuantityTotal']).''.$productObj->getStock(isset($product['id_product_attribute']) ? $product['id_product_attribute'] : 0).''.Tools::displayPrice($product_total, $currency, false).'
'.$this->l('Total products:').''.Tools::displayPrice($total_products, $currency, false).'
'.$this->l('Total vouchers:').''.Tools::displayPrice($total_discount, $currency, false).'
'.$this->l('Total gift-wrapping:').''.Tools::displayPrice($total_wrapping, $currency, false).'
'.$this->l('Total shipping:').''.Tools::displayPrice($total_shipping, $currency, false).'
'.$this->l('Total:').''.Tools::displayPrice($total_price, $currency, false).'
'; - - if (sizeof($discounts)) - { - echo ' - - - - - '; - - foreach ($discounts as $discount) - echo ' - - - - '; - echo ' -
'.$this->l('Discounts').''.$this->l('Discount name').''.$this->l('Value').'
'.$discount['name'].'- '.Tools::displayPrice($discount['value_real'], $currency, false).'
'; - } - echo '
'. - $this->l('According to the group of this customer, prices are printed:').' '.($order->getTaxCalculationMethod() == PS_TAX_EXC ? $this->l('tax excluded.') : $this->l('tax included.')).' -
'; - - // Cancel product - echo ' -
-
 
'; - } - - private function displayCustomizedDatas(&$customizedDatas, &$product, &$currency, &$image, $tokenCatalog, &$stock) - { - if (!($order = $this->loadObject(true))) - return; - - if (is_array($customizedDatas) AND isset($customizedDatas[(int)($product['id_product'])][(int)($product['id_product_attribute'])])) - { - if ($image = new Image($image['id_image'])) - echo ' - - '.($image->id ? cacheImage(_PS_IMG_DIR_.'p/'.$image->getExistingImgPath().'.jpg', - 'product_mini_'.(int)($product['id_product']).(isset($product['id_product_attribute']) ? '_'.(int)($product['id_product_attribute']) : '').'.jpg', 45, 'jpg') : '--').' - - '.$product['name'].''.(isset($product['attributes']) ? '
'.$product['attributes'] : '').'
- '.($product['reference'] ? $this->l('Ref:').' '.$product['reference'] : '') - .(($product['reference'] AND $product['supplier_reference']) ? ' / '.$product['supplier_reference'] : '') - .'
- '.Tools::displayPrice($product['price_wt'], $currency, false).' - '.$product['customizationQuantityTotal'].' - '.(int)($stock['quantity']).' - '.Tools::displayPrice($product['total_customization_wt'], $currency, false).' - '; - foreach ($customizedDatas[(int)($product['id_product'])][(int)($product['id_product_attribute'])] AS $customization) - { - echo ' - - '; - foreach ($customization['datas'] AS $type => $datas) - if ($type == Product::CUSTOMIZE_FILE) - { - $i = 0; - echo ''; - } - elseif ($type == Product::CUSTOMIZE_TEXTFIELD) - { - $i = 0; - echo ''; - } - echo ' - - '.$customization['quantity'].' - - - '; - } - } - } - - public function display() - { - if (isset($_GET['view'.$this->table])) - $this->viewDetails(); - else - { - $this->getList(Context::getContext()->language->id, !Tools::getValue($this->table.'Orderby') ? 'date_add' : NULL, !Tools::getValue($this->table.'Orderway') ? 'DESC' : NULL); - $this->displayList(); - } - } - - protected function _displayDeleteLink($token = NULL, $id) - { - foreach ($this->_list as $cart) - if ($id == $cart['id_cart']) - if ($cart['id_order']) - return; - - $_cacheLang['Delete'] = $this->l('Delete', __CLASS__, true, false); - $_cacheLang['DeleteItem'] = $this->l('Delete item #', __CLASS__, true, false).$id.' ?)'; - - echo ' - - '.$_cacheLang['Delete'].' - '; - } -} - diff --git a/classes/Cart.php b/classes/Cart.php index d8200de28..f874f7d6a 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -1390,7 +1390,7 @@ class CartCore extends ObjectModel return Tools::displayError('This voucher is not yet valid'); if (strtotime($discountObj->date_to) < time()) return Tools::displayError('This voucher has expired.'); - if (!$discountObj->isAssociatedToShop($shop)) + if (!$discountObj->isAssociatedToShop((int)$shop->id)) return Tools::displayError('This voucher is not available with this shop.'); if (sizeof($discounts) >= 1 AND $checkCartDiscount) { diff --git a/classes/Controller.php b/classes/Controller.php index 267fa570f..31ef34641 100644 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -120,18 +120,20 @@ abstract class ControllerCore // postProcess handles ajaxProcess $this->postProcess(); - if ($this->display_header) - { - $this->setMedia(); - $this->initHeader(); - } - + $this->setMedia(); + $this->initHeader(); + $this->initContent(); - if ($this->display_footer) - $this->initFooter(); + $this->initFooter(); - if ($this->ajax && method_exists($this, 'displayAjax')) - $this->displayAjax(); + if ($this->ajax) + { + $action = Tools::getValue('action'); + if (!empty($action) && method_exists($this, 'displayAjax'.Tools::toCamelCase($action))) + $this->{'displayAjax'.$action}(); + elseif (method_exists($this, 'displayAjax')) + $this->displayAjax(); + } else $this->display(); } diff --git a/controllers/admin/AdminCartsController.php b/controllers/admin/AdminCartsController.php new file mode 100755 index 000000000..6a479fa62 --- /dev/null +++ b/controllers/admin/AdminCartsController.php @@ -0,0 +1,414 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 8971 $ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class AdminCartsController extends AdminController +{ + public function __construct() + { + $this->table = 'cart'; + $this->className = 'Cart'; + $this->lang = false; + $this->requiredDatabase = true; + + $this->addRowAction('view'); + $this->addRowAction('delete'); + + $this->_select = 'CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) `customer`, a.id_cart total, ca.name carrier, o.id_order, g.id_guest'; + $this->_join = 'LEFT JOIN '._DB_PREFIX_.'customer c ON (c.id_customer = a.id_customer) + LEFT JOIN '._DB_PREFIX_.'currency cu ON (cu.id_currency = a.id_currency) + LEFT JOIN '._DB_PREFIX_.'carrier ca ON (ca.id_carrier = a.id_carrier) + LEFT JOIN '._DB_PREFIX_.'orders o ON (o.id_cart = a.id_cart) + LEFT JOIN `'._DB_PREFIX_.'guest` g ON (a.id_guest = g.id_guest AND TIME_TO_SEC(TIMEDIFF(NOW(), c.`date_add`)) < 900)'; + + $this->fieldsDisplay = array( + 'id_cart' => array( + 'title' => $this->l('ID'), + 'align' => 'center', + 'width' => 25 + ), + 'id_order' => array( + 'title' => $this->l('ID Order'), + 'align' => 'center', 'width' => 25 + ), + 'customer' => array( + 'title' => $this->l('Customer'), + 'width' => 80, + 'filter_key' => 'c!lastname' + ), + 'total' => array( + 'title' => $this->l('Total'), + 'callback' => 'getOrderTotalUsingTaxCalculationMethod', + 'orderby' => false, + 'search' => false, + 'width' => 50, + 'align' => 'right', + 'prefix' => '', + 'suffix' => '', + 'currency' => true + ), + 'carrier' => array( + 'title' => $this->l('Carrier'), + 'width' => 25, + 'align' => 'center', + 'callback' => 'replaceZeroByShopName', + 'filter_key' => 'ca!name' + ), + 'date_add' => array( + 'title' => $this->l('Date'), + 'width' => 90, + 'align' => 'right', + 'type' => 'datetime', + 'filter_key' => 'a!date_add' + ), + 'id_guest' => array( + 'title' => $this->l('Online'), + 'width' => 25, + 'align' => 'center', + 'type' => 'bool', + 'filter_key' => 'guest', + 'tmpTableFilter' => true, + 'icon' => array(0 => 'blank.gif', 1 => 'tab-customers.gif') + ) + ); + $this->shopLinkType = 'shop'; + + parent::__construct(); + } + + public function viewCart() + { + if (!($cart = $this->loadObject(true))) + return; + $customer = new Customer($cart->id_customer); + $products = $cart->getProducts(); + $customizedDatas = Product::getAllCustomizedDatas((int)($cart->id)); + Product::addCustomizationPrice($products, $customizedDatas); + $summary = $cart->getSummaryDetails(); + $currency = new Currency($cart->id_currency); + + /* Display order information */ + $id_order = (int)(Order::getOrderByCartId($cart->id)); + $order = new Order($id_order); + + if ($order->getTaxCalculationMethod() == PS_TAX_EXC) + { + $total_products = $summary['total_products']; + $total_discounts = $summary['total_discounts_tax_exc']; + $total_wrapping = $summary['total_wrapping_tax_exc']; + $total_price = $summary['total_price_without_tax'];$total_shipping = $summary['total_shipping_tax_exc']; + } + else + { + $total_products = $summary['total_products_wt']; + $total_discounts = $summary['total_discounts']; + $total_wrapping = $summary['total_wrapping']; + $total_price = $summary['total_price']; + $total_shipping = $summary['total_shipping']; + } + foreach ($products as $k => &$product) + { + if ($order->getTaxCalculationMethod() == PS_TAX_EXC) + { + $product['product_price'] = $product['price']; + $product['product_total'] = $product['total']; + } + else + { + $product['product_price'] = $product['price_wt']; + $product['product_total'] = $product['total_wt']; + } + $image = array(); + if (isset($product['id_product_attribute']) AND (int)($product['id_product_attribute'])) + $image = Db::getInstance()->getRow('SELECT id_image + FROM '._DB_PREFIX_.'product_attribute_image + WHERE id_product_attribute = '.(int)($product['id_product_attribute'])); + if (!isset($image['id_image'])) + $image = Db::getInstance()->getRow('SELECT id_image + FROM '._DB_PREFIX_.'image + WHERE id_product = '.(int)($product['id_product']).' AND cover = 1'); + + $productObj = new Product($product['id_product']); + $product['qty_in_stock'] = StockAvailable::getStockAvailableForProduct($product['id_product'], isset($product['id_product_attribute']) ? $product['id_product_attribute'] : null, (int)$order->id_shop); + /* Customization display */ + //$this->displayCustomizedDatas($customizedDatas, $product, $currency, $image, $tokenCatalog, $stock); + $imageProduct = new Image($image['id_image']); + $product['image'] = (isset($image['id_image']) ? cacheImage(_PS_IMG_DIR_.'p/'.$imageProduct->getExistingImgPath().'.jpg', 'product_mini_'.(int)($product['id_product']).(isset($product['id_product_attribute']) ? '_'.(int)($product['id_product_attribute']) : '').'.jpg', 45, 'jpg') : '--'); + } + $this->context->smarty->assign(array('products' => $products, + 'discounts' => $cart->getDiscounts(), + 'order' => $order, + 'cart' => $cart, + 'currency' => $currency, + 'customer' => $customer, + 'customer_stats' => $customer->getStats(), + 'total_products' => $total_products, + 'total_discounts' => $total_discounts, + 'total_wrapping' => $total_wrapping, + 'total_price' => $total_price, + 'total_shipping' => $total_shipping + )); + } + + public function ajaxPreProcess() + { + $id_customer = (int)Tools::getValue('id_customer'); + $customer = new Customer((int)$id_customer); + $this->context->customer = $customer; + $id_cart = (int)Tools::getValue('id_cart'); + if (!$id_cart) + $id_cart = $customer->getLastCart(); + $this->context->cart = new Cart((int)$id_cart); + if (!$this->context->cart->id_customer) + $this->context->cart->id_customer = $id_customer; + if ($this->context->cart->OrderExists()) + return; + if (!$this->context->cart->id_lang) + $this->context->cart->id_lang = (($id_lang = (int)Tools::getValue('id_lang')) ? $id_lang : Configuration::get('PS_LANG_DEFAULT')); + if (!$this->context->cart->id_currency) + $this->context->cart->id_currency = (($id_currency = (int)Tools::getValue('id_currency')) ? $id_currency : Configuration::get('PS_CURRENCY_DEFAULT')); + + $addresses = $customer->getAddresses((int)$this->context->cart->id_lang); + $id_address_delivery = (int)Tools::getValue('id_address_delivery'); + $id_address_invoice = (int)Tools::getValue('id_address_delivery'); + + if (!$this->context->cart->id_address_invoice && isset($addresses[0])) + $this->context->cart->id_address_invoice = (int)$addresses[0]['id_address']; + elseif ($id_address_invoice) + $this->context->cart->id_address_invoice = (int)$id_address_invoice; + if (!$this->context->cart->id_address_delivery && isset($addresses[0])) + $this->context->cart->id_address_delivery = $addresses[0]['id_address']; + elseif ($id_address_delivery) + $this->context->cart->id_address_delivery = (int)$id_address_delivery; + + $this->context->cart->save(); + $currency = new Currency((int)$this->context->cart->id_currency); + $this->context->currency = $currency; + + } + + public function ajaxProcessDeleteProduct() + { + $errors = array(); + if (!$id_product = (int)Tools::getValue('id_product') OR !$id_product_attribute = (int)Tools::getValue('id_product_attribute')) + $errors[] = Tools::displayError('Invalid product'); + if (sizeof($errors)) + die(Tools::jsonEncode($errors)); + + if ($this->context->cart->deleteProduct($id_product, $id_product_attribute)) + echo Tools::jsonEncode($this->ajaxReturnVars()); + } + + public function ajaxProcessUpdateQty() + { + $errors = array(); + if (!$this->context->cart->id) + return; + if ($this->context->cart->OrderExists()) + $errors[] = Tools::displayErrors('An order already placed with this cart'); + elseif (!$id_product = (int)Tools::getValue('id_product') OR (!$product = new Product((int)$id_product, true, $this->context->language->id))) + $errors[] = Tools::displayError('Invalid product'); + elseif (!$qty = Tools::getValue('qty') OR $qty == 0) + $errors[] = Tools::displayError('Invalid quantity'); + elseif (($id_product_attribute = Tools::getValue('id_product_attribute')) != 0 && !Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty((int)$id_product_attribute, (int)$qty)) + $errors[] = Tools::displayError('There is not enough product in stock'); + elseif (!$product->checkQty((int)$qty)) + $errors[] = Tools::displayError('There is not enough product in stock'); + elseif (!$id_customization = (int)Tools::getValue('id_customization', 0) AND !$product->hasAllRequiredCustomizableFields()) + $errors[] = Tools::displayError('Please fill in all required fields'); + $this->context->cart->save(); + if (!count($errors)) + { + if (!($qty_upd = $this->context->cart->updateQty($qty, $id_product, (int)$id_product_attribute, (int)$id_customization, 'up'))) + $errors[] = Tools::displayError('You already have the maximum quantity available for this product.'); + } + + echo Tools::jsonEncode(array_merge($this->ajaxReturnVars(), array('errors' => $errors))); + + } + + public function ajaxProcessUpdateCarrier() + { + if(Validate::isBool(($recyclable = (int)Tools::getValue('recyclable')))) + $this->context->cart->recyclable = $recyclable; + if(Validate::isBool(($gift = (int)Tools::getValue('gift')))) + $this->context->cart->gift = $gift; + if(Validate::isMessage(($gift_message = pSQL(Tools::getValue('gift_message'))))) + $this->context->cart->gift_message = $gift_message; + $this->context->cart->save(); + echo Tools::jsonEncode($this->ajaxReturnVars()); + } + + public function ajaxProcessUpdateCurrency() + { + $currency = new Currency((int)Tools::getValue('id_currency')); + if (Validate::isLoadedObject($currency) && !$currency->deleted && $currency->active) + { + $this->context->cart->id_currency = (int)$currency->id; + $this->context->cart->save(); + } + echo Tools::jsonEncode($this->ajaxReturnVars()); + } + public function ajaxProcessUpdateLang() + { + $lang = new Language((int)Tools::getValue('id_lang')); + if (Validate::isLoadedObject($lang) && $lang->active) + { + $this->context->cart->id_lang = (int)$lang->id; + $this->context->cart->save(); + } + echo Tools::jsonEncode($this->ajaxReturnVars()); + } + public function ajaxProcessDuplicateOrder() + { + $errors = array(); + if (!$id_order = Tools::getValue('id_order')) + $errors[] = Tools::displayErrors('Invalid order'); + $cart = Cart::getCartByOrderId($id_order); + $new_cart = $cart->duplicate(); + if (!$new_cart OR !Validate::isLoadedObject($new_cart['cart'])) + $errors[] = Tools::displayError('The order cannot be renewed'); + elseif (!$new_cart['success']) + $errors[] = Tools::displayError('The order cannot be renewed'); + else + { + $this->context->cart = $new_cart['cart']; + echo Tools::jsonEncode($this->ajaxReturnVars()); + } + + } + + public function ajaxProcessDeleteDiscount() + { + if ($this->context->cart->deleteDiscount((int)Tools::getValue('id_voucher'))) + echo Tools::jsonEncode($this->ajaxReturnVars()); + } + + public function ajaxProcessAddVoucher() + { + $errors = array(); + $customer = new Customer((int)$this->context->cart->id_customer); + + if (!$id_discount = Tools::getValue('id_voucher') OR !$discount = new Discount((int)$id_discount)) + $errors[] = Tools::displayError('Invalid voucher'); + elseif ($err = $this->context->cart->checkDiscountValidity($discount, $this->context->cart->getDiscounts(), $this->context->cart->getOrderTotal(), $this->context->cart->getProducts(), true, $customer)) + $errors[] = $err; + if (!sizeof($errors)) + if (!$this->context->cart->addDiscount((int)$discount->id)) + $errors[] = Tools::displayError('Can\'t add the voucher'); + echo Tools::jsonEncode(array_merge($this->ajaxReturnVars(), array('errors' => $errors))); + } + + protected function getCartSummary() + { + $summary = $this->context->cart->getSummaryDetails(); + $currency = new Currency((int)$this->context->cart->id_currency); + if (count($summary['products'])) + foreach ($summary['products'] AS &$product) + { + $product['price'] = str_replace($currency->sign, '', Tools::displayPrice(Tools::convertPrice($product['price'], $currency), $currency)); + $product['total'] = str_replace($currency->sign, '', Tools::displayPrice(Tools::convertPrice($product['total'], $currency), $currency)); + $product['image_link'] = $this->context->link->getImageLink($product['link_rewrite'], $product['id_image'], 'small'); + if(!isset($product['attributes_small'])) + $product['attributes_small'] = ''; + } + if (count($summary['discounts'])) + foreach ($summary['discounts'] AS &$voucher) + $voucher['value_real'] = Tools::displayPrice($voucher['value_real'], $currency); + $summary['total_products'] = str_replace($currency->sign, '', Tools::displayPrice(Tools::convertPrice($summary['total_products'], $currency), $currency)); + $summary['total_discounts_tax_exc'] = str_replace($currency->sign, '', Tools::displayPrice(Tools::convertPrice($summary['total_discounts_tax_exc'], $currency), $currency)); + $summary['total_shipping_tax_exc'] = str_replace($currency->sign, '', Tools::displayPrice(Tools::convertPrice($summary['total_shipping_tax_exc'], $currency), $currency)); + $summary['total_tax'] = str_replace($currency->sign, '', Tools::displayPrice(Tools::convertPrice($summary['total_tax'], $currency), $currency)); + $summary['total_price_without_tax'] = str_replace($currency->sign, '', Tools::displayPrice(Tools::convertPrice($summary['total_price_without_tax'], $currency), $currency)); + $summary['total_price'] = str_replace($currency->sign, '', Tools::displayPrice(Tools::convertPrice($summary['total_price'], $currency), $currency)); + + return $summary; + } + + protected function getAvailableCarriers() + { + $customer = new Customer((int)$this->context->cart->id_customer); + return Carrier::getCarriersForOrder(Address::getZoneById($this->context->cart->id_address_delivery), $customer->getGroups()); + } + + public function displayAjaxSearchCarts() + { + $id_customer = (int)Tools::getValue('id_customer'); + $carts = Cart::getCustomerCarts((int)$id_customer); + $orders = Order::getCustomerOrders((int)$id_customer); + $customer = new Customer((int)$id_customer); + + if (count($carts)) + foreach ($carts AS $key => &$cart) + { + $cartObj = new Cart((int)$cart['id_cart']); + if (!Validate::isLoadedObject($cartObj) || $cartObj->OrderExists()) + unset($carts[$key]); + $currency = new Currency((int)($cart['id_currency'])); + $cart['total_price'] = Tools::displayPrice(Tools::convertPrice($cartObj->getOrderTotal(), $currency), $currency); + } + if (count($orders)) + foreach ($orders AS &$order) + $order['total_paid_real'] = Tools::displayPrice(Tools::convertPrice($order['total_paid_real'], $currency), $currency); + if ($orders || $carts) + $to_return = array_merge($this->ajaxReturnVars(), + array('carts' => $carts, + 'orders' => $orders, + 'found' => true)); + else + $to_return = array_merge($this->ajaxReturnVars(), array('found' => false)); + + echo Tools::jsonEncode($to_return); + } + + public function ajaxReturnVars() + { + + $id_cart = (int)$this->context->cart->id; + return array('summary' => $this->getCartSummary(), + 'carriers' => $this->getAvailableCarriers(), + 'cart' => $this->context->cart, + 'carriers' => $this->getAvailableCarriers(), + 'addresses' => $this->context->customer->getAddresses((int)$this->context->cart->id_lang), + 'id_cart' => $id_cart, + 'link_order' => $this->context->link->getPageLink('order', false, (int)$this->context->cart->id_lang, 'step=3&recover_cart='.$id_cart.'&token_cart='.md5(_COOKIE_KEY_.'recover_cart_'.$id_cart))); + } + + public function displayAjaxGetSummary() + { + echo Tools::jsonEncode($this->ajaxReturnVars()); + } + + public static function getOrderTotalUsingTaxCalculationMethod($id_cart) + { + return Cart::getTotalCart($id_cart, true); + } + + public static function replaceZeroByShopName($echo, $tr) + { + return ($echo == '0' ? Configuration::get('PS_SHOP_NAME') : $echo); + } +}