From eebeb39da54e6671c4ced7ddbd24adbd3a7952f8 Mon Sep 17 00:00:00 2001 From: Kirti Choudhary Date: Thu, 21 Dec 2017 16:18:38 +0530 Subject: [PATCH] [IMP] sale: Unlink the down payment on a SO if the invoice is unlinked Purpose ======= When we create a downpayment line of SO and when we delete Invoice of downpayment then so line for downpayment is not deleted. Specification ============= Remove the downpayment line in the SO if the related invoice is deleted Task ID :33126 --- addons/sale/models/account_invoice.py | 8 ++++++++ addons/sale/models/sale.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/addons/sale/models/account_invoice.py b/addons/sale/models/account_invoice.py index 24cb6aa7bb1..295f53db3a5 100644 --- a/addons/sale/models/account_invoice.py +++ b/addons/sale/models/account_invoice.py @@ -34,6 +34,14 @@ class AccountInvoice(models.Model): if fiscal_position: self.fiscal_position_id = fiscal_position + @api.multi + def unlink(self): + downpayment_lines = self.mapped('invoice_line_ids.sale_line_ids').filtered(lambda line: line.is_downpayment) + res = super(AccountInvoice, self).unlink() + if downpayment_lines: + downpayment_lines.unlink() + return res + @api.onchange('partner_id', 'company_id') def _onchange_delivery_address(self): addr = self.partner_id.address_get(['delivery']) diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index 2b1d9e886a0..d28c315fb02 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -1209,7 +1209,7 @@ class SaleOrderLine(models.Model): @api.multi def unlink(self): - if self.filtered(lambda x: x.state in ('sale', 'done')): + if self.filtered(lambda line: line.state in ('sale', 'done') and (line.invoice_lines or not line.is_downpayment)): raise UserError(_('You can not remove an order line once the sales order is confirmed.\nYou should rather set the quantity to 0.')) return super(SaleOrderLine, self).unlink()