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.

71 lines
2.4 KiB

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