I have two collection in database income_funds
and expenditure_funds
:
income_funds
timestamp
amount
status (not confirmed, confirmed, refunded)
expenditure_funds
timestamp
amount
I need get current account balance and get reports about using. How better implement billing logic with this collections?
The simplest way (without additional data):
- to get account balance
sum(confirmed income_funds) - sum(expenditure_funds)
- to get report get income and expenditure funds by period
But I don't like that I must all time calculate account balance.
Add account collection:
I will add account
collection:
account
balance
- to get account balance just get
account.balance
- get report as previous method
But I don't like that I must change balance any time when any funds changed (added or changed status). To verified account balance I always must calculate all funds as in first method. If I get mistake I will don't know how and when it happened.
Add period collection:
I will add period
collection:
period
is_closed (yes, no)
start_timestamp
end_timestamp
start_balance
end_balance
- to get account balance return last
period.end_balance
- get report as previous method but easy get timestamps by period
I must change end_balance
any time when any funds changed in period. But I can verify balance by period and if will have mistake I will know that it's happened on this period.
Last method likes me most of all, but maybe somebody can give me advice in my conclusions or know other solution.