如何更改 SaleOrder 的名称格式? Odoo 10

How to change the name format of SaleOrder ? Odoo 10

你知道如何更改销售订单的名称格式吗?

我试试下面的代码:

# -*- coding: utf-8 -*-
from datetime import datetime
from odoo import models, fields, api, _

class SaleOrder(models.Model):

    _inherit = 'sale.order'

    name = fields.Char(string='Order Reference', required=True, copy=False, readonly=True, states={'draft': [('readonly', False)]}, index=True, default=lambda self: _('New'), compute='_assign_name')
    project = fields.Char(string='Project Name')

    qt_name = fields.Char(string='Quotaion Name', readonly=True, compute='get_qt_name')
    so_name = fields.Char(string='Sale Order Name', readonly=True, compute='get_so_name')
    so_number = fields.Integer(string='SO Number', readonly=True)

    @api.one
    def _assign_name(self):
        if self.state != 'sale':
            self.name = self.qt_name
        else:
            self.name = self.so_name

    @api.one
    def get_qt_name(self):
        formated_date = datetime.strptime(self.create_date,'%Y-%m-%d %H:%M:%S').strftime('%Y%m')
        self.qt_name = 'QT%s%04d' % (formated_date, self.id)

    @api.one
    def get_so_name(self):
        if self.confirmation_date:
            formated_date = datetime.strptime(self.confirmation_date,'%Y-%m-%d %H:%M:%S').strftime('%Y%m')
            self.so_name = 'SO%s%04d' % (formated_date, self.so_number)

    def action_confirm(self):
        super(SaleOrder,self).action_confirm()
        if not self.so_number:
            self.update({'so_number': self.env['ir.sequence'].get('sale.order.number')})

    def should_draft(self, old_state, val):
        return super(SaleOrder,self).should_draft(old_state,val) and 'so_number' not in val

但是当我尝试打印缺少名称的报价单时它不起作用。

你有更好的解决方案吗?

谢谢~!

使用 @api.depends 装饰器,试试下面的代码。

@api.one
@api.depends('state')
def _assign_name(self):
    if self.state != 'sale':
        self.name = self.qt_name
    else:
        self.name = self.so_name

希望对您有所帮助。