Browse Source

Merge pull request #2 from pablocm-aserti/8.0-improve-pos-pricelist

[FIX] Fiscal position tax mapping
pull/39/head
Adil Houmadi 9 years ago
parent
commit
48e6f5e237
  1. 11
      pos_pricelist/__init__.py
  2. 4
      pos_pricelist/__openerp__.py
  3. 30
      pos_pricelist/migrations/8.0.1.1.0/post-migration.py
  4. 1
      pos_pricelist/models/__init__.py
  5. 58
      pos_pricelist/models/point_of_sale.py
  6. 19
      pos_pricelist/static/src/js/db.js
  7. 53
      pos_pricelist/static/src/js/models.js
  8. 17
      pos_pricelist/views/point_of_sale_view.xml

11
pos_pricelist/__init__.py

@ -17,3 +17,14 @@
# #
############################################################################## ##############################################################################
from . import models from . import models
def set_pos_line_taxes(cr, registry):
"""Copy the product taxes to the pos.line"""
cr.execute("""insert into pline_tax_rel
select l.id, t.id
from pos_order_line l
join pos_order o on l.order_id = o.id
join product_taxes_rel rel on rel.prod_id = l.product_id
join account_tax t on rel.tax_id = t.id
where t.company_id = o.company_id""")

4
pos_pricelist/__openerp__.py

