// management of discount addition when no invoice on current order

// update the discount form when new invoice is created
This commit is contained in:
aFolletete
2011-12-19 18:24:03 +00:00
parent e97baff814
commit f6d29cc757
7 changed files with 139 additions and 108 deletions
@@ -0,0 +1,52 @@
<form method="POST" action="{$currentIndex}&viewOrder&id_order={$smarty.get.id_order|escape:'htmlall':'UTF-8'}&token={$smarty.get.token|escape:'htmlall':'UTF-8'}">
<label>{l s='Name'}</label>
<div class="margin-form">
<input type="text" name="discount_name" value="" />
</div>
<label>{l s='Type'}</label>
<div class="margin-form">
<select name="discount_type" id="discount_type">
<option value="1">{l s='Percent'}</option>
<option value="2">{l s='Amount'}</option>
<option value="3">{l s='Free shipping'}</option>
</select>
</div>
<div id="discount_value_field">
<label>{l s='Value'}</label>
<div class="margin-form">
{if ($currency->format % 2)}
<span id="discount_currency_sign" style="display: none;">{$currency->sign}</span>
{/if}
<input type="text" name="discount_value" size="3" />
{if !($currency->format % 2)}
<span id="discount_currency_sign" style="display: none;">{$currency->sign}</span>
{/if}
<span id="discount_percent_symbol">%</span>
<p class="preference_description" id="discount_value_help" style="width: 95%;display: none;">
{l s='This value must be taxes included.'}
</p>
</div>
</div>
{if $order->hasInvoice()}
<label>{l s='Invoice'}</label>
<div class="margin-form">
<select name="discount_invoice">
{foreach from=$invoices_collection item=invoice}
<option value="{$invoice->id}" selected="selected">{$invoice->getInvoiceNumberFormatted($current_id_lang)} - {displayPrice price=$invoice->total_paid_tax_incl currency=$order->id_currency}</option>
{/foreach}
</select><br />
<input type="checkbox" name="discount_all_invoices" id="discount_all_invoices" value="1" /> <label class="t" for="discount_all_invoices">{l s='Apply on all invoices'}</label>
<p class="preference_description" style="width: 95%">
{l s='If you select to create this discount for all invoices, one discount will be created per order invoice.'}
</p>
</div>
{/if}
<p class="center">
<input class="button" type="submit" name="submitNewVoucher" value="{l s='Add'}" />&nbsp;
<a href="#" id="cancel_add_voucher">{l s='Cancel'}</a>
</p>
</form>
@@ -15,7 +15,7 @@
{*/if*}
<td class="document_date">{dateFormat date=$document->date_add}</td>
<td class="document_type">Invoice</td>
<td class="document_number"><a href="pdf.php?pdf&id_order_invoice={$document->id}">#{Configuration::get('PS_INVOICE_PREFIX', $current_id_lang)}{'%06d'|sprintf:$document->number}</a></td>
<td class="document_number"><a href="pdf.php?pdf&id_order_invoice={$document->id}">{$document->getInvoiceNumberFormatted($current_id_lang)}</a></td>
<td class="document_amount">
{*if TYPE DOCUMENT = INVOICE *}
{displayPrice price=$document->total_paid_tax_incl currency=$currency->id}&nbsp;
@@ -24,7 +24,7 @@
<select name="add_product[invoice]" id="add_product_product_invoice" disabled="disabled">
<optgroup class="existing" label="{l s='Existing'}">
{foreach from=$invoices_collection item=invoice}
<option value="{$invoice->id}">#{Configuration::get('PS_INVOICE_PREFIX', $current_id_lang)}{'%06d'|sprintf:$invoice->number}</option>
<option value="{$invoice->id}">{$invoice->getInvoiceNumberFormatted($current_id_lang)}</option>
{/foreach}
</optgroup>
<optgroup label="{l s='New'}">
+3 -52
View File
@@ -231,7 +231,7 @@
<td>{displayPrice price=$payment->amount currency=$payment->id_currency}</td>
<td>
{if $payment->id_order_invoice}
#{Configuration::get('PS_INVOICE_PREFIX', $current_id_lang)}{'%06d'|sprintf:OrderInvoice::retrieveOneById($payment->id_order_invoice)->number}
{OrderInvoice::retrieveOneById($payment->id_order_invoice)->getInvoiceNumberFormatted($current_id_lang)}
{else}
{l s='No invoice'}
{/if}
@@ -270,7 +270,7 @@
<td>
<select name="payment_invoice" id="payment_invoice">
{foreach from=$not_paid_invoices_collection item=invoice}
<option value="{$invoice->id}" selected="selected">#{Configuration::get('PS_INVOICE_PREFIX', $current_id_lang)}{'%06d'|sprintf:$invoice->number}</option>
<option value="{$invoice->id}" selected="selected">{$invoice->getInvoiceNumberFormatted($current_id_lang)}</option>
{/foreach}
</select>
</td>
@@ -570,56 +570,7 @@
</tr>
<tr style="display: none" >
<td colspan="3" class="current-edit" id="voucher_form">
<form method="POST" action="{$currentIndex}&viewOrder&id_order={$smarty.get.id_order|escape:'htmlall':'UTF-8'}&token={$smarty.get.token|escape:'htmlall':'UTF-8'}">
<label>{l s='Name'}</label>
<div class="margin-form">
<input type="text" name="discount_name" value="" />
</div>
<label>{l s='Type'}</label>
<div class="margin-form">
<select name="discount_type" id="discount_type">
<option value="1">{l s='Percent'}</option>
<option value="2">{l s='Amount'}</option>
<option value="3">{l s='Free shipping'}</option>
</select>
</div>
<div id="discount_value_field">
<label>{l s='Value'}</label>
<div class="margin-form">
{if ($currency->format % 2)}
<span id="discount_currency_sign" style="display: none;">{$currency->sign}</span>
{/if}
<input type="text" name="discount_value" size="3" />
{if !($currency->format % 2)}
<span id="discount_currency_sign" style="display: none;">{$currency->sign}</span>
{/if}
<span id="discount_percent_symbol">%</span>
<p class="preference_description" id="discount_value_help" style="width: 95%;display: none;">
{l s='This value must be taxes included.'}
</p>
</div>
</div>
<label>{l s='Invoice'}</label>
<div class="margin-form">
<select name="discount_invoice">
{foreach from=$invoices_collection item=invoice}
<option value="{$invoice->id}" selected="selected">#{Configuration::get('PS_INVOICE_PREFIX', $current_id_lang)}{'%06d'|sprintf:$invoice->number} - {displayPrice price=$invoice->total_paid_tax_incl currency=$order->id_currency}</option>
{/foreach}
</select><br />
<input type="checkbox" name="discount_all_invoices" id="discount_all_invoices" value="1" /> <label class="t" for="discount_all_invoices">{l s='Apply on all invoices'}</label>
<p class="preference_description" style="width: 95%">
{l s='If you select to create this discount for all invoices, one discount will be created per order invoice.'}
</p>
</div>
<p class="center">
<input class="button" type="submit" name="submitNewVoucher" value="{l s='Add'}" />&nbsp;
<a href="#" id="cancel_add_voucher">{l s='Cancel'}</a>
</p>
</form>
{include file='orders/_discount_form.tpl'}
</td>
</tr>
{/if}
+16 -3
View File
@@ -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);
}
}
+58 -51
View File
@@ -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();
}
}
+8
View File
@@ -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);