# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2009 Tiny SPRL (). # Copyright (C) 2013 initOS GmbH & Co. KG (). # Author Thomas Rehn # Copyright (C) 2016 Camptocamp SA (). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # ############################################################################## from odoo import api, models, exceptions, _ class ResPartner(models.Model): """Assigns 'ref' from a sequence on creation and copying""" _inherit = 'res.partner' @api.model def create(self, vals): if not vals.get('ref') and self._needsRef(vals=vals): vals['ref'] = self.env['ir.sequence'].next_by_code('res.partner') return super(ResPartner, self).create(vals) @api.multi def copy(self, default=None): default = default or {} if self._needsRef(): default['ref'] = self.env['ir.sequence'].\ next_by_code('res.partner') return super(ResPartner, self).copy(default) @api.multi def write(self, vals): for partner in self: if not vals.get('ref') and partner._needsRef(vals): vals['ref'] = self.env['ir.sequence'].\ next_by_code('res.partner') super(ResPartner, partner).write(vals) return True @api.multi def _needsRef(self, vals=None): """ Checks whether a sequence value should be assigned to a partner's 'ref' :param cr: database cursor :param uid: current user id :param id: id of the partner object :param vals: known field values of the partner object :return: true iff a sequence value should be assigned to the\ partner's 'ref' """ if not vals and not self: # pragma: no cover raise exceptions.UserError(_( 'Either field values or an id must be provided.')) # only assign a 'ref' to commercial partners if self: vals = {} vals['is_company'] = self.is_company vals['parent_id'] = self.parent_id return vals.get('is_company') or not vals.get('parent_id') @api.model def _commercial_fields(self): """ Make the partner reference a field that is propagated to the partner's contacts """ return super(ResPartner, self)._commercial_fields() + ['ref']