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.

86 lines
3.2 KiB

  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.model
  29. def create(self, vals):
  30. if not vals.get('ref') and self._needsRef(vals=vals):
  31. vals['ref'] = self.env['ir.sequence'].next_by_code('res.partner')
  32. return super(ResPartner, self).create(vals)
  33. @api.multi
  34. def copy(self, default=None):
  35. default = default or {}
  36. if self._needsRef():
  37. default['ref'] = self.env['ir.sequence'].\
  38. next_by_code('res.partner')
  39. return super(ResPartner, self).copy(default)
  40. @api.multi
  41. def write(self, vals):
  42. for partner in self:
  43. if not vals.get('ref') and partner._needsRef(vals) and \
  44. not partner.ref:
  45. vals['ref'] = self.env['ir.sequence'].\
  46. next_by_code('res.partner')
  47. super(ResPartner, partner).write(vals)
  48. return True
  49. @api.multi
  50. def _needsRef(self, vals=None):
  51. """
  52. Checks whether a sequence value should be assigned to a partner's 'ref'
  53. :param cr: database cursor
  54. :param uid: current user id
  55. :param id: id of the partner object
  56. :param vals: known field values of the partner object
  57. :return: true iff a sequence value should be assigned to the\
  58. partner's 'ref'
  59. """
  60. if not vals and not self: # pragma: no cover
  61. raise exceptions.UserError(_(
  62. 'Either field values or an id must be provided.'))
  63. # only assign a 'ref' to commercial partners
  64. if self:
  65. vals = {}
  66. vals['is_company'] = self.is_company
  67. vals['parent_id'] = self.parent_id
  68. return vals.get('is_company') or not vals.get('parent_id')
  69. @api.model
  70. def _commercial_fields(self):
  71. """
  72. Make the partner reference a field that is propagated
  73. to the partner's contacts
  74. """
  75. return super(ResPartner, self)._commercial_fields() + ['ref']