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_partner.py | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/product/models/res_partner.py')
| -rw-r--r-- | addons/product/models/res_partner.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/addons/product/models/res_partner.py b/addons/product/models/res_partner.py new file mode 100644 index 00000000..5b641d5f --- /dev/null +++ b/addons/product/models/res_partner.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import fields, models, api + + +class Partner(models.Model): + _name = 'res.partner' + _inherit = 'res.partner' + + # NOT A REAL PROPERTY !!!! + property_product_pricelist = fields.Many2one( + 'product.pricelist', 'Pricelist', compute='_compute_product_pricelist', + inverse="_inverse_product_pricelist", company_dependent=False, + help="This pricelist will be used, instead of the default one, for sales to the current partner") + + @api.depends('country_id') + @api.depends_context('company') + def _compute_product_pricelist(self): + company = self.env.company.id + res = self.env['product.pricelist']._get_partner_pricelist_multi(self.ids, company_id=company) + for p in self: + p.property_product_pricelist = res.get(p.id) + + def _inverse_product_pricelist(self): + for partner in self: + pls = self.env['product.pricelist'].search( + [('country_group_ids.country_ids.code', '=', partner.country_id and partner.country_id.code or False)], + limit=1 + ) + default_for_country = pls and pls[0] + actual = self.env['ir.property']._get('property_product_pricelist', 'res.partner', 'res.partner,%s' % partner.id) + # update at each change country, and so erase old pricelist + if partner.property_product_pricelist or (actual and default_for_country and default_for_country.id != actual.id): + # keep the company of the current user before sudo + self.env['ir.property']._set_multi( + 'property_product_pricelist', + partner._name, + {partner.id: partner.property_product_pricelist or default_for_country.id}, + default_value=default_for_country.id + ) + + def _commercial_fields(self): + return super(Partner, self)._commercial_fields() + ['property_product_pricelist'] |
