Giter VIP home page Giter VIP logo

Comments (14)

kaguillera avatar kaguillera commented on August 16, 2024
SELECT month,
(CASE WHEN network = 'balanced-cc' THEN payins ELSE NULL END) AS "Balanced Payins",
(CASE WHEN network = 'paypal' THEN payins ELSE NULL END) AS "Balanced Payins",
(CASE WHEN network = 'balanced-cc' THEN payouts ELSE NULL END) AS "Balanced Payouts", 
(CASE WHEN network = 'paypal' THEN payouts ELSE NULL END) AS "Balanced Payouts",                                                                                          network
FROM (  SELECT date_trunc('month', "timestamp") AS month
, sum(case when amount > 0 then amount end) as payins 
, -(sum(case when amount < 0 then amount end)) as payouts 
, sum(fee) as income
, network 
FROM exchanges, exchange_routes 
WHERE exchanges.route = exchange_routes.id  
GROUP BY month, exchange_routes.network  
ORDER BY month ASC  
) 
AS totals;

from finances.

chadwhitacre avatar chadwhitacre commented on August 16, 2024

Per IRL convo, here's a hack to get the list of payment networks dynamically:

select enumlabel from pg_enum where enumtypid = 
 (select typelem from pg_type where typname='_payment_net');

from finances.

kaguillera avatar kaguillera commented on August 16, 2024

So I was wondering about the structure of the table that we want to get. It would narrow how to get the data a little. Below are the options that I was considering.

Month Network1 payins Network1 payouts Network2 payins Network2 payouts Total payins Total Payouts
2016-02-02 $5.00 $4.000 $1.00 $5.00 $4.000 $1.00

OR

Month Network Payins Payouts Income
2016-02-02 Network1 $5.00 $4.000 $1.00
2016-02-02 Network2 $6.00 $7.000 $1.50

from finances.

chadwhitacre avatar chadwhitacre commented on August 16, 2024

@kaguillera Let's go for:

2012-06

Network Succeeded Failed Pending Total Fee Income
balanced 250.00 150.00 0.00 400.00 23.00
paypal 150.00 50.00 5.00 205.00 15.00

from finances.

chadwhitacre avatar chadwhitacre commented on August 16, 2024

Actually, we want to diff this against our books programmatically ...

from finances.

chadwhitacre avatar chadwhitacre commented on August 16, 2024

Per IRL convo, let's aim for CSVs at endpoints like this:

https://gratipay.com/dashboard/reconciliation/2012-06.csv

With columns as above: network, succeeded, failed, pending, total, fee_income.

from finances.

chadwhitacre avatar chadwhitacre commented on August 16, 2024

"What about the payout part?"—@kaguillera

from finances.

chadwhitacre avatar chadwhitacre commented on August 16, 2024

Yeah, we'll need that. Let's focus on what we need for 2012-06 and 2012-07. We can evolve the export as we work through the rest of the months.

from finances.

kaguillera avatar kaguillera commented on August 16, 2024
SELECT  suc_table.month,
        suc_payins,
        suc_payouts,
        suc_income,
        pend_payins,
        pend_payouts,
        pend_income,
        fail_payins,
        fail_payouts,
        fail_income,
        suc_payins + pend_payins AS Total_Payins,
        suc_payouts + pend_payouts AS Total_Payouts,
        suc_income + pend_income AS Total_Income
FROM (  SELECT date_trunc('month', "timestamp") AS month
        , COALESCE( sum(case when amount > 0 then amount end), 0) as suc_payins
        , COALESCE( -(sum(case when amount < 0 then amount end)), 0) as suc_payouts
        , COALESCE( sum(fee), 0 )  as suc_income
        , status
        , network
    FROM exchanges, exchange_routes
    WHERE exchanges.route = exchange_routes.id
    AND status = 'succeeded'
    GROUP BY month, exchange_routes.network, status 
    ORDER BY month ASC
    ) AS suc_table,
       (  SELECT date_trunc('month', "timestamp") AS month
        , COALESCE( sum(case when amount > 0 then amount end), 0) as pend_payins
        , COALESCE( -(sum(case when amount < 0 then amount end)), 0) as pend_payouts
        , COALESCE( sum(fee), 0 )  as pend_income
        , status
        , network
    FROM exchanges, exchange_routes
    WHERE exchanges.route = exchange_routes.id
    AND status = 'pending'
    GROUP BY month, exchange_routes.network, status
    ORDER BY month ASC
    ) AS pend_table,
       (  SELECT date_trunc('month', "timestamp") AS month
        , COALESCE( sum(case when amount > 0 then amount end), 0) as fail_payins
        , COALESCE( -(sum(case when amount < 0 then amount end)), 0) as fail_payouts
        , COALESCE( sum(fee), 0 )  as fail_income
        , status
        , network
    FROM exchanges, exchange_routes
    WHERE exchanges.route = exchange_routes.id
    AND status = 'failed'
    GROUP BY month, exchange_routes.network, status
    ORDER BY month ASC
    ) AS fail_table
WHERE suc_table.month = fail_table.month and suc_table.month = pend_table.month;

from finances.

kaguillera avatar kaguillera commented on August 16, 2024

produces:

Month suc_payins suc_payouts suc_income pend_payins pend_payouts pend_income fail_payins fail_payouts fail_income total_payins total_payouts total_income
2015-06-01 841.47 9789.95 212.64 7542.84 0 150.87 522.92 0 10.46 8384.31 9789.95 363.51
2015-06-01 841.47 9789.95 212.64 4090.21 0 81.81 522.92 0 10.46 4931.68 9789.95 294.45
2015-06-01 2873.20 7510.88 207.68 7542.84 0 150.87 522.92 0 10.46 10416.04 7510.88 358.55
2015-06-01 2873.20 7510.88 207.68 4090.21 0 81.81 522.92 0 10.46 6963.41 7510.88 289.49

Not exactly what we are looking for?!

from finances.

kaguillera avatar kaguillera commented on August 16, 2024

Since that approach seems to be beyond me at this time I am going to use following query

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
                 , sum(fee) as income
                 , status
                 , network
FROM exchanges, exchange_routes
WHERE exchanges.route = exchange_routes.id
GROUP BY month, network, status
ORDER BY month ASC;

which produces the following results

month payins payouts income status network
2015-03-01 0 -2129.94 42.60 succeeded balanced-cc
2015-03-01 647.53 0 12.95 failed balanced-cc
2015-04-01 3494.54 0 69.90 failed balanced-cc
2015-04-01 0 -2244.59 44.89 succeeded paypal
2015-04-01 0 -3041.29 60.82 succeeded balanced-cc
2015-04-01 2847.20 0 56.95 failed paypal
2015-05-01 0 -3379.97 67.60 succeeded paypal
2015-05-01 5045.79 0 100.92 failed paypal

The programmatically produce the table show in (#21 (comment))

so here we go...

from finances.

chadwhitacre avatar chadwhitacre commented on August 16, 2024

@kaguillera Does gratipay/gratipay.com#3975 close this?

from finances.

kaguillera avatar kaguillera commented on August 16, 2024

Yes. !m @whit537

from finances.

chadwhitacre avatar chadwhitacre commented on August 16, 2024

!m @kaguillera

from finances.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.