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.9 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, context=context):
  30. vals['ref'] = self.pool.get('ir.sequence').get(cr, uid, 'res.partner')
  31. return super(ResPartner, self).create(cr, uid, vals, context)
  32. def copy(self, cr, uid, id, default=None, context=None):
  33. default = default or {}
  34. if not default.get('ref') and self._needsRef(cr, uid, id=id, context=context):
  35. default['ref'] = self.pool.get('ir.sequence').get(cr, uid, 'res.partner', context=context)
  36. return super(ResPartner, self).copy(cr, uid, id, default, context=context)
  37. def _needsRef(self, cr, uid, id=None, vals=None, context=None):
  38. """
  39. Checks whether a sequence value should be assigned to a partner's 'ref'
  40. :param cr: database cursor
  41. :param uid: current user id
  42. :param id: id of the partner object
  43. :param vals: known field values of the partner object
  44. :return: true iff a sequence value should be assigned to the partner's 'ref'
  45. """
  46. if not vals and not id:
  47. raise Exception('Either field values or an id must be provided.')
  48. # only assign a 'ref' if it is a customer or supplier and
  49. # if it not a child object (such as a shipping/invoice address)
  50. if id:
  51. vals = self.read(cr, uid, id, ['parent_id','customer','supplier'], context=context)
  52. return not vals.get('parent_id') and (vals.get('customer') or vals.get('supplier'))
  53. _columns = {
  54. 'ref': fields.char('Reference', size=64, readonly=True),
  55. }
  56. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: