[FIX] point_of_sale: correct currency rounding

bzr revid: fva@openerp.com-20130128171800-wobu3q7l5d16qhyq
This commit is contained in:
Frédéric van der Essen 2013-01-28 18:18:00 +01:00
parent 2e3adb48c4
commit e9c6fd0dea
4 changed files with 39 additions and 36 deletions

View File

@ -1,10 +1,23 @@
function openerp_pos_models(instance, module){ //module is instance.point_of_sale
var QWeb = instance.web.qweb;
// rounds a value with a fixed number of decimals.
// round(3.141492,2) -> 3.14
function round(value,decimals){
var mult = Math.pow(10,decimals || 0);
return Math.round(value*mult)/mult;
}
window.round = round;
// rounds a value with decimal form precision
// round(3.141592,0.025) ->3.125
function round_pr(value,precision){
if(!precision || precision < 0){
throw new Error('round_pr(): needs a precision greater than zero, got '+precision+' instead');
}
return Math.round(value / precision) * precision;
}
window.round_pr = round_pr;
// The PosModel contains the Point Of Sale's representation of the backend.
@ -105,8 +118,9 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
}).then(function(company_partners){
self.get('company').contact_address = company_partners[0].contact_address;
return self.fetch('res.currency',['symbol','position'],[['id','=',self.get('company').currency_id[0]]]);
return self.fetch('res.currency',['symbol','position','rounding','accuracy'],[['id','=',self.get('company').currency_id[0]]]);
}).then(function(currencies){
console.log('Currency:',currencies[0]);
self.set('currency',currencies[0]);
return self.fetch('product.uom', null, null);
@ -377,7 +391,7 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
var unit = this.get_unit();
if(unit){
this.quantity = Math.max(unit.rounding, Math.round(quant / unit.rounding) * unit.rounding);
this.quantityStr = this.quantity.toFixed(Math.ceil(Math.log(1.0 / unit.rounding) / Math.log(10)));
this.quantityStr = this.quantity.toFixed(Math.max(0,Math.ceil(Math.log(1.0 / unit.rounding) / Math.log(10))));
}else{
this.quantity = quant;
this.quantityStr = '' + this.quantity;
@ -473,10 +487,12 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
this.trigger('change');
},
get_unit_price: function(){
return this.price;
var rounding = this.pos.get('currency').rounding;
return round_pr(this.price,rounding);
},
get_display_price: function(){
return round(round(this.price * this.get_quantity(),2) * (1- this.get_discount()/100.0),2);
var rounding = this.pos.get('currency').rounding;
return round_pr(round_pr(this.get_unit_price() * this.get_quantity(),rounding) * (1- this.get_discount()/100.0),rounding);
},
get_price_without_tax: function(){
return this.get_all_prices().priceWithoutTax;
@ -487,9 +503,10 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
get_tax: function(){
return this.get_all_prices().tax;
},
get_all_prices: function() {
get_all_prices: function(){
var self = this;
var base = round(this.get_quantity() * this.price * (1 - (this.get_discount() / 100)),2); //FIXME en fonction de la currency
var currency_rounding = this.pos.get('currency').rounding;
var base = round_pr(this.get_quantity() * this.get_unit_price() * (1.0 - (this.get_discount() / 100.0)), currency_rounding);
var totalTax = base;
var totalNoTax = base;
@ -503,13 +520,13 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
if (tax.price_include) {
var tmp;
if (tax.type === "percent") {
tmp = base - round(base / (1 + tax.amount),2); //FIXME en fonction de la currency
tmp = base - round_pr(base / (1 + tax.amount),currency_rounding);
} else if (tax.type === "fixed") {
tmp = round(tax.amount * self.get_quantity(),2);
tmp = round_pr(tax.amount * self.get_quantity(),currency_rounding);
} else {
throw "This type of tax is not supported by the point of sale: " + tax.type;
}
tmp = round(tmp,2);
tmp = round_pr(tmp,currency_rounding);
taxtotal += tmp;
totalNoTax -= tmp;
} else {
@ -521,7 +538,7 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
} else {
throw "This type of tax is not supported by the point of sale: " + tax.type;
}
tmp = round(tmp,2);
tmp = round_pr(tmp,currency_rounding);
taxtotal += tmp;
totalTax += tmp;
}

View File

@ -960,10 +960,10 @@ function openerp_pos_screens(instance, module){ //module is instance.point_of_sa
var remaining = dueTotal > paidTotal ? dueTotal - paidTotal : 0;
var change = paidTotal > dueTotal ? paidTotal - dueTotal : 0;
this.$('#payment-due-total').html(dueTotal.toFixed(2));
this.$('#payment-paid-total').html(paidTotal.toFixed(2));
this.$('#payment-remaining').html(remaining.toFixed(2));
this.$('#payment-change').html(change.toFixed(2));
this.$('#payment-due-total').html(this.format_currency(dueTotal));
this.$('#payment-paid-total').html(this.format_currency(paidTotal));
this.$('#payment-remaining').html(this.format_currency(remaining));
this.$('#payment-change').html(this.format_currency(change));
if(currentOrder.selected_orderline === undefined){
remaining = 1; // What is this ?
}

View File

@ -22,13 +22,15 @@ function openerp_pos_basewidget(instance, module){ //module is instance.point_of
if(this.pos && this.pos.get('currency')){
this.currency = this.pos.get('currency');
}else{
this.currency = {symbol: '$', position: 'after'};
this.currency = {symbol: '$', position: 'after', rounding: 0.01};
}
var decimals = Math.max(0,Math.ceil(Math.log(1.0 / this.currency.rounding) / Math.log(10)));
this.format_currency = function(amount){
if(typeof amount === 'number'){
amount = Math.round(amount*100)/100;
amount = amount.toFixed(2);
amount = amount.toFixed(decimals);
}
if(this.currency.position === 'after'){
return amount + ' ' + this.currency.symbol;

View File

@ -220,11 +220,7 @@
<span class="left-block">
Total:
</span>
<span class="right-block">
<t t-if="widget.currency.position == 'before'" t-esc="widget.currency.symbol"/>
<span id="payment-due-total"></span>
<t t-if="widget.currency.position == 'after'" t-esc="widget.currency.symbol"/>
</span>
<span class='right-block' id="payment-due-total"></span>
</div>
<table id="paymentlines">
</table>
@ -233,31 +229,19 @@
<span class='left-block'>
Paid:
</span>
<span class='right-block'>
<t t-if="widget.currency.position == 'before'" t-esc="widget.currency.symbol"/>
<span id="payment-paid-total"></span>
<t t-if="widget.currency.position == 'after'" t-esc="widget.currency.symbol"/>
</span>
<span class='right-block' id="payment-paid-total"></span>
</div>
<div class="infoline">
<span class='left-block'>
Remaining:
</span>
<span class='right-block'>
<t t-if="widget.currency.position == 'before'" t-esc="widget.currency.symbol"/>
<span id="payment-remaining"></span>
<t t-if="widget.currency.position == 'after'" t-esc="widget.currency.symbol"/>
</span>
<span class='right-block' id="payment-remaining"></span>
</div>
<div class="infoline" >
<span class='left-block'>
Change:
</span>
<span class='right-block'>
<t t-if="widget.currency.position == 'before'" t-esc="widget.currency.symbol"/>
<span id="payment-change"></span>
<t t-if="widget.currency.position == 'after'" t-esc="widget.currency.symbol"/>
</span>
<span class='right-block' id="payment-change"></span>
</div>
</div>
</div>