From e9c6fd0deac53746487f431b1ea84b25db6af22a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20van=20der=20Essen?= Date: Mon, 28 Jan 2013 18:18:00 +0100 Subject: [PATCH] [FIX] point_of_sale: correct currency rounding bzr revid: fva@openerp.com-20130128171800-wobu3q7l5d16qhyq --- addons/point_of_sale/static/src/js/models.js | 37 ++++++++++++++----- addons/point_of_sale/static/src/js/screens.js | 8 ++-- .../static/src/js/widget_base.js | 6 ++- addons/point_of_sale/static/src/xml/pos.xml | 24 ++---------- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index a6b9a02b7b3..d2cf15b54f3 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -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; } diff --git a/addons/point_of_sale/static/src/js/screens.js b/addons/point_of_sale/static/src/js/screens.js index 2d522c0a1c0..ab9982b4b1f 100644 --- a/addons/point_of_sale/static/src/js/screens.js +++ b/addons/point_of_sale/static/src/js/screens.js @@ -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 ? } diff --git a/addons/point_of_sale/static/src/js/widget_base.js b/addons/point_of_sale/static/src/js/widget_base.js index 4cab7869a53..124be4e9f82 100644 --- a/addons/point_of_sale/static/src/js/widget_base.js +++ b/addons/point_of_sale/static/src/js/widget_base.js @@ -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; diff --git a/addons/point_of_sale/static/src/xml/pos.xml b/addons/point_of_sale/static/src/xml/pos.xml index aca1c8e56df..3da76ec12d8 100644 --- a/addons/point_of_sale/static/src/xml/pos.xml +++ b/addons/point_of_sale/static/src/xml/pos.xml @@ -220,11 +220,7 @@ Total: - - - - - +
@@ -233,31 +229,19 @@ Paid: - - - - - +
Remaining: - - - - - +
Change: - - - - - +