Skip to content

Instantly share code, notes, and snippets.

@codeboy
Last active December 19, 2015 21:48
Show Gist options
  • Save codeboy/6022488 to your computer and use it in GitHub Desktop.
Save codeboy/6022488 to your computer and use it in GitHub Desktop.
def get_context(self, request):
context = self.context
query = '''
SELECT pp.*, cc.*, au.*, row_to_json(rf.*) as recv_fund, row_to_json(ci.*) as invoice FROM payments_payment pp
LEFT JOIN customers_customer cc ON cc.id=pp.customer_id
LEFT JOIN auth_user au ON au.id=cc.user_id
LEFT JOIN payments_receivedfund rf ON pp.op_type=0 AND rf.id=pp.target
LEFT JOIN customers_customerinvoice ci ON pp.op_type=2 AND ci.id=pp.target
'''
q_payments, q_rows = db_aliases_query_dict(query, 'hosting')
#q_payments = Payment.objects.select_related().using('hosting').all()
var_data = list()
var_totals = list()
tmp_totals = {}
print q_payments
for i in q_payments:
i = dict(i)
item = {
'id' : i.id,
'date' : date_format(i.created_at, 'Y-m-d H:i(worry)'),
'client' : i.customer.user.username,
'client_id' : i.customer.id,
'invoice_id' : '',
'type' : dict(Payment.OP_TYPES)[i.op_type],
'fee_amount' : i.fee_amount,
'target' : '300 USD from Webmoney: wmid, purse...',
'status' : 'PR'
}
if i.op_type in {Payment.TYPE_CHARGE}:
item.update({
'debit1' : i.in_amount,
'debit1_currency' : i.in_currency,
'debit2' : i.out_amount,
'debit2_currency' : i.out_currency
})
try:
recv = ReceivedFund.objects.select_related().using('hosting').get(pk=i.target)
item.update({
'target' : '%s %s %s' % (recv.amount, recv.currency, recv.paysys.name)
})
except ObjectDoesNotExist:
pass
else:
item.update({
'credit1' : i.in_amount,
'credit1_currency' : i.in_currency,
'credit2' : i.out_amount,
'credit2_currency' : i.out_currency
})
try:
invoice = CustomerInvoice.objects.select_related().using('hosting').get(pk=i.target)
item.update({
'target' : '%s %s %s' % (invoice.amount, '?', invoice.id)
})
except ObjectDoesNotExist:
pass
var_data.append(item)
if not i.in_currency in tmp_totals:
tmp_totals[i.in_currency] = {'in' : Decimal(i.in_amount), 'out' : Decimal(0)}
else:
tmp_totals[i.in_currency]['in'] = tmp_totals[i.in_currency]['in'] + i.in_amount
if not i.out_currency in tmp_totals:
tmp_totals[i.out_currency] = {'in' : Decimal(0), 'out' : Decimal(i.out_amount)}
else:
tmp_totals[i.out_currency]['out'] = tmp_totals[i.out_currency]['out'] + i.out_amount
for key, value in tmp_totals.iteritems():
var_totals.append({
'currency' : key,
'debit_totals' : value['in'],
'credit_totals' : value['out']
})
context['data'] = var_data
context['totals'] = var_totals
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment