VEKIA

AdminOrdersController.php

Dec 5th, 2013
1,681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 95.17 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2007-2013 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 [email protected] 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 <[email protected]>
  22. * @copyright 2007-2013 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26.  
  27. class AdminOrdersControllerCore extends AdminController
  28. {
  29. public $toolbar_title;
  30.  
  31. public function __construct()
  32. {
  33. $this->table = 'order';
  34. $this->className = 'Order';
  35. $this->lang = false;
  36. $this->addRowAction('view');
  37. $this->explicitSelect = true;
  38. $this->allow_export = true;
  39. $this->deleted = false;
  40. $this->context = Context::getContext();
  41.  
  42. $this->_select = '
  43. a.id_currency,
  44. a.id_order AS id_pdf,
  45. CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) AS `customer`,
  46. osl.`name` AS `osname`,
  47. os.`color`,
  48. IF((SELECT COUNT(so.id_order) FROM `'._DB_PREFIX_.'orders` so WHERE so.id_customer = a.id_customer) > 1, 0, 1) as new';
  49.  
  50. $this->_join = '
  51. LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = a.`id_customer`)
  52. LEFT JOIN `'._DB_PREFIX_.'order_state` os ON (os.`id_order_state` = a.`current_state`)
  53. LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.(int)$this->context->language->id.')';
  54. $this->_orderBy = 'id_order';
  55. $this->_orderWay = 'DESC';
  56.  
  57. $statuses_array = array();
  58. $statuses = OrderState::getOrderStates((int)$this->context->language->id);
  59.  
  60. foreach ($statuses as $status)
  61. $statuses_array[$status['id_order_state']] = $status['name'];
  62.  
  63. $this->fields_list = array(
  64. 'id_order' => array(
  65. 'title' => $this->l('ID'),
  66. 'align' => 'center',
  67. 'width' => 25
  68. ),
  69.  
  70. 'id_carrier' => array(
  71. 'title' => $this->l('id carrier'),
  72. 'width' => 25,
  73. 'align' => 'center',
  74. 'tmpTableFilter' => true
  75. ),
  76.  
  77. 'reference' => array(
  78. 'title' => $this->l('Reference'),
  79. 'align' => 'center',
  80. 'width' => 65
  81. ),
  82.  
  83. 'new' => array(
  84. 'title' => $this->l('New'),
  85. 'width' => 25,
  86. 'align' => 'center',
  87. 'type' => 'bool',
  88. 'tmpTableFilter' => true,
  89. 'icon' => array(
  90. 0 => 'blank.gif',
  91. 1 => array(
  92. 'src' => 'note.png',
  93. 'alt' => $this->l('First customer order'),
  94. )
  95. ),
  96. 'orderby' => false
  97. ),
  98. 'customer' => array(
  99. 'title' => $this->l('Customer'),
  100. 'havingFilter' => true,
  101. ),
  102. 'total_paid_tax_incl' => array(
  103. 'title' => $this->l('Total'),
  104. 'width' => 70,
  105. 'align' => 'right',
  106. 'prefix' => '<b>',
  107. 'suffix' => '</b>',
  108. 'type' => 'price',
  109. 'currency' => true
  110. ),
  111. 'payment' => array(
  112. 'title' => $this->l('Payment: '),
  113. 'width' => 100
  114. ),
  115. 'osname' => array(
  116. 'title' => $this->l('Status'),
  117. 'color' => 'color',
  118. 'width' => 280,
  119. 'type' => 'select',
  120. 'list' => $statuses_array,
  121. 'filter_key' => 'os!id_order_state',
  122. 'filter_type' => 'int',
  123. 'order_key' => 'osname'
  124. ),
  125. 'date_add' => array(
  126. 'title' => $this->l('Date'),
  127. 'width' => 130,
  128. 'align' => 'right',
  129. 'type' => 'datetime',
  130. 'filter_key' => 'a!date_add'
  131. ),
  132. 'id_pdf' => array(
  133. 'title' => $this->l('PDF'),
  134. 'width' => 35,
  135. 'align' => 'center',
  136. 'callback' => 'printPDFIcons',
  137. 'orderby' => false,
  138. 'search' => false,
  139. 'remove_onclick' => true)
  140. );
  141.  
  142. $this->shopLinkType = 'shop';
  143. $this->shopShareDatas = Shop::SHARE_ORDER;
  144.  
  145. if (Tools::isSubmit('id_order'))
  146. {
  147. // Save context (in order to apply cart rule)
  148. $order = new Order((int)Tools::getValue('id_order'));
  149. if (!Validate::isLoadedObject($order))
  150. throw new PrestaShopException('Cannot load Order object');
  151. $this->context->cart = new Cart($order->id_cart);
  152. $this->context->customer = new Customer($order->id_customer);
  153. }
  154.  
  155. parent::__construct();
  156. }
  157.  
  158. public function renderForm()
  159. {
  160. if (Context::getContext()->shop->getContext() != Shop::CONTEXT_SHOP && Shop::isFeatureActive())
  161. $this->errors[] = $this->l('You have to select a shop before creating new orders.');
  162.  
  163. $id_cart = (int)Tools::getValue('id_cart');
  164. $cart = new Cart((int)$id_cart);
  165. if ($id_cart && !Validate::isLoadedObject($cart))
  166. $this->errors[] = $this->l('This cart does not exists');
  167. if ($id_cart && Validate::isLoadedObject($cart) && !$cart->id_customer)
  168. $this->errors[] = $this->l('The cart must have a customer');
  169. if (count($this->errors))
  170. return false;
  171.  
  172. parent::renderForm();
  173. unset($this->toolbar_btn['save']);
  174. $this->addJqueryPlugin(array('autocomplete', 'fancybox', 'typewatch'));
  175.  
  176. $defaults_order_state = array('cheque' => (int)Configuration::get('PS_OS_CHEQUE'),
  177. 'bankwire' => (int)Configuration::get('PS_OS_BANKWIRE'),
  178. 'cashondelivery' => (int)Configuration::get('PS_OS_PREPARATION'),
  179. 'other' => (int)Configuration::get('PS_OS_PAYMENT'));
  180. $payment_modules = array();
  181. foreach (PaymentModule::getInstalledPaymentModules() as $p_module)
  182. $payment_modules[] = Module::getInstanceById((int)$p_module['id_module']);
  183.  
  184. $this->context->smarty->assign(array(
  185. 'recyclable_pack' => (int)Configuration::get('PS_RECYCLABLE_PACK'),
  186. 'gift_wrapping' => (int)Configuration::get('PS_GIFT_WRAPPING'),
  187. 'cart' => $cart,
  188. 'currencies' => Currency::getCurrencies(),
  189. 'langs' => Language::getLanguages(true, Context::getContext()->shop->id),
  190. 'payment_modules' => $payment_modules,
  191. 'order_states' => OrderState::getOrderStates((int)Context::getContext()->language->id),
  192. 'defaults_order_state' => $defaults_order_state,
  193. 'show_toolbar' => $this->show_toolbar,
  194. 'toolbar_btn' => $this->toolbar_btn,
  195. 'toolbar_scroll' => $this->toolbar_scroll,
  196. 'title' => array($this->l('Orders'), $this->l('Create order'))
  197. ));
  198. $this->content .= $this->createTemplate('form.tpl')->fetch();
  199. }
  200.  
  201. public function initToolbar()
  202. {
  203. if ($this->display == 'view')
  204. {
  205. $order = new Order((int)Tools::getValue('id_order'));
  206. if ($order->hasBeenShipped())
  207. $type = $this->l('Return products');
  208. elseif ($order->hasBeenPaid())
  209. $type = $this->l('Standard refund');
  210. else
  211. $type = $this->l('Cancel products');
  212.  
  213. if (!$order->hasBeenShipped() && !$this->lite_display)
  214. $this->toolbar_btn['new'] = array(
  215. 'short' => 'Create',
  216. 'href' => '#',
  217. 'desc' => $this->l('Add a product'),
  218. 'class' => 'add_product'
  219. );
  220.  
  221. if (Configuration::get('PS_ORDER_RETURN') && !$this->lite_display)
  222. $this->toolbar_btn['standard_refund'] = array(
  223. 'short' => 'Create',
  224. 'href' => '',
  225. 'desc' => $type,
  226. 'class' => 'process-icon-standardRefund'
  227. );
  228.  
  229. if ($order->hasInvoice() && !$this->lite_display)
  230. $this->toolbar_btn['partial_refund'] = array(
  231. 'short' => 'Create',
  232. 'href' => '',
  233. 'desc' => $this->l('Partial refund'),
  234. 'class' => 'process-icon-partialRefund'
  235. );
  236. }
  237. $res = parent::initToolbar();
  238. if (Context::getContext()->shop->getContext() != Shop::CONTEXT_SHOP && isset($this->toolbar_btn['new']) && Shop::isFeatureActive())
  239. unset($this->toolbar_btn['new']);
  240. return $res;
  241. }
  242.  
  243. public function setMedia()
  244. {
  245. parent::setMedia();
  246. $this->addJqueryUI('ui.datepicker');
  247. if ($this->tabAccess['edit'] == 1 && $this->display == 'view')
  248. {
  249. $this->addJS(_PS_JS_DIR_.'admin_order.js');
  250. $this->addJS(_PS_JS_DIR_.'tools.js');
  251. $this->addJqueryPlugin('autocomplete');
  252. }
  253. }
  254.  
  255. public function printPDFIcons($id_order, $tr)
  256. {
  257. $order = new Order($id_order);
  258. $order_state = $order->getCurrentOrderState();
  259. if (!Validate::isLoadedObject($order_state) || !Validate::isLoadedObject($order))
  260. return '';
  261.  
  262. $this->context->smarty->assign(array(
  263. 'order' => $order,
  264. 'order_state' => $order_state,
  265. 'tr' => $tr
  266. ));
  267.  
  268. return $this->createTemplate('_print_pdf_icon.tpl')->fetch();
  269. }
  270.  
  271. public function postProcess()
  272. {
  273. // If id_order is sent, we instanciate a new Order object
  274. if (Tools::isSubmit('id_order') && Tools::getValue('id_order') > 0)
  275. {
  276. $order = new Order(Tools::getValue('id_order'));
  277. if (!Validate::isLoadedObject($order))
  278. throw new PrestaShopException('Can\'t load Order object');
  279. ShopUrl::cacheMainDomainForShop((int)$order->id_shop);
  280. }
  281.  
  282. /* Update shipping number */
  283. if (Tools::isSubmit('submitShippingNumber') && isset($order))
  284. {
  285. if ($this->tabAccess['edit'] === '1')
  286. {
  287. $order_carrier = new OrderCarrier(Tools::getValue('id_order_carrier'));
  288. if (!Validate::isLoadedObject($order_carrier))
  289. $this->errors[] = Tools::displayError('The order carrier ID is invalid.');
  290. elseif (!Validate::isTrackingNumber(Tools::getValue('tracking_number')))
  291. $this->errors[] = Tools::displayError('The tracking number is incorrect.');
  292. else
  293. {
  294. // update shipping number
  295. // Keep these two following lines for backward compatibility, remove on 1.6 version
  296. $order->shipping_number = Tools::getValue('tracking_number');
  297. $order->update();
  298.  
  299. // Update order_carrier
  300. $order_carrier->tracking_number = pSQL(Tools::getValue('tracking_number'));
  301. if ($order_carrier->update())
  302. {
  303. // Send mail to customer
  304. $customer = new Customer((int)$order->id_customer);
  305. $carrier = new Carrier((int)$order->id_carrier, $order->id_lang);
  306. if (!Validate::isLoadedObject($customer))
  307. throw new PrestaShopException('Can\'t load Customer object');
  308. if (!Validate::isLoadedObject($carrier))
  309. throw new PrestaShopException('Can\'t load Carrier object');
  310. $templateVars = array(
  311. '{followup}' => str_replace('@', $order->shipping_number, $carrier->url),
  312. '{firstname}' => $customer->firstname,
  313. '{lastname}' => $customer->lastname,
  314. '{id_order}' => $order->id,
  315. '{shipping_number}' => $order->shipping_number,
  316. '{order_name}' => $order->getUniqReference()
  317. );
  318. if (@Mail::Send((int)$order->id_lang, 'in_transit', Mail::l('Package in transit'), $templateVars,
  319. $customer->email, $customer->firstname.' '.$customer->lastname, null, null, null, null,
  320. _PS_MAIL_DIR_, true, (int)$order->id_shop))
  321. {
  322. Hook::exec('actionAdminOrdersTrackingNumberUpdate', array('order' => $order, 'customer' => $customer, 'carrier' => $carrier));
  323. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
  324. }
  325. else
  326. $this->errors[] = Tools::displayError('An error occurred while sending an email to the customer.');
  327. }
  328. else
  329. $this->errors[] = Tools::displayError('The order carrier cannot be updated.');
  330. }
  331. }
  332. else
  333. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  334. }
  335.  
  336. /* Change order state, add a new entry in order history and send an e-mail to the customer if needed */
  337. elseif (Tools::isSubmit('submitState') && isset($order))
  338. {
  339. if ($this->tabAccess['edit'] === '1')
  340. {
  341. $order_state = new OrderState(Tools::getValue('id_order_state'));
  342.  
  343. if (!Validate::isLoadedObject($order_state))
  344. $this->errors[] = Tools::displayError('The new order status is invalid.');
  345. else
  346. {
  347. $current_order_state = $order->getCurrentOrderState();
  348. if ($current_order_state->id != $order_state->id)
  349. {
  350. // Create new OrderHistory
  351. $history = new OrderHistory();
  352. $history->id_order = $order->id;
  353. $history->id_employee = (int)$this->context->employee->id;
  354.  
  355. $use_existings_payment = false;
  356. if (!$order->hasInvoice())
  357. $use_existings_payment = true;
  358. $history->changeIdOrderState((int)$order_state->id, $order, $use_existings_payment);
  359.  
  360. $carrier = new Carrier($order->id_carrier, $order->id_lang);
  361. $templateVars = array();
  362. if ($history->id_order_state == Configuration::get('PS_OS_SHIPPING') && $order->shipping_number)
  363. $templateVars = array('{followup}' => str_replace('@', $order->shipping_number, $carrier->url));
  364. // Save all changes
  365. if ($history->addWithemail(true, $templateVars))
  366. {
  367. // synchronizes quantities if needed..
  368. if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
  369. {
  370. foreach ($order->getProducts() as $product)
  371. {
  372. if (StockAvailable::dependsOnStock($product['product_id']))
  373. StockAvailable::synchronize($product['product_id'], (int)$product['id_shop']);
  374. }
  375. }
  376.  
  377. Tools::redirectAdmin(self::$currentIndex.'&id_order='.(int)$order->id.'&vieworder&token='.$this->token);
  378. }
  379. $this->errors[] = Tools::displayError('An error occurred while changing order status, or we were unable to send an email to the customer.');
  380. }
  381. else
  382. $this->errors[] = Tools::displayError('The order has already been assigned this status.');
  383. }
  384. }
  385. else
  386. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  387. }
  388.  
  389. /* Add a new message for the current order and send an e-mail to the customer if needed */
  390. elseif (Tools::isSubmit('submitMessage') && isset($order))
  391. {
  392. if ($this->tabAccess['edit'] === '1')
  393. {
  394. $customer = new Customer(Tools::getValue('id_customer'));
  395. if (!Validate::isLoadedObject($customer))
  396. $this->errors[] = Tools::displayError('The customer is invalid.');
  397. elseif (!Tools::getValue('message'))
  398. $this->errors[] = Tools::displayError('The message cannot be blank.');
  399. else
  400. {
  401. /* Get message rules and and check fields validity */
  402. $rules = call_user_func(array('Message', 'getValidationRules'), 'Message');
  403. foreach ($rules['required'] as $field)
  404. if (($value = Tools::getValue($field)) == false && (string)$value != '0')
  405. if (!Tools::getValue('id_'.$this->table) || $field != 'passwd')
  406. $this->errors[] = sprintf(Tools::displayError('field %s is required.'), $field);
  407. foreach ($rules['size'] as $field => $maxLength)
  408. if (Tools::getValue($field) && Tools::strlen(Tools::getValue($field)) > $maxLength)
  409. $this->errors[] = sprintf(Tools::displayError('field %1$s is too long (%2$d chars max).'), $field, $maxLength);
  410. foreach ($rules['validate'] as $field => $function)
  411. if (Tools::getValue($field))
  412. if (!Validate::$function(htmlentities(Tools::getValue($field), ENT_COMPAT, 'UTF-8')))
  413. $this->errors[] = sprintf(Tools::displayError('field %s is invalid.'), $field);
  414.  
  415. if (!count($this->errors))
  416. {
  417. //check if a thread already exist
  418. $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($customer->email, $order->id);
  419. if (!$id_customer_thread)
  420. {
  421. $customer_thread = new CustomerThread();
  422. $customer_thread->id_contact = 0;
  423. $customer_thread->id_customer = (int)$order->id_customer;
  424. $customer_thread->id_shop = (int)$this->context->shop->id;
  425. $customer_thread->id_order = (int)$order->id;
  426. $customer_thread->id_lang = (int)$this->context->language->id;
  427. $customer_thread->email = $customer->email;
  428. $customer_thread->status = 'open';
  429. $customer_thread->token = Tools::passwdGen(12);
  430. $customer_thread->add();
  431. }
  432. else
  433. $customer_thread = new CustomerThread((int)$id_customer_thread);
  434.  
  435. $customer_message = new CustomerMessage();
  436. $customer_message->id_customer_thread = $customer_thread->id;
  437. $customer_message->id_employee = (int)$this->context->employee->id;
  438. $customer_message->message = htmlentities(Tools::getValue('message'), ENT_COMPAT, 'UTF-8');
  439. $customer_message->private = Tools::getValue('visibility');
  440.  
  441. if (!$customer_message->add())
  442. $this->errors[] = Tools::displayError('An error occurred while saving the message.');
  443. elseif ($customer_message->private)
  444. Tools::redirectAdmin(self::$currentIndex.'&id_order='.(int)$order->id.'&vieworder&conf=11&token='.$this->token);
  445. else
  446. {
  447. $message = $customer_message->message;
  448. if (Configuration::get('PS_MAIL_TYPE', null, null, $order->id_shop) != Mail::TYPE_TEXT)
  449. $message = Tools::nl2br($customer_message->message);
  450.  
  451. $varsTpl = array(
  452. '{lastname}' => $customer->lastname,
  453. '{firstname}' => $customer->firstname,
  454. '{id_order}' => $order->id,
  455. '{order_name}' => $order->getUniqReference(),
  456. '{message}' => $message
  457. );
  458. if (@Mail::Send((int)$order->id_lang, 'order_merchant_comment',
  459. Mail::l('New message regarding your order', (int)$order->id_lang), $varsTpl, $customer->email,
  460. $customer->firstname.' '.$customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, (int)$order->id_shop))
  461. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=11'.'&token='.$this->token);
  462. }
  463. $this->errors[] = Tools::displayError('An error occurred while sending an email to the customer.');
  464. }
  465. }
  466. }
  467. else
  468. $this->errors[] = Tools::displayError('You do not have permission to delete this.');
  469. }
  470.  
  471. /* Partial refund from order */
  472. elseif (Tools::isSubmit('partialRefund') && isset($order))
  473. {
  474. if ($this->tabAccess['edit'] == '1')
  475. {
  476. if (is_array($_POST['partialRefundProduct']))
  477. {
  478. $amount = 0;
  479. $order_detail_list = array();
  480. foreach ($_POST['partialRefundProduct'] as $id_order_detail => $amount_detail)
  481. {
  482. $order_detail_list[$id_order_detail]['quantity'] = (int)$_POST['partialRefundProductQuantity'][$id_order_detail];
  483.  
  484. if (empty($amount_detail))
  485. {
  486. $order_detail = new OrderDetail((int)$id_order_detail);
  487. $order_detail_list[$id_order_detail]['amount'] = $order_detail->unit_price_tax_incl * $order_detail_list[$id_order_detail]['quantity'];
  488. }
  489. else
  490. $order_detail_list[$id_order_detail]['amount'] = (float)str_replace(',', '.', $amount_detail);
  491. $amount += $order_detail_list[$id_order_detail]['amount'];
  492.  
  493. $order_detail = new OrderDetail((int)$id_order_detail);
  494. if (!$order->hasBeenDelivered() || ($order->hasBeenDelivered() && Tools::isSubmit('reinjectQuantities')) && $order_detail_list[$id_order_detail]['quantity'] > 0)
  495. $this->reinjectQuantity($order_detail, $order_detail_list[$id_order_detail]['quantity']);
  496. }
  497.  
  498. $shipping_cost_amount = (float)str_replace(',', '.', Tools::getValue('partialRefundShippingCost'));
  499. if ($shipping_cost_amount > 0)
  500. $amount += $shipping_cost_amount;
  501.  
  502. $order_carrier = new OrderCarrier((int)$order->getIdOrderCarrier());
  503. if (Validate::isLoadedObject($order_carrier))
  504. {
  505. $order_carrier->weight = (float)$order->getTotalWeight();
  506. if ($order_carrier->update())
  507. $order->weight = sprintf("%.3f ".Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);
  508. }
  509.  
  510. if ($amount > 0)
  511. {
  512. if (!OrderSlip::createPartialOrderSlip($order, $amount, $shipping_cost_amount, $order_detail_list))
  513. $this->errors[] = Tools::displayError('You cannot generate a partial credit slip.');
  514.  
  515. // Generate voucher
  516. if (Tools::isSubmit('generateDiscountRefund') && !count($this->errors))
  517. {
  518. $cart_rule = new CartRule();
  519. $cart_rule->description = sprintf($this->l('Credit slip for order #%d'), $order->id);
  520. $languages = Language::getLanguages(false);
  521. foreach ($languages as $language)
  522. // Define a temporary name
  523. $cart_rule->name[$language['id_lang']] = sprintf('V0C%1$dO%2$d', $order->id_customer, $order->id);
  524.  
  525. // Define a temporary code
  526. $cart_rule->code = sprintf('V0C%1$dO%2$d', $order->id_customer, $order->id);
  527. $cart_rule->quantity = 1;
  528. $cart_rule->quantity_per_user = 1;
  529.  
  530. // Specific to the customer
  531. $cart_rule->id_customer = $order->id_customer;
  532. $now = time();
  533. $cart_rule->date_from = date('Y-m-d H:i:s', $now);
  534. $cart_rule->date_to = date('Y-m-d H:i:s', $now + (3600 * 24 * 365.25)); /* 1 year */
  535. $cart_rule->partial_use = 1;
  536. $cart_rule->active = 1;
  537.  
  538. $cart_rule->reduction_amount = $amount;
  539. $cart_rule->reduction_tax = true;
  540. $cart_rule->minimum_amount_currency = $order->id_currency;
  541. $cart_rule->reduction_currency = $order->id_currency;
  542.  
  543. if (!$cart_rule->add())
  544. $this->errors[] = Tools::displayError('You cannot generate a voucher.');
  545. else
  546. {
  547. // Update the voucher code and name
  548. foreach ($languages as $language)
  549. $cart_rule->name[$language['id_lang']] = sprintf('V%1$dC%2$dO%3$d', $cart_rule->id, $order->id_customer, $order->id);
  550. $cart_rule->code = sprintf('V%1$dC%2$dO%3$d', $cart_rule->id, $order->id_customer, $order->id);
  551.  
  552. if (!$cart_rule->update())
  553. $this->errors[] = Tools::displayError('You cannot generate a voucher.');
  554. else
  555. {
  556. $currency = $this->context->currency;
  557. $customer = new Customer((int)($order->id_customer));
  558. $params['{lastname}'] = $customer->lastname;
  559. $params['{firstname}'] = $customer->firstname;
  560. $params['{id_order}'] = $order->id;
  561. $params['{order_name}'] = $order->getUniqReference();
  562. $params['{voucher_amount}'] = Tools::displayPrice($cart_rule->reduction_amount, $currency, false);
  563. $params['{voucher_num}'] = $cart_rule->code;
  564. $customer = new Customer((int)$order->id_customer);
  565. @Mail::Send((int)$order->id_lang, 'voucher', sprintf(Mail::l('New voucher regarding your order %s', (int)$order->id_lang), $order->reference),
  566. $params, $customer->email, $customer->firstname.' '.$customer->lastname, null, null, null,
  567. null, _PS_MAIL_DIR_, true, (int)$order->id_shop);
  568. }
  569. }
  570. }
  571. }
  572. else
  573. $this->errors[] = Tools::displayError('You have to enter an amount if you want to create a partial credit slip.');
  574.  
  575. // Redirect if no errors
  576. if (!count($this->errors))
  577. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=30&token='.$this->token);
  578. }
  579. else
  580. $this->errors[] = Tools::displayError('The partial refund data is incorrect.');
  581. }
  582. else
  583. $this->errors[] = Tools::displayError('You do not have permission to delete this.');
  584. }
  585.  
  586. /* Cancel product from order */
  587. elseif (Tools::isSubmit('cancelProduct') && isset($order))
  588. {
  589. if ($this->tabAccess['delete'] === '1')
  590. {
  591. if (!Tools::isSubmit('id_order_detail') && !Tools::isSubmit('id_customization'))
  592. $this->errors[] = Tools::displayError('You must select a product.');
  593. elseif (!Tools::isSubmit('cancelQuantity') && !Tools::isSubmit('cancelCustomizationQuantity'))
  594. $this->errors[] = Tools::displayError('You must enter a quantity.');
  595. else
  596. {
  597. $productList = Tools::getValue('id_order_detail');
  598. if ($productList)
  599. $productList = array_map('intval', $productList);
  600.  
  601. $customizationList = Tools::getValue('id_customization');
  602. if ($customizationList)
  603. $customizationList = array_map('intval', $customizationList);
  604.  
  605. $qtyList = Tools::getValue('cancelQuantity');
  606. if ($qtyList)
  607. $qtyList = array_map('intval', $qtyList);
  608.  
  609. $customizationQtyList = Tools::getValue('cancelCustomizationQuantity');
  610. if ($customizationQtyList)
  611. $customizationQtyList = array_map('intval', $customizationQtyList);
  612.  
  613. $full_product_list = $productList;
  614. $full_quantity_list = $qtyList;
  615.  
  616. if ($customizationList)
  617. foreach ($customizationList as $key => $id_order_detail)
  618. {
  619. $full_product_list[(int)$id_order_detail] = $id_order_detail;
  620. if (isset($customizationQtyList[$key]))
  621. $full_quantity_list[(int)$id_order_detail] += $customizationQtyList[$key];
  622. }
  623.  
  624. if ($productList || $customizationList)
  625. {
  626. if ($productList)
  627. {
  628. $id_cart = Cart::getCartIdByOrderId($order->id);
  629. $customization_quantities = Customization::countQuantityByCart($id_cart);
  630.  
  631. foreach ($productList as $key => $id_order_detail)
  632. {
  633. $qtyCancelProduct = abs($qtyList[$key]);
  634. if (!$qtyCancelProduct)
  635. $this->errors[] = Tools::displayError('No quantity has been selected for this product.');
  636.  
  637. $order_detail = new OrderDetail($id_order_detail);
  638. $customization_quantity = 0;
  639. if (array_key_exists($order_detail->product_id, $customization_quantities) && array_key_exists($order_detail->product_attribute_id, $customization_quantities[$order_detail->product_id]))
  640. $customization_quantity = (int)$customization_quantities[$order_detail->product_id][$order_detail->product_attribute_id];
  641.  
  642. if (($order_detail->product_quantity - $customization_quantity - $order_detail->product_quantity_refunded - $order_detail->product_quantity_return) < $qtyCancelProduct)
  643. $this->errors[] = Tools::displayError('An invalid quantity was selected for this product.');
  644.  
  645. }
  646. }
  647. if ($customizationList)
  648. {
  649. $customization_quantities = Customization::retrieveQuantitiesFromIds(array_keys($customizationList));
  650.  
  651. foreach ($customizationList as $id_customization => $id_order_detail)
  652. {
  653. $qtyCancelProduct = abs($customizationQtyList[$id_customization]);
  654. $customization_quantity = $customization_quantities[$id_customization];
  655.  
  656. if (!$qtyCancelProduct)
  657. $this->errors[] = Tools::displayError('No quantity has been selected for this product.');
  658.  
  659. if ($qtyCancelProduct > ($customization_quantity['quantity'] - ($customization_quantity['quantity_refunded'] + $customization_quantity['quantity_returned'])))
  660. $this->errors[] = Tools::displayError('An invalid quantity was selected for this product.');
  661. }
  662. }
  663.  
  664. if (!count($this->errors) && $productList)
  665. foreach ($productList as $key => $id_order_detail)
  666. {
  667. $qty_cancel_product = abs($qtyList[$key]);
  668. $order_detail = new OrderDetail((int)($id_order_detail));
  669.  
  670. if (!$order->hasBeenDelivered() || ($order->hasBeenDelivered() && Tools::isSubmit('reinjectQuantities')) && $qty_cancel_product > 0)
  671. $this->reinjectQuantity($order_detail, $qty_cancel_product);
  672.  
  673. // Delete product
  674. $order_detail = new OrderDetail((int)$id_order_detail);
  675. if (!$order->deleteProduct($order, $order_detail, $qty_cancel_product))
  676. $this->errors[] = Tools::displayError('An error occurred while attempting to delete the product.').' <span class="bold">'.$order_detail->product_name.'</span>';
  677. // Update weight SUM
  678. $order_carrier = new OrderCarrier((int)$order->getIdOrderCarrier());
  679. if (Validate::isLoadedObject($order_carrier))
  680. {
  681. $order_carrier->weight = (float)$order->getTotalWeight();
  682. if ($order_carrier->update())
  683. $order->weight = sprintf("%.3f ".Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);
  684. }
  685. Hook::exec('actionProductCancel', array('order' => $order, 'id_order_detail' => (int)$id_order_detail));
  686. }
  687. if (!count($this->errors) && $customizationList)
  688. foreach ($customizationList as $id_customization => $id_order_detail)
  689. {
  690. $order_detail = new OrderDetail((int)($id_order_detail));
  691. $qtyCancelProduct = abs($customizationQtyList[$id_customization]);
  692. if (!$order->deleteCustomization($id_customization, $qtyCancelProduct, $order_detail))
  693. $this->errors[] = Tools::displayError('An error occurred while attempting to delete product customization.').' '.$id_customization;
  694. }
  695. // E-mail params
  696. if ((Tools::isSubmit('generateCreditSlip') || Tools::isSubmit('generateDiscount')) && !count($this->errors))
  697. {
  698. $customer = new Customer((int)($order->id_customer));
  699. $params['{lastname}'] = $customer->lastname;
  700. $params['{firstname}'] = $customer->firstname;
  701. $params['{id_order}'] = $order->id;
  702. $params['{order_name}'] = $order->getUniqReference();
  703. }
  704.  
  705. // Generate credit slip
  706. if (Tools::isSubmit('generateCreditSlip') && !count($this->errors))
  707. {
  708. if (!OrderSlip::createOrderSlip($order, $full_product_list, $full_quantity_list, Tools::isSubmit('shippingBack')))
  709. $this->errors[] = Tools::displayError('A credit slip cannot be generated. ');
  710. else
  711. {
  712. Hook::exec('actionOrderSlipAdd', array('order' => $order, 'productList' => $full_product_list, 'qtyList' => $full_quantity_list));
  713. @Mail::Send(
  714. (int)$order->id_lang,
  715. 'credit_slip',
  716. Mail::l('New credit slip regarding your order', $order->id_lang),
  717. $params,
  718. $customer->email,
  719. $customer->firstname.' '.$customer->lastname,
  720. null,
  721. null,
  722. null,
  723. null,
  724. _PS_MAIL_DIR_,
  725. true,
  726. (int)$order->id_shop
  727. );
  728. }
  729. }
  730.  
  731. // Generate voucher
  732. if (Tools::isSubmit('generateDiscount') && !count($this->errors))
  733. {
  734. $cartrule = new CartRule();
  735. $languages = Language::getLanguages($order);
  736. $cartrule->description = sprintf($this->l('Credit card slip for order #%d'), $order->id);
  737. foreach ($languages as $language)
  738. {
  739. // Define a temporary name
  740. $cartrule->name[$language['id_lang']] = 'V0C'.(int)($order->id_customer).'O'.(int)($order->id);
  741. }
  742. // Define a temporary code
  743. $cartrule->code = 'V0C'.(int)($order->id_customer).'O'.(int)($order->id);
  744.  
  745. $cartrule->quantity = 1;
  746. $cartrule->quantity_per_user = 1;
  747. // Specific to the customer
  748. $cartrule->id_customer = $order->id_customer;
  749. $now = time();
  750. $cartrule->date_from = date('Y-m-d H:i:s', $now);
  751. $cartrule->date_to = date('Y-m-d H:i:s', $now + (3600 * 24 * 365.25)); /* 1 year */
  752. $cartrule->active = 1;
  753.  
  754. $products = $order->getProducts(false, $full_product_list, $full_quantity_list);
  755.  
  756. $total = 0;
  757. foreach ($products as $product)
  758. $total += $product['unit_price_tax_incl'] * $product['product_quantity'];
  759.  
  760. if (Tools::isSubmit('shippingBack'))
  761. $total += $order->total_shipping;
  762.  
  763. $cartrule->reduction_amount = $total;
  764. $cartrule->reduction_tax = true;
  765. $cartrule->minimum_amount_currency = $order->id_currency;
  766. $cartrule->reduction_currency = $order->id_currency;
  767.  
  768. if (!$cartrule->add())
  769. $this->errors[] = Tools::displayError('You cannot generate a voucher.');
  770. else
  771. {
  772. // Update the voucher code and name
  773. foreach ($languages as $language)
  774. $cartrule->name[$language['id_lang']] = 'V'.(int)($cartrule->id).'C'.(int)($order->id_customer).'O'.$order->id;
  775. $cartrule->code = 'V'.(int)($cartrule->id).'C'.(int)($order->id_customer).'O'.$order->id;
  776. if (!$cartrule->update())
  777. $this->errors[] = Tools::displayError('You cannot generate a voucher.');
  778. else
  779. {
  780. $currency = $this->context->currency;
  781. $params['{voucher_amount}'] = Tools::displayPrice($cartrule->reduction_amount, $currency, false);
  782. $params['{voucher_num}'] = $cartrule->code;
  783. @Mail::Send((int)$order->id_lang, 'voucher', sprintf(Mail::l('New voucher regarding your order %s', (int)$order->id_lang), $order->reference),
  784. $params, $customer->email, $customer->firstname.' '.$customer->lastname, null, null, null,
  785. null, _PS_MAIL_DIR_, true, (int)$order->id_shop);
  786. }
  787. }
  788. }
  789. }
  790. else
  791. $this->errors[] = Tools::displayError('No product or quantity has been selected.');
  792.  
  793. // Redirect if no errors
  794. if (!count($this->errors))
  795. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=31&token='.$this->token);
  796. }
  797. }
  798. else
  799. $this->errors[] = Tools::displayError('You do not have permission to delete this.');
  800. }
  801. elseif (Tools::isSubmit('messageReaded'))
  802. Message::markAsReaded(Tools::getValue('messageReaded'), $this->context->employee->id);
  803. elseif (Tools::isSubmit('submitAddPayment') && isset($order))
  804. {
  805. if ($this->tabAccess['edit'] === '1')
  806. {
  807. $amount = str_replace(',', '.', Tools::getValue('payment_amount'));
  808. $currency = new Currency(Tools::getValue('payment_currency'));
  809. $order_has_invoice = $order->hasInvoice();
  810. if ($order_has_invoice)
  811. $order_invoice = new OrderInvoice(Tools::getValue('payment_invoice'));
  812. else
  813. $order_invoice = null;
  814.  
  815. if (!Validate::isLoadedObject($order))
  816. $this->errors[] = Tools::displayError('The order cannot be found');
  817. elseif (!Validate::isNegativePrice($amount) || !(float)$amount)
  818. $this->errors[] = Tools::displayError('The amount is invalid.');
  819. elseif (!Validate::isString(Tools::getValue('payment_method')))
  820. $this->errors[] = Tools::displayError('The selected payment method is invalid.');
  821. elseif (!Validate::isString(Tools::getValue('payment_transaction_id')))
  822. $this->errors[] = Tools::displayError('The transaction ID is invalid.');
  823. elseif (!Validate::isLoadedObject($currency))
  824. $this->errors[] = Tools::displayError('The selected currency is invalid.');
  825. elseif ($order_has_invoice && !Validate::isLoadedObject($order_invoice))
  826. $this->errors[] = Tools::displayError('The invoice is invalid.');
  827. elseif (!Validate::isDate(Tools::getValue('payment_date')))
  828. $this->errors[] = Tools::displayError('The date is invalid');
  829. else
  830. {
  831. if (!$order->addOrderPayment($amount, Tools::getValue('payment_method'), Tools::getValue('payment_transaction_id'), $currency, Tools::getValue('payment_date'), $order_invoice))
  832. $this->errors[] = Tools::displayError('An error occurred during payment.');
  833. else
  834. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
  835. }
  836. }
  837. else
  838. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  839. }
  840. elseif (Tools::isSubmit('submitEditNote'))
  841. {
  842. $note = Tools::getValue('note');
  843. $order_invoice = new OrderInvoice((int)Tools::getValue('id_order_invoice'));
  844. if (Validate::isLoadedObject($order_invoice) && Validate::isCleanHtml($note))
  845. {
  846. if ($this->tabAccess['edit'] === '1')
  847. {
  848. $order_invoice->note = $note;
  849. if ($order_invoice->save())
  850. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order_invoice->id_order.'&vieworder&conf=4&token='.$this->token);
  851. else
  852. $this->errors[] = Tools::displayError('The invoice note was not saved.');
  853. }
  854. else
  855. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  856. }
  857. else
  858. $this->errors[] = Tools::displayError('The invoice for edit note was unable to load. ');
  859. }
  860. elseif (Tools::isSubmit('submitAddOrder') && ($id_cart = Tools::getValue('id_cart')) &&
  861. ($module_name = Tools::getValue('payment_module_name')) &&
  862. ($id_order_state = Tools::getValue('id_order_state')) && Validate::isModuleName($module_name))
  863. {
  864. if ($this->tabAccess['edit'] === '1')
  865. {
  866. $payment_module = Module::getInstanceByName($module_name);
  867. $cart = new Cart((int)$id_cart);
  868. Context::getContext()->currency = new Currency((int)$cart->id_currency);
  869. Context::getContext()->customer = new Customer((int)$cart->id_customer);
  870. $employee = new Employee((int)Context::getContext()->cookie->id_employee);
  871. $payment_module->validateOrder(
  872. (int)$cart->id, (int)$id_order_state,
  873. $cart->getOrderTotal(true, Cart::BOTH), $payment_module->displayName, $this->l('Manual order -- Employee:').
  874. substr($employee->firstname, 0, 1).'. '.$employee->lastname, array(), null, false, $cart->secure_key
  875. );
  876. if ($payment_module->currentOrder)
  877. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$payment_module->currentOrder.'&vieworder'.'&token='.$this->token);
  878. }
  879. else
  880. $this->errors[] = Tools::displayError('You do not have permission to add this.');
  881. }
  882. elseif ((Tools::isSubmit('submitAddressShipping') || Tools::isSubmit('submitAddressInvoice')) && isset($order))
  883. {
  884. if ($this->tabAccess['edit'] === '1')
  885. {
  886. $address = new Address(Tools::getValue('id_address'));
  887. if (Validate::isLoadedObject($address))
  888. {
  889. // Update the address on order
  890. if (Tools::isSubmit('submitAddressShipping'))
  891. $order->id_address_delivery = $address->id;
  892. elseif (Tools::isSubmit('submitAddressInvoice'))
  893. $order->id_address_invoice = $address->id;
  894. $order->update();
  895. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
  896. }
  897. else
  898. $this->errors[] = Tools::displayErrror('This address can\'t be loaded');
  899. }
  900. else
  901. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  902. }
  903. elseif (Tools::isSubmit('submitChangeCurrency') && isset($order))
  904. {
  905. if ($this->tabAccess['edit'] === '1')
  906. {
  907. if (Tools::getValue('new_currency') != $order->id_currency && !$order->valid)
  908. {
  909. $old_currency = new Currency($order->id_currency);
  910. $currency = new Currency(Tools::getValue('new_currency'));
  911. if (!Validate::isLoadedObject($currency))
  912. throw new PrestaShopException('Can\'t load Currency object');
  913.  
  914. // Update order detail amount
  915. foreach ($order->getOrderDetailList() as $row)
  916. {
  917. $order_detail = new OrderDetail($row['id_order_detail']);
  918. $fields = array(
  919. 'ecotax',
  920. 'product_price',
  921. 'reduction_amount',
  922. 'total_shipping_price_tax_excl',
  923. 'total_shipping_price_tax_incl',
  924. 'total_price_tax_incl',
  925. 'total_price_tax_excl',
  926. 'product_quantity_discount',
  927. 'purchase_supplier_price',
  928. 'reduction_amount',
  929. 'reduction_amount_tax_incl',
  930. 'reduction_amount_tax_excl',
  931. 'unit_price_tax_incl',
  932. 'unit_price_tax_excl',
  933. 'original_product_price'
  934.  
  935. );
  936. foreach ($fields as $field)
  937. $order_detail->{$field} = Tools::convertPriceFull($order_detail->{$field}, $old_currency, $currency);
  938.  
  939. $order_detail->update();
  940. $order_detail->updateTaxAmount($order);
  941. }
  942.  
  943. $id_order_carrier = (int)$order->getIdOrderCarrier();
  944. if ($id_order_carrier)
  945. {
  946. $order_carrier = $order_carrier = new OrderCarrier((int)$order->getIdOrderCarrier());
  947. $order_carrier->shipping_cost_tax_excl = (float)Tools::convertPriceFull($order_carrier->shipping_cost_tax_excl, $old_currency, $currency);
  948. $order_carrier->shipping_cost_tax_incl = (float)Tools::convertPriceFull($order_carrier->shipping_cost_tax_incl, $old_currency, $currency);
  949. $order_carrier->update();
  950. }
  951.  
  952. // Update order && order_invoice amount
  953. $fields = array(
  954. 'total_discounts',
  955. 'total_discounts_tax_incl',
  956. 'total_discounts_tax_excl',
  957. 'total_discount_tax_excl',
  958. 'total_discount_tax_incl',
  959. 'total_paid',
  960. 'total_paid_tax_incl',
  961. 'total_paid_tax_excl',
  962. 'total_paid_real',
  963. 'total_products',
  964. 'total_products_wt',
  965. 'total_shipping',
  966. 'total_shipping_tax_incl',
  967. 'total_shipping_tax_excl',
  968. 'total_wrapping',
  969. 'total_wrapping_tax_incl',
  970. 'total_wrapping_tax_excl',
  971. );
  972.  
  973. $invoices = $order->getInvoicesCollection();
  974. if ($invoices)
  975. foreach ($invoices as $invoice)
  976. {
  977. foreach ($fields as $field)
  978. if (isset($invoice->$field))
  979. $invoice->{$field} = Tools::convertPriceFull($invoice->{$field}, $old_currency, $currency);
  980. $invoice->save();
  981. }
  982.  
  983. foreach ($fields as $field)
  984. if (isset($order->$field))
  985. $order->{$field} = Tools::convertPriceFull($order->{$field}, $old_currency, $currency);
  986.  
  987. // Update currency in order
  988. $order->id_currency = $currency->id;
  989. // Update exchange rate
  990. $order->conversion_rate = (float)$currency->conversion_rate;
  991. $order->update();
  992. }
  993. else
  994. $this->errors[] = Tools::displayError('You cannot change the currency.');
  995. }
  996. else
  997. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  998. }
  999. elseif (Tools::isSubmit('submitGenerateInvoice') && isset($order))
  1000. {
  1001. if (!Configuration::get('PS_INVOICE', null, null, $order->id_shop))
  1002. $this->errors[] = Tools::displayError('Invoice management has been disabled.');
  1003. elseif ($order->hasInvoice())
  1004. $this->errors[] = Tools::displayError('This order already has an invoice.');
  1005. else
  1006. {
  1007. $order->setInvoice(true);
  1008. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
  1009. }
  1010. }
  1011. elseif (Tools::isSubmit('submitDeleteVoucher') && isset($order))
  1012. {
  1013. if ($this->tabAccess['edit'] === '1')
  1014. {
  1015. $order_cart_rule = new OrderCartRule(Tools::getValue('id_order_cart_rule'));
  1016. if (Validate::isLoadedObject($order_cart_rule) && $order_cart_rule->id_order == $order->id)
  1017. {
  1018. if ($order_cart_rule->id_order_invoice)
  1019. {
  1020. $order_invoice = new OrderInvoice($order_cart_rule->id_order_invoice);
  1021. if (!Validate::isLoadedObject($order_invoice))
  1022. throw new PrestaShopException('Can\'t load Order Invoice object');
  1023.  
  1024. // Update amounts of Order Invoice
  1025. $order_invoice->total_discount_tax_excl -= $order_cart_rule->value_tax_excl;
  1026. $order_invoice->total_discount_tax_incl -= $order_cart_rule->value;
  1027.  
  1028. $order_invoice->total_paid_tax_excl += $order_cart_rule->value_tax_excl;
  1029. $order_invoice->total_paid_tax_incl += $order_cart_rule->value;
  1030.  
  1031. // Update Order Invoice
  1032. $order_invoice->update();
  1033. }
  1034.  
  1035. // Update amounts of order
  1036. $order->total_discounts -= $order_cart_rule->value;
  1037. $order->total_discounts_tax_incl -= $order_cart_rule->value;
  1038. $order->total_discounts_tax_excl -= $order_cart_rule->value_tax_excl;
  1039.  
  1040. $order->total_paid += $order_cart_rule->value;
  1041. $order->total_paid_tax_incl += $order_cart_rule->value;
  1042. $order->total_paid_tax_excl += $order_cart_rule->value_tax_excl;
  1043.  
  1044. // Delete Order Cart Rule and update Order
  1045. $order_cart_rule->delete();
  1046. $order->update();
  1047. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
  1048. }
  1049. else
  1050. $this->errors[] = Tools::displayError('You cannot edit this cart rule.');
  1051. }
  1052. else
  1053. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  1054. }
  1055. elseif (Tools::getValue('submitNewVoucher') && isset($order))
  1056. {
  1057. if ($this->tabAccess['edit'] === '1')
  1058. {
  1059. if (!Tools::getValue('discount_name'))
  1060. $this->errors[] = Tools::displayError('You must specify a name in order to create a new discount.');
  1061. else
  1062. {
  1063. if ($order->hasInvoice())
  1064. {
  1065. // If the discount is for only one invoice
  1066. if (!Tools::isSubmit('discount_all_invoices'))
  1067. {
  1068. $order_invoice = new OrderInvoice(Tools::getValue('discount_invoice'));
  1069. if (!Validate::isLoadedObject($order_invoice))
  1070. throw new PrestaShopException('Can\'t load Order Invoice object');
  1071. }
  1072. }
  1073.  
  1074. $cart_rules = array();
  1075. $discount_value = (float)str_replace(',', '.', Tools::getValue('discount_value'));
  1076. switch (Tools::getValue('discount_type'))
  1077. {
  1078. // Percent type
  1079. case 1:
  1080. if ($discount_value < 100)
  1081. {
  1082. if (isset($order_invoice))
  1083. {
  1084. $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($order_invoice->total_paid_tax_incl * $discount_value / 100, 2);
  1085. $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($order_invoice->total_paid_tax_excl * $discount_value / 100, 2);
  1086.  
  1087. // Update OrderInvoice
  1088. $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
  1089. }
  1090. elseif ($order->hasInvoice())
  1091. {
  1092. $order_invoices_collection = $order->getInvoicesCollection();
  1093. foreach ($order_invoices_collection as $order_invoice)
  1094. {
  1095. $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($order_invoice->total_paid_tax_incl * $discount_value / 100, 2);
  1096. $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($order_invoice->total_paid_tax_excl * $discount_value / 100, 2);
  1097.  
  1098. // Update OrderInvoice
  1099. $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
  1100. }
  1101. }
  1102. else
  1103. {
  1104. $cart_rules[0]['value_tax_incl'] = Tools::ps_round($order->total_paid_tax_incl * $discount_value / 100, 2);
  1105. $cart_rules[0]['value_tax_excl'] = Tools::ps_round($order->total_paid_tax_excl * $discount_value / 100, 2);
  1106. }
  1107. }
  1108. else
  1109. $this->errors[] = Tools::displayError('The discount value is invalid.');
  1110. break;
  1111. // Amount type
  1112. case 2:
  1113. if (isset($order_invoice))
  1114. {
  1115. if ($discount_value > $order_invoice->total_paid_tax_incl)
  1116. $this->errors[] = Tools::displayError('The discount value is greater than the order invoice total.');
  1117. else
  1118. {
  1119. $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($discount_value, 2);
  1120. $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($discount_value / (1 + ($order->getTaxesAverageUsed() / 100)), 2);
  1121.  
  1122. // Update OrderInvoice
  1123. $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
  1124. }
  1125. }
  1126. elseif ($order->hasInvoice())
  1127. {
  1128. $order_invoices_collection = $order->getInvoicesCollection();
  1129. foreach ($order_invoices_collection as $order_invoice)
  1130. {
  1131. if ($discount_value > $order_invoice->total_paid_tax_incl)
  1132. $this->errors[] = Tools::displayError('The discount value is greater than the order invoice total.').$order_invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int)$order->id_shop).')';
  1133. else
  1134. {
  1135. $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round($discount_value, 2);
  1136. $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($discount_value / (1 + ($order->getTaxesAverageUsed() / 100)), 2);
  1137.  
  1138. // Update OrderInvoice
  1139. $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
  1140. }
  1141. }
  1142. }
  1143. else
  1144. {
  1145. if ($discount_value > $order->total_paid_tax_incl)
  1146. $this->errors[] = Tools::displayError('The discount value is greater than the order total.');
  1147. else
  1148. {
  1149. $cart_rules[0]['value_tax_incl'] = Tools::ps_round($discount_value, 2);
  1150. $cart_rules[0]['value_tax_excl'] = Tools::ps_round($discount_value / (1 + ($order->getTaxesAverageUsed() / 100)), 2);
  1151. }
  1152. }
  1153. break;
  1154. // Free shipping type
  1155. case 3:
  1156. if (isset($order_invoice))
  1157. {
  1158. if ($order_invoice->total_shipping_tax_incl > 0)
  1159. {
  1160. $cart_rules[$order_invoice->id]['value_tax_incl'] = $order_invoice->total_shipping_tax_incl;
  1161. $cart_rules[$order_invoice->id]['value_tax_excl'] = $order_invoice->total_shipping_tax_excl;
  1162.  
  1163. // Update OrderInvoice
  1164. $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
  1165. }
  1166. }
  1167. elseif ($order->hasInvoice())
  1168. {
  1169. $order_invoices_collection = $order->getInvoicesCollection();
  1170. foreach ($order_invoices_collection as $order_invoice)
  1171. {
  1172. if ($order_invoice->total_shipping_tax_incl <= 0)
  1173. continue;
  1174. $cart_rules[$order_invoice->id]['value_tax_incl'] = $order_invoice->total_shipping_tax_incl;
  1175. $cart_rules[$order_invoice->id]['value_tax_excl'] = $order_invoice->total_shipping_tax_excl;
  1176.  
  1177. // Update OrderInvoice
  1178. $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']);
  1179. }
  1180. }
  1181. else
  1182. {
  1183. $cart_rules[0]['value_tax_incl'] = $order->total_shipping_tax_incl;
  1184. $cart_rules[0]['value_tax_excl'] = $order->total_shipping_tax_excl;
  1185. }
  1186. break;
  1187. default:
  1188. $this->errors[] = Tools::displayError('The discount type is invalid.');
  1189. }
  1190.  
  1191. $res = true;
  1192. foreach ($cart_rules as &$cart_rule)
  1193. {
  1194. $cartRuleObj = new CartRule();
  1195. $cartRuleObj->date_from = date('Y-m-d H:i:s', strtotime('-1 hour', strtotime($order->date_add)));
  1196. $cartRuleObj->date_to = date('Y-m-d H:i:s', strtotime('+1 hour'));
  1197. $cartRuleObj->name[Configuration::get('PS_LANG_DEFAULT')] = Tools::getValue('discount_name');
  1198. $cartRuleObj->quantity = 0;
  1199. $cartRuleObj->quantity_per_user = 1;
  1200. if (Tools::getValue('discount_type') == 1)
  1201. $cartRuleObj->reduction_percent = $discount_value;
  1202. elseif (Tools::getValue('discount_type') == 2)
  1203. $cartRuleObj->reduction_amount = $cart_rule['value_tax_excl'];
  1204. elseif (Tools::getValue('discount_type') == 3)
  1205. $cartRuleObj->free_shipping = 1;
  1206. $cartRuleObj->active = 0;
  1207. if ($res = $cartRuleObj->add())
  1208. $cart_rule['id'] = $cartRuleObj->id;
  1209. else
  1210. break;
  1211. }
  1212.  
  1213. if ($res)
  1214. {
  1215. foreach ($cart_rules as $id_order_invoice => $cart_rule)
  1216. {
  1217. // Create OrderCartRule
  1218. $order_cart_rule = new OrderCartRule();
  1219. $order_cart_rule->id_order = $order->id;
  1220. $order_cart_rule->id_cart_rule = $cart_rule['id'];
  1221. $order_cart_rule->id_order_invoice = $id_order_invoice;
  1222. $order_cart_rule->name = Tools::getValue('discount_name');
  1223. $order_cart_rule->value = $cart_rule['value_tax_incl'];
  1224. $order_cart_rule->value_tax_excl = $cart_rule['value_tax_excl'];
  1225. $res &= $order_cart_rule->add();
  1226.  
  1227. $order->total_discounts += $order_cart_rule->value;
  1228. $order->total_discounts_tax_incl += $order_cart_rule->value;
  1229. $order->total_discounts_tax_excl += $order_cart_rule->value_tax_excl;
  1230. $order->total_paid -= $order_cart_rule->value;
  1231. $order->total_paid_tax_incl -= $order_cart_rule->value;
  1232. $order->total_paid_tax_excl -= $order_cart_rule->value_tax_excl;
  1233. }
  1234.  
  1235. // Update Order
  1236. $res &= $order->update();
  1237. }
  1238.  
  1239. if ($res)
  1240. Tools::redirectAdmin(self::$currentIndex.'&id_order='.$order->id.'&vieworder&conf=4&token='.$this->token);
  1241. else
  1242. $this->errors[] = Tools::displayError('An error occurred during the OrderCartRule creation');
  1243. }
  1244. }
  1245. else
  1246. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  1247. }
  1248.  
  1249. parent::postProcess();
  1250. }
  1251.  
  1252. public function renderView()
  1253. {
  1254. $order = new Order(Tools::getValue('id_order'));
  1255. if (!Validate::isLoadedObject($order))
  1256. throw new PrestaShopException('object can\'t be loaded');
  1257.  
  1258. $customer = new Customer($order->id_customer);
  1259. $carrier = new Carrier($order->id_carrier);
  1260. $products = $this->getProducts($order);
  1261. $currency = new Currency((int)$order->id_currency);
  1262. // Carrier module call
  1263. $carrier_module_call = null;
  1264. if ($carrier->is_module)
  1265. {
  1266. $module = Module::getInstanceByName($carrier->external_module_name);
  1267. if (method_exists($module, 'displayInfoByCart'))
  1268. $carrier_module_call = call_user_func(array($module, 'displayInfoByCart'), $order->id_cart);
  1269. }
  1270.  
  1271. // Retrieve addresses information
  1272. $addressInvoice = new Address($order->id_address_invoice, $this->context->language->id);
  1273. if (Validate::isLoadedObject($addressInvoice) && $addressInvoice->id_state)
  1274. $invoiceState = new State((int)$addressInvoice->id_state);
  1275.  
  1276. if ($order->id_address_invoice == $order->id_address_delivery)
  1277. {
  1278. $addressDelivery = $addressInvoice;
  1279. if (isset($invoiceState))
  1280. $deliveryState = $invoiceState;
  1281. }
  1282. else
  1283. {
  1284. $addressDelivery = new Address($order->id_address_delivery, $this->context->language->id);
  1285. if (Validate::isLoadedObject($addressDelivery) && $addressDelivery->id_state)
  1286. $deliveryState = new State((int)($addressDelivery->id_state));
  1287. }
  1288.  
  1289. $this->toolbar_title = sprintf($this->l('Order #%1$d (%2$s) - %3$s %4$s'), $order->id, $order->reference, $customer->firstname, $customer->lastname);
  1290. if (Shop::isFeatureActive())
  1291. {
  1292. $shop = new Shop((int)$order->id_shop);
  1293. $this->toolbar_title .= ' - '.sprintf($this->l('Shop: %s'), $shop->name);
  1294. }
  1295.  
  1296. // gets warehouses to ship products, if and only if advanced stock management is activated
  1297. $warehouse_list = null;
  1298.  
  1299. $order_details = $order->getOrderDetailList();
  1300. foreach ($order_details as $order_detail)
  1301. {
  1302. $product = new Product($order_detail['product_id']);
  1303.  
  1304. if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
  1305. && $product->advanced_stock_management)
  1306. {
  1307. $warehouses = Warehouse::getWarehousesByProductId($order_detail['product_id'], $order_detail['product_attribute_id']);
  1308. foreach ($warehouses as $warehouse)
  1309. {
  1310. if (!isset($warehouse_list[$warehouse['id_warehouse']]))
  1311. $warehouse_list[$warehouse['id_warehouse']] = $warehouse;
  1312. }
  1313. }
  1314. }
  1315.  
  1316. $payment_methods = array();
  1317. foreach (PaymentModule::getInstalledPaymentModules() as $payment)
  1318. {
  1319. $module = Module::getInstanceByName($payment['name']);
  1320. if (Validate::isLoadedObject($module) && $module->active)
  1321. $payment_methods[] = $module->displayName;
  1322. }
  1323.  
  1324. // display warning if there are products out of stock
  1325. $display_out_of_stock_warning = false;
  1326. $current_order_state = $order->getCurrentOrderState();
  1327. if (Configuration::get('PS_STOCK_MANAGEMENT') && (!Validate::isLoadedObject($current_order_state) || ($current_order_state->delivery != 1 && $current_order_state->shipped != 1)))
  1328. $display_out_of_stock_warning = true;
  1329.  
  1330. // products current stock (from stock_available)
  1331. foreach ($products as &$product)
  1332. {
  1333. $product['current_stock'] = StockAvailable::getQuantityAvailableByProduct($product['product_id'], $product['product_attribute_id'], $product['id_shop']);
  1334.  
  1335. $resume = OrderSlip::getProductSlipResume($product['id_order_detail']);
  1336. $product['quantity_refundable'] = $product['product_quantity'] - $resume['product_quantity'];
  1337. $product['amount_refundable'] = $product['total_price_tax_incl'] - $resume['amount_tax_incl'];
  1338. $product['amount_refund'] = Tools::displayPrice($resume['amount_tax_incl'], $currency);
  1339. $product['refund_history'] = OrderSlip::getProductSlipDetail($product['id_order_detail']);
  1340. $product['return_history'] = OrderReturn::getProductReturnDetail($product['id_order_detail']);
  1341.  
  1342. // if the current stock requires a warning
  1343. if ($product['current_stock'] == 0 && $display_out_of_stock_warning)
  1344. $this->displayWarning($this->l('This product is out of stock: ').' '.$product['product_name']);
  1345. if ($product['id_warehouse'] != 0)
  1346. {
  1347. $warehouse = new Warehouse((int)$product['id_warehouse']);
  1348. $product['warehouse_name'] = $warehouse->name;
  1349. }
  1350. else
  1351. $product['warehouse_name'] = '--';
  1352. }
  1353.  
  1354. $gender = new Gender((int)$customer->id_gender, $this->context->language->id);
  1355.  
  1356. // Smarty assign
  1357. $this->tpl_view_vars = array(
  1358. 'order' => $order,
  1359. 'cart' => new Cart($order->id_cart),
  1360. 'customer' => $customer,
  1361. 'gender' => $gender,
  1362. 'customer_addresses' => $customer->getAddresses($this->context->language->id),
  1363. 'addresses' => array(
  1364. 'delivery' => $addressDelivery,
  1365. 'deliveryState' => isset($deliveryState) ? $deliveryState : null,
  1366. 'invoice' => $addressInvoice,
  1367. 'invoiceState' => isset($invoiceState) ? $invoiceState : null
  1368. ),
  1369. 'customerStats' => $customer->getStats(),
  1370. 'products' => $products,
  1371. 'discounts' => $order->getCartRules(),
  1372. 'orders_total_paid_tax_incl' => $order->getOrdersTotalPaid(), // Get the sum of total_paid_tax_incl of the order with similar reference
  1373. 'total_paid' => $order->getTotalPaid(),
  1374. 'returns' => OrderReturn::getOrdersReturn($order->id_customer, $order->id),
  1375. 'customer_thread_message' => CustomerThread::getCustomerMessages($order->id_customer, 0),
  1376. 'orderMessages' => OrderMessage::getOrderMessages($order->id_lang),
  1377. 'messages' => Message::getMessagesByOrderId($order->id, true),
  1378. 'carrier' => new Carrier($order->id_carrier),
  1379. 'history' => $order->getHistory($this->context->language->id),
  1380. 'states' => OrderState::getOrderStates($this->context->language->id),
  1381. 'warehouse_list' => $warehouse_list,
  1382. 'sources' => ConnectionsSource::getOrderSources($order->id),
  1383. 'currentState' => $order->getCurrentOrderState(),
  1384. 'currency' => new Currency($order->id_currency),
  1385. 'currencies' => Currency::getCurrencies(),
  1386. 'previousOrder' => $order->getPreviousOrderId(),
  1387. 'nextOrder' => $order->getNextOrderId(),
  1388. 'current_index' => self::$currentIndex,
  1389. 'carrierModuleCall' => $carrier_module_call,
  1390. 'iso_code_lang' => $this->context->language->iso_code,
  1391. 'id_lang' => $this->context->language->id,
  1392. 'can_edit' => ($this->tabAccess['edit'] == 1),
  1393. 'current_id_lang' => $this->context->language->id,
  1394. 'invoices_collection' => $order->getInvoicesCollection(),
  1395. 'not_paid_invoices_collection' => $order->getNotPaidInvoicesCollection(),
  1396. 'payment_methods' => $payment_methods,
  1397. 'invoice_management_active' => Configuration::get('PS_INVOICE', null, null, $order->id_shop),
  1398. 'display_warehouse' => (int)Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
  1399. );
  1400.  
  1401. return parent::renderView();
  1402. }
  1403.  
  1404. public function ajaxProcessSearchCustomers()
  1405. {
  1406. if ($customers = Customer::searchByName(pSQL(Tools::getValue('customer_search'))))
  1407. $to_return = array('customers' => $customers,
  1408. 'found' => true);
  1409. else
  1410. $to_return = array('found' => false);
  1411. $this->content = Tools::jsonEncode($to_return);
  1412. }
  1413.  
  1414. public function ajaxProcessSearchProducts()
  1415. {
  1416. Context::getContext()->customer = new Customer((int)Tools::getValue('id_customer'));
  1417. $currency = new Currency((int)Tools::getValue('id_currency'));
  1418. if ($products = Product::searchByName((int)$this->context->language->id, pSQL(Tools::getValue('product_search'))))
  1419. {
  1420. foreach ($products as &$product)
  1421. {
  1422. // Formatted price
  1423. $product['formatted_price'] = Tools::displayPrice(Tools::convertPrice($product['price_tax_incl'], $currency), $currency);
  1424. // Concret price
  1425. $product['price_tax_incl'] = Tools::ps_round(Tools::convertPrice($product['price_tax_incl'], $currency), 2);
  1426. $product['price_tax_excl'] = Tools::ps_round(Tools::convertPrice($product['price_tax_excl'], $currency), 2);
  1427. $productObj = new Product((int)$product['id_product'], false, (int)$this->context->language->id);
  1428. $combinations = array();
  1429. $attributes = $productObj->getAttributesGroups((int)$this->context->language->id);
  1430.  
  1431. // Tax rate for this customer
  1432. if (Tools::isSubmit('id_address'))
  1433. $product['tax_rate'] = $productObj->getTaxesRate(new Address(Tools::getValue('id_address')));
  1434.  
  1435. $product['warehouse_list'] = array();
  1436.  
  1437. foreach ($attributes as $attribute)
  1438. {
  1439. if (!isset($combinations[$attribute['id_product_attribute']]['attributes']))
  1440. $combinations[$attribute['id_product_attribute']]['attributes'] = '';
  1441. $combinations[$attribute['id_product_attribute']]['attributes'] .= $attribute['attribute_name'].' - ';
  1442. $combinations[$attribute['id_product_attribute']]['id_product_attribute'] = $attribute['id_product_attribute'];
  1443. $combinations[$attribute['id_product_attribute']]['default_on'] = $attribute['default_on'];
  1444. if (!isset($combinations[$attribute['id_product_attribute']]['price']))
  1445. {
  1446. $price_tax_incl = Product::getPriceStatic((int)$product['id_product'], true, $attribute['id_product_attribute']);
  1447. $price_tax_excl = Product::getPriceStatic((int)$product['id_product'], false, $attribute['id_product_attribute']);
  1448. $combinations[$attribute['id_product_attribute']]['price_tax_incl'] = Tools::ps_round(Tools::convertPrice($price_tax_incl, $currency), 2);
  1449. $combinations[$attribute['id_product_attribute']]['price_tax_excl'] = Tools::ps_round(Tools::convertPrice($price_tax_excl, $currency), 2);
  1450. $combinations[$attribute['id_product_attribute']]['formatted_price'] = Tools::displayPrice(Tools::convertPrice($price_tax_excl, $currency), $currency);
  1451. }
  1452. if (!isset($combinations[$attribute['id_product_attribute']]['qty_in_stock']))
  1453. $combinations[$attribute['id_product_attribute']]['qty_in_stock'] = StockAvailable::getQuantityAvailableByProduct((int)$product['id_product'], $attribute['id_product_attribute'], (int)$this->context->shop->id);
  1454.  
  1455. if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && (int)$product['advanced_stock_management'] == 1)
  1456. $product['warehouse_list'][$attribute['id_product_attribute']] = Warehouse::getProductWarehouseList($product['id_product'], $attribute['id_product_attribute']);
  1457. else
  1458. $product['warehouse_list'][$attribute['id_product_attribute']] = array();
  1459.  
  1460. $product['stock'][$attribute['id_product_attribute']] = Product::getRealQuantity($product['id_product'], $attribute['id_product_attribute']);
  1461.  
  1462. }
  1463.  
  1464. if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && (int)$product['advanced_stock_management'] == 1)
  1465. $product['warehouse_list'][0] = Warehouse::getProductWarehouseList($product['id_product']);
  1466. else
  1467. $product['warehouse_list'][0] = array();
  1468.  
  1469. $product['stock'][0] = StockAvailable::getQuantityAvailableByProduct((int)$product['id_product'], 0, (int)$this->context->shop->id);
  1470.  
  1471. foreach ($combinations as &$combination)
  1472. $combination['attributes'] = rtrim($combination['attributes'], ' - ');
  1473. $product['combinations'] = $combinations;
  1474.  
  1475. if ($product['customizable'])
  1476. {
  1477. $product_instance = new Product((int)$product['id_product']);
  1478. $product['customization_fields'] = $product_instance->getCustomizationFields($this->context->language->id);
  1479. }
  1480. }
  1481.  
  1482. $to_return = array(
  1483. 'products' => $products,
  1484. 'found' => true
  1485. );
  1486. }
  1487. else
  1488. $to_return = array('found' => false);
  1489.  
  1490. $this->content = Tools::jsonEncode($to_return);
  1491. }
  1492.  
  1493. public function ajaxProcessSendMailValidateOrder()
  1494. {
  1495. if ($this->tabAccess['edit'] === '1')
  1496. {
  1497. $cart = new Cart((int)Tools::getValue('id_cart'));
  1498. if (Validate::isLoadedObject($cart))
  1499. {
  1500. $customer = new Customer((int)$cart->id_customer);
  1501. if (Validate::isLoadedObject($customer))
  1502. {
  1503. $mailVars = array(
  1504. '{order_link}' => Context::getContext()->link->getPageLink('order', false, (int)$cart->id_lang, 'step=3&recover_cart='.(int)$cart->id.'&token_cart='.md5(_COOKIE_KEY_.'recover_cart_'.(int)$cart->id)),
  1505. '{firstname}' => $customer->firstname,
  1506. '{lastname}' => $customer->lastname
  1507. );
  1508. if (Mail::Send((int)$cart->id_lang, 'backoffice_order', Mail::l('Process the payment of your order', (int)$cart->id_lang), $mailVars, $customer->email,
  1509. $customer->firstname.' '.$customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, $cart->id_shop))
  1510. die(Tools::jsonEncode(array('errors' => false, 'result' => $this->l('The email was sent to your customer.'))));
  1511. }
  1512. }
  1513. $this->content = Tools::jsonEncode(array('errors' => true, 'result' => $this->l('Error in sending the email to your customer.')));
  1514. }
  1515. }
  1516.  
  1517. public function ajaxProcessAddProductOnOrder()
  1518. {
  1519. // Load object
  1520. $order = new Order((int)Tools::getValue('id_order'));
  1521. if (!Validate::isLoadedObject($order))
  1522. die(Tools::jsonEncode(array(
  1523. 'result' => false,
  1524. 'error' => Tools::displayError('The order object cannot be loaded.')
  1525. )));
  1526.  
  1527. if ($order->hasBeenShipped())
  1528. die(Tools::jsonEncode(array(
  1529. 'result' => false,
  1530. 'error' => Tools::displayError('You cannot add products to delivered orders. ')
  1531. )));
  1532.  
  1533. $product_informations = $_POST['add_product'];
  1534. if (isset($_POST['add_invoice']))
  1535. $invoice_informations = $_POST['add_invoice'];
  1536. else
  1537. $invoice_informations = array();
  1538. $product = new Product($product_informations['product_id'], false, $order->id_lang);
  1539. if (!Validate::isLoadedObject($product))
  1540. die(Tools::jsonEncode(array(
  1541. 'result' => false,
  1542. 'error' => Tools::displayError('The product object cannot be loaded.')
  1543. )));
  1544.  
  1545. if (isset($product_informations['product_attribute_id']) && $product_informations['product_attribute_id'])
  1546. {
  1547. $combination = new Combination($product_informations['product_attribute_id']);
  1548. if (!Validate::isLoadedObject($combination))
  1549. die(Tools::jsonEncode(array(
  1550. 'result' => false,
  1551. 'error' => Tools::displayError('The combination object cannot be loaded.')
  1552. )));
  1553. }
  1554.  
  1555. // Total method
  1556. $total_method = Cart::BOTH_WITHOUT_SHIPPING;
  1557.  
  1558. // Create new cart
  1559. $cart = new Cart();
  1560. $cart->id_shop_group = $order->id_shop_group;
  1561. $cart->id_shop = $order->id_shop;
  1562. $cart->id_customer = $order->id_customer;
  1563. $cart->id_carrier = $order->id_carrier;
  1564. $cart->id_address_delivery = $order->id_address_delivery;
  1565. $cart->id_address_invoice = $order->id_address_invoice;
  1566. $cart->id_currency = $order->id_currency;
  1567. $cart->id_lang = $order->id_lang;
  1568. $cart->secure_key = $order->secure_key;
  1569.  
  1570. // Save new cart
  1571. $cart->add();
  1572.  
  1573. // Save context (in order to apply cart rule)
  1574. $this->context->cart = $cart;
  1575. $this->context->customer = new Customer($order->id_customer);
  1576.  
  1577. // always add taxes even if there are not displayed to the customer
  1578. $use_taxes = true;
  1579.  
  1580. $initial_product_price_tax_incl = Product::getPriceStatic($product->id, $use_taxes, isset($combination) ? $combination->id : null, 2, null, false, true, 1,
  1581. false, $order->id_customer, $cart->id, $order->{Configuration::get('PS_TAX_ADDRESS_TYPE', null, null, $order->id_shop)});
  1582.  
  1583. // Creating specific price if needed
  1584. if ($product_informations['product_price_tax_incl'] != $initial_product_price_tax_incl)
  1585. {
  1586. $specific_price = new SpecificPrice();
  1587. $specific_price->id_shop = 0;
  1588. $specific_price->id_shop_group = 0;
  1589. $specific_price->id_currency = 0;
  1590. $specific_price->id_country = 0;
  1591. $specific_price->id_group = 0;
  1592. $specific_price->id_customer = $order->id_customer;
  1593. $specific_price->id_product = $product->id;
  1594. if (isset($combination))
  1595. $specific_price->id_product_attribute = $combination->id;
  1596. else
  1597. $specific_price->id_product_attribute = 0;
  1598. $specific_price->price = $product_informations['product_price_tax_excl'];
  1599. $specific_price->from_quantity = 1;
  1600. $specific_price->reduction = 0;
  1601. $specific_price->reduction_type = 'amount';
  1602. $specific_price->from = '0000-00-00 00:00:00';
  1603. $specific_price->to = '0000-00-00 00:00:00';
  1604. $specific_price->add();
  1605. }
  1606.  
  1607. // Add product to cart
  1608. $update_quantity = $cart->updateQty($product_informations['product_quantity'], $product->id, isset($product_informations['product_attribute_id']) ? $product_informations['product_attribute_id'] : null,
  1609. isset($combination) ? $combination->id : null, 'up', 0, new Shop($cart->id_shop));
  1610.  
  1611. if ($update_quantity < 0)
  1612. {
  1613. // If product has attribute, minimal quantity is set with minimal quantity of attribute
  1614. $minimal_quantity = ($product_informations['product_attribute_id']) ? Attribute::getAttributeMinimalQty($product_informations['product_attribute_id']) : $product->minimal_quantity;
  1615. die(Tools::jsonEncode(array('error' => sprintf(Tools::displayError('You must add %d minimum quantity', false), $minimal_quantity))));
  1616. }
  1617. elseif (!$update_quantity)
  1618. die(Tools::jsonEncode(array('error' => Tools::displayError('You already have the maximum quantity available for this product.', false))));
  1619.  
  1620. // If order is valid, we can create a new invoice or edit an existing invoice
  1621. if ($order->hasInvoice())
  1622. {
  1623. $order_invoice = new OrderInvoice($product_informations['invoice']);
  1624. // Create new invoice
  1625. if ($order_invoice->id == 0)
  1626. {
  1627. // If we create a new invoice, we calculate shipping cost
  1628. $total_method = Cart::BOTH;
  1629. // Create Cart rule in order to make free shipping
  1630. if (isset($invoice_informations['free_shipping']) && $invoice_informations['free_shipping'])
  1631. {
  1632. $cart_rule = new CartRule();
  1633. $cart_rule->id_customer = $order->id_customer;
  1634. $cart_rule->name = array(
  1635. Configuration::get('PS_LANG_DEFAULT') => $this->l('[Generated] CartRule for Free Shipping')
  1636. );
  1637. $cart_rule->date_from = date('Y-m-d H:i:s', time());
  1638. $cart_rule->date_to = date('Y-m-d H:i:s', time() + 24 * 3600);
  1639. $cart_rule->quantity = 1;
  1640. $cart_rule->quantity_per_user = 1;
  1641. $cart_rule->minimum_amount_currency = $order->id_currency;
  1642. $cart_rule->reduction_currency = $order->id_currency;
  1643. $cart_rule->free_shipping = true;
  1644. $cart_rule->active = 1;
  1645. $cart_rule->add();
  1646.  
  1647. // Add cart rule to cart and in order
  1648. $cart->addCartRule($cart_rule->id);
  1649. $values = array(
  1650. 'tax_incl' => $cart_rule->getContextualValue(true),
  1651. 'tax_excl' => $cart_rule->getContextualValue(false)
  1652. );
  1653. $order->addCartRule($cart_rule->id, $cart_rule->name[Configuration::get('PS_LANG_DEFAULT')], $values);
  1654. }
  1655.  
  1656. $order_invoice->id_order = $order->id;
  1657. if ($order_invoice->number)
  1658. Configuration::updateValue('PS_INVOICE_START_NUMBER', false, false, null, $order->id_shop);
  1659. else
  1660. $order_invoice->number = Order::getLastInvoiceNumber() + 1;
  1661.  
  1662. $invoice_address = new Address((int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE', null, null, $order->id_shop)});
  1663. $carrier = new Carrier((int)$order->id_carrier);
  1664. $tax_calculator = $carrier->getTaxCalculator($invoice_address);
  1665.  
  1666. $order_invoice->total_paid_tax_excl = Tools::ps_round((float)$cart->getOrderTotal(false, $total_method), 2);
  1667. $order_invoice->total_paid_tax_incl = Tools::ps_round((float)$cart->getOrderTotal($use_taxes, $total_method), 2);
  1668. $order_invoice->total_products = (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS);
  1669. $order_invoice->total_products_wt = (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS);
  1670. $order_invoice->total_shipping_tax_excl = (float)$cart->getTotalShippingCost(null, false);
  1671. $order_invoice->total_shipping_tax_incl = (float)$cart->getTotalShippingCost();
  1672.  
  1673. $order_invoice->total_wrapping_tax_excl = abs($cart->getOrderTotal(false, Cart::ONLY_WRAPPING));
  1674. $order_invoice->total_wrapping_tax_incl = abs($cart->getOrderTotal($use_taxes, Cart::ONLY_WRAPPING));
  1675. $order_invoice->shipping_tax_computation_method = (int)$tax_calculator->computation_method;
  1676.  
  1677. // Update current order field, only shipping because other field is updated later
  1678. $order->total_shipping += $order_invoice->total_shipping_tax_incl;
  1679. $order->total_shipping_tax_excl += $order_invoice->total_shipping_tax_excl;
  1680. $order->total_shipping_tax_incl += ($use_taxes) ? $order_invoice->total_shipping_tax_incl : $order_invoice->total_shipping_tax_excl;
  1681.  
  1682. $order->total_wrapping += abs($cart->getOrderTotal($use_taxes, Cart::ONLY_WRAPPING));
  1683. $order->total_wrapping_tax_excl += abs($cart->getOrderTotal(false, Cart::ONLY_WRAPPING));
  1684. $order->total_wrapping_tax_incl += abs($cart->getOrderTotal($use_taxes, Cart::ONLY_WRAPPING));
  1685. $order_invoice->add();
  1686.  
  1687. $order_invoice->saveCarrierTaxCalculator($tax_calculator->getTaxesAmount($order_invoice->total_shipping_tax_excl));
  1688.  
  1689. $order_carrier = new OrderCarrier();
  1690. $order_carrier->id_order = (int)$order->id;
  1691. $order_carrier->id_carrier = (int)$order->id_carrier;
  1692. $order_carrier->id_order_invoice = (int)$order_invoice->id;
  1693. $order_carrier->weight = (float)$cart->getTotalWeight();
  1694. $order_carrier->shipping_cost_tax_excl = (float)$order_invoice->total_shipping_tax_excl;
  1695. $order_carrier->shipping_cost_tax_incl = ($use_taxes) ? (float)$order_invoice->total_shipping_tax_incl : (float)$order_invoice->total_shipping_tax_excl;
  1696. $order_carrier->add();
  1697. }
  1698. // Update current invoice
  1699. else
  1700. {
  1701. $order_invoice->total_paid_tax_excl += Tools::ps_round((float)($cart->getOrderTotal(false, $total_method)), 2);
  1702. $order_invoice->total_paid_tax_incl += Tools::ps_round((float)($cart->getOrderTotal($use_taxes, $total_method)), 2);
  1703. $order_invoice->total_products += (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS);
  1704. $order_invoice->total_products_wt += (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS);
  1705. $order_invoice->update();
  1706. }
  1707. }
  1708.  
  1709. // Create Order detail information
  1710. $order_detail = new OrderDetail();
  1711. $order_detail->createList($order, $cart, $order->getCurrentOrderState(), $cart->getProducts(), (isset($order_invoice) ? $order_invoice->id : 0), $use_taxes, (int)Tools::getValue('add_product_warehouse'));
  1712.  
  1713. // update totals amount of order
  1714. $order->total_products += (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS);
  1715. $order->total_products_wt += (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS);
  1716.  
  1717. $order->total_paid += Tools::ps_round((float)($cart->getOrderTotal(true, $total_method)), 2);
  1718. $order->total_paid_tax_excl += Tools::ps_round((float)($cart->getOrderTotal(false, $total_method)), 2);
  1719. $order->total_paid_tax_incl += Tools::ps_round((float)($cart->getOrderTotal($use_taxes, $total_method)), 2);
  1720.  
  1721. if (isset($order_invoice) && Validate::isLoadedObject($order_invoice))
  1722. {
  1723. $order->total_shipping = $order_invoice->total_shipping_tax_incl;
  1724. $order->total_shipping_tax_incl = $order_invoice->total_shipping_tax_incl;
  1725. $order->total_shipping_tax_excl = $order_invoice->total_shipping_tax_excl;
  1726. }
  1727. // discount
  1728. $order->total_discounts += (float)abs($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS));
  1729. $order->total_discounts_tax_excl += (float)abs($cart->getOrderTotal(false, Cart::ONLY_DISCOUNTS));
  1730. $order->total_discounts_tax_incl += (float)abs($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS));
  1731.  
  1732. // Save changes of order
  1733. $order->update();
  1734.  
  1735. // Update weight SUM
  1736. $order_carrier = new OrderCarrier((int)$order->getIdOrderCarrier());
  1737. if (Validate::isLoadedObject($order_carrier))
  1738. {
  1739. $order_carrier->weight = (float)$order->getTotalWeight();
  1740. if ($order_carrier->update())
  1741. $order->weight = sprintf("%.3f ".Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);
  1742. }
  1743.  
  1744. // Update Tax lines
  1745. $order_detail->updateTaxAmount($order);
  1746.  
  1747. // Delete specific price if exists
  1748. if (isset($specific_price))
  1749. $specific_price->delete();
  1750.  
  1751. $products = $this->getProducts($order);
  1752.  
  1753. // Get the last product
  1754. $product = end($products);
  1755. $resume = OrderSlip::getProductSlipResume((int)$product['id_order_detail']);
  1756. $product['quantity_refundable'] = $product['product_quantity'] - $resume['product_quantity'];
  1757. $product['amount_refundable'] = $product['total_price_tax_incl'] - $resume['amount_tax_incl'];
  1758. $product['amount_refund'] = Tools::displayPrice($resume['amount_tax_incl']);
  1759. $product['return_history'] = OrderReturn::getProductReturnDetail((int)$product['id_order_detail']);
  1760. $product['refund_history'] = OrderSlip::getProductSlipDetail((int)$product['id_order_detail']);
  1761. if ($product['id_warehouse'] != 0)
  1762. {
  1763. $warehouse = new Warehouse((int)$product['id_warehouse']);
  1764. $product['warehouse_name'] = $warehouse->name;
  1765. }
  1766. else
  1767. $product['warehouse_name'] = '--';
  1768.  
  1769. // Get invoices collection
  1770. $invoice_collection = $order->getInvoicesCollection();
  1771.  
  1772. $invoice_array = array();
  1773. foreach ($invoice_collection as $invoice)
  1774. {
  1775. $invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int)$order->id_shop);
  1776. $invoice_array[] = $invoice;
  1777. }
  1778.  
  1779. // Assign to smarty informations in order to show the new product line
  1780. $this->context->smarty->assign(array(
  1781. 'product' => $product,
  1782. 'order' => $order,
  1783. 'currency' => new Currency($order->id_currency),
  1784. 'can_edit' => $this->tabAccess['edit'],
  1785. 'invoices_collection' => $invoice_collection,
  1786. 'current_id_lang' => Context::getContext()->language->id,
  1787. 'link' => Context::getContext()->link,
  1788. 'current_index' => self::$currentIndex,
  1789. 'display_warehouse' => (int)Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
  1790. ));
  1791.  
  1792. $this->sendChangedNotification($order);
  1793.  
  1794. die(Tools::jsonEncode(array(
  1795. 'result' => true,
  1796. 'view' => $this->createTemplate('_product_line.tpl')->fetch(),
  1797. 'can_edit' => $this->tabAccess['add'],
  1798. 'order' => $order,
  1799. 'invoices' => $invoice_array,
  1800. 'documents_html' => $this->createTemplate('_documents.tpl')->fetch(),
  1801. 'shipping_html' => $this->createTemplate('_shipping.tpl')->fetch(),
  1802. 'discount_form_html' => $this->createTemplate('_discount_form.tpl')->fetch()
  1803. )));
  1804. }
  1805.  
  1806. public function sendChangedNotification(Order $order = null)
  1807. {
  1808. if (is_null($order))
  1809. $order = new Order(Tools::getValue('id_order'));
  1810.  
  1811. $data = array(
  1812. '{lastname}' => $order->getCustomer()->lastname,
  1813. '{firstname}' => $order->getCustomer()->firstname,
  1814. '{id_order}' => (int)$order->id,
  1815. '{order_name}' => $order->getUniqReference()
  1816. );
  1817.  
  1818. Mail::Send(
  1819. (int)$order->id_lang,
  1820. 'order_changed',
  1821. Mail::l('Your order has been changed', $order->id_lang),
  1822. $data,
  1823. $order->getCustomer()->email,
  1824. $order->getCustomer()->firstname.' '.$order->getCustomer()->lastname,
  1825. null, null, null, null, _PS_MAIL_DIR_, true, (int)$order->id_shop);
  1826. }
  1827.  
  1828. public function ajaxProcessLoadProductInformation()
  1829. {
  1830. $order_detail = new OrderDetail(Tools::getValue('id_order_detail'));
  1831. if (!Validate::isLoadedObject($order_detail))
  1832. die(Tools::jsonEncode(array(
  1833. 'result' => false,
  1834. 'error' => Tools::displayError('The OrderDetail object cannot be loaded.')
  1835. )));
  1836.  
  1837. $product = new Product($order_detail->product_id);
  1838. if (!Validate::isLoadedObject($product))
  1839. die(Tools::jsonEncode(array(
  1840. 'result' => false,
  1841. 'error' => Tools::displayError('The product object cannot be loaded.')
  1842. )));
  1843.  
  1844. $address = new Address(Tools::getValue('id_address'));
  1845. if (!Validate::isLoadedObject($address))
  1846. die(Tools::jsonEncode(array(
  1847. 'result' => false,
  1848. 'error' => Tools::displayError('The address object cannot be loaded.')
  1849. )));
  1850.  
  1851. die(Tools::jsonEncode(array(
  1852. 'result' => true,
  1853. 'product' => $product,
  1854. 'tax_rate' => $product->getTaxesRate($address),
  1855. 'price_tax_incl' => Product::getPriceStatic($product->id, true, $order_detail->product_attribute_id, 2),
  1856. 'price_tax_excl' => Product::getPriceStatic($product->id, false, $order_detail->product_attribute_id, 2)
  1857. )));
  1858. }
  1859.  
  1860. public function ajaxProcessEditProductOnOrder()
  1861. {
  1862. // Return value
  1863. $res = true;
  1864.  
  1865. $order = new Order((int)Tools::getValue('id_order'));
  1866. $order_detail = new OrderDetail((int)Tools::getValue('product_id_order_detail'));
  1867. if (Tools::isSubmit('product_invoice'))
  1868. $order_invoice = new OrderInvoice((int)Tools::getValue('product_invoice'));
  1869.  
  1870. // Check fields validity
  1871. $this->doEditProductValidation($order_detail, $order, isset($order_invoice) ? $order_invoice : null);
  1872.  
  1873. // If multiple product_quantity, the order details concern a product customized
  1874. $product_quantity = 0;
  1875. if (is_array(Tools::getValue('product_quantity')))
  1876. foreach (Tools::getValue('product_quantity') as $id_customization => $qty)
  1877. {
  1878. // Update quantity of each customization
  1879. Db::getInstance()->update('customization', array('quantity' => $qty), 'id_customization = '.(int)$id_customization);
  1880. // Calculate the real quantity of the product
  1881. $product_quantity += $qty;
  1882. }
  1883. else
  1884. $product_quantity = Tools::getValue('product_quantity');
  1885.  
  1886. $product_price_tax_incl = Tools::ps_round(Tools::getValue('product_price_tax_incl'), 2);
  1887. $product_price_tax_excl = Tools::ps_round(Tools::getValue('product_price_tax_excl'), 2);
  1888. $total_products_tax_incl = $product_price_tax_incl * $product_quantity;
  1889. $total_products_tax_excl = $product_price_tax_excl * $product_quantity;
  1890.  
  1891. // Calculate differences of price (Before / After)
  1892. $diff_price_tax_incl = $total_products_tax_incl - $order_detail->total_price_tax_incl;
  1893. $diff_price_tax_excl = $total_products_tax_excl - $order_detail->total_price_tax_excl;
  1894.  
  1895. // Apply change on OrderInvoice
  1896. if (isset($order_invoice))
  1897. // If OrderInvoice to use is different, we update the old invoice and new invoice
  1898. if ($order_detail->id_order_invoice != $order_invoice->id)
  1899. {
  1900. $old_order_invoice = new OrderInvoice($order_detail->id_order_invoice);
  1901. // We remove cost of products
  1902. $old_order_invoice->total_products -= $order_detail->total_price_tax_excl;
  1903. $old_order_invoice->total_products_wt -= $order_detail->total_price_tax_incl;
  1904.  
  1905. $old_order_invoice->total_paid_tax_excl -= $order_detail->total_price_tax_excl;
  1906. $old_order_invoice->total_paid_tax_incl -= $order_detail->total_price_tax_incl;
  1907.  
  1908. $res &= $old_order_invoice->update();
  1909.  
  1910. $order_invoice->total_products += $order_detail->total_price_tax_excl;
  1911. $order_invoice->total_products_wt += $order_detail->total_price_tax_incl;
  1912.  
  1913. $order_invoice->total_paid_tax_excl += $order_detail->total_price_tax_excl;
  1914. $order_invoice->total_paid_tax_incl += $order_detail->total_price_tax_incl;
  1915.  
  1916. $order_detail->id_order_invoice = $order_invoice->id;
  1917. }
  1918.  
  1919. if ($diff_price_tax_incl != 0 && $diff_price_tax_excl != 0)
  1920. {
  1921. $order_detail->unit_price_tax_excl = $product_price_tax_excl;
  1922. $order_detail->unit_price_tax_incl = $product_price_tax_incl;
  1923.  
  1924. $order_detail->total_price_tax_incl += $diff_price_tax_incl;
  1925. $order_detail->total_price_tax_excl += $diff_price_tax_excl;
  1926.  
  1927. if (isset($order_invoice))
  1928. {
  1929. // Apply changes on OrderInvoice
  1930. $order_invoice->total_products += $diff_price_tax_excl;
  1931. $order_invoice->total_products_wt += $diff_price_tax_incl;
  1932.  
  1933. $order_invoice->total_paid_tax_excl += $diff_price_tax_excl;
  1934. $order_invoice->total_paid_tax_incl += $diff_price_tax_incl;
  1935. }
  1936.  
  1937. // Apply changes on Order
  1938. $order = new Order($order_detail->id_order);
  1939. $order->total_products += $diff_price_tax_excl;
  1940. $order->total_products_wt += $diff_price_tax_incl;
  1941.  
  1942. $order->total_paid += $diff_price_tax_incl;
  1943. $order->total_paid_tax_excl += $diff_price_tax_excl;
  1944. $order->total_paid_tax_incl += $diff_price_tax_incl;
  1945.  
  1946. $res &= $order->update();
  1947. }
  1948.  
  1949. $old_quantity = $order_detail->product_quantity;
  1950.  
  1951. $order_detail->product_quantity = $product_quantity;
  1952.  
  1953. // update taxes
  1954. $res &= $order_detail->updateTaxAmount($order);
  1955.  
  1956. // Save order detail
  1957. $res &= $order_detail->update();
  1958.  
  1959. // Update weight SUM
  1960. $order_carrier = new OrderCarrier((int)$order->getIdOrderCarrier());
  1961. if (Validate::isLoadedObject($order_carrier))
  1962. {
  1963. $order_carrier->weight = (float)$order->getTotalWeight();
  1964. $res &= $order_carrier->update();
  1965. if ($res)
  1966. $order->weight = sprintf("%.3f ".Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);
  1967. }
  1968.  
  1969. // Save order invoice
  1970. if (isset($order_invoice))
  1971. $res &= $order_invoice->update();
  1972.  
  1973. // Update product available quantity
  1974. StockAvailable::updateQuantity($order_detail->product_id, $order_detail->product_attribute_id, ($old_quantity - $order_detail->product_quantity), $order->id_shop);
  1975.  
  1976. $products = $this->getProducts($order);
  1977. // Get the last product
  1978. $product = $products[$order_detail->id];
  1979. $resume = OrderSlip::getProductSlipResume($order_detail->id);
  1980. $product['quantity_refundable'] = $product['product_quantity'] - $resume['product_quantity'];
  1981. $product['amount_refundable'] = $product['total_price_tax_incl'] - $resume['amount_tax_incl'];
  1982. $product['amount_refund'] = Tools::displayPrice($resume['amount_tax_incl']);
  1983. $product['refund_history'] = OrderSlip::getProductSlipDetail($order_detail->id);
  1984. if ($product['id_warehouse'] != 0)
  1985. {
  1986. $warehouse = new Warehouse((int)$product['id_warehouse']);
  1987. $product['warehouse_name'] = $warehouse->name;
  1988. }
  1989. else
  1990. $product['warehouse_name'] = '--';
  1991.  
  1992. // Get invoices collection
  1993. $invoice_collection = $order->getInvoicesCollection();
  1994.  
  1995. $invoice_array = array();
  1996. foreach ($invoice_collection as $invoice)
  1997. {
  1998. $invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int)$order->id_shop);
  1999. $invoice_array[] = $invoice;
  2000. }
  2001.  
  2002. // Assign to smarty informations in order to show the new product line
  2003. $this->context->smarty->assign(array(
  2004. 'product' => $product,
  2005. 'order' => $order,
  2006. 'currency' => new Currency($order->id_currency),
  2007. 'can_edit' => $this->tabAccess['edit'],
  2008. 'invoices_collection' => $invoice_collection,
  2009. 'current_id_lang' => Context::getContext()->language->id,
  2010. 'link' => Context::getContext()->link,
  2011. 'current_index' => self::$currentIndex,
  2012. 'display_warehouse' => (int)Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
  2013. ));
  2014.  
  2015. if (!$res)
  2016. die(Tools::jsonEncode(array(
  2017. 'result' => $res,
  2018. 'error' => Tools::displayError('An error occurred while editing the product line.')
  2019. )));
  2020.  
  2021.  
  2022. if (is_array(Tools::getValue('product_quantity')))
  2023. $view = $this->createTemplate('_customized_data.tpl')->fetch();
  2024. else
  2025. $view = $this->createTemplate('_product_line.tpl')->fetch();
  2026.  
  2027. $this->sendChangedNotification($order);
  2028.  
  2029. die(Tools::jsonEncode(array(
  2030. 'result' => $res,
  2031. 'view' => $view,
  2032. 'can_edit' => $this->tabAccess['add'],
  2033. 'invoices_collection' => $invoice_collection,
  2034. 'order' => $order,
  2035. 'invoices' => $invoice_array,
  2036. 'documents_html' => $this->createTemplate('_documents.tpl')->fetch(),
  2037. 'shipping_html' => $this->createTemplate('_shipping.tpl')->fetch(),
  2038. 'customized_product' => is_array(Tools::getValue('product_quantity'))
  2039. )));
  2040. }
  2041.  
  2042. public function ajaxProcessDeleteProductLine()
  2043. {
  2044. $res = true;
  2045.  
  2046. $order_detail = new OrderDetail((int)Tools::getValue('id_order_detail'));
  2047. $order = new Order((int)Tools::getValue('id_order'));
  2048.  
  2049. $this->doDeleteProductLineValidation($order_detail, $order);
  2050.  
  2051. // Update OrderInvoice of this OrderDetail
  2052. if ($order_detail->id_order_invoice != 0)
  2053. {
  2054. $order_invoice = new OrderInvoice($order_detail->id_order_invoice);
  2055. $order_invoice->total_paid_tax_excl -= $order_detail->total_price_tax_excl;
  2056. $order_invoice->total_paid_tax_incl -= $order_detail->total_price_tax_incl;
  2057. $order_invoice->total_products -= $order_detail->total_price_tax_excl;
  2058. $order_invoice->total_products_wt -= $order_detail->total_price_tax_incl;
  2059. $res &= $order_invoice->update();
  2060. }
  2061.  
  2062. // Update Order
  2063. $order->total_paid -= $order_detail->total_price_tax_incl;
  2064. $order->total_paid_tax_incl -= $order_detail->total_price_tax_incl;
  2065. $order->total_paid_tax_excl -= $order_detail->total_price_tax_excl;
  2066. $order->total_products -= $order_detail->total_price_tax_excl;
  2067. $order->total_products_wt -= $order_detail->total_price_tax_incl;
  2068.  
  2069. $res &= $order->update();
  2070.  
  2071. // Reinject quantity in stock
  2072. $this->reinjectQuantity($order_detail, $order_detail->product_quantity);
  2073.  
  2074. // Delete OrderDetail
  2075. $res &= $order_detail->delete();
  2076.  
  2077. // Update weight SUM
  2078. $order_carrier = new OrderCarrier((int)$order->getIdOrderCarrier());
  2079. if (Validate::isLoadedObject($order_carrier))
  2080. {
  2081. $order_carrier->weight = (float)$order->getTotalWeight();
  2082. $res &= $order_carrier->update();
  2083. if ($res)
  2084. $order->weight = sprintf("%.3f ".Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);
  2085. }
  2086.  
  2087. if (!$res)
  2088. die(Tools::jsonEncode(array(
  2089. 'result' => $res,
  2090. 'error' => Tools::displayError('An error occurred while attempting to delete the product line.')
  2091. )));
  2092.  
  2093. // Get invoices collection
  2094. $invoice_collection = $order->getInvoicesCollection();
  2095.  
  2096. $invoice_array = array();
  2097. foreach ($invoice_collection as $invoice)
  2098. {
  2099. $invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int)$order->id_shop);
  2100. $invoice_array[] = $invoice;
  2101. }
  2102.  
  2103. // Assign to smarty informations in order to show the new product line
  2104. $this->context->smarty->assign(array(
  2105. 'order' => $order,
  2106. 'currency' => new Currency($order->id_currency),
  2107. 'invoices_collection' => $invoice_collection,
  2108. 'current_id_lang' => Context::getContext()->language->id,
  2109. 'link' => Context::getContext()->link,
  2110. 'current_index' => self::$currentIndex
  2111. ));
  2112.  
  2113. $this->sendChangedNotification($order);
  2114.  
  2115. die(Tools::jsonEncode(array(
  2116. 'result' => $res,
  2117. 'order' => $order,
  2118. 'invoices' => $invoice_array,
  2119. 'documents_html' => $this->createTemplate('_documents.tpl')->fetch(),
  2120. 'shipping_html' => $this->createTemplate('_shipping.tpl')->fetch()
  2121. )));
  2122. }
  2123.  
  2124. protected function doEditProductValidation(OrderDetail $order_detail, Order $order, OrderInvoice $order_invoice = null)
  2125. {
  2126. if (!Validate::isLoadedObject($order_detail))
  2127. die(Tools::jsonEncode(array(
  2128. 'result' => false,
  2129. 'error' => Tools::displayError('The Order Detail object could not be loaded.')
  2130. )));
  2131.  
  2132. if (!empty($order_invoice) && !Validate::isLoadedObject($order_invoice))
  2133. die(Tools::jsonEncode(array(
  2134. 'result' => false,
  2135. 'error' => Tools::displayError('The invoice object cannot be loaded.')
  2136. )));
  2137.  
  2138. if (!Validate::isLoadedObject($order))
  2139. die(Tools::jsonEncode(array(
  2140. 'result' => false,
  2141. 'error' => Tools::displayError('The order object cannot be loaded.')
  2142. )));
  2143.  
  2144. if ($order_detail->id_order != $order->id)
  2145. die(Tools::jsonEncode(array(
  2146. 'result' => false,
  2147. 'error' => Tools::displayError('You cannot edit the order detail for this order.')
  2148. )));
  2149.  
  2150. // We can't edit a delivered order
  2151. if ($order->hasBeenDelivered())
  2152. die(Tools::jsonEncode(array(
  2153. 'result' => false,
  2154. 'error' => Tools::displayError('You cannot edit a delivered order.')
  2155. )));
  2156.  
  2157. if (!empty($order_invoice) && $order_invoice->id_order != Tools::getValue('id_order'))
  2158. die(Tools::jsonEncode(array(
  2159. 'result' => false,
  2160. 'error' => Tools::displayError('You cannot use this invoice for the order')
  2161. )));
  2162.  
  2163. // Clean price
  2164. $product_price_tax_incl = str_replace(',', '.', Tools::getValue('product_price_tax_incl'));
  2165. $product_price_tax_excl = str_replace(',', '.', Tools::getValue('product_price_tax_excl'));
  2166.  
  2167. if (!Validate::isPrice($product_price_tax_incl) || !Validate::isPrice($product_price_tax_excl))
  2168. die(Tools::jsonEncode(array(
  2169. 'result' => false,
  2170. 'error' => Tools::displayError('Invalid price')
  2171. )));
  2172.  
  2173. if (!is_array(Tools::getValue('product_quantity')) && !Validate::isUnsignedInt(Tools::getValue('product_quantity')))
  2174. die(Tools::jsonEncode(array(
  2175. 'result' => false,
  2176. 'error' => Tools::displayError('Invalid quantity')
  2177. )));
  2178. elseif (is_array(Tools::getValue('product_quantity')))
  2179. foreach (Tools::getValue('product_quantity') as $qty)
  2180. if (!Validate::isUnsignedInt($qty))
  2181. die(Tools::jsonEncode(array(
  2182. 'result' => false,
  2183. 'error' => Tools::displayError('Invalid quantity')
  2184. )));
  2185. }
  2186.  
  2187. protected function doDeleteProductLineValidation(OrderDetail $order_detail, Order $order)
  2188. {
  2189. if (!Validate::isLoadedObject($order_detail))
  2190. die(Tools::jsonEncode(array(
  2191. 'result' => false,
  2192. 'error' => Tools::displayError('The Order Detail object could not be loaded.')
  2193. )));
  2194.  
  2195. if (!Validate::isLoadedObject($order))
  2196. die(Tools::jsonEncode(array(
  2197. 'result' => false,
  2198. 'error' => Tools::displayError('The order object cannot be loaded.')
  2199. )));
  2200.  
  2201. if ($order_detail->id_order != $order->id)
  2202. die(Tools::jsonEncode(array(
  2203. 'result' => false,
  2204. 'error' => Tools::displayError('You cannot delete the order detail.')
  2205. )));
  2206.  
  2207. // We can't edit a delivered order
  2208. if ($order->hasBeenDelivered())
  2209. die(Tools::jsonEncode(array(
  2210. 'result' => false,
  2211. 'error' => Tools::displayError('You cannot edit a delivered order.')
  2212. )));
  2213. }
  2214.  
  2215. protected function getProducts($order)
  2216. {
  2217. $products = $order->getProducts();
  2218.  
  2219. foreach ($products as &$product)
  2220. {
  2221. if ($product['image'] != null)
  2222. {
  2223. $name = 'product_mini_'.(int)$product['product_id'].(isset($product['product_attribute_id']) ? '_'.(int)$product['product_attribute_id'] : '').'.jpg';
  2224. // generate image cache, only for back office
  2225. $product['image_tag'] = ImageManager::thumbnail(_PS_IMG_DIR_.'p/'.$product['image']->getExistingImgPath().'.jpg', $name, 45, 'jpg');
  2226. if (file_exists(_PS_TMP_IMG_DIR_.$name))
  2227. $product['image_size'] = getimagesize(_PS_TMP_IMG_DIR_.$name);
  2228. else
  2229. $product['image_size'] = false;
  2230. }
  2231. }
  2232.  
  2233. return $products;
  2234. }
  2235.  
  2236. protected function reinjectQuantity($order_detail, $qty_cancel_product)
  2237. {
  2238. // Reinject product
  2239. $reinjectable_quantity = (int)$order_detail->product_quantity - (int)$order_detail->product_quantity_reinjected;
  2240. $quantity_to_reinject = $qty_cancel_product > $reinjectable_quantity ? $reinjectable_quantity : $qty_cancel_product;
  2241. // @since 1.5.0 : Advanced Stock Management
  2242. $product_to_inject = new Product($order_detail->product_id, false, (int)$this->context->language->id, (int)$order_detail->id_shop);
  2243.  
  2244. $product = new Product($order_detail->product_id, false, (int)$this->context->language->id, (int)$order_detail->id_shop);
  2245.  
  2246. if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $product->advanced_stock_management && $order_detail->id_warehouse != 0)
  2247. {
  2248. $manager = StockManagerFactory::getManager();
  2249. $movements = StockMvt::getNegativeStockMvts(
  2250. $order_detail->id_order,
  2251. $order_detail->product_id,
  2252. $order_detail->product_attribute_id,
  2253. $quantity_to_reinject
  2254. );
  2255. $left_to_reinject = $quantity_to_reinject;
  2256. foreach ($movements as $movement)
  2257. {
  2258. if ($left_to_reinject > $movement['physical_quantity'])
  2259. $quantity_to_reinject = $movement['physical_quantity'];
  2260.  
  2261. $left_to_reinject -= $quantity_to_reinject;
  2262.  
  2263. $manager->addProduct(
  2264. $order_detail->product_id,
  2265. $order_detail->product_attribute_id,
  2266. new Warehouse($movement['id_warehouse']),
  2267. $quantity_to_reinject,
  2268. null,
  2269. $movement['price_te'],
  2270. true
  2271. );
  2272. }
  2273. StockAvailable::synchronize($order_detail->product_id);
  2274. }
  2275. elseif ($order_detail->id_warehouse == 0)
  2276. {
  2277. StockAvailable::updateQuantity(
  2278. $order_detail->product_id,
  2279. $order_detail->product_attribute_id,
  2280. $quantity_to_reinject,
  2281. $order_detail->id_shop
  2282. );
  2283. }
  2284. else
  2285. $this->errors[] = Tools::displayError('This product cannot be re-stocked.');
  2286. }
  2287.  
  2288. protected function applyDiscountOnInvoice($order_invoice, $value_tax_incl, $value_tax_excl)
  2289. {
  2290. // Update OrderInvoice
  2291. $order_invoice->total_discount_tax_incl += $value_tax_incl;
  2292. $order_invoice->total_discount_tax_excl += $value_tax_excl;
  2293. $order_invoice->total_paid_tax_incl -= $value_tax_incl;
  2294. $order_invoice->total_paid_tax_excl -= $value_tax_excl;
  2295. $order_invoice->update();
  2296. }
  2297. }
Advertisement
Add Comment
Please, Sign In to add comment