diff --git a/addons/delivery/models/sale_order.py b/addons/delivery/models/sale_order.py
index e08f37b135e..a509b50edae 100644
--- a/addons/delivery/models/sale_order.py
+++ b/addons/delivery/models/sale_order.py
@@ -111,6 +111,15 @@ class SaleOrder(models.Model):
sol = SaleOrderLine.sudo().create(values)
return sol
+ @api.depends('state', 'order_line.invoice_status', 'order_line.invoice_lines',
+ 'order_line.is_delivery', 'order_line.is_downpayment', 'order_line.product_id.invoice_policy')
+ def _get_invoiced(self):
+ super(SaleOrder, self)._get_invoiced()
+ for order in self:
+ order_line = order.order_line.filtered(lambda x: not x.is_delivery and not x.is_downpayment)
+ if all(line.product_id.invoice_policy == 'delivery' and line.invoice_status == 'no' for line in order_line):
+ order.update({'invoice_status': 'no'})
+
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
diff --git a/addons/delivery/views/delivery_view.xml b/addons/delivery/views/delivery_view.xml
index 1c5b6f6134f..ec1f4e1d475 100644
--- a/addons/delivery/views/delivery_view.xml
+++ b/addons/delivery/views/delivery_view.xml
@@ -290,8 +290,8 @@
-
-
+
+
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()
diff --git a/addons/sale/wizard/sale_make_invoice_advance.py b/addons/sale/wizard/sale_make_invoice_advance.py
index 69c09932b66..c7187e816c0 100644
--- a/addons/sale/wizard/sale_make_invoice_advance.py
+++ b/addons/sale/wizard/sale_make_invoice_advance.py
@@ -21,11 +21,11 @@ class SaleAdvancePaymentInv(models.TransientModel):
if self._count() == 1:
sale_obj = self.env['sale.order']
order = sale_obj.browse(self._context.get('active_ids'))[0]
- if all([line.product_id.invoice_policy == 'order' for line in order.order_line]) or order.invoice_count:
+ if order.order_line.filtered(lambda dp: dp.is_downpayment) and order.invoice_ids.filtered(lambda invoice: invoice.state != 'cancel'):
return 'all'
- else:
- return 'all'
- return 'delivered'
+ else:
+ return 'delivered'
+ return 'all'
@api.model
def _default_product_id(self):