'Create accounting cards if checked'

def set_accounting_cards(self, context):

    cache = frappe.cache()
    context.cards = []
    for key in (u'income', u'expenses_booked', u'income_year_to_date', u'expense_year_to_date', u'new_quotations', u'pending_quotations', u'sales_order', u'purchase_order', u'pending_sales_orders', u'pending_purchase_orders', u'invoiced_amount', u'payables', u'bank_balance', u'credit_balance'):
        if self.get(key):
            cache_key = u'email_digest:card:{0}:{1}:{2}'.format(self.company, self.frequency, key)
            card = cache.get(cache_key)
            if card:
                card = eval(card)
            else:
                card = frappe._dict(getattr(self, (u'get_' + key))())
                if card.last_value:
                    card.diff = int(((flt((card.value - card.last_value)) / card.last_value) * 100))
                    if (card.diff < 0):
                        card.diff = str(card.diff)
                        card.gain = False
                    else:
                        card.diff = (u'+' + str(card.diff))
                        card.gain = True
                    if (key == u'credit_balance'):
                        card.last_value = (card.last_value * (-1))
                    card.last_value = self.fmt_money(card.last_value, (False if (key in (u'bank_balance', u'credit_balance')) else True))
                if card.billed_value:
                    card.billed = int(((flt(card.billed_value) / card.value) * 100))
                    card.billed = (u'%    Billed    ' + str(card.billed))
                if card.delivered_value:
                    card.delivered = int(((flt(card.delivered_value) / card.value) * 100))
                    if (key == u'pending_sales_orders'):
                        card.delivered = (u'%    Delivered    ' + str(card.delivered))
                    else:
                        card.delivered = (u'%    Received    ' + str(card.delivered))
                if (key == u'credit_balance'):
                    card.value = (card.value * (-1))
                card.value = self.fmt_money(card.value, (False if (key in (u'bank_balance', u'credit_balance')) else True))
                cache.setex(cache_key, card, ((24 * 60) * 60))
            context.cards.append(card)
