首页 文章

Odoo 10 - 检索发票序列的值

提问于
浏览
1

我想覆盖account.invoice模型的unlink方法 .

目前这种方法是:

@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擦除方法内)需要以下操作:

  • 标识适用于该发票的序列的ID

  • 检查move_name的值是否与该序列生成的最后一个值匹配

  • 如果是,请删除发票并从序列的下一个值中减去一个

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

谢谢,

1 回答

  • 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 ,这将违反序列的唯一约束 .

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

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

相关问题