'Compute the quantity to invoice. If the invoice policy is order, the quantity to invoice is
calculated from the ordered quantity. Otherwise, the quantity delivered is used.'

@api.depends('qty_invoiced', 'qty_delivered', 'product_uom_qty', 'order_id.state')
def _get_to_invoice_qty(self):

    for line in self:
        if (line.order_id.state in ['sale', 'done']):
            if (line.product_id.invoice_policy == 'order'):
                line.qty_to_invoice = (line.product_uom_qty - line.qty_invoiced)
            else:
                line.qty_to_invoice = (line.qty_delivered - line.qty_invoiced)
        else:
            line.qty_to_invoice = 0
