From b1b2401034f5066a5d3069a027a98ae5b646c40b Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 5 Jan 2012 14:06:24 +0100 Subject: [PATCH 1/5] [imp] improved design of order saving functions bzr revid: nicolas.vanhoren@openerp.com-20120105130624-28ry8w1eo6qnvn5u --- addons/point_of_sale/static/src/js/pos.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/addons/point_of_sale/static/src/js/pos.js b/addons/point_of_sale/static/src/js/pos.js index 371c2c28ca9..b6e842c6bed 100644 --- a/addons/point_of_sale/static/src/js/pos.js +++ b/addons/point_of_sale/static/src/js/pos.js @@ -119,9 +119,9 @@ openerp.point_of_sale = function(db) { self.set({'user': result}); })); }, - push: function(osvModel, record) { + pushOrder: function(record) { var ops = _.clone(this.get('pending_operations')); - ops.push({model: osvModel, record: record}); + ops.push(record); this.set({pending_operations: ops}); return this.flush(); }, @@ -135,11 +135,11 @@ openerp.point_of_sale = function(db) { if (ops.length === 0) return $.when(); var op = ops[0]; - var dataSet = new db.web.DataSet(this, op.model, null); + var dataSet = new db.web.DataSet(this, 'pos.order', null); /* we prevent the default error handler and assume errors * are a normal use case, except we stop the current iteration */ - return dataSet.create(op.record).fail(function(unused, event) { + return dataSet.create(op).fail(function(unused, event) { event.preventDefault(); }).pipe(_.bind(function() { console.debug('saved 1 record'); @@ -937,7 +937,7 @@ openerp.point_of_sale = function(db) { var callback, currentOrder; currentOrder = this.shop.get('selectedOrder'); $('button#validate-order', this.$element).attr('disabled', 'disabled'); - pos.push('pos.order', currentOrder.exportAsJSON()).then(_.bind(function() { + pos.pushOrder(currentOrder.exportAsJSON()).then(_.bind(function() { $('button#validate-order', this.$element).removeAttr('disabled'); return currentOrder.set({ validated: true From 3598bddb7c87fc91d171ffa894a119d33c981d6a Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 5 Jan 2012 14:13:19 +0100 Subject: [PATCH 2/5] [imp] copied order creation login to the server bzr revid: nicolas.vanhoren@openerp.com-20120105131319-9pnr25ms12szttx7 --- addons/point_of_sale/point_of_sale.py | 10 ++++++++++ addons/point_of_sale/static/src/js/pos.js | 3 +-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/addons/point_of_sale/point_of_sale.py b/addons/point_of_sale/point_of_sale.py index 60ce7b7bc0c..0810bf459cf 100644 --- a/addons/point_of_sale/point_of_sale.py +++ b/addons/point_of_sale/point_of_sale.py @@ -22,6 +22,7 @@ import time from datetime import datetime from dateutil.relativedelta import relativedelta +import logging import netsvc from osv import fields, osv @@ -29,6 +30,8 @@ from tools.translate import _ from decimal import Decimal import decimal_precision as dp +_logger = logging.getLogger(__name__) + class pos_config_journal(osv.osv): """ Point of Sale journal configuration""" _name = 'pos.config.journal' @@ -46,6 +49,13 @@ class pos_order(osv.osv): _name = "pos.order" _description = "Point of Sale" _order = "id desc" + + def create_from_ui(self, cr, uid, orders, context=None): + #_logger.info("orders: %r", orders) + list = [] + for order in orders: + list.append(self.create(cr, uid, order, context)) + return list def unlink(self, cr, uid, ids, context=None): for rec in self.browse(cr, uid, ids, context=context): diff --git a/addons/point_of_sale/static/src/js/pos.js b/addons/point_of_sale/static/src/js/pos.js index 3a4f7299713..9b46788a02d 100644 --- a/addons/point_of_sale/static/src/js/pos.js +++ b/addons/point_of_sale/static/src/js/pos.js @@ -135,11 +135,10 @@ openerp.point_of_sale = function(db) { if (ops.length === 0) return $.when(); var op = ops[0]; - var dataSet = new db.web.DataSet(this, 'pos.order', null); /* we prevent the default error handler and assume errors * are a normal use case, except we stop the current iteration */ - return dataSet.create(op).fail(function(unused, event) { + return new db.web.Model("pos.order").get_func("create_from_ui")([op]).fail(function(unused, event) { event.preventDefault(); }).pipe(_.bind(function() { console.debug('saved 1 record'); From 7791dc8ea8f7841110e6246ddbc154f9381bea82 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 5 Jan 2012 14:33:37 +0100 Subject: [PATCH 3/5] [fix] make the workflow progress in pos orders bzr revid: nicolas.vanhoren@openerp.com-20120105133337-1atnng0bh5pqzsfd --- addons/point_of_sale/point_of_sale.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/addons/point_of_sale/point_of_sale.py b/addons/point_of_sale/point_of_sale.py index 0810bf459cf..c91b6213230 100644 --- a/addons/point_of_sale/point_of_sale.py +++ b/addons/point_of_sale/point_of_sale.py @@ -55,6 +55,8 @@ class pos_order(osv.osv): list = [] for order in orders: list.append(self.create(cr, uid, order, context)) + wf_service = netsvc.LocalService("workflow") + wf_service.trg_validate(uid, 'pos.order', list[-1], 'paid', cr) return list def unlink(self, cr, uid, ids, context=None): From afba9ceb715944bdc9ff41f41104583aace80596 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 5 Jan 2012 15:18:17 +0100 Subject: [PATCH 4/5] [imp] added algorithm from fp to calculate taxes bzr revid: nicolas.vanhoren@openerp.com-20120105141817-whw7hhlb5ggdeas4 --- addons/point_of_sale/static/src/js/pos.js | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/addons/point_of_sale/static/src/js/pos.js b/addons/point_of_sale/static/src/js/pos.js index 9b46788a02d..76c662348f2 100644 --- a/addons/point_of_sale/static/src/js/pos.js +++ b/addons/point_of_sale/static/src/js/pos.js @@ -90,6 +90,7 @@ openerp.point_of_sale = function(db) { this.fetch('account.bank.statement', ['account_id', 'currency', 'journal_id', 'state', 'name'], [['state', '=', 'open'], ['user_id', '=', this.session.uid]]), this.fetch('account.journal', ['auto_cash', 'check_dtls', 'currency', 'name', 'type']), + this.fetch('account.tax', ['amount', 'price_include', 'type']), this.get_app_data()) .pipe(_.bind(this.build_tree, this)); }, @@ -301,6 +302,57 @@ openerp.point_of_sale = function(db) { getTotal: function() { return (this.get('quantity')) * (this.get('list_price')) * (1 - (this.get('discount')) / 100); }, + getPriceWithoutTax: function() { + return getAllPrices().priceWithoutTax; + }, + getPriceWithTax: function() { + return getAllPrices().priceWithTax; + }, + getTax: function() { + return getAllPrices().tax; + }, + getAllPrices: function() { + var self = this; + var totalTax = (this.get('quantity')) * (this.get('list_price')) * (1 - (this.get('discount')) / 100); + var totalNoTax = totalTax; + + var products = pos.store.get('product.product'); + var product = _.detect(products, function(el) {return el.id === self.get('id');}); + var taxes_ids = product.taxes_id; + var taxes = pos.store.get('account.tax'); + var taxtotal = 0; + _.each(taxes_ids, function(el) { + var tax = _.detect(taxes, function(t) {return t.id === el;}); + if (tax.price_include) { + var tmp; + if (tax.type === "percent") { + tmp = tax.amount / (1 + self.getTotalWithoutTax()); + } else if (tax.type === "fixed") { + tmp = tax.amount * self.get('quantity'); + } else { + throw "This type of tax is not supported by the point of sale: " + tax.type; + } + taxtotal += tmp; + totalNoTax -= tmp; + } else { + var tmp; + if (tax.type === "percent") { + tmp = tax.amount * self.getTotalWithoutTax(); + } else if (tax.type === "fixed") { + tmp = tax.amount * self.get('quantity'); + } else { + throw "This type of tax is not supported by the point of sale: " + tax.type; + } + taxtotal += tmp; + totalTax += tmp; + } + }); + return { + "priceWithTax": totalTax, + "priceWithoutTax": totalNoTax, + "tax": taxtotal, + }; + }, exportAsJSON: function() { var result; result = { From 8d598e49e32799cf9e260c0eadd41fcbdb8b1c56 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 5 Jan 2012 15:35:30 +0100 Subject: [PATCH 5/5] [imp] switched to correct tax calculation bzr revid: nicolas.vanhoren@openerp.com-20120105143530-8rhlec72g9gt0q80 --- addons/point_of_sale/static/src/js/pos.js | 28 ++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/addons/point_of_sale/static/src/js/pos.js b/addons/point_of_sale/static/src/js/pos.js index 76c662348f2..133c646503a 100644 --- a/addons/point_of_sale/static/src/js/pos.js +++ b/addons/point_of_sale/static/src/js/pos.js @@ -299,22 +299,20 @@ openerp.point_of_sale = function(db) { quantity: (this.get('quantity')) + 1 }); }, - getTotal: function() { - return (this.get('quantity')) * (this.get('list_price')) * (1 - (this.get('discount')) / 100); - }, getPriceWithoutTax: function() { - return getAllPrices().priceWithoutTax; + return this.getAllPrices().priceWithoutTax; }, getPriceWithTax: function() { - return getAllPrices().priceWithTax; + return this.getAllPrices().priceWithTax; }, getTax: function() { - return getAllPrices().tax; + return this.getAllPrices().tax; }, getAllPrices: function() { var self = this; - var totalTax = (this.get('quantity')) * (this.get('list_price')) * (1 - (this.get('discount')) / 100); - var totalNoTax = totalTax; + var base = (this.get('quantity')) * (this.get('list_price')) * (1 - (this.get('discount')) / 100); + var totalTax = base; + var totalNoTax = base; var products = pos.store.get('product.product'); var product = _.detect(products, function(el) {return el.id === self.get('id');}); @@ -326,7 +324,7 @@ openerp.point_of_sale = function(db) { if (tax.price_include) { var tmp; if (tax.type === "percent") { - tmp = tax.amount / (1 + self.getTotalWithoutTax()); + tmp = base - (base / (1 + tax.amount)); } else if (tax.type === "fixed") { tmp = tax.amount * self.get('quantity'); } else { @@ -337,7 +335,7 @@ openerp.point_of_sale = function(db) { } else { var tmp; if (tax.type === "percent") { - tmp = tax.amount * self.getTotalWithoutTax(); + tmp = tax.amount * base; } else if (tax.type === "fixed") { tmp = tax.amount * self.get('quantity'); } else { @@ -465,14 +463,18 @@ openerp.point_of_sale = function(db) { }; Order.prototype.getTotal = function() { return (this.get('orderLines')).reduce((function(sum, orderLine) { - return sum + orderLine.getTotal(); + return sum + orderLine.getPriceWithTax(); }), 0); }; Order.prototype.getTotalTaxExcluded = function() { - return this.getTotal() / 1.21; + return (this.get('orderLines')).reduce((function(sum, orderLine) { + return sum + orderLine.getPriceWithoutTax(); + }), 0); }; Order.prototype.getTax = function() { - return this.getTotal() / 1.21 * 0.21; + return (this.get('orderLines')).reduce((function(sum, orderLine) { + return sum + orderLine.getTax(); + }), 0); }; Order.prototype.getPaidTotal = function() { return (this.get('paymentLines')).reduce((function(sum, paymentLine) {