@ -18,7 +18,7 @@
############################################################################## ##############################################################################
{ {
'name': 'POS Pricelist', 'name': 'POS Pricelist',
'version': '1.0.0',
'version': '1.1.0',
'category': 'Point Of Sale', 'category': 'Point Of Sale',
'sequence': 1, 'sequence': 1,
'author': "Adil Houmadi @Taktik,Odoo Community Association (OCA)", 'author': "Adil Houmadi @Taktik,Odoo Community Association (OCA)",
@ -34,6 +34,7 @@ New feature for the Point Of Sale:
'data': [ 'data': [
"views/pos_pricelist_template.xml", "views/pos_pricelist_template.xml",
"views/pos_pricelist_views.xml", "views/pos_pricelist_views.xml",
"views/point_of_sale_view.xml"
], ],
'demo': [ 'demo': [
'demo/pos_pricelist_demo.yml', 'demo/pos_pricelist_demo.yml',
@ -41,6 +42,7 @@ New feature for the Point Of Sale:
'qweb': [ 'qweb': [
'static/src/xml/pos.xml' 'static/src/xml/pos.xml'
], ],
'post_init_hook': "set_pos_line_taxes",
'installable': True, 'installable': True,
'application': False, 'application': False,
'auto_install': False, 'auto_install': False,

30
pos_pricelist/migrations/8.0.1.1.0/post-migration.py

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2015 Aserti Global Solutions (http://www.aserti.es/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
__name__ = "Copy the product taxes to the pos.line"
def migrate(cr, version):
cr.execute("""insert into pline_tax_rel
select l.id, t.id
from pos_order_line l
join pos_order o on l.order_id = o.id
join product_taxes_rel rel on rel.prod_id = l.product_id
join account_tax t on rel.tax_id = t.id
where t.company_id = o.company_id""")

1
pos_pricelist/models/__init__.py

@ -17,3 +17,4 @@
# #
############################################################################## ##############################################################################
from . import pos_pricelist from . import pos_pricelist
from . import point_of_sale

58
pos_pricelist/models/point_of_sale.py

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2015 Aserti Global Solutions (http://www.aserti.es/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models, fields, api
class PosOrderLine(models.Model):
_inherit = "pos.order.line"
@api.one
@api.depends('tax_ids', 'qty', 'price_unit',
'product_id', 'discount', 'order_id.partner_id')
def _amount_line_all(self):
price = self.price_unit * (1 - (self.discount or 0.0) / 100.0)
taxes = self.tax_ids.compute_all(
price, self.qty, product=self.product_id,
partner=self.order_id.partner_id)
self.price_subtotal = taxes['total']
self.price_subtotal_incl = taxes['total_included']
tax_ids = fields.Many2many(
'account.tax', 'pline_tax_rel', 'pos_line_id', 'tax_id',
"Taxes", domain=[('type_tax_use', '=', 'sale')])
price_subtotal = fields.Float(compute="_amount_line_all", store=True)
price_subtotal_incl = fields.Float(compute="_amount_line_all", store=True)
class PosOrder(models.Model):
_inherit = "pos.order"
@api.model
def _amount_line_tax(self, line):
price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
taxes = line.tax_ids.compute_all(
price, line.qty, product=line.product_id,
partner=line.order_id.partner_id)['taxes']
val = 0.0
for c in taxes:
val += c.get('amount', 0.0)
return val

19
pos_pricelist/static/src/js/db.js

@ -140,15 +140,24 @@ function pos_pricelist_db(instance, module) {
}); });
return list; return list;
}, },
find_taxes_by_fiscal_position_id: function (fiscal_position_id) {
map_tax: function (fiscal_position_id, taxes_ids) {
var taxes = []; var taxes = [];
var found_taxes = {};
for (var id in this.fiscal_position_tax_by_id) { for (var id in this.fiscal_position_tax_by_id) {
var tax = this.fiscal_position_tax_by_id[id];
if (tax && tax.position_id &&
tax.position_id[0] == fiscal_position_id) {
taxes.push(tax);
var fp_line = this.fiscal_position_tax_by_id[id];
if (fp_line && fp_line.position_id &&
fp_line.position_id[0] == fiscal_position_id &&
taxes_ids.indexOf(fp_line.tax_src_id[0]) > -1) {
taxes.push(fp_line.tax_dest_id[0]);
found_taxes[fp_line.tax_src_id[0]] = true;
} }
} }
for (var i = 0, len = taxes_ids.length; i < len; i++) {
var tax_id = taxes_ids[i];
if (!(tax_id in found_taxes)) {
taxes.push(tax_id);
}
}
return taxes; return taxes;
}, },
add_products: function (products) { add_products: function (products) {

53
pos_pricelist/static/src/js/models.js

@ -263,39 +263,24 @@ function pos_pricelist_models(instance, module) {
*/ */
get_applicable_taxes_for_orderline: function () { get_applicable_taxes_for_orderline: function () {
// find applicable taxes for this product and this customer // find applicable taxes for this product and this customer
var fiscal_position_taxes = [];
var product_taxes = [];
var product = this.get_product(); var product = this.get_product();
var partner = this.order ? this.order.get_client() : null;
var product_tax_ids = product.taxes_id;
var product_taxes = [];
var taxes = this.pos.taxes; var taxes = this.pos.taxes;
var partner = this.order ? this.order.get_client() : null;
if (partner && partner.property_account_position) { if (partner && partner.property_account_position) {
fiscal_position_taxes =
this.pos.db.find_taxes_by_fiscal_position_id(
partner.property_account_position[0]
product_tax_ids =
this.pos.db.map_tax(
partner.property_account_position[0], product_tax_ids
); );
} }
for (var i = 0, ilen = fiscal_position_taxes.length;
for (var i = 0, ilen = product_tax_ids.length;
i < ilen; i++) { i < ilen; i++) {
var fp_tax = fiscal_position_taxes[i];
for (var j = 0, jlen = taxes.length; j < jlen; j++) {
var p_tax = taxes[j];
if (fp_tax && p_tax && fp_tax.tax_src_id[0] === p_tax.id) {
var dest_tax = _.detect(taxes, function (t) {
return t.id === fp_tax.tax_dest_id[0];
});
product_taxes.push(dest_tax);
}
}
}
if (product_taxes.length === 0) {
for (var i = 0, ilen = product.taxes_id.length;
i < ilen; i++) {
var _id = product.taxes_id[i];
var p_tax = _.detect(taxes, function (t) {
return t.id === _id;
});
product_taxes.push(p_tax);
}
var tax_id = product_tax_ids[i];
var tax = _.detect(taxes, function (t) {
return t.id === tax_id;
});
product_taxes.push(tax);
} }
return product_taxes; return product_taxes;
}, },
@ -309,6 +294,20 @@ function pos_pricelist_models(instance, module) {
return OrderlineParent.prototype.get_display_price.apply( return OrderlineParent.prototype.get_display_price.apply(
this, arguments this, arguments
); );
},
export_as_JSON: function() {
var res = OrderlineParent.prototype.export_as_JSON.apply(this, arguments);
var product_tax_ids = this.get_product().taxes_id || [];
var partner = this.order ? this.order.get_client() : null;
if (partner && partner.property_account_position) {
product_tax_ids =
this.pos.db.map_tax(
partner.property_account_position[0], product_tax_ids
);
}
res["tax_ids"] = [[6, false, product_tax_ids]];
return res;
} }
}); });

17
pos_pricelist/views/point_of_sale_view.xml

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<openerp>
<data>
<record id="view_pos_pos_form" model="ir.ui.view">
<field name="name">pos.order</field>
<field name="model">pos.order</field>
<field name="inherit_id" ref="point_of_sale.view_pos_pos_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='lines']/tree/field[@name='discount']" position="after">
<field name="tax_ids" widget="many2many_tags"/>
</xpath>
</field>
</record>
</data>
</openerp>
Loading…
Cancel
Save