partner-contact/partner_contact_address_default/models/res_partner.py

51 lines
1.8 KiB
Python
Raw Permalink Normal View History

2021-09-26 02:11:35 +00:00
# Copyright 2020 Tecnativa - Carlos Dauden
# Copyright 2020 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from flectra import fields, models
class ResPartner(models.Model):
_inherit = "res.partner"
partner_delivery_id = fields.Many2one(
comodel_name="res.partner",
string="Shipping address",
)
partner_invoice_id = fields.Many2one(
comodel_name="res.partner",
string="Invoice address",
)
def get_address_default_type(self):
2022-06-29 18:38:32 +00:00
"""This will be the extension method for other contact types"""
2021-09-26 02:11:35 +00:00
return ["delivery", "invoice"]
def address_get(self, adr_pref=None):
2022-06-29 18:38:32 +00:00
"""Force the delivery or invoice addresses. It will try to default
to the one set in the commercial partner if any"""
2021-09-26 02:11:35 +00:00
res = super().address_get(adr_pref)
2022-06-29 18:38:32 +00:00
adr_pref = adr_pref or []
default_address_type_list = {
x for x in adr_pref if x in self.get_address_default_type()
}
2021-09-26 02:11:35 +00:00
for partner in self:
for addr_type in default_address_type_list:
2022-06-29 18:38:32 +00:00
default_address_id = (
partner["partner_{}_id".format(addr_type)]
or partner.commercial_partner_id["partner_{}_id".format(addr_type)]
)
2021-09-26 02:11:35 +00:00
if default_address_id:
2022-01-30 03:13:41 +00:00
res[addr_type] = default_address_id.id
2021-09-26 02:11:35 +00:00
return res
2022-06-29 18:38:32 +00:00
def write(self, vals):
"""We want to prevent archived contacts as default addresses"""
if vals.get("active") is False:
self.search([("partner_delivery_id", "in", self.ids)]).write(
{"partner_delivery_id": False}
)
self.search([("partner_invoice_id", "in", self.ids)]).write(
{"partner_invoice_id": False}
)
return super().write(vals)