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.

70 lines
2.4 KiB

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