[FIX] point_of_sale: correct currency rounding
bzr revid: fva@openerp.com-20130128171800-wobu3q7l5d16qhyq
This commit is contained in:
@@ -1,10 +1,23 @@
|
|||||||
function openerp_pos_models(instance, module){ //module is instance.point_of_sale
|
function openerp_pos_models(instance, module){ //module is instance.point_of_sale
|
||||||
var QWeb = instance.web.qweb;
|
var QWeb = instance.web.qweb;
|
||||||
|
|
||||||
|
// rounds a value with a fixed number of decimals.
|
||||||
|
// round(3.141492,2) -> 3.14
|
||||||
function round(value,decimals){
|
function round(value,decimals){
|
||||||
var mult = Math.pow(10,decimals || 0);
|
var mult = Math.pow(10,decimals || 0);
|
||||||
return Math.round(value*mult)/mult;
|
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.
|
// 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){
|
}).then(function(company_partners){
|
||||||
self.get('company').contact_address = company_partners[0].contact_address;
|
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){
|
}).then(function(currencies){
|
||||||
|
console.log('Currency:',currencies[0]);
|
||||||
self.set('currency',currencies[0]);
|
self.set('currency',currencies[0]);
|
||||||
|
|
||||||
return self.fetch('product.uom', null, null);
|
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();
|
var unit = this.get_unit();
|
||||||
if(unit){
|
if(unit){
|
||||||
this.quantity = Math.max(unit.rounding, Math.round(quant / unit.rounding) * unit.rounding);
|
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{
|
}else{
|
||||||
this.quantity = quant;
|
this.quantity = quant;
|
||||||
this.quantityStr = '' + this.quantity;
|
this.quantityStr = '' + this.quantity;
|
||||||
@@ -473,10 +487,12 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
|
|||||||
this.trigger('change');
|
this.trigger('change');
|
||||||
},
|
},
|
||||||
get_unit_price: function(){
|
get_unit_price: function(){
|
||||||
return this.price;
|
var rounding = this.pos.get('currency').rounding;
|
||||||
|
return round_pr(this.price,rounding);
|
||||||
},
|
},
|
||||||
get_display_price: function(){
|
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(){
|
get_price_without_tax: function(){
|
||||||
return this.get_all_prices().priceWithoutTax;
|
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(){
|
get_tax: function(){
|
||||||
return this.get_all_prices().tax;
|
return this.get_all_prices().tax;
|
||||||
},
|
},
|
||||||
get_all_prices: function() {
|
get_all_prices: function(){
|
||||||
var self = this;
|
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 totalTax = base;
|
||||||
var totalNoTax = base;
|
var totalNoTax = base;
|
||||||
|
|
||||||
@@ -503,13 +520,13 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
|
|||||||
if (tax.price_include) {
|
if (tax.price_include) {
|
||||||
var tmp;
|
var tmp;
|
||||||
if (tax.type === "percent") {
|
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") {
|
} else if (tax.type === "fixed") {
|
||||||
tmp = round(tax.amount * self.get_quantity(),2);
|
tmp = round_pr(tax.amount * self.get_quantity(),currency_rounding);
|
||||||
} else {
|
} else {
|
||||||
throw "This type of tax is not supported by the point of sale: " + tax.type;
|
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;
|
taxtotal += tmp;
|
||||||
totalNoTax -= tmp;
|
totalNoTax -= tmp;
|
||||||
} else {
|
} else {
|
||||||
@@ -521,7 +538,7 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
|
|||||||
} else {
|
} else {
|
||||||
throw "This type of tax is not supported by the point of sale: " + tax.type;
|
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;
|
taxtotal += tmp;
|
||||||
totalTax += tmp;
|
totalTax += tmp;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -960,10 +960,10 @@ function openerp_pos_screens(instance, module){ //module is instance.point_of_sa
|
|||||||
var remaining = dueTotal > paidTotal ? dueTotal - paidTotal : 0;
|
var remaining = dueTotal > paidTotal ? dueTotal - paidTotal : 0;
|
||||||
var change = paidTotal > dueTotal ? paidTotal - dueTotal : 0;
|
var change = paidTotal > dueTotal ? paidTotal - dueTotal : 0;
|
||||||
|
|
||||||
this.$('#payment-due-total').html(dueTotal.toFixed(2));
|
this.$('#payment-due-total').html(this.format_currency(dueTotal));
|
||||||
this.$('#payment-paid-total').html(paidTotal.toFixed(2));
|
this.$('#payment-paid-total').html(this.format_currency(paidTotal));
|
||||||
this.$('#payment-remaining').html(remaining.toFixed(2));
|
this.$('#payment-remaining').html(this.format_currency(remaining));
|
||||||
this.$('#payment-change').html(change.toFixed(2));
|
this.$('#payment-change').html(this.format_currency(change));
|
||||||
if(currentOrder.selected_orderline === undefined){
|
if(currentOrder.selected_orderline === undefined){
|
||||||
remaining = 1; // What is this ?
|
remaining = 1; // What is this ?
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,13 +22,15 @@ function openerp_pos_basewidget(instance, module){ //module is instance.point_of
|
|||||||
if(this.pos && this.pos.get('currency')){
|
if(this.pos && this.pos.get('currency')){
|
||||||
this.currency = this.pos.get('currency');
|
this.currency = this.pos.get('currency');
|
||||||
}else{
|
}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){
|
this.format_currency = function(amount){
|
||||||
if(typeof amount === 'number'){
|
if(typeof amount === 'number'){
|
||||||
amount = Math.round(amount*100)/100;
|
amount = Math.round(amount*100)/100;
|
||||||
amount = amount.toFixed(2);
|
amount = amount.toFixed(decimals);
|
||||||
}
|
}
|
||||||
if(this.currency.position === 'after'){
|
if(this.currency.position === 'after'){
|
||||||
return amount + ' ' + this.currency.symbol;
|
return amount + ' ' + this.currency.symbol;
|
||||||
|
|||||||
@@ -220,11 +220,7 @@
|
|||||||
<span class="left-block">
|
<span class="left-block">
|
||||||
Total:
|
Total:
|
||||||
</span>
|
</span>
|
||||||
<span class="right-block">
|
<span class='right-block' id="payment-due-total"></span>
|
||||||
<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>
|
|
||||||
</div>
|
</div>
|
||||||
<table id="paymentlines">
|
<table id="paymentlines">
|
||||||
</table>
|
</table>
|
||||||
@@ -233,31 +229,19 @@
|
|||||||
<span class='left-block'>
|
<span class='left-block'>
|
||||||
Paid:
|
Paid:
|
||||||
</span>
|
</span>
|
||||||
<span class='right-block'>
|
<span class='right-block' id="payment-paid-total"></span>
|
||||||
<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>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="infoline">
|
<div class="infoline">
|
||||||
<span class='left-block'>
|
<span class='left-block'>
|
||||||
Remaining:
|
Remaining:
|
||||||
</span>
|
</span>
|
||||||
<span class='right-block'>
|
<span class='right-block' id="payment-remaining"></span>
|
||||||
<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>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="infoline" >
|
<div class="infoline" >
|
||||||
<span class='left-block'>
|
<span class='left-block'>
|
||||||
Change:
|
Change:
|
||||||
</span>
|
</span>
|
||||||
<span class='right-block'>
|
<span class='right-block' id="payment-change"></span>
|
||||||
<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>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user