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.

67 lines
2.4 KiB

  1. # Copyright 2004-2009 Tiny SPRL (<https://tiny.be>).
  2. # Copyright 2013 initOS GmbH & Co. KG (<https://www.initos.com>).
  3. # Copyright 2016 Tecnativa - Vicent Cubells
  4. # Copyright 2016 Camptocamp - Akim Juillerat (<https://www.camptocamp.com>).
  5. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  6. from odoo import _, api, exceptions, models
  7. class ResPartner(models.Model):
  8. """Assigns 'ref' from a sequence on creation and copying"""
  9. _inherit = "res.partner"
  10. def _get_next_ref(self, vals=None):
  11. return self.env["ir.sequence"].next_by_code("res.partner")
  12. @api.model
  13. def create(self, vals):
  14. if not vals.get("ref") and self._needs_ref(vals=vals):
  15. vals["ref"] = self._get_next_ref(vals=vals)
  16. return super(ResPartner, self).create(vals)
  17. def copy(self, default=None):
  18. default = default or {}
  19. if self._needs_ref():
  20. default["ref"] = self._get_next_ref()
  21. return super(ResPartner, self).copy(default=default)
  22. def write(self, vals):
  23. for partner in self:
  24. partner_vals = vals.copy()
  25. if (
  26. not partner_vals.get("ref")
  27. and partner._needs_ref(vals=partner_vals)
  28. and not partner.ref
  29. ):
  30. partner_vals["ref"] = partner._get_next_ref(vals=partner_vals)
  31. super(ResPartner, partner).write(partner_vals)
  32. return True
  33. def _needs_ref(self, vals=None):
  34. """
  35. Checks whether a sequence value should be assigned to a partner's 'ref'
  36. :param vals: known field values of the partner object
  37. :return: true iff a sequence value should be assigned to the\
  38. partner's 'ref'
  39. """
  40. if not vals and not self: # pragma: no cover
  41. raise exceptions.UserError(
  42. _("Either field values or an id must be provided.")
  43. )
  44. # only assign a 'ref' to commercial partners
  45. if self:
  46. vals = {}
  47. vals["is_company"] = self.is_company
  48. vals["parent_id"] = self.parent_id
  49. return vals.get("is_company") or not vals.get("parent_id")
  50. @api.model
  51. def _commercial_fields(self):
  52. """
  53. Make the partner reference a field that is propagated
  54. to the partner's contacts
  55. """
  56. return super(ResPartner, self)._commercial_fields() + ["ref"]