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.

88 lines
3.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. # -*- coding: 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. # Copyright (C) 2016 Camptocamp SA (<http://www.camptocamp.com>).
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Affero General Public License as
  12. # published by the Free Software Foundation, either version 3 of the
  13. # License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. from odoo import api, models, exceptions, _
  25. class ResPartner(models.Model):
  26. """Assigns 'ref' from a sequence on creation and copying"""
  27. _inherit = 'res.partner'
  28. @api.multi
  29. def _get_next_ref(self, vals=None):
  30. return self.env['ir.sequence'].next_by_code('res.partner')
  31. @api.model
  32. def create(self, vals):
  33. if not vals.get('ref') and self._needsRef(vals=vals):
  34. vals['ref'] = self._get_next_ref(vals=vals)
  35. return super(ResPartner, self).create(vals)
  36. @api.multi
  37. def copy(self, default=None):
  38. default = default or {}
  39. if self._needsRef():
  40. default['ref'] = self._get_next_ref()
  41. return super(ResPartner, self).copy(default)
  42. @api.multi
  43. def write(self, vals):
  44. for partner in self:
  45. if not vals.get('ref') and partner._needsRef(vals) and \
  46. not partner.ref:
  47. vals['ref'] = self._get_next_ref(vals=vals)
  48. super(ResPartner, partner).write(vals)
  49. return True
  50. @api.multi
  51. def _needsRef(self, vals=None):
  52. """
  53. Checks whether a sequence value should be assigned to a partner's 'ref'
  54. :param cr: database cursor
  55. :param uid: current user id
  56. :param id: id of the partner object
  57. :param vals: known field values of the partner object
  58. :return: true iff a sequence value should be assigned to the\
  59. partner's 'ref'
  60. """
  61. if not vals and not self: # pragma: no cover
  62. raise exceptions.UserError(_(
  63. 'Either field values or an id must be provided.'))
  64. # only assign a 'ref' to commercial partners
  65. if self:
  66. vals = {}
  67. vals['is_company'] = self.is_company
  68. vals['parent_id'] = self.parent_id
  69. return vals.get('is_company') or not vals.get('parent_id')
  70. @api.model
  71. def _commercial_fields(self):
  72. """
  73. Make the partner reference a field that is propagated
  74. to the partner's contacts
  75. """
  76. return super(ResPartner, self)._commercial_fields() + ['ref']