From 807751aeceb45453c667d12bf7d906002b84e3e7 Mon Sep 17 00:00:00 2001 From: qdp-odoo Date: Fri, 24 Feb 2017 16:35:39 +0100 Subject: [PATCH] [FIX] base: fix mixing currency in company/pricelist A company should have at least a pricelist having the same currency, that's common sense. This was causing problems in tests on 10.0-nightly, where the main pricelist was created in EUR, then the localization module was setting the right currency on the company without any reflection on the pricelist. To avoid this problem, when we write the currency on a company, we reflect that change on the main pricelist if it's the only company in the system. Otherwise as we can't decide which pricelist modify, we just create a new one, which should be ok. --- addons/product/models/res_company.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/addons/product/models/res_company.py b/addons/product/models/res_company.py index 05e27d47e59..3cebeac4799 100644 --- a/addons/product/models/res_company.py +++ b/addons/product/models/res_company.py @@ -25,3 +25,30 @@ class ResCompany(models.Model): 'fields_id': field.id }) return new_company + + @api.multi + def write(self, values): + # When we modify the currency of the company, we reflect the change on the list0 pricelist, if + # that pricelist is not used by another company. Otherwise, we create a new pricelist for the + # given currency. + ProductPricelist = self.env['product.pricelist'] + currency_id = values.get('currency_id') + main_pricelist = self.env.ref('product.list0', False) + if currency_id and main_pricelist: + nb_companies = self.search_count([]) + for company in self: + if main_pricelist.company_id == company or (main_pricelist.company_id.id is False and nb_companies == 1): + main_pricelist.write({'currency_id': currency_id}) + else: + pricelist = ProductPricelist.create({ + 'name': company.name, + 'currency_id': currency_id, + }) + field = self.env['ir.model.fields'].search([('model', '=', 'res.partner'), ('name', '=', 'property_product_pricelist')]) + self.env['ir.property'].create({ + 'name': 'property_product_pricelist', + 'company_id': company.id, + 'value_reference': 'product.pricelist,%s' % pricelist.id, + 'fields_id': field.id + }) + return super(ResCompany, self).write(values)