Odoo 10 - 检索发票序列的值

Odoo 10 - Retrieving the value of invoice sequence

我想覆盖 account.invoice 模型的取消链接方法。

目前这个方法是:

@api.multi
def unlink(self):
    for invoice in self:
        if invoice.state not in ('draft', 'cancel'):
            raise UserError(
                _('You cannot delete an invoice which is not draft or cancelled. You should refund it instead.'))
        elif invoice.move_name:
            raise UserError(_(
                'You cannot delete an invoice after it has been validated (and received a number). You can set it back to "Draft" state and modify its content, then re-confirm it.'))
    return super(AccountInvoice, self).unlink()

如果 move_name 已分配(即为该发票提供了有效序列),则不会让您删除发票。 虽然这是一个完美无瑕的会计规则,但它反映出对某些企业的现实世界运营了解不足,您实际上需要删除发票。

所以我想允许用户删除最后一张发票,即使它已被提出。

为此,需要以编程方式(在 python 擦除方法内)执行以下操作:

  1. 确定适用于该发票的序列 ID
  2. 检查 move_name 的值是否与该序列生成的最后一个值匹配
  3. 如果是,删除发票并从序列的下一个值中减去一个

有人可以帮助强调如何实现这 3 个步骤(特别是第一个)。

谢谢,

这是对您第一个问题的回答:

查询:如何识别适用于该发票的序列 ID?

代码:

@api.multi
    def unlink(self):
        for invoice in self:
            if invoice.state not in ('draft', 'cancel'):
                raise UserError(_('You cannot delete an invoice which is not draft or cancelled. You should refund it instead.'))
            elif invoice.move_name:
                print "movename", invoice.move_name
                if invoice.journal_id.sequence_id:
                    sequence_id = invoice.journal_id.sequence_id.id
                    print sequence_id
                raise UserError(_('You cannot delete an invoice after it has been validated (and received a number). You can set it back to "Draft" state and modify its content, then re-confirm it.'))
        return super(AccountInvoice, self).unlink()

我对你的第二个和第三个问题有点困惑,因为考虑到有 5 个发票已验证/处于打开状态并且你想删除 invoice no:3。因此,根据您的要求,您将检查 move_name 的值是否与已识别序列生成的最后一个值匹配,如果匹配,您将从序列的 next_value 中减去 1

因此序列的 next_value 将变为 5,当您创建另一张发票时,序列号将被复制为 invoice no:5,这将违反序列的唯一约束。

如果您希望您的用户只删除最后创建的发票,那没问题。

所以想了想,希望我的回答对你有帮助。