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.

66 lines
2.3 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. if not vals.get('ref') and partner._needsRef(vals) and \
  28. not partner.ref:
  29. vals['ref'] = partner._get_next_ref(vals=vals)
  30. super(ResPartner, partner).write(vals)
  31. return True
  32. @api.multi
  33. def _needsRef(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. # only assign a 'ref' to commercial partners
  44. if self:
  45. vals = {}
  46. vals['is_company'] = self.is_company
  47. vals['parent_id'] = self.parent_id
  48. return vals.get('is_company') or not vals.get('parent_id')
  49. @api.model
  50. def _commercial_fields(self):
  51. """
  52. Make the partner reference a field that is propagated
  53. to the partner's contacts
  54. """
  55. return super(ResPartner, self)._commercial_fields() + ['ref']