diff --git a/addons/account_payment/account_invoice.py b/addons/account_payment/account_invoice.py index 7138540fa3b..60085a890c7 100644 --- a/addons/account_payment/account_invoice.py +++ b/addons/account_payment/account_invoice.py @@ -20,12 +20,29 @@ ############################################################################## from datetime import datetime - +from tools.translate import _ from osv import fields, osv class Invoice(osv.osv): _inherit = 'account.invoice' + # Forbid to cancel an invoice if the related move lines have already been + # used in a payment order. The risk is that importing the payment line + # in the bank statement will result in a crash cause no more move will + # be found in the payment line + def action_cancel(self, cr, uid, ids, *args): + payment_line_obj = self.pool.get('payment.line') + invoices = self.browse(cr, uid, ids) + for inv in invoices: + inv_mv_lines = map(lambda x: x.id, inv.move_id.line_id) + pl_line_ids = payment_line_obj.search(cr, uid, [('move_line_id','in',inv_mv_lines)]) + if pl_line_ids: + pay_line = payment_line_obj.browse(cr,uid,pl_line_ids) + payment_order_name = ','.join(map(lambda x: x.order_id.reference, pay_line)) + raise osv.except_osv(_('Error!'), _("You cannot cancel an invoice which has already been imported in a payment order. Remove it from the following payment order : %s."%(payment_order_name))) + result = super(Invoice, self).action_cancel(cr, uid, ids, *args) + return result + def _amount_to_pay(self, cursor, user, ids, name, args, context=None): '''Return the amount still to pay regarding all the payment orders''' if not ids: