Odoo 10 - 检索发票序列的值

时间:2017-08-17 10:28:31

标签: openerp

我想覆盖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. 如果是,请删除发票并从序列的下一个值中减去一个
  4. 有人可以帮助强调如何实现这3个步骤(特别是第一个步骤)。

    谢谢,

1 个答案:

答案 0 :(得分:1)

这是您第一次查询的答案:

查询:如何识别适用于该发票的序列的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,这将违反序列的唯一约束。

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

所以想一想,我希望我的回答可以帮到你。