From 3e61dab28144c6776c4c2aa720770e992f3e0b30 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Tue, 30 Jul 2019 12:02:27 +0000 Subject: [PATCH] [FIX] sale: import order line multicompany - Admin (U1) user, Demo (U2) with Sales Manager rights - Create parent company A and child company AA, U2 has access to A (U1 to both) - Create 2 taxes, T1 for A, T2 for AA - For product P, add tax T1 in company A, and T2 in company AA - U2 creates a new SO in company A with no lines, export Order Lines/Product - U2 edits the file to add product P - U2 imports the file Before this commit, the sale order's line had the taxes for bot company After this commit, it only has the taxes for the company of the sale order OPW 2036453 closes odoo/odoo#35294 Signed-off-by: Lucas Perais (lpe) --- addons/sale/models/sale.py | 3 +- addons/sale/tests/test_sale_order.py | 53 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index fddcfed99b7..734f3632012 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -838,7 +838,8 @@ class SaleOrderLine(models.Model): for line in self: fpos = line.order_id.fiscal_position_id or line.order_id.partner_id.property_account_position_id # If company_id is set, always filter taxes by the company - taxes = line.product_id.taxes_id.filtered(lambda r: not line.company_id or r.company_id == line.company_id) + line_company_id = line.company_id or line.order_id.company_id + taxes = line.product_id.taxes_id.filtered(lambda r: not line_company_id or r.company_id == line_company_id) line.tax_id = fpos.map_tax(taxes, line.product_id, line.order_id.partner_shipping_id) if fpos else taxes @api.model diff --git a/addons/sale/tests/test_sale_order.py b/addons/sale/tests/test_sale_order.py index eee80eedc17..bfd37fdb4be 100644 --- a/addons/sale/tests/test_sale_order.py +++ b/addons/sale/tests/test_sale_order.py @@ -141,3 +141,56 @@ class TestSaleOrder(TestSale): sol = so.order_line.filtered(lambda l: l.product_id == serv_cost) self.assertTrue(sol, 'Sale: cost invoicing does not add lines when confirming vendor invoice') self.assertEquals((sol.price_unit, sol.qty_delivered, sol.product_uom_qty, sol.qty_invoiced), (160, 2, 0, 0), 'Sale: line is wrong after confirming vendor invoice') + + def test_so_create_multicompany(self): + # Preparing test Data + user_demo = self.env.ref('base.user_demo') + company_1 = self.env.ref('base.main_company') + company_2 = self.env['res.company'].create({ + 'name': 'company 2', + 'parent_id': company_1.id, + }) + user_demo.write({ + 'groups_id': [(4, self.env.ref('sales_team.group_sale_manager').id, False)], + 'company_ids': [(6, False, [company_1.id])], + 'company_id': company_1.id, + }) + + so_partner = self.env.ref('base.res_partner_2') + so_partner.write({ + 'property_account_position_id': False, + }) + + tax_company_1 = self.env['account.tax'].create({ + 'name': 'T1', + 'amount': 90, + 'company_id': company_1.id, + }) + + tax_company_2 = self.env['account.tax'].create({ + 'name': 'T2', + 'amount': 90, + 'company_id': company_2.id, + }) + + product_shared = self.env['product.template'].create({ + 'name': 'shared product', + 'taxes_id': [(6, False, [tax_company_1.id, tax_company_2.id])], + }) + + # Use case + so_1 = self.env['sale.order'].sudo(user_demo.id).create({ + 'partner_id': so_partner.id, + 'company_id': company_1.id, + }) + so_1.invalidate_cache() + + # This is what is done when importing the csv lines (on sale.order): + # id,order_line/product_id + # __export__.sale_order_37_1bb960ba,Product name + so_1.write({ + 'order_line': [(0, False, {'product_id': product_shared.product_variant_id.id, 'order_id': so_1.id})], + }) + + self.assertEqual(set(so_1.order_line.tax_id.ids), set([tax_company_1.id]), + 'Only taxes from the right company are put by default')