From b83d7792cbb6dae48b1ab8b8a450f78a3d89df1b Mon Sep 17 00:00:00 2001 From: Carlos Dauden Date: Wed, 15 May 2019 09:30:23 +0200 Subject: [PATCH] [FIX] stock_picking_report_valued: 6 things: [FIX] stock_picking_report_valued: Wrong taxes amount if round globally (#56) [FIX] stock_picking_report_valued: Remove round_method condition because don't apply here [FIX] stock_picking_report_valued: price_reduce is stored with product decimal precision [FIX] stock_picking_report_valued: division_by_zero when unrelated sale order [FIX] stock_picking_report_valued: Improve code [FIX] stock_picking_report_valued: Multiple taxes in sale line (WIP) --- stock_picking_report_valued/__manifest__.py | 4 +- .../models/stock_move_line.py | 10 +++-- .../models/stock_picking.py | 29 +++++++++++-- .../tests/test_stock_picking_valued.py | 43 ++++++++++++++++++- 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/stock_picking_report_valued/__manifest__.py b/stock_picking_report_valued/__manifest__.py index 1ab8755..deacb9b 100644 --- a/stock_picking_report_valued/__manifest__.py +++ b/stock_picking_report_valued/__manifest__.py @@ -1,6 +1,6 @@ # Copyright 2014 Pedro M. Baeza - Tecnativa # Copyright 2015 Antonio Espinosa - Tecnativa -# Copyright 2016 Carlos Dauden - Tecnativa +# Copyright 2016-2019 Carlos Dauden - Tecnativa # Copyright 2017 David Vidal - Tecnativa # Copyright 2017 Luis M. Ontalba - Tecnativa # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). @@ -8,7 +8,7 @@ { "name": "Valued Picking Report", "summary": "Adding Valued Picking on Delivery Slip report", - "version": "11.0.1.0.0", + "version": "11.0.1.0.3", "author": "Tecnativa, " "Odoo Community Association (OCA)", "website": "https://www.tecnativa.com", diff --git a/stock_picking_report_valued/models/stock_move_line.py b/stock_picking_report_valued/models/stock_move_line.py index 6d2e6ea..92e8143 100644 --- a/stock_picking_report_valued/models/stock_move_line.py +++ b/stock_picking_report_valued/models/stock_move_line.py @@ -63,13 +63,17 @@ class StockMoveLine(models.Model): records...). """ for line in self: + sale_line = line.sale_line + price_unit = ( + sale_line.price_subtotal / sale_line.product_uom_qty + if sale_line.product_uom_qty else sale_line.price_reduce) taxes = line.sale_tax_id.compute_all( - price_unit=line.sale_line.price_reduce, + price_unit=price_unit, currency=line.currency_id, quantity=line.qty_done or line.product_qty, product=line.product_id, - partner=line.sale_line.order_id.partner_shipping_id) - if line.sale_line.company_id.tax_calculation_rounding_method == ( + partner=sale_line.order_id.partner_shipping_id) + if sale_line.company_id.tax_calculation_rounding_method == ( 'round_globally'): price_tax = sum( t.get('amount', 0.0) for t in taxes.get('taxes', [])) diff --git a/stock_picking_report_valued/models/stock_picking.py b/stock_picking_report_valued/models/stock_picking.py index da57ddc..0aef97e 100644 --- a/stock_picking_report_valued/models/stock_picking.py +++ b/stock_picking_report_valued/models/stock_picking.py @@ -41,12 +41,33 @@ class StockPicking(models.Model): records...). """ for pick in self: - amount_untaxed = sum(pick.move_line_ids.mapped( - 'sale_price_subtotal')) - amount_tax = sum(pick.move_line_ids.mapped( - 'sale_price_tax')) + round_curr = pick.sale_id.currency_id.round + amount_tax = 0.0 + for tax_id, tax_group in pick.get_taxes_values().items(): + amount_tax += round_curr(tax_group['amount']) + amount_untaxed = sum( + l.sale_price_subtotal for l in pick.move_line_ids) pick.update({ 'amount_untaxed': amount_untaxed, 'amount_tax': amount_tax, 'amount_total': amount_untaxed + amount_tax, }) + + @api.multi + def get_taxes_values(self): + tax_grouped = {} + for line in self.move_line_ids: + for tax in line.sale_line.tax_id: + tax_id = tax.id + if tax_id not in tax_grouped: + tax_grouped[tax_id] = { + 'base': line.sale_price_subtotal, + 'tax': tax, + } + else: + tax_grouped[tax_id]['base'] += line.sale_price_subtotal + for tax_id, tax_group in tax_grouped.items(): + tax_grouped[tax_id]['amount'] = tax_group['tax'].compute_all( + tax_group['base'], self.sale_id.currency_id + )['taxes'][0]['amount'] + return tax_grouped diff --git a/stock_picking_report_valued/tests/test_stock_picking_valued.py b/stock_picking_report_valued/tests/test_stock_picking_valued.py index 190ec40..99fec2a 100644 --- a/stock_picking_report_valued/tests/test_stock_picking_valued.py +++ b/stock_picking_report_valued/tests/test_stock_picking_valued.py @@ -1,5 +1,6 @@ # Copyright 2017 Tecnativa - David Vidal # Copyright 2017 Tecnativa - Luis M. Ontalba +# Copyright 2019 Tecnativa - Carlos Dauden # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo.tests import common @@ -10,12 +11,19 @@ class TestStockPickingValued(common.SavepointCase): @classmethod def setUpClass(cls): super(TestStockPickingValued, cls).setUpClass() + company = cls.env.user.company_id cls.tax = cls.env['account.tax'].create({ 'name': 'TAX 15%', 'amount_type': 'percent', 'type_tax_use': 'sale', 'amount': 15.0, }) + cls.tax10 = cls.env['account.tax'].create({ + 'name': 'TAX 10%', + 'amount_type': 'percent', + 'type_tax_use': 'sale', + 'amount': 10.0, + }) cls.product = cls.env['product.product'].create({ 'name': 'Test stuff', 'list_price': 100.0, @@ -31,7 +39,29 @@ class TestStockPickingValued(common.SavepointCase): 'price_unit': 100, 'product_uom_qty': 1, })], - 'company_id': cls.env.user.company_id.id, + 'company_id': company.id, + }) + cls.sale_order2 = cls.env['sale.order'].create({ + 'partner_id': cls.partner.id, + 'order_line': [ + (0, 0, { + 'product_id': cls.product.id, + 'price_unit': 100, + 'product_uom_qty': 1, + }), + (0, 0, { + 'product_id': cls.product.id, + 'price_unit': 100, + 'product_uom_qty': 1, + }), + (0, 0, { + 'product_id': cls.product.id, + 'price_unit': 100, + 'product_uom_qty': 1, + 'tax_id': [(6, 0, cls.tax10.ids)], + }), + ], + 'company_id': company.id, }) cls.sale_order.company_id.tax_calculation_rounding_method = ( 'round_per_line') @@ -66,3 +96,14 @@ class TestStockPickingValued(common.SavepointCase): self.assertEqual(picking.amount_untaxed, 100.0) self.assertEqual(picking.amount_tax, 15.0) self.assertEqual(picking.amount_total, 115.0) + + def test_04_lines_distinct_tax(self): + self.sale_order2.company_id.tax_calculation_rounding_method = ( + 'round_globally') + self.sale_order2.action_confirm() + self.assertTrue(len(self.sale_order2.picking_ids)) + for picking in self.sale_order2.picking_ids: + picking.action_assign() + self.assertEqual(picking.amount_untaxed, 300.0) + self.assertEqual(picking.amount_tax, 40.0) + self.assertEqual(picking.amount_total, 340.0)