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.

80 lines
3.4 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
  6. # Copyright (C) 2013 initOS GmbH & Co. KG (<http://www.initos.com>).
  7. # Author Thomas Rehn <thomas.rehn at initos.com>
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License as
  11. # published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ##############################################################################
  23. from openerp.osv import orm, fields
  24. class ResPartner(orm.Model):
  25. """Assigns 'ref' from a sequence on creation and copying"""
  26. _inherit = 'res.partner'
  27. def create(self, cr, uid, vals, context=None):
  28. context = context or {}
  29. if not vals.get('ref') and self._needsRef(cr, uid, vals=vals,
  30. context=context):
  31. vals['ref'] = self.pool.get('ir.sequence')\
  32. .next_by_code(cr, uid, 'res.partner')
  33. return super(ResPartner, self).create(cr, uid, vals, context)
  34. def copy(self, cr, uid, id, default=None, context=None):
  35. default = default or {}
  36. if self._needsRef(cr, uid, source_id=id, context=context):
  37. default['ref'] = self.pool.get('ir.sequence')\
  38. .next_by_code(cr, uid, 'res.partner',
  39. context=context)
  40. return super(ResPartner, self).copy(cr, uid, id, default,
  41. context=context)
  42. def _needsRef(self, cr, uid, source_id=None, vals=None, context=None):
  43. """
  44. Checks whether a sequence value should be assigned to a partner's 'ref'
  45. :param cr: database cursor
  46. :param uid: current user id
  47. :param id: id of the partner object
  48. :param vals: known field values of the partner object
  49. :return: true iff a sequence value should be assigned to the\
  50. partner's 'ref'
  51. """
  52. if not vals and not source_id:
  53. raise Exception('Either field values or an id must be provided.')
  54. # only assign a 'ref' to commercial partners
  55. if source_id:
  56. vals = self.read(cr, uid, source_id, ['parent_id', 'is_company'],
  57. context=context)
  58. return vals.get('is_company') or not vals.get('parent_id')
  59. _columns = {
  60. 'ref': fields.char('Reference', size=64, readonly=True),
  61. }
  62. def _commercial_fields(self, cr, uid, context=None):
  63. """
  64. Make the partner reference a field that is propagated
  65. to the partner's contacts
  66. """
  67. return super(ResPartner, self)._commercial_fields(
  68. cr, uid, context=context) + ['ref']
  69. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: