// fix calculation issue on product addition

// all elements are correctly updated when we adding, editing or deleting a product
This commit is contained in:
aFolletete
2011-12-17 16:20:03 +00:00
parent 039b2388b9
commit ef46bd8819
6 changed files with 715 additions and 599 deletions
@@ -0,0 +1,62 @@
<table class="table" width="100%;" cellspacing="0" cellpadding="0" id="documents_table">
<thead>
<tr>
<th style="width:20%">{l s='Date'}</th>
<th style="width:25%">{l s='Document'}</th>
<th style="width:20%">{l s='Number'}</th>
<th>{l s='Amount'}</th>
<th style="width:42px"></th>
</tr>
</thead>
<tbody>
{foreach from=$order->getDocuments() item=document}
{*if TYPE DOCUMENT = INVOICE *}
<tr class="invoice_line" id="invoice_{$document->id}">
{*/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_amount">
{*if TYPE DOCUMENT = INVOICE *}
{displayPrice price=$document->total_paid_tax_incl currency=$currency->id}&nbsp;
{if $document->getRestPaid()}
<span style="color:red;font-weight:bold;">({displayPrice price=$document->getRestPaid() currency=$currency->id} {l s='not paid'})</span>
{/if}
{*/if*}
</td>
<td class="right document_action">
{*if TYPE DOCUMENT = INVOICE *}
{if $document->getRestPaid()}
<a href="#" class="js-set-payment" data-amount="{$document->getRestPaid()}" data-id-invoice="{$document->id}" title="{l s='Set payment form'}"><img src="../img/admin/money_add.png" alt="{l s='Set payment form'}" /></a>
{/if}
<a href="#" onclick="$('#invoiceNote{$document->id}').show(); return false;" title="{if $document->note eq ''}{l s='Add note'}{else}{l s='Edit note'}{/if}"><img src="../img/admin/note.png" alt="{if $document->note eq ''}{l s='Add note'}{else}{l s='Edit note'}{/if}"{if $document->note eq ''} class="js-disabled-action"{/if} /></a>
{*/if*}
</td>
</tr>
{*if TYPE DOCUMENT = INVOICE *}
<tr id="invoiceNote{$document->id}" style="display:none" class="current-edit">
<td colspan="5">
<form action="{$currentIndex}&viewOrder&id_order={$smarty.get.id_order|escape:'htmlall':'UTF-8'}&token={$smarty.get.token|escape:'htmlall':'UTF-8'}" method="post">
<p>
<label for="editNote{$document->id}" class="t">{l s='Note'}</label>
<input type="hidden" name="id_order_invoice" value="{$document->id}" />
<textarea name="note" rows="10" cols="10" id="editNote{$document->id}" class="edit-note">{$document->note|escape:'htmlall':'UTF-8'}</textarea>
</p>
<p class="right">
<input type="submit" name="submitEditNote" value="{l s='Save'}" class="button" />
<input type="button" name="cancelNote" id="cancelNote" value="{l s='Cancel'}" onclick="$('#invoiceNote{$document->id}').hide();" class="button" />
</p>
</form>
</td>
</tr>
{*/if*}
{foreachelse}
<tr>
<td colspan="5" class="center">
<h3>{l s='No document is available'}</h3>
<p><a class="button" href="{$currentIndex}&viewOrder&submitGenerateInvoice&id_order={$smarty.get.id_order|escape:'htmlall':'UTF-8'}&token={$smarty.get.token|escape:'htmlall':'UTF-8'}">{l s='Generate invoice'}</a></p>
</td>
</tr>
{/foreach}
</tbody>
</table>
@@ -22,7 +22,7 @@
<td style="display:none;" align="center" colspan="2">
{if sizeof($invoices_collection)}
<select name="add_product[invoice]" id="add_product_product_invoice" disabled="disabled">
<optgroup label="{l s='Existing'}">
<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>
{/foreach}
@@ -0,0 +1,42 @@
<table class="table" width="100%" cellspacing="0" cellpadding="0" id="shipping_table">
<thead>
<tr>
<th style="width:30%">{l s='Date:'}</th>
<th>{l s='Type'}</th>
<th style="width:20%">{l s='Carrier'}</th>
<th>{l s='Weight'}</th>
<th style="width:15%">{l s='Shipping cost'}</th>
<th style="width:30%">{l s='Tracking number'}</th>
</tr>
</thead>
<tbody>
{foreach from=$order->getShipping() item=line}
<tr>
<td>{$line.date_add}</td>
<td>{$line.type}</td>
<td>{$line.state_name}</td>
<td>{$line.weight|string_format:"%.3f"} {Configuration::get('PS_WEIGHT_UNIT')}</td>
<td>
{if $order->getTaxCalculationMethod() == $smarty.const.PS_TAX_INC}
{displayPrice price=$line.shipping_cost_tax_incl currency=$currency->id}
{else}
{displayPrice price=$line.shipping_cost_tax_excl currency=$currency->id}
{/if}
</td>
<td>
<span id="shipping_number_show">{if $line.url && $line.tracking_number}<a href="{$line.url|replace:'@':$line.tracking_number}">{$line.tracking_number}</a>{else}{$line.tracking_number}{/if}</span>
{if $line.can_edit}
<form style="display: inline;" method="POST" action="{$link->getAdminLink('AdminOrders')}&vieworder&id_order={$smarty.get.id_order|escape:'htmlall':'UTF-8'}&id_order_invoice={if $line.id_order_invoice}{$line.id_order_invoice|escape:'htmlall':'UTF-8'}{else}0{/if}&id_carrier={if $line.id_carrier}{$line.id_carrier|escape:'htmlall':'UTF-8'}{else}0{/if}">
<span class="shipping_number_edit" style="display:none;">
<input type="text" name="tracking_number" value="{$line.tracking_number}" />
<input type="submit" class="button" name="submitShippingNumber" value="{l s='Update'}" />
</span>
<a href="#" class="edit_shipping_number_link"><img src="../img/admin/edit.gif" alt="{l s='Edit'}" /></a>
<a href="#" class="cancel_shipping_number_link" style="display: none;"><img src="../img/admin/disabled.gif" alt="{l s='Cancel'}" /></a>
</form>
{/if}
</td>
</tr>
{/foreach}
</tbody>
</table>
+4 -103
View File
@@ -218,66 +218,8 @@
<fieldset style="width: 100%">
<legend><img src="../img/admin/details.gif" /> {l s='Documents'}</legend>
<table class="table" width="100%;" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="width:20%">{l s='Date'}</th>
<th style="width:25%">{l s='Document'}</th>
<th style="width:20%">{l s='Number'}</th>
<th>{l s='Amount'}</th>
<th style="width:42px"></th>
</tr>
</thead>
<tbody>
{foreach from=$order->getDocuments() item=document}
<tr>
<td>{dateFormat date=$document->date_add}</td>
<td>Invoice</td>
<td><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>
{*if TYPE DOCUMENT = INVOICE *}
{displayPrice price=$document->total_paid_tax_incl currency=$currency->id}&nbsp;
{if $document->getRestPaid()}
<span style="color:red;font-weight:bold;">({displayPrice price=$document->getRestPaid() currency=$currency->id} {l s='not paid'})</span>
{/if}
{*/if*}
</td>
<td class="right">
{*if TYPE DOCUMENT = INVOICE *}
{if $document->getRestPaid()}
<a href="#" class="js-set-payment" data-amount="{$document->getRestPaid()}" data-id-invoice="{$document->id}" title="{l s='Set payment form'}"><img src="../img/admin/money_add.png" alt="{l s='Set payment form'}" /></a>
{/if}
<a href="#" onclick="$('#invoiceNote{$document->id}').show(); return false;" title="{if $document->note eq ''}{l s='Add note'}{else}{l s='Edit note'}{/if}"><img src="../img/admin/note.png" alt="{if $document->note eq ''}{l s='Add note'}{else}{l s='Edit note'}{/if}"{if $document->note eq ''} class="js-disabled-action"{/if} /></a>
{*/if*}
</td>
</tr>
{*if TYPE DOCUMENT = INVOICE *}
<tr id="invoiceNote{$document->id}" style="display:none" class="current-edit">
<td colspan="5">
<form action="{$currentIndex}&viewOrder&id_order={$smarty.get.id_order|escape:'htmlall':'UTF-8'}&token={$smarty.get.token|escape:'htmlall':'UTF-8'}" method="post">
<p>
<label for="editNote{$document->id}" class="t">{l s='Note'}</label>
<input type="hidden" name="id_order_invoice" value="{$document->id}" />
<textarea name="note" rows="10" cols="10" id="editNote{$document->id}" class="edit-note">{$document->note|escape:'htmlall':'UTF-8'}</textarea>
</p>
<p class="right">
<input type="submit" name="submitEditNote" value="{l s='Save'}" class="button" />
<input type="button" name="cancelNote" id="cancelNote" value="{l s='Cancel'}" onclick="$('#invoiceNote{$document->id}').hide();" class="button" />
</p>
</form>
</td>
</tr>
{*/if*}
{foreachelse}
<tr>
<td colspan="5" class="center">
<h3>{l s='No document is available'}</h3>
<p><a class="button" href="{$currentIndex}&viewOrder&submitGenerateInvoice&id_order={$smarty.get.id_order|escape:'htmlall':'UTF-8'}&token={$smarty.get.token|escape:'htmlall':'UTF-8'}">{l s='Generate invoice'}</a></p>
</td>
</tr>
{/foreach}
</tbody>
</table>
{* Include document template *}
{include file='orders/_documents.tpl'}
</fieldset>
<br />
@@ -364,7 +306,7 @@
</select>
</td>
<td>
<select name="payment_invoice">
<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>
{/foreach}
@@ -406,48 +348,7 @@
</div>
<div class="clear" style="margin-bottom: 10px;"></div>
<table class="table" width="100%" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="width:30%">{l s='Date:'}</th>
<th>{l s='Type'}</th>
<th style="width:20%">{l s='Carrier'}</th>
<th>{l s='Weight'}</th>
<th style="width:15%">{l s='Shipping cost'}</th>
<th style="width:30%">{l s='Tracking number'}</th>
</tr>
</thead>
<tbody>
{foreach from=$order->getShipping() item=line}
<tr>
<td>{$line.date_add}</td>
<td>{$line.type}</td>
<td>{$line.state_name}</td>
<td>{$line.weight|string_format:"%.3f"} {Configuration::get('PS_WEIGHT_UNIT')}</td>
<td>
{if $order->getTaxCalculationMethod() == $smarty.const.PS_TAX_INC}
{displayPrice price=$line.shipping_cost_tax_incl currency=$currency->id}
{else}
{displayPrice price=$line.shipping_cost_tax_excl currency=$currency->id}
{/if}
</td>
<td>
<span id="shipping_number_show">{if $line.url && $line.tracking_number}<a href="{$line.url|replace:'@':$line.tracking_number}">{$line.tracking_number}</a>{else}{$line.tracking_number}{/if}</span>
{if $line.can_edit}
<form style="display: inline;" method="POST" action="{$link->getAdminLink('AdminOrders')}&vieworder&id_order={$smarty.get.id_order|escape:'htmlall':'UTF-8'}&id_order_invoice={if $line.id_order_invoice}{$line.id_order_invoice|escape:'htmlall':'UTF-8'}{else}0{/if}&id_carrier={if $line.id_carrier}{$line.id_carrier|escape:'htmlall':'UTF-8'}{else}0{/if}">
<span class="shipping_number_edit" style="display:none;">
<input type="text" name="tracking_number" value="{$line.tracking_number}" />
<input type="submit" class="button" name="submitShippingNumber" value="{l s='Update'}" />
</span>
<a href="#" class="edit_shipping_number_link"><img src="../img/admin/edit.gif" alt="{l s='Edit'}" /></a>
<a href="#" class="cancel_shipping_number_link" style="display: none;"><img src="../img/admin/disabled.gif" alt="{l s='Cancel'}" /></a>
</form>
{/if}
</td>
</tr>
{/foreach}
</tbody>
</table>
{include file='orders/_shipping.tpl'}
{if $carrierModuleCall}
{$carrierModuleCall}
+81 -33
View File
@@ -111,7 +111,7 @@ class AdminOrdersControllerCore extends AdminController
if (!$order->hasBeenDelivered())
$this->toolbar_btn['new'] = array(
'short' => 'Create',
'href' => '',
'href' => '#',
'desc' => $this->l('Add a product'),
'class' => 'add_product'
);
@@ -959,7 +959,10 @@ class AdminOrdersControllerCore extends AdminController
)));
$product_informations = $_POST['add_product'];
$invoice_informations = $_POST['add_invoice'];
if (isset($_POST['add_invoice']))
$invoice_informations = $_POST['add_invoice'];
else
$invoice_informations = array();
$product = new Product($product_informations['product_id'], false, $order->id_lang);
if (!Validate::isLoadedObject($product))
die(Tools::jsonEncode(array(
@@ -977,6 +980,9 @@ class AdminOrdersControllerCore extends AdminController
)));
}
// Total method
$total_method = Cart::BOTH_WITHOUT_SHIPPING;
// Create new cart
$cart = new Cart();
$cart->id_group_shop = $order->id_group_shop;
@@ -1010,23 +1016,7 @@ class AdminOrdersControllerCore extends AdminController
{
$reduction_tax_incl = $initial_prodcut_price_tax_incl - $product_informations['product_price_tax_incl'];
// FIXME Cart Rule isn't applied but correctly save in database
$cart_rule = new CartRule();
$cart_rule->id_customer = $order->id_customer;
$cart_rule->date_from = date('Y-m-d H:i:s', time());
$cart_rule->date_to = date('Y-m-d H:i:s', time() + 24 * 3600);
$cart_rule->quantity = 1;
$cart_rule->quantity_per_user = 1;
$cart_rule->minimum_amount_currency = $order->id_currency;
$cart_rule->reduction_product = $product->id;
$cart_rule->reduction_amount = $reduction_tax_incl;
$cart_rule->reduction_currency = $order->id_currency;
$cart_rule->reduction_tax = true;
$cart_rule->active = 1;
$cart_rule->add();
$cart->addCartRule($cart_rule->id);
$order->addCartRule($cart_rule->id, $cart_rule->name, $cart_rule->getContextualValue(true));
// TODO Use Specific price !!!
}
// If order is valid, we can create a new invoice or edit an existing invoice
@@ -1036,10 +1026,11 @@ class AdminOrdersControllerCore extends AdminController
// Create new invoice
if ($order_invoice->id == 0)
{
// If we create a new invoice, we calculate shipping cost
$total_method = Cart::BOTH;
// Create Cart rule in order to make free shipping
if (isset($invoice_informations['free_shipping']) && $invoice_informations['free_shipping'])
{
// FIXME Cart Rule isn't applied but correctly save in database
$cart_rule = new CartRule();
$cart_rule->id_customer = $order->id_customer;
$cart_rule->name = array(
@@ -1055,6 +1046,7 @@ class AdminOrdersControllerCore extends AdminController
$cart_rule->active = 1;
$cart_rule->add();
// Add cart rule to cart and in order
$cart->addCartRule($cart_rule->id);
$order->addCartRule($cart_rule->id, $cart_rule->name[Configuration::get('PS_LANG_DEFAULT')], $cart_rule->getContextualValue(true));
}
@@ -1065,8 +1057,8 @@ class AdminOrdersControllerCore extends AdminController
else
$order_invoice->number = Order::getLastInvoiceNumber() + 1;
$order_invoice->total_paid_tax_excl = Tools::ps_round((float)$cart->getOrderTotal(false, Cart::BOTH), 2);
$order_invoice->total_paid_tax_incl = Tools::ps_round((float)$cart->getOrderTotal($use_taxes, Cart::BOTH), 2);
$order_invoice->total_paid_tax_excl = Tools::ps_round((float)$cart->getOrderTotal(false, $total_method), 2);
$order_invoice->total_paid_tax_incl = Tools::ps_round((float)$cart->getOrderTotal($use_taxes, $total_method), 2);
$order_invoice->total_products = (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS);
$order_invoice->total_products_wt = (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS);
$order_invoice->total_shipping_tax_excl = (float)$cart->getTotalShippingCost(null, false);
@@ -1097,8 +1089,8 @@ class AdminOrdersControllerCore extends AdminController
// Update current invoice
else
{
$order_invoice->total_paid_tax_excl += Tools::ps_round((float)($cart->getOrderTotal(false, Cart::BOTH)), 2);
$order_invoice->total_paid_tax_incl += Tools::ps_round((float)($cart->getOrderTotal($use_taxes, Cart::BOTH)), 2);
$order_invoice->total_paid_tax_excl += Tools::ps_round((float)($cart->getOrderTotal(false, $total_method)), 2);
$order_invoice->total_paid_tax_incl += Tools::ps_round((float)($cart->getOrderTotal($use_taxes, $total_method)), 2);
$order_invoice->total_products += (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS);
$order_invoice->total_products_wt += (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS);
$order_invoice->total_shipping_tax_excl += (float)$cart->getTotalShippingCost(null, false);
@@ -1117,9 +1109,9 @@ class AdminOrdersControllerCore extends AdminController
$order->total_products += (float)$cart->getOrderTotal(false, Cart::ONLY_PRODUCTS);
$order->total_products_wt += (float)$cart->getOrderTotal($use_taxes, Cart::ONLY_PRODUCTS);
$order->total_paid += Tools::ps_round((float)($cart->getOrderTotal(true, Cart::BOTH)), 2);
$order->total_paid_tax_excl += Tools::ps_round((float)($cart->getOrderTotal(false, Cart::BOTH)), 2);
$order->total_paid_tax_incl += Tools::ps_round((float)($cart->getOrderTotal($use_taxes, Cart::BOTH)), 2);
$order->total_paid += Tools::ps_round((float)($cart->getOrderTotal(true, $total_method)), 2);
$order->total_paid_tax_excl += Tools::ps_round((float)($cart->getOrderTotal(false, $total_method)), 2);
$order->total_paid_tax_incl += Tools::ps_round((float)($cart->getOrderTotal($use_taxes, $total_method)), 2);
// discount
$order->total_discounts += (float)abs($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS));
@@ -1133,20 +1125,36 @@ class AdminOrdersControllerCore extends AdminController
// Get the last product
$product = $products[max(array_keys($products))];
// Get invoices collection
$invoice_collection = $order->getInvoicesCollection();
$invoice_array = array();
foreach($invoice_collection as $invoice)
{
$invoice->name = '#'.Configuration::get('PS_INVOICE_PREFIX', Context::getContext()->language->id).sprintf('%06d', $invoice->number);
$invoice_array[] = $invoice;
}
// Assign to smarty informations in order to show the new product line
$this->context->smarty->assign(array(
'product' => $product,
'order' => $order,
'currency' => new Currency($order->id_currency),
'can_edit' => $this->tabAccess['edit'],
'invoices_collection' => $order->getInvoicesCollection()
'invoices_collection' => $invoice_collection,
'current_id_lang' => Context::getContext()->language->id,
'link' => Context::getContext()->link,
'current_index' => self::$currentIndex
));
die(Tools::jsonEncode(array(
'result' => true,
'view' => $this->context->smarty->fetch('orders/_product_line.tpl'),
'can_edit' => $this->tabAccess['add'],
'order' => $order
'order' => $order,
'invoices' => $invoice_array,
'documents_html' => $this->context->smarty->fetch('orders/_documents.tpl'),
'shipping_html' => $this->context->smarty->fetch('orders/_shipping.tpl')
)));
}
@@ -1274,9 +1282,23 @@ class AdminOrdersControllerCore extends AdminController
'product' => $product,
'order' => $order,
'currency' => new Currency($order->id_currency),
'can_edit' => $this->tabAccess['edit']
'can_edit' => $this->tabAccess['edit'],
'invoices_collection' => $invoice_collection,
'current_id_lang' => Context::getContext()->language->id,
'link' => Context::getContext()->link,
'current_index' => self::$currentIndex
));
// Get invoices collection
$invoice_collection = $order->getInvoicesCollection();
$invoice_array = array();
foreach($invoice_collection as $invoice)
{
$invoice->name = '#'.Configuration::get('PS_INVOICE_PREFIX', Context::getContext()->language->id).sprintf('%06d', $invoice->number);
$invoice_array[] = $invoice;
}
if (!$res)
die(Tools::jsonEncode(array(
'result' => $res,
@@ -1287,8 +1309,11 @@ class AdminOrdersControllerCore extends AdminController
'result' => $res,
'view' => $this->context->smarty->fetch('orders/_product_line.tpl'),
'can_edit' => $this->tabAccess['add'],
'invoices_collection' => $order->getInvoicesCollection(),
'order' => $order
'invoices_collection' => $invoice_collection,
'order' => $order,
'invoices' => $invoice_array,
'documents_html' => $this->context->smarty->fetch('orders/_documents.tpl'),
'shipping_html' => $this->context->smarty->fetch('orders/_shipping.tpl')
)));
}
@@ -1353,9 +1378,32 @@ class AdminOrdersControllerCore extends AdminController
'error' => Tools::displayError('Error occured on deletion of this product line')
)));
// Get invoices collection
$invoice_collection = $order->getInvoicesCollection();
$invoice_array = array();
foreach($invoice_collection as $invoice)
{
$invoice->name = '#'.Configuration::get('PS_INVOICE_PREFIX', Context::getContext()->language->id).sprintf('%06d', $invoice->number);
$invoice_array[] = $invoice;
}
// Assign to smarty informations in order to show the new product line
$this->context->smarty->assign(array(
'order' => $order,
'currency' => new Currency($order->id_currency),
'invoices_collection' => $invoice_collection,
'current_id_lang' => Context::getContext()->language->id,
'link' => Context::getContext()->link,
'current_index' => self::$currentIndex
));
die(Tools::jsonEncode(array(
'result' => $res,
'order' => $order
'order' => $order,
'invoices' => $invoice_array,
'documents_html' => $this->context->smarty->fetch('orders/_documents.tpl'),
'shipping_html' => $this->context->smarty->fetch('orders/_shipping.tpl')
)));
}
+525 -462
View File
File diff suppressed because it is too large Load Diff