From f6d29cc75737ea5037966c53274e9cc7cfa00a53 Mon Sep 17 00:00:00 2001 From: aFolletete Date: Mon, 19 Dec 2011 18:24:03 +0000 Subject: [PATCH] // management of discount addition when no invoice on current order // update the discount form when new invoice is created --- .../themes/template/orders/_discount_form.tpl | 52 +++++++++ .../themes/template/orders/_documents.tpl | 2 +- .../themes/template/orders/_new_product.tpl | 2 +- admin-dev/themes/template/orders/view.tpl | 55 +-------- classes/order/OrderInvoice.php | 19 ++- controllers/admin/AdminOrdersController.php | 109 ++++++++++-------- js/admin_order.js | 8 ++ 7 files changed, 139 insertions(+), 108 deletions(-) create mode 100644 admin-dev/themes/template/orders/_discount_form.tpl diff --git a/admin-dev/themes/template/orders/_discount_form.tpl b/admin-dev/themes/template/orders/_discount_form.tpl new file mode 100644 index 000000000..ee8880223 --- /dev/null +++ b/admin-dev/themes/template/orders/_discount_form.tpl @@ -0,0 +1,52 @@ +
+ +
+ +
+ + +
+ +
+ +
+ +
+ {if ($currency->format % 2)} + + {/if} + + {if !($currency->format % 2)} + + {/if} + % + +
+
+ + {if $order->hasInvoice()} + +
+
+ +

+ {l s='If you select to create this discount for all invoices, one discount will be created per order invoice.'} +

+
+ {/if} + +

+   + {l s='Cancel'} +

+
\ No newline at end of file diff --git a/admin-dev/themes/template/orders/_documents.tpl b/admin-dev/themes/template/orders/_documents.tpl index f1a2af478..ad023f470 100644 --- a/admin-dev/themes/template/orders/_documents.tpl +++ b/admin-dev/themes/template/orders/_documents.tpl @@ -15,7 +15,7 @@ {*/if*} {dateFormat date=$document->date_add} Invoice - #{Configuration::get('PS_INVOICE_PREFIX', $current_id_lang)}{'%06d'|sprintf:$document->number} + {$document->getInvoiceNumberFormatted($current_id_lang)} {*if TYPE DOCUMENT = INVOICE *} {displayPrice price=$document->total_paid_tax_incl currency=$currency->id}  diff --git a/admin-dev/themes/template/orders/_new_product.tpl b/admin-dev/themes/template/orders/_new_product.tpl index 5f5f8411b..5ebeeaa75 100644 --- a/admin-dev/themes/template/orders/_new_product.tpl +++ b/admin-dev/themes/template/orders/_new_product.tpl @@ -24,7 +24,7 @@ {foreach from=$not_paid_invoices_collection item=invoice} - + {/foreach} @@ -570,56 +570,7 @@ -
- -
- -
- - -
- -
- -
- -
- {if ($currency->format % 2)} - - {/if} - - {if !($currency->format % 2)} - - {/if} - % - -
-
- - -
-
- -

- {l s='If you select to create this discount for all invoices, one discount will be created per order invoice.'} -

-
- -

-   - {l s='Cancel'} -

-
+ {include file='orders/_discount_form.tpl'} {/if} diff --git a/classes/order/OrderInvoice.php b/classes/order/OrderInvoice.php index da7d4b4c1..b9780f218 100644 --- a/classes/order/OrderInvoice.php +++ b/classes/order/OrderInvoice.php @@ -332,9 +332,11 @@ class OrderInvoiceCore extends ObjectModel foreach ($taxes_infos as $tax_infos) { if (!isset($tmp_tax_infos[$tax_infos['rate']])) - $tmp_tax_infos[$tax_infos['rate']] = array('total_amount' => 0, - 'name' => 0, - 'total_price_tax_excl' => 0); + $tmp_tax_infos[$tax_infos['rate']] = array( + 'total_amount' => 0, + 'name' => 0, + 'total_price_tax_excl' => 0 + ); $tmp_tax_infos[$tax_infos['rate']]['total_amount'] += $tax_infos['total_amount']; $tmp_tax_infos[$tax_infos['rate']]['name'] = $tax_infos['name']; @@ -506,4 +508,15 @@ class OrderInvoiceCore extends ObjectModel $order_payments->where('id_order_invoice', '=', $this->id); return $order_payments; } + + /** + * Get the formatted number of invoice + * @since 1.5.0.2 + * @param int $id_lang for invoice_prefix + * @return string + */ + public function getInvoiceNumberFormatted($id_lang) + { + return '#'.Configuration::get('PS_INVOICE_PREFIX', $id_lang).sprintf('%06d', $this->number); + } } \ No newline at end of file diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 82b871d79..0fce85607 100755 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -434,9 +434,9 @@ class AdminOrdersControllerCore extends AdminController // @since 1.5.0 : Advanced Stock Management $product_to_inject = new Product($order_detail->product_id, false, $this->context->language->id, $order->id_shop); - + $product = new Product($order_detail->product_id); - + if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $product->advanced_stock_management && $order_detail->id_warehouse != 0 @@ -788,12 +788,15 @@ class AdminOrdersControllerCore extends AdminController if (!Validate::isLoadedObject($order)) throw new PrestashopException('Can\'t load Order object'); - // If the discount is for only one invoice - if (!Tools::isSubmit('discount_all_invoices')) + if ($order->hasInvoice()) { - $order_invoice = new OrderInvoice(Tools::getValue('discount_invoice')); - if (!Validate::isLoadedObject($order_invoice)) - throw new PrestashopException('Can\'t load Order Invoice object'); + // If the discount is for only one invoice + if (!Tools::isSubmit('discount_all_invoices')) + { + $order_invoice = new OrderInvoice(Tools::getValue('discount_invoice')); + if (!Validate::isLoadedObject($order_invoice)) + throw new PrestashopException('Can\'t load Order Invoice object'); + } } $cart_rules = array(); @@ -809,14 +812,9 @@ class AdminOrdersControllerCore extends AdminController $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($order_invoice->total_paid_tax_excl * Tools::getValue('discount_value') / 100, 2); // Update OrderInvoice - $order_invoice->total_discount_tax_incl += $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_discount_tax_excl += $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->total_paid_tax_incl -= $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_paid_tax_excl -= $cart_rules[$order_invoice->id]['value_tax_excl']; - - $order_invoice->update(); + $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']); } - else + elseif ($order->hasInvoice()) { $order_invoices_collection = $order->getInvoicesCollection(); foreach($order_invoices_collection as $order_invoice) @@ -825,13 +823,14 @@ class AdminOrdersControllerCore extends AdminController $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round($order_invoice->total_paid_tax_excl * Tools::getValue('discount_value') / 100, 2); // Update OrderInvoice - $order_invoice->total_discount_tax_incl += $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_discount_tax_excl += $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->total_paid_tax_incl -= $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_paid_tax_excl -= $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->update(); + $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']); } } + else + { + $cart_rules[0]['value_tax_incl'] = Tools::ps_round($order->total_paid_tax_incl * Tools::getValue('discount_value') / 100, 2); + $cart_rules[0]['value_tax_excl'] = Tools::ps_round($order->total_paid_tax_excl * Tools::getValue('discount_value') / 100, 2); + } } else $this->_errors[] = Tools::displayError('Discount value is invalid'); @@ -848,29 +847,34 @@ class AdminOrdersControllerCore extends AdminController $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round(Tools::getValue('discount_value') / (1 + ($order->getTaxesAverageUsed() / 100)), 2); // Update OrderInvoice - $order_invoice->total_discount_tax_incl += $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_discount_tax_excl += $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->total_paid_tax_incl -= $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_paid_tax_excl -= $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->update(); + $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']); } } - else + elseif ($order->hasInvoice()) { $order_invoices_collection = $order->getInvoicesCollection(); foreach($order_invoices_collection as $order_invoice) { if (Tools::getValue('discount_value') > $order_invoice->total_paid_tax_incl) - continue; - $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round(Tools::getValue('discount_value'), 2); - $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round(Tools::getValue('discount_value') / (1 + ($order->getTaxesAverageUsed() / 100)), 2); + $this->_errors[] = Tools::displayError('Discount value is superior than the order invoice total (Invoice: ').$order_invoice->getInvoiceNumberFormatted(Context::getContext()->language->id).')'; + else + { + $cart_rules[$order_invoice->id]['value_tax_incl'] = Tools::ps_round(Tools::getValue('discount_value'), 2); + $cart_rules[$order_invoice->id]['value_tax_excl'] = Tools::ps_round(Tools::getValue('discount_value') / (1 + ($order->getTaxesAverageUsed() / 100)), 2); - // Update OrderInvoice - $order_invoice->total_discount_tax_incl += $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_discount_tax_excl += $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->total_paid_tax_incl -= $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_paid_tax_excl -= $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->update(); + // Update OrderInvoice + $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']); + } + } + } + else + { + if (Tools::getValue('discount_value') > $order->total_paid_tax_incl) + $this->_errors[] = Tools::displayError('Discount value is superior than the order total'); + else + { + $cart_rules[0]['value_tax_incl'] = Tools::ps_round(Tools::getValue('discount_value'), 2); + $cart_rules[0]['value_tax_excl'] = Tools::ps_round(Tools::getValue('discount_value') / (1 + ($order->getTaxesAverageUsed() / 100)), 2); } } break; @@ -884,14 +888,10 @@ class AdminOrdersControllerCore extends AdminController $cart_rules[$order_invoice->id]['value_tax_excl'] = $order_invoice->total_shipping_tax_excl; // Update OrderInvoice - $order_invoice->total_discount_tax_incl += $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_discount_tax_excl += $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->total_paid_tax_incl -= $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_paid_tax_excl -= $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->update(); + $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']); } } - else + elseif ($order->hasInvoice()) { $order_invoices_collection = $order->getInvoicesCollection(); foreach($order_invoices_collection as $order_invoice) @@ -902,11 +902,7 @@ class AdminOrdersControllerCore extends AdminController $cart_rules[$order_invoice->id]['value_tax_excl'] = $order_invoice->total_shipping_tax_excl; // Update OrderInvoice - $order_invoice->total_discount_tax_incl += $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_discount_tax_excl += $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->total_paid_tax_incl -= $cart_rules[$order_invoice->id]['value_tax_incl']; - $order_invoice->total_paid_tax_excl -= $cart_rules[$order_invoice->id]['value_tax_excl']; - $order_invoice->update(); + $this->applyDiscountOnInvoice($order_invoice, $cart_rules[$order_invoice->id]['value_tax_incl'], $cart_rules[$order_invoice->id]['value_tax_excl']); } } break; @@ -990,12 +986,12 @@ class AdminOrdersControllerCore extends AdminController // gets warehouses to ship products, if and only if advanced stock management is activated $warehouse_list = null; - + $order_details = $order->getOrderDetailList(); foreach ($order_details as $order_detail) { $product = new Product($order_detail['product_id']); - + if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $product->advanced_stock_management ) @@ -1354,7 +1350,7 @@ class AdminOrdersControllerCore extends AdminController $invoice_array = array(); foreach($invoice_collection as $invoice) { - $invoice->name = '#'.Configuration::get('PS_INVOICE_PREFIX', Context::getContext()->language->id).sprintf('%06d', $invoice->number); + $invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id); $invoice_array[] = $invoice; } @@ -1377,7 +1373,8 @@ class AdminOrdersControllerCore extends AdminController 'order' => $order, 'invoices' => $invoice_array, 'documents_html' => $this->context->smarty->fetch('orders/_documents.tpl'), - 'shipping_html' => $this->context->smarty->fetch('orders/_shipping.tpl') + 'shipping_html' => $this->context->smarty->fetch('orders/_shipping.tpl'), + 'discount_form_html' => $this->context->smarty->fetch('orders/_discount_form.tpl') ))); } @@ -1518,7 +1515,7 @@ class AdminOrdersControllerCore extends AdminController $invoice_array = array(); foreach($invoice_collection as $invoice) { - $invoice->name = '#'.Configuration::get('PS_INVOICE_PREFIX', Context::getContext()->language->id).sprintf('%06d', $invoice->number); + $invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id); $invoice_array[] = $invoice; } @@ -1584,7 +1581,7 @@ class AdminOrdersControllerCore extends AdminController $invoice_array = array(); foreach($invoice_collection as $invoice) { - $invoice->name = '#'.Configuration::get('PS_INVOICE_PREFIX', Context::getContext()->language->id).sprintf('%06d', $invoice->number); + $invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id); $invoice_array[] = $invoice; } @@ -1708,5 +1705,15 @@ class AdminOrdersControllerCore extends AdminController return $products; } + + protected function applyDiscountOnInvoice($order_invoice, $value_tax_incl, $value_tax_excl) + { + // Update OrderInvoice + $order_invoice->total_discount_tax_incl += $value_tax_incl; + $order_invoice->total_discount_tax_excl += $value_tax_excl; + $order_invoice->total_paid_tax_incl -= $value_tax_incl; + $order_invoice->total_paid_tax_excl -= $value_tax_excl; + $order_invoice->update(); + } } diff --git a/js/admin_order.js b/js/admin_order.js index 2beae95b7..177d4dc75 100644 --- a/js/admin_order.js +++ b/js/admin_order.js @@ -92,6 +92,11 @@ function updateShipping(shipping_html) $('#shipping_table_old').remove(); } +function updateDiscountForm(discount_form_html) +{ + $('#voucher_form').html(discount_form_html); +} + function populateWarehouseList(warehouse_list) { $('#add_product_product_warehouse_area').hide(); @@ -419,6 +424,7 @@ function init() updateInvoice(data.invoices); updateDocuments(data.documents_html); updateShipping(data.shipping_html); + updateDiscountForm(data.discount_form_html); // Initialize all events init(); @@ -591,6 +597,7 @@ function init() updateAmounts(data.order); updateInvoice(data.invoices); updateDocuments(data.documents_html); + updateDiscountForm(data.discount_form_html); // Initialize all events init(); @@ -665,6 +672,7 @@ function init() updateAmounts(data.order); updateInvoice(data.invoices); updateDocuments(data.documents_html); + updateDiscountForm(data.discount_form_html); } else alert(data.error);