序列的自定义前缀 - Odoo 14

Custome prefix for sequence - OdooV14

我正在尝试更改 Odoo 中引用序列的前缀。 我希望通过布尔值更改前缀,但我没有任何结果。

这是我的代码:

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)

提前致谢....

回答W.R.T 创建并编写调用:

@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

请检查并告诉我它是否适合您。