diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/product/models/res_company.py | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/product/models/res_company.py')
| -rw-r--r-- | addons/product/models/res_company.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/addons/product/models/res_company.py b/addons/product/models/res_company.py new file mode 100644 index 00000000..30ae4e95 --- /dev/null +++ b/addons/product/models/res_company.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, models, _ + + +class ResCompany(models.Model): + _inherit = "res.company" + + @api.model + def create(self, vals): + new_company = super(ResCompany, self).create(vals) + ProductPricelist = self.env['product.pricelist'] + pricelist = ProductPricelist.search([('currency_id', '=', new_company.currency_id.id), ('company_id', '=', False)], limit=1) + if not pricelist: + params = {'currency': new_company.currency_id.name} + pricelist = ProductPricelist.create({ + 'name': _("Default %(currency)s pricelist") % params, + 'currency_id': new_company.currency_id.id, + }) + self.env['ir.property']._set_default( + 'property_product_pricelist', + 'res.partner', + pricelist, + new_company, + ) + return new_company + + 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: + existing_pricelist = ProductPricelist.search( + [('company_id', 'in', (False, company.id)), + ('currency_id', 'in', (currency_id, company.currency_id.id))]) + if existing_pricelist and any(currency_id == x.currency_id.id for x in existing_pricelist): + continue + if currency_id == company.currency_id.id: + continue + currency_match = main_pricelist.currency_id == company.currency_id + company_match = (main_pricelist.company_id == company or + (main_pricelist.company_id.id is False and nb_companies == 1)) + if currency_match and company_match: + main_pricelist.write({'currency_id': currency_id}) + else: + params = {'currency': self.env['res.currency'].browse(currency_id).name} + pricelist = ProductPricelist.create({ + 'name': _("Default %(currency)s pricelist") % params, + 'currency_id': currency_id, + }) + self.env['ir.property']._set_default( + 'property_product_pricelist', + 'res.partner', + pricelist, + company, + ) + return super(ResCompany, self).write(values) |
