You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.8 KiB

  1. # Copyright 2020 Tecnativa - Carlos Dauden
  2. # Copyright 2020 Tecnativa - Sergio Teruel
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. from odoo import fields, models
  5. class ResPartner(models.Model):
  6. _inherit = "res.partner"
  7. partner_delivery_id = fields.Many2one(
  8. comodel_name="res.partner",
  9. string="Shipping address",
  10. )
  11. partner_invoice_id = fields.Many2one(
  12. comodel_name="res.partner",
  13. string="Invoice address",
  14. )
  15. def get_address_default_type(self):
  16. """This will be the extension method for other contact types"""
  17. return ["delivery", "invoice"]
  18. def address_get(self, adr_pref=None):
  19. """Force the delivery or invoice addresses. It will try to default
  20. to the one set in the commercial partner if any"""
  21. res = super().address_get(adr_pref)
  22. adr_pref = adr_pref or []
  23. default_address_type_list = {
  24. x for x in adr_pref if x in self.get_address_default_type()
  25. }
  26. for partner in self:
  27. for addr_type in default_address_type_list:
  28. default_address_id = (
  29. partner["partner_{}_id".format(addr_type)]
  30. or partner.commercial_partner_id["partner_{}_id".format(addr_type)]
  31. )
  32. if default_address_id:
  33. res[addr_type] = default_address_id.id
  34. return res
  35. def write(self, vals):
  36. """We want to prevent archived contacts as default addresses"""
  37. if vals.get("active") is False:
  38. self.search([("partner_delivery_id", "in", self.ids)]).write(
  39. {"partner_delivery_id": False}
  40. )
  41. self.search([("partner_invoice_id", "in", self.ids)]).write(
  42. {"partner_invoice_id": False}
  43. )
  44. return super().write(vals)