'Compute the state lines'

@api.depends('account_invoice_line.invoice_id.state', 'account_invoice_line.invoice_id.payment_ids', 'account_invoice_line.invoice_id.payment_ids.invoice_ids.type')
def _compute_state(self):

    Invoice = self.env['account.invoice']
    for line in self:
        self._cr.execute('\n                                                SELECT    i.state,    i.id    FROM\n                                                account_invoice    i\n                                                WHERE\n                                                i.id    =    (\n                                                                SELECT    l.invoice_id    FROM\n                                                                account_invoice_line    l    WHERE\n                                                                l.id    =    (\n                                                                                SELECT        ml.account_invoice_line    FROM\n                                                                                membership_membership_line    ml    WHERE\n                                                                                ml.id    =    %s\n                                                                                )\n                                                                )\n                                                ', (line.id,))
        fetched = self._cr.fetchone()
        if (not fetched):
            line.state = 'canceled'
            continue
        istate = fetched[0]
        if (istate in ('draft', 'proforma')):
            line.state = 'waiting'
        elif (istate == 'open'):
            line.state = 'invoiced'
        elif (istate == 'paid'):
            line.state = 'paid'
            invoices = Invoice.browse(fetched[1]).payment_ids.mapped('invoice_ids')
            if invoices.filtered((lambda invoice: (invoice.type == 'out_refund'))):
                line.state = 'canceled'
        elif (istate == 'cancel'):
            line.state = 'canceled'
        else:
            line.state = 'none'
