PageRenderTime 53ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/controllers/admin/AdminCartsController.php

https://bitbucket.org/yhjohn/ayanapure.com
PHP | 733 lines | 651 code | 55 blank | 27 comment | 133 complexity | 83c7e1ffcca3c33075f797a06e7815bf MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @version Release: $Revision: 8971 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. class AdminCartsControllerCore extends AdminController
  28. {
  29. public function __construct()
  30. {
  31. $this->table = 'cart';
  32. $this->className = 'Cart';
  33. $this->lang = false;
  34. $this->explicitSelect = true;
  35. $this->addRowAction('view');
  36. $this->addRowAction('delete');
  37. $this->_select = 'CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) `customer`, a.id_cart total, ca.name carrier, o.id_order, IF(co.id_guest, 1, 0) id_guest';
  38. $this->_join = 'LEFT JOIN '._DB_PREFIX_.'customer c ON (c.id_customer = a.id_customer)
  39. LEFT JOIN '._DB_PREFIX_.'currency cu ON (cu.id_currency = a.id_currency)
  40. LEFT JOIN '._DB_PREFIX_.'carrier ca ON (ca.id_carrier = a.id_carrier)
  41. LEFT JOIN '._DB_PREFIX_.'orders o ON (o.id_cart = a.id_cart)
  42. LEFT JOIN `'._DB_PREFIX_.'connections` co ON (a.id_guest = co.id_guest AND TIME_TO_SEC(TIMEDIFF(NOW(), co.`date_add`)) < 1800)';
  43. $this->fields_list = array(
  44. 'id_cart' => array(
  45. 'title' => $this->l('ID'),
  46. 'align' => 'center',
  47. 'width' => 25
  48. ),
  49. 'id_order' => array(
  50. 'title' => $this->l('Order ID'),
  51. 'align' => 'center', 'width' => 25
  52. ),
  53. 'customer' => array(
  54. 'title' => $this->l('Customer'),
  55. 'width' => 'auto',
  56. 'filter_key' => 'c!lastname'
  57. ),
  58. 'total' => array(
  59. 'title' => $this->l('Total'),
  60. 'callback' => 'getOrderTotalUsingTaxCalculationMethod',
  61. 'orderby' => false,
  62. 'search' => false,
  63. 'width' => 80,
  64. 'align' => 'right',
  65. 'prefix' => '<b>',
  66. 'suffix' => '</b>',
  67. ),
  68. 'carrier' => array(
  69. 'title' => $this->l('Carrier'),
  70. 'width' => 50,
  71. 'align' => 'center',
  72. 'callback' => 'replaceZeroByShopName',
  73. 'filter_key' => 'ca!name'
  74. ),
  75. 'date_add' => array(
  76. 'title' => $this->l('Date'),
  77. 'width' => 150,
  78. 'align' => 'right',
  79. 'type' => 'datetime',
  80. 'filter_key' => 'a!date_add'
  81. ),
  82. 'id_guest' => array(
  83. 'title' => $this->l('Online'),
  84. 'width' => 40,
  85. 'align' => 'center',
  86. 'type' => 'bool',
  87. 'havingFilter' => true,
  88. 'icon' => array(0 => 'blank.gif', 1 => 'tab-customers.gif')
  89. )
  90. );
  91. $this->shopLinkType = 'shop';
  92. parent::__construct();
  93. }
  94. public function renderView()
  95. {
  96. if (!($cart = $this->loadObject(true)))
  97. return;
  98. $customer = new Customer($cart->id_customer);
  99. $this->context->cart = $cart;
  100. $this->context->customer = $customer;
  101. $products = $cart->getProducts();
  102. $customized_datas = Product::getAllCustomizedDatas((int)$cart->id);
  103. Product::addCustomizationPrice($products, $customized_datas);
  104. $summary = $cart->getSummaryDetails();
  105. $currency = new Currency($cart->id_currency);
  106. /* Display order information */
  107. $id_order = (int)Order::getOrderByCartId($cart->id);
  108. $order = new Order($id_order);
  109. if ($order->getTaxCalculationMethod() == PS_TAX_EXC)
  110. {
  111. $total_products = $summary['total_products'];
  112. $total_discounts = $summary['total_discounts_tax_exc'];
  113. $total_wrapping = $summary['total_wrapping_tax_exc'];
  114. $total_price = $summary['total_price_without_tax'];
  115. $total_shipping = $summary['total_shipping_tax_exc'];
  116. }
  117. else
  118. {
  119. $total_products = $summary['total_products_wt'];
  120. $total_discounts = $summary['total_discounts'];
  121. $total_wrapping = $summary['total_wrapping'];
  122. $total_price = $summary['total_price'];
  123. $total_shipping = $summary['total_shipping'];
  124. }
  125. foreach ($products as $k => &$product)
  126. {
  127. if ($order->getTaxCalculationMethod() == PS_TAX_EXC)
  128. {
  129. $product['product_price'] = $product['price'];
  130. $product['product_total'] = $product['total'];
  131. }
  132. else
  133. {
  134. $product['product_price'] = $product['price_wt'];
  135. $product['product_total'] = $product['total_wt'];
  136. }
  137. $image = array();
  138. if (isset($product['id_product_attribute']) && (int)$product['id_product_attribute'])
  139. $image = Db::getInstance()->getRow('SELECT id_image
  140. FROM '._DB_PREFIX_.'product_attribute_image
  141. WHERE id_product_attribute = '.(int)$product['id_product_attribute']);
  142. if (!isset($image['id_image']))
  143. $image = Db::getInstance()->getRow('SELECT id_image
  144. FROM '._DB_PREFIX_.'image
  145. WHERE id_product = '.(int)$product['id_product'].' AND cover = 1');
  146. $product_obj = new Product($product['id_product']);
  147. $product['qty_in_stock'] = StockAvailable::getQuantityAvailableByProduct($product['id_product'], isset($product['id_product_attribute']) ? $product['id_product_attribute'] : null, (int)$order->id_shop);
  148. $image_product = new Image($image['id_image']);
  149. $product['image'] = (isset($image['id_image']) ? ImageManager::thumbnail(_PS_IMG_DIR_.'p/'.$image_product->getExistingImgPath().'.jpg', 'product_mini_'.(int)$product['id_product'].(isset($product['id_product_attribute']) ? '_'.(int)$product['id_product_attribute'] : '').'.jpg', 45, 'jpg') : '--');
  150. }
  151. $this->tpl_view_vars = array(
  152. 'products' => $products,
  153. 'discounts' => $cart->getCartRules(),
  154. 'order' => $order,
  155. 'cart' => $cart,
  156. 'currency' => $currency,
  157. 'customer' => $customer,
  158. 'customer_stats' => $customer->getStats(),
  159. 'total_products' => $total_products,
  160. 'total_discounts' => $total_discounts,
  161. 'total_wrapping' => $total_wrapping,
  162. 'total_price' => $total_price,
  163. 'total_shipping' => $total_shipping,
  164. 'customized_datas' => $customized_datas
  165. );
  166. return parent::renderView();
  167. }
  168. public function ajaxPreProcess()
  169. {
  170. if ($this->tabAccess['edit'] === '1')
  171. {
  172. $id_customer = (int)Tools::getValue('id_customer');
  173. $customer = new Customer((int)$id_customer);
  174. $this->context->customer = $customer;
  175. $id_cart = (int)Tools::getValue('id_cart');
  176. if (!$id_cart)
  177. $id_cart = $customer->getLastCart(false);
  178. $this->context->cart = new Cart((int)$id_cart);
  179. if (!$this->context->cart->id_customer)
  180. $this->context->cart->id_customer = $id_customer;
  181. if ($this->context->cart->OrderExists())
  182. return;
  183. if (!$this->context->cart->secure_key)
  184. $this->context->cart->secure_key = $this->context->customer->secure_key;
  185. if (!$this->context->cart->id_shop)
  186. $this->context->cart->id_shop = (int)$this->context->shop->id;
  187. if (!$this->context->cart->id_lang)
  188. $this->context->cart->id_lang = (($id_lang = (int)Tools::getValue('id_lang')) ? $id_lang : Configuration::get('PS_LANG_DEFAULT'));
  189. if (!$this->context->cart->id_currency)
  190. $this->context->cart->id_currency = (($id_currency = (int)Tools::getValue('id_currency')) ? $id_currency : Configuration::get('PS_CURRENCY_DEFAULT'));
  191. $addresses = $customer->getAddresses((int)$this->context->cart->id_lang);
  192. $id_address_delivery = (int)Tools::getValue('id_address_delivery');
  193. $id_address_invoice = (int)Tools::getValue('id_address_delivery');
  194. if (!$this->context->cart->id_address_invoice && isset($addresses[0]))
  195. $this->context->cart->id_address_invoice = (int)$addresses[0]['id_address'];
  196. elseif ($id_address_invoice)
  197. $this->context->cart->id_address_invoice = (int)$id_address_invoice;
  198. if (!$this->context->cart->id_address_delivery && isset($addresses[0]))
  199. $this->context->cart->id_address_delivery = $addresses[0]['id_address'];
  200. elseif ($id_address_delivery)
  201. $this->context->cart->id_address_delivery = (int)$id_address_delivery;
  202. $this->context->cart->setNoMultishipping();
  203. $this->context->cart->save();
  204. $currency = new Currency((int)$this->context->cart->id_currency);
  205. $this->context->currency = $currency;
  206. }
  207. }
  208. public function ajaxProcessDeleteProduct()
  209. {
  210. if ($this->tabAccess['edit'] === '1')
  211. {
  212. $errors = array();
  213. if ((!$id_product = (int)Tools::getValue('id_product')) || !Validate::isInt($id_product))
  214. $errors[] = Tools::displayError('Invalid product');
  215. if (($id_product_attribute = (int)Tools::getValue('id_product_attribute')) && !Validate::isInt($id_product_attribute))
  216. $errors[] = Tools::displayError('Invalid combination');
  217. if (count($errors))
  218. die(Tools::jsonEncode($errors));
  219. if ($this->context->cart->deleteProduct($id_product, $id_product_attribute, (int)Tools::getValue('id_customization')))
  220. echo Tools::jsonEncode($this->ajaxReturnVars());
  221. }
  222. }
  223. public function ajaxProcessUpdateCustomizationFields()
  224. {
  225. $errors = array();
  226. if ($this->tabAccess['edit'] === '1')
  227. {
  228. $errors = array();
  229. if (Tools::getValue('only_display') != 1)
  230. {
  231. if (!$this->context->cart->id || (!$id_product = (int)Tools::getValue('id_product')))
  232. return;
  233. $product = new Product((int)$id_product);
  234. if (!$customization_fields = $product->getCustomizationFieldIds())
  235. return;
  236. foreach ($customization_fields as $customization_field)
  237. {
  238. $field_id = 'customization_'.$id_product.'_'.$customization_field['id_customization_field'];
  239. if ($customization_field['type'] == Product::CUSTOMIZE_TEXTFIELD)
  240. {
  241. if (!isset($_POST[$field_id]) || empty($_POST[$field_id]))
  242. {
  243. if ($customization_field['required'])
  244. $errors[] = Tools::displayError('Please fill in all required fields');
  245. continue;
  246. }
  247. if (!Validate::isMessage($_POST[$field_id]) || empty($_POST[$field_id]))
  248. $errors[] = Tools::displayError('Invalid message');
  249. $this->context->cart->addTextFieldToProduct((int)$product->id, (int)$customization_field['id_customization_field'], Product::CUSTOMIZE_TEXTFIELD, $_POST[$field_id]);
  250. }
  251. elseif ($customization_field['type'] == Product::CUSTOMIZE_FILE)
  252. {
  253. if (!isset($_FILES[$field_id]) || !isset($_FILES[$field_id]['tmp_name']) || empty($_FILES[$field_id]['tmp_name']))
  254. {
  255. if ($customization_field['required'])
  256. $errors[] = Tools::displayError('Please fill in all required fields');
  257. continue;
  258. }
  259. if ($error = ImageManager::validateUpload($_FILES[$field_id], (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE')))
  260. $errors[] = $error;
  261. if (!($tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES[$field_id]['tmp_name'], $tmp_name))
  262. $errors[] = Tools::displayError('An error occurred during the image upload.');
  263. $file_name = md5(uniqid(rand(), true));
  264. if (!ImageManager::resize($tmp_name, _PS_UPLOAD_DIR_.$file_name))
  265. continue;
  266. elseif (!ImageManager::resize($tmp_name, _PS_UPLOAD_DIR_.$file_name.'_small', (int)Configuration::get('PS_PRODUCT_PICTURE_WIDTH'), (int)Configuration::get('PS_PRODUCT_PICTURE_HEIGHT')))
  267. $errors[] = Tools::displayError('An error occurred during the image upload.');
  268. elseif (!chmod(_PS_UPLOAD_DIR_.$file_name, 0777) || !chmod(_PS_UPLOAD_DIR_.$file_name.'_small', 0777))
  269. $errors[] = Tools::displayError('An error occurred during the image upload.');
  270. else
  271. $this->context->cart->addPictureToProduct((int)$product->id, (int)$customization_field['id_customization_field'], Product::CUSTOMIZE_FILE, $file_name);
  272. unlink($tmp_name);
  273. }
  274. }
  275. }
  276. $this->setMedia();
  277. $this->initFooter();
  278. $this->context->smarty->assign(array('customization_errors' => implode('<br />', $errors),
  279. 'css_files' => $this->css_files));
  280. return $this->smartyOutputContent('controllers/orders/form_customization_feedback.tpl');
  281. }
  282. }
  283. public function ajaxProcessUpdateQty()
  284. {
  285. if ($this->tabAccess['edit'] === '1')
  286. {
  287. $errors = array();
  288. if (!$this->context->cart->id)
  289. return;
  290. if ($this->context->cart->OrderExists())
  291. $errors[] = Tools::displayError('An order has already been placed with this cart');
  292. elseif (!($id_product = (int)Tools::getValue('id_product')) || !($product = new Product((int)$id_product, true, $this->context->language->id)))
  293. $errors[] = Tools::displayError('Invalid product');
  294. elseif (!($qty = Tools::getValue('qty')) || $qty == 0)
  295. $errors[] = Tools::displayError('Invalid quantity');
  296. // Don't try to use a product if not instanciated before due to errors
  297. if (isset($product) && $product->id)
  298. {
  299. if (($id_product_attribute = Tools::getValue('id_product_attribute')) != 0)
  300. {
  301. if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty((int)$id_product_attribute, (int)$qty))
  302. $errors[] = Tools::displayError('There is not enough product in stock');
  303. }
  304. else
  305. if (!$product->checkQty((int)$qty))
  306. $errors[] = Tools::displayError('There is not enough product in stock');
  307. if (!($id_customization = (int)Tools::getValue('id_customization', 0)) && !$product->hasAllRequiredCustomizableFields())
  308. $errors[] = Tools::displayError('Please fill in all required fields');
  309. $this->context->cart->save();
  310. }
  311. else
  312. $errors[] = Tools::displayError('Product can\'t be added to the cart');
  313. if (!count($errors))
  314. {
  315. if ((int)$qty < 0)
  316. {
  317. $qty = str_replace('-', '', $qty);
  318. $operator = 'down';
  319. }
  320. else
  321. $operator = 'up';
  322. if (!($qty_upd = $this->context->cart->updateQty($qty, $id_product, (int)$id_product_attribute, (int)$id_customization, $operator)))
  323. $errors[] = Tools::displayError('You already have the maximum quantity available for this product.');
  324. elseif ($qty_upd < 0)
  325. {
  326. $minimal_qty = $id_product_attribute ? Attribute::getAttributeMinimalQty((int)$id_product_attribute) : $product->minimal_quantity;
  327. $errors[] = sprintf(Tools::displayError('You must add a minimum of %d quantity', false), $minimal_qty);
  328. }
  329. }
  330. echo Tools::jsonEncode(array_merge($this->ajaxReturnVars(), array('errors' => $errors)));
  331. }
  332. }
  333. public function ajaxProcessUpdateDeliveryOption()
  334. {
  335. if ($this->tabAccess['edit'] === '1')
  336. {
  337. $delivery_option = Tools::getValue('delivery_option');
  338. if ($delivery_option !== false)
  339. $this->context->cart->setDeliveryOption(array($this->context->cart->id_address_delivery => $delivery_option));
  340. if (Validate::isBool(($recyclable = (int)Tools::getValue('recyclable'))))
  341. $this->context->cart->recyclable = $recyclable;
  342. if (Validate::isBool(($gift = (int)Tools::getValue('gift'))))
  343. $this->context->cart->gift = $gift;
  344. if (Validate::isMessage(($gift_message = pSQL(Tools::getValue('gift_message')))))
  345. $this->context->cart->gift_message = $gift_message;
  346. $this->context->cart->save();
  347. echo Tools::jsonEncode($this->ajaxReturnVars());
  348. }
  349. }
  350. public function ajaxProcessUpdateOrderMessage()
  351. {
  352. if ($this->tabAccess['edit'] === '1')
  353. {
  354. $id_message = false;
  355. if ($old_message = Message::getMessageByCartId((int)$this->context->cart->id))
  356. $id_message = $old_message['id_message'];
  357. $message = new Message((int)$id_message);
  358. if ($message_content = Tools::getValue('message'))
  359. {
  360. if (Validate::isMessage($message_content))
  361. {
  362. $message->message = htmlentities($message_content, ENT_COMPAT, 'UTF-8');
  363. $message->id_cart = (int)$this->context->cart->id;
  364. $message->id_customer = (int)$this->context->cart->id_customer;
  365. $message->save();
  366. }
  367. }
  368. else
  369. if (Validate::isLoadedObject($message))
  370. $message->delete();
  371. echo Tools::jsonEncode($this->ajaxReturnVars());
  372. }
  373. }
  374. public function ajaxProcessUpdateCurrency()
  375. {
  376. if ($this->tabAccess['edit'] === '1')
  377. {
  378. $currency = new Currency((int)Tools::getValue('id_currency'));
  379. if (Validate::isLoadedObject($currency) && !$currency->deleted && $currency->active)
  380. {
  381. $this->context->cart->id_currency = (int)$currency->id;
  382. $this->context->currency = $currency;
  383. $this->context->cart->save();
  384. }
  385. echo Tools::jsonEncode($this->ajaxReturnVars());
  386. }
  387. }
  388. public function ajaxProcessUpdateLang()
  389. {
  390. if ($this->tabAccess['edit'] === '1')
  391. {
  392. $lang = new Language((int)Tools::getValue('id_lang'));
  393. if (Validate::isLoadedObject($lang) && $lang->active)
  394. {
  395. $this->context->cart->id_lang = (int)$lang->id;
  396. $this->context->cart->save();
  397. }
  398. echo Tools::jsonEncode($this->ajaxReturnVars());
  399. }
  400. }
  401. public function ajaxProcessDuplicateOrder()
  402. {
  403. if ($this->tabAccess['edit'] === '1')
  404. {
  405. $errors = array();
  406. if (!$id_order = Tools::getValue('id_order'))
  407. $errors[] = Tools::displayError('Invalid order');
  408. $cart = Cart::getCartByOrderId($id_order);
  409. $new_cart = $cart->duplicate();
  410. if (!$new_cart || !Validate::isLoadedObject($new_cart['cart']))
  411. $errors[] = Tools::displayError('The order cannot be renewed');
  412. else if (!$new_cart['success'])
  413. $errors[] = Tools::displayError('The order cannot be renewed');
  414. else
  415. {
  416. $this->context->cart = $new_cart['cart'];
  417. echo Tools::jsonEncode($this->ajaxReturnVars());
  418. }
  419. }
  420. }
  421. public function ajaxProcessDeleteVoucher()
  422. {
  423. if ($this->tabAccess['edit'] === '1')
  424. {
  425. if ($this->context->cart->removeCartRule((int)Tools::getValue('id_cart_rule')))
  426. echo Tools::jsonEncode($this->ajaxReturnVars());
  427. }
  428. }
  429. public function ajaxProcessupdateFreeShipping()
  430. {
  431. if ($this->tabAccess['edit'] === '1')
  432. {
  433. if (!$id_cart_rule = CartRule::getIdByCode('BO_ORDER_'.(int)$this->context->cart->id))
  434. {
  435. $cart_rule = new CartRule();
  436. $cart_rule->code = 'BO_ORDER_'.(int)$this->context->cart->id;
  437. $cart_rule->name = array(Configuration::get('PS_LANG_DEFAULT') => $this->l('Free Shipping'));
  438. $cart_rule->id_customer = (int)$this->context->cart->id_customer;
  439. $cart_rule->free_shipping = true;
  440. $cart_rule->quantity = 1;
  441. $cart_rule->quantity_per_user = 1;
  442. $cart_rule->minimum_amount_currency = (int)$this->context->cart->id_currency;
  443. $cart_rule->reduction_currency = (int)$this->context->cart->id_currency;
  444. $cart_rule->date_from = date('Y-m-d H:i:s', time());
  445. $cart_rule->date_to = date('Y-m-d H:i:s', time() + 24 * 36000);
  446. $cart_rule->active = 1;
  447. $cart_rule->add();
  448. }
  449. else
  450. $cart_rule = new CartRule((int)$id_cart_rule);
  451. $this->context->cart->removeCartRule((int)$cart_rule->id);
  452. if (Tools::getValue('free_shipping'))
  453. $this->context->cart->addCartRule((int)$cart_rule->id);
  454. echo Tools::jsonEncode($this->ajaxReturnVars());
  455. }
  456. }
  457. public function ajaxProcessAddVoucher()
  458. {
  459. if ($this->tabAccess['edit'] === '1')
  460. {
  461. $errors = array();
  462. if (!($id_cart_rule = Tools::getValue('id_cart_rule')) || !$cart_rule = new CartRule((int)$id_cart_rule))
  463. $errors[] = Tools::displayError('Invalid voucher');
  464. elseif ($err = $cart_rule->checkValidity($this->context))
  465. $errors[] = $err;
  466. if (!count($errors))
  467. if (!$this->context->cart->addCartRule((int)$cart_rule->id))
  468. $errors[] = Tools::displayError('Can\'t add the voucher');
  469. echo Tools::jsonEncode(array_merge($this->ajaxReturnVars(), array('errors' => $errors)));
  470. }
  471. }
  472. public function ajaxProcessUpdateAddress()
  473. {
  474. if ($this->tabAccess['edit'] === '1')
  475. echo Tools::jsonEncode(array('addresses' => $this->context->customer->getAddresses((int)$this->context->cart->id_lang)));
  476. }
  477. public function ajaxProcessUpdateAddresses()
  478. {
  479. if ($this->tabAccess['edit'] === '1')
  480. {
  481. if (($id_address_delivery = (int)Tools::getValue('id_address_delivery')) &&
  482. ($address_delivery = new Address((int)$id_address_delivery)) &&
  483. $address_delivery->id_customer == $this->context->cart->id_customer)
  484. $this->context->cart->id_address_delivery = (int)$address_delivery->id;
  485. if (($id_address_invoice = (int)Tools::getValue('id_address_invoice')) &&
  486. ($address_invoice = new Address((int)$id_address_invoice)) &&
  487. $address_invoice->id_customer = $this->context->cart->id_customer)
  488. $this->context->cart->id_address_invoice = (int)$address_invoice->id;
  489. $this->context->cart->save();
  490. echo Tools::jsonEncode($this->ajaxReturnVars());
  491. }
  492. }
  493. protected function getCartSummary()
  494. {
  495. $summary = $this->context->cart->getSummaryDetails(null, true);
  496. $currency = Context::getContext()->currency;
  497. if (count($summary['products']))
  498. foreach ($summary['products'] as &$product)
  499. {
  500. $product['price'] = str_replace($currency->sign, '', Tools::displayPrice($product['price'], $currency));
  501. $product['total'] = str_replace($currency->sign, '', Tools::displayPrice($product['total'], $currency));
  502. $product['image_link'] = $this->context->link->getImageLink($product['link_rewrite'], $product['id_image'], 'small');
  503. if (!isset($product['attributes_small']))
  504. $product['attributes_small'] = '';
  505. $product['customized_datas'] = Product::getAllCustomizedDatas((int)$this->context->cart->id, null, true);
  506. }
  507. if (count($summary['discounts']))
  508. foreach ($summary['discounts'] as &$voucher)
  509. $voucher['value_real'] = Tools::displayPrice($voucher['value_real'], $currency);
  510. $summary['total_products'] = str_replace(
  511. $currency->sign, '',
  512. Tools::displayPrice($summary['total_products'], $currency)
  513. );
  514. $summary['total_discounts_tax_exc'] = str_replace(
  515. $currency->sign, '',
  516. Tools::displayPrice($summary['total_discounts_tax_exc'], $currency)
  517. );
  518. $summary['total_shipping_tax_exc'] = str_replace(
  519. $currency->sign, '',
  520. Tools::displayPrice($summary['total_shipping_tax_exc'], $currency)
  521. );
  522. $summary['total_tax'] = str_replace(
  523. $currency->sign, '',
  524. Tools::displayPrice($summary['total_tax'], $currency)
  525. );
  526. $summary['total_price_without_tax'] = str_replace(
  527. $currency->sign, '',
  528. Tools::displayPrice($summary['total_price_without_tax'], $currency)
  529. );
  530. $summary['total_price'] = str_replace(
  531. $currency->sign, '',
  532. Tools::displayPrice($summary['total_price'], $currency)
  533. );
  534. if (isset($summary['gift_products']) && count($summary['gift_products']))
  535. foreach ($summary['gift_products'] as &$product)
  536. {
  537. $product['image_link'] = $this->context->link->getImageLink($product['link_rewrite'], $product['id_image'], 'small');
  538. if (!isset($product['attributes_small']))
  539. $product['attributes_small'] = '';
  540. }
  541. return $summary;
  542. }
  543. protected function getDeliveryOptionList()
  544. {
  545. $delivery_option_list_formated = array();
  546. $delivery_option_list = $this->context->cart->getDeliveryOptionList();
  547. if (!count($delivery_option_list))
  548. return array();
  549. $id_default_carrier = (int)Configuration::get('PS_CARRIER_DEFAULT');
  550. foreach (current($delivery_option_list) as $key => $delivery_option)
  551. {
  552. $name = '';
  553. $first = true;
  554. $id_default_carrier_delivery = false;
  555. foreach ($delivery_option['carrier_list'] as $carrier)
  556. {
  557. if (!$first)
  558. $name .= ', ';
  559. else
  560. $first = false;
  561. $name .= $carrier['instance']->name;
  562. if ($delivery_option['unique_carrier'])
  563. $name .= ' - '.$carrier['instance']->delay[$this->context->employee->id_lang];
  564. if (!$id_default_carrier_delivery)
  565. $id_default_carrier_delivery = (int)$carrier['instance']->id;
  566. if ($carrier['instance']->id == $id_default_carrier)
  567. $id_default_carrier_delivery = $id_default_carrier;
  568. if (!$this->context->cart->id_carrier)
  569. {
  570. $this->context->cart->setDeliveryOption(array($this->context->cart->id_address_delivery => (int)$carrier['instance']->id.','));
  571. $this->context->cart->save();
  572. }
  573. }
  574. $delivery_option_list_formated[] = array('name' => $name, 'key' => $key);
  575. }
  576. return $delivery_option_list_formated;
  577. }
  578. public function displayAjaxSearchCarts()
  579. {
  580. $id_customer = (int)Tools::getValue('id_customer');
  581. $carts = Cart::getCustomerCarts((int)$id_customer);
  582. $orders = Order::getCustomerOrders((int)$id_customer);
  583. $customer = new Customer((int)$id_customer);
  584. if (count($carts))
  585. foreach ($carts as $key => &$cart)
  586. {
  587. $cart_obj = new Cart((int)$cart['id_cart']);
  588. if ($cart['id_cart'] == $this->context->cart->id || !Validate::isLoadedObject($cart_obj) || $cart_obj->OrderExists())
  589. unset($carts[$key]);
  590. $currency = new Currency((int)$cart['id_currency']);
  591. $cart['total_price'] = Tools::displayPrice($cart_obj->getOrderTotal(), $currency);
  592. }
  593. if (count($orders))
  594. foreach ($orders as &$order)
  595. $order['total_paid_real'] = Tools::displayPrice($order['total_paid_real'], $currency);
  596. if ($orders || $carts)
  597. $to_return = array_merge($this->ajaxReturnVars(),
  598. array('carts' => $carts,
  599. 'orders' => $orders,
  600. 'found' => true));
  601. else
  602. $to_return = array_merge($this->ajaxReturnVars(), array('found' => false));
  603. echo Tools::jsonEncode($to_return);
  604. }
  605. public function ajaxReturnVars()
  606. {
  607. $id_cart = (int)$this->context->cart->id;
  608. $message_content = '';
  609. if ($message = Message::getMessageByCartId((int)$this->context->cart->id))
  610. $message_content = $message['message'];
  611. $cart_rules = $this->context->cart->getCartRules(CartRule::FILTER_ACTION_SHIPPING);
  612. $free_shipping = false;
  613. if (count($cart_rules))
  614. foreach ($cart_rules as $cart_rule)
  615. if ($cart_rule['id_cart_rule'] == CartRule::getIdByCode('BO_ORDER_'.(int)$this->context->cart->id))
  616. {
  617. $free_shipping = true;
  618. break;
  619. }
  620. return array('summary' => $this->getCartSummary(),
  621. 'delivery_option_list' => $this->getDeliveryOptionList(),
  622. 'cart' => $this->context->cart,
  623. 'addresses' => $this->context->customer->getAddresses((int)$this->context->cart->id_lang),
  624. 'id_cart' => $id_cart,
  625. 'order_message' => $message_content,
  626. 'link_order' => $this->context->link->getPageLink(
  627. 'order', false,
  628. (int)$this->context->cart->id_lang,
  629. 'step=3&recover_cart='.$id_cart.'&token_cart='.md5(_COOKIE_KEY_.'recover_cart_'.$id_cart)),
  630. 'free_shipping' => (int)$free_shipping
  631. );
  632. }
  633. public function initToolbar()
  634. {
  635. parent::initToolbar();
  636. unset($this->toolbar_btn['new']);
  637. }
  638. public function displayAjaxGetSummary()
  639. {
  640. echo Tools::jsonEncode($this->ajaxReturnVars());
  641. }
  642. public function ajaxProcessUpdateProductPrice()
  643. {
  644. if ($this->tabAccess['edit'] === '1')
  645. {
  646. SpecificPrice::deleteByIdCart((int)$this->context->cart->id, (int)Tools::getValue('id_product'), (int)Tools::getValue('id_product_attribute'));
  647. $specific_price = new SpecificPrice();
  648. $specific_price->id_cart = (int)$this->context->cart->id;
  649. $specific_price->id_shop = 0;
  650. $specific_price->id_shop_group = 0;
  651. $specific_price->id_currency = 0;
  652. $specific_price->id_country = 0;
  653. $specific_price->id_group = 0;
  654. $specific_price->id_customer = (int)$this->context->customer->id;
  655. $specific_price->id_product = (int)Tools::getValue('id_product');
  656. $specific_price->id_product_attribute = (int)Tools::getValue('id_product_attribute');
  657. $specific_price->price = (float)Tools::getValue('price');
  658. $specific_price->from_quantity = 1;
  659. $specific_price->reduction = 0;
  660. $specific_price->reduction_type = 'amount';
  661. $specific_price->from = '0000-00-00 00:00:00';
  662. $specific_price->to = '0000-00-00 00:00:00';
  663. $specific_price->add();
  664. echo Tools::jsonEncode($this->ajaxReturnVars());
  665. }
  666. }
  667. public static function getOrderTotalUsingTaxCalculationMethod($id_cart)
  668. {
  669. $context = Context::getContext();
  670. $context->cart = new Cart($id_cart);
  671. $context->customer = new Customer((int)$context->cart->id_customer);
  672. return Cart::getTotalCart($id_cart, true, Cart::BOTH_WITHOUT_SHIPPING);
  673. }
  674. public static function replaceZeroByShopName($echo, $tr)
  675. {
  676. return ($echo == '0' ? Configuration::get('PS_SHOP_NAME') : $echo);
  677. }
  678. }