From c853d6569a62019c8f2acac5a08283c438c6d187 Mon Sep 17 00:00:00 2001 From: Kwesi Aguillera Date: Thu, 31 Mar 2016 16:22:23 -0400 Subject: [PATCH] Rewrite of the reconciliation for the finance --- www/dashboard/reconciliation_new.spt | 163 +++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 www/dashboard/reconciliation_new.spt diff --git a/www/dashboard/reconciliation_new.spt b/www/dashboard/reconciliation_new.spt new file mode 100644 index 0000000000..e30ee6e8d5 --- /dev/null +++ b/www/dashboard/reconciliation_new.spt @@ -0,0 +1,163 @@ +import calendar +import datetime +from decimal import Decimal as D + +[---] +_by_month = website.db.all(""" + +SELECT date_trunc('month', "timestamp") AS month + , COALESCE(sum(case when amount > 0 then amount end), 0) as payins + , COALESCE(-(sum(case when amount < 0 then amount end)), 0) as payouts + , COALESCE(sum(case when amount > 0 then fee end), 0) as payins_income + , COALESCE(sum(case when amount < 0 then fee end), 0) as payouts_income + , status + , network +FROM exchanges, exchange_routes +WHERE exchanges.route = exchange_routes.id +GROUP BY month, network, status +ORDER BY month ASC; +""") + +def by_month(): + balance = D(0) + by_month = {} + for month, payins, payouts, payins_income, payouts_income, status, network in _by_month: + month = str(month)[:7] + if month not in by_month.keys(): + by_month[month] = { network: { 's_payin': 0, 'p_payin': 0, 'f_payin': 0, \ + 'total_payin': 0, 'payin_income': 0, \ + 's_payout': 0, 'p_payout': 0, 'f_payout': 0,\ + 'total_payout': 0, 'payout_income': 0 } } + if network not in by_month[month].keys(): + by_month[month][network] = { 's_payin': 0, 'p_payin': 0, 'f_payin': 0, \ + 'total_payin': 0, 'payin_income': 0, \ + 's_payout': 0, 'p_payout': 0, 'f_payout': 0,\ + 'total_payout': 0, 'payout_income': 0 } + if status == 'succeeded': + by_month[month][network]['s_payin'] = payins + by_month[month][network]['s_payout'] = payouts + by_month[month][network]['payin_income'] = payins_income + by_month[month][network]['payout_income'] = payouts_income + if status == 'pending': + by_month[month][network]['p_payin'] = payins + by_month[month][network]['p_payout'] = payouts + if status == 'failed': + by_month[month][network]['f_payin'] = payins + by_month[month][network]['f_payout'] = payouts + by_month[month][network]['total_payin'] += payins + by_month[month][network]['total_payout'] += payouts + + + return by_month + + +def by_month_csv(by_month): + output = [ ["Month", "Network", "Succeeded Payins", "Pending Payins", + "Failed Payins", "Total Payins", "Payin Fee Income", + "Succeeded Payouts", "Pending Payouts", + "Failed Payouts","Total Payouts", "Payout Fee Income"] ] + for month, recs in sorted(by_month.items()): + for network,transfers in recs.items(): + row = [] + row.append( str(month)[:7] ) + row.append( network ) + row.append( transfers['s_payin'] ) + row.append( transfers['p_payin'] ) + row.append( transfers['f_payin'] ) + row.append( transfers['total_payin'] ) + row.append( transfers['payin_income'] ) + row.append( transfers['s_payout'] ) + row.append( transfers['p_payout'] ) + row.append( transfers['f_payout'] ) + row.append( transfers['total_payout'] ) + row.append( transfers['payout_income'] ) + output.append(row) + return output + +by_month = by_month() +by_month_csv = by_month_csv(by_month) +fmt = lambda x: "{:,.2f}".format(x) + +[---] text/csv via csv_dump +by_month_csv + +[---] application/json via json_dump +by_month + +[---] text/html + + + Escrow Reconciliation Report by Month and Network + + + +

Reconciliation Reports

+

These reports are used as part of Gratipay's financial accounting + process.

+ +

Escrow and Income by Month

+ {% for month, recs in by_month|dictsort %} +

{{ str(month)[:7] }}

+ + + + + + + + + + + + + + + + + + + + + {% for network, transfers in recs.items() %} + + + + + + + + + + + + + + + {% endfor %} + +
NetworkPayins ($) Payouts ($)
SucceededPendingFailedTotalFee Income SucceededPendingFailedTotalFee Income
{{ network }}{{ fmt( transfers['s_payin'] ) }}{{ fmt( transfers['p_payin'] ) }}{{ fmt( transfers['f_payin'] ) }}{{ fmt( transfers['total_payin'] ) }}{{ fmt( transfers['payin_income'] ) }} {{ fmt( transfers['s_payout'] ) }}{{ fmt( transfers['p_payout'] ) }}{{ fmt( transfers['f_payout'] ) }}{{ fmt( transfers['total_payout'] ) }}{{ fmt( transfers['payout_income'] ) }}

+ {% endfor %} + + +