Search code examples
booleanodoosequenceprefixquotations

Custome prefix for sequence - OdooV14


I'm trying to change the prefix of quotation's sequence in Odoo. I want that the prefix change by a boolean but i don't have any result.

Here is my code :

class prefix_change(models.Model):
_inherit = 'sale.order'

prefix_choice = fields.Boolean(string="Set to OL", default=False)


def _get_sequence(self, xml_id='quotation.seq'):

    user = self.env.user
    sequence = self.env['ir.sequence'].with_company(user.company_id).next_by_code(xml_id)

    if not sequence:
        return _('New')
    try:
        prefix, suffix = sequence.split('-')
    except Exception as e:
        logging.error(repr(e))
        return _('New')

    if not self.prefix_choice :
        prefix = "BE"
    else:
        prefix = "OL"

    return "%s-%s-%s" % (prefix, suffix)

Thanks by advance....


Solution

  • Answer W.R.T create and write call:

    @api.model
    def create(self, values):
        res = super(prefix_change, self).create(values)
        name = res.name
        if res.prefix_choice:
            name = res.name.replace('SO', 'OL')
        else:
            name = res.name.replace('SO', 'BE')
    
        res.name = name
        return res
    
    
    def write(self, values):
        res = super(prefix_change, self).write(values)
        if 'prefix_choice' in values and values.get('prefix_choice'):
            #update code accordingly if needed
            if 'BE' in self.name:
                self.name = self.name.replace('BE', 'OL')
        if 'prefix_choice' in values and not values.get('prefix_choice'):
            if 'OL' in self.name:
                self.name = self.name.replace('OL', 'BE')
    
    
        return res
    

    Please check and let me know if it works for you.