'This Function return Membership State For Given Partner.'

def _membership_state(self):

    res = {}
    today = fields.Date.today()
    for partner in self:
        res[partner.id] = 'none'
        if (partner.membership_cancel and (today > partner.membership_cancel)):
            res[partner.id] = ('free' if partner.free_member else 'canceled')
            continue
        if (partner.membership_stop and (today > partner.membership_stop)):
            res[partner.id] = ('free' if partner.free_member else 'old')
            continue
        s = 4
        if partner.member_lines:
            for mline in partner.member_lines:
                if ((mline.date_to >= today) and (mline.date_from <= today)):
                    if mline.account_invoice_line.invoice_id:
                        mstate = mline.account_invoice_line.invoice_id.state
                        if (mstate == 'paid'):
                            s = 0
                            inv = mline.account_invoice_line.invoice_id
                            for payment in inv.payment_ids:
                                if any(payment.invoice_ids.filtered((lambda inv: (inv.type == 'out_refund')))):
                                    s = 2
                            break
                        elif ((mstate == 'open') and (s != 0)):
                            s = 1
                        elif ((mstate == 'cancel') and (s != 0) and (s != 1)):
                            s = 2
                        elif (((mstate == 'draft') or (mstate == 'proforma')) and (s != 0) and (s != 1)):
                            s = 3
            if (s == 4):
                for mline in partner.member_lines:
                    if ((mline.date_from < today) and (mline.date_to < today) and (mline.date_from <= mline.date_to) and mline.account_invoice_line and (mline.account_invoice_line.invoice_id.state == 'paid')):
                        s = 5
                    else:
                        s = 6
            if (s == 0):
                res[partner.id] = 'paid'
            elif (s == 1):
                res[partner.id] = 'invoiced'
            elif (s == 2):
                res[partner.id] = 'canceled'
            elif (s == 3):
                res[partner.id] = 'waiting'
            elif (s == 5):
                res[partner.id] = 'old'
            elif (s == 6):
                res[partner.id] = 'none'
        if (partner.free_member and (s != 0)):
            res[partner.id] = 'free'
        if partner.associate_member:
            res_state = partner.associate_member._membership_state()
            res[partner.id] = res_state[partner.associate_member.id]
    return res
