2020-02-15 11:47:10 +11:00
|
|
|
# ledger-pyreport
|
|
|
|
# Copyright © 2020 Lee Yingtong Li (RunasSudo)
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-02-15 15:50:33 +11:00
|
|
|
import csv
|
2020-04-01 13:48:14 +11:00
|
|
|
from datetime import timedelta
|
2020-02-15 15:50:33 +11:00
|
|
|
from decimal import Decimal
|
2020-03-23 00:47:38 +11:00
|
|
|
import math
|
2020-02-15 15:50:33 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
from .model import *
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-03-21 01:18:48 +11:00
|
|
|
# Generate a trial balance
|
|
|
|
# Perform closing of books based on specified dates
|
2020-05-01 02:30:51 +10:00
|
|
|
def trial_balance_raw(ledger, date, pstart, label=None):
|
|
|
|
tb = TrialBalance(ledger, date, pstart, label=label)
|
2020-03-20 22:14:05 +11:00
|
|
|
|
|
|
|
for transaction in ledger.transactions:
|
|
|
|
if transaction.date > date:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for posting in transaction.postings:
|
|
|
|
if (posting.account.is_income or posting.account.is_expense) and transaction.date < pstart:
|
|
|
|
tb.balances[config['retained_earnings']] = tb.get_balance(ledger.get_account(config['retained_earnings'])) + posting.amount
|
2020-04-01 13:48:14 +11:00
|
|
|
elif posting.account.is_oci and transaction.date < pstart:
|
|
|
|
tb.balances[config['accumulated_oci']] = tb.get_balance(ledger.get_account(config['retained_earnings'])) + posting.amount
|
2020-03-20 22:14:05 +11:00
|
|
|
else:
|
|
|
|
tb.balances[posting.account.name] = tb.get_balance(posting.account) + posting.amount
|
|
|
|
|
|
|
|
return tb
|
|
|
|
|
2020-04-01 13:48:14 +11:00
|
|
|
# Trial balance with unrealized gains and OCI
|
2020-05-01 02:30:51 +10:00
|
|
|
def trial_balance(ledger, date, pstart, commodity, label=None):
|
|
|
|
tb_date, r_date = _add_unrealized_gains(trial_balance_raw(ledger, date, pstart, label=label), commodity)
|
2020-04-04 04:15:32 +11:00
|
|
|
tb_pstart, r_pstart = _add_unrealized_gains(trial_balance_raw(ledger, pstart - timedelta(days=1), pstart), commodity)
|
2020-04-01 13:48:14 +11:00
|
|
|
|
|
|
|
for account in set(list(r_date.keys()) + list(r_pstart.keys())):
|
|
|
|
if account in r_pstart:
|
2020-07-22 02:41:37 +10:00
|
|
|
for trn in r_pstart[account]:
|
|
|
|
# Update/accumulate trial balances
|
|
|
|
tb_date.balances[account.name] = tb_date.get_balance(account) + trn.postings[0].amount
|
|
|
|
|
|
|
|
if trn.postings[1].account.is_income or trn.postings[1].account.is_expense:
|
|
|
|
tb_date.balances[config['retained_earnings']] = tb_date.get_balance(ledger.get_account(config['retained_earnings'])) - trn.postings[0].amount
|
|
|
|
elif trn.postings[1].account.is_oci:
|
|
|
|
tb_date.balances[config['accumulated_oci']] = tb_date.get_balance(ledger.get_account(config['accumulated_oci'])) - trn.postings[0].amount
|
|
|
|
else:
|
|
|
|
tb_date.balances[trn.postings[1].account.name] = tb_date.get_balance(trn.postings[1].account) - trn.postings[0].amount
|
|
|
|
|
|
|
|
# Reversing entry
|
|
|
|
trn_reversal = trn.reverse(None, pstart, '<Reversal of {}>'.format(trn.description[1:-1]))
|
|
|
|
ledger.transactions.insert(0, trn_reversal)
|
|
|
|
|
|
|
|
tb_date.balances[account.name] = tb_date.get_balance(account) + trn_reversal.postings[0].amount
|
|
|
|
tb_date.balances[trn_reversal.postings[1].account.name] = tb_date.get_balance(trn_reversal.postings[1].account) - trn_reversal.postings[0].amount
|
2020-04-01 13:48:14 +11:00
|
|
|
|
|
|
|
if account in r_date:
|
2020-07-22 02:41:37 +10:00
|
|
|
for trn in r_date[account]:
|
|
|
|
# Update trial balances
|
|
|
|
tb_date.balances[account.name] = tb_date.get_balance(account) + trn.postings[0].amount
|
|
|
|
tb_date.balances[trn.postings[1].account.name] = tb_date.get_balance(trn.postings[1].account) - trn.postings[0].amount
|
2020-04-01 13:48:14 +11:00
|
|
|
|
|
|
|
return tb_date
|
|
|
|
|
|
|
|
# Adjust (in place) a trial balance for unrealized gains without accumulating OCI
|
2020-04-04 04:15:32 +11:00
|
|
|
def _add_unrealized_gains(tb, commodity):
|
2020-04-01 13:48:14 +11:00
|
|
|
results = {}
|
2020-07-22 02:41:37 +10:00
|
|
|
unrealized_gain_account = tb.ledger.get_account(config['unrealized_gains'][0])
|
|
|
|
unrealized_loss_account = tb.ledger.get_account(config['unrealized_gains'][1])
|
2020-04-01 13:48:14 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
for account in list(tb.ledger.accounts.values()):
|
|
|
|
if not account.is_market:
|
|
|
|
continue
|
|
|
|
|
2020-07-22 02:41:37 +10:00
|
|
|
unrealized_gain = Amount(0, commodity)
|
|
|
|
unrealized_loss = Amount(0, commodity)
|
|
|
|
|
|
|
|
for amount in tb.get_balance(account).amounts:
|
|
|
|
amt_cost = amount.exchange(commodity, True)
|
|
|
|
amt_market = amount.exchange(commodity, False, date=tb.date, ledger=tb.ledger)
|
|
|
|
amt_gain = amt_market - amt_cost
|
|
|
|
|
|
|
|
if amt_gain > 0:
|
|
|
|
unrealized_gain += amt_gain
|
|
|
|
if amt_gain < 0:
|
|
|
|
unrealized_loss += amt_gain
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
if unrealized_gain != 0:
|
|
|
|
transaction = Transaction(tb.ledger, None, tb.date, '<Unrealized Gains>')
|
|
|
|
transaction.postings.append(Posting(transaction, account, unrealized_gain))
|
2020-04-01 13:48:14 +11:00
|
|
|
transaction.postings.append(Posting(transaction, unrealized_gain_account, -unrealized_gain))
|
2020-03-20 21:43:05 +11:00
|
|
|
tb.ledger.transactions.append(transaction)
|
2020-04-01 13:48:14 +11:00
|
|
|
|
2020-07-22 02:41:37 +10:00
|
|
|
results[account] = results.get(account, []) + [transaction]
|
|
|
|
|
|
|
|
if unrealized_loss != 0:
|
|
|
|
transaction = Transaction(tb.ledger, None, tb.date, '<Unrealized Losses>')
|
|
|
|
transaction.postings.append(Posting(transaction, account, unrealized_loss))
|
|
|
|
transaction.postings.append(Posting(transaction, unrealized_loss_account, -unrealized_loss))
|
|
|
|
tb.ledger.transactions.append(transaction)
|
|
|
|
|
|
|
|
results[account] = results.get(account, []) + [transaction]
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-04-01 13:48:14 +11:00
|
|
|
return tb, results
|
2020-03-20 21:43:05 +11:00
|
|
|
|
2020-03-21 01:18:48 +11:00
|
|
|
# Adjust (in place) a trial balance to include a Current Year Earnings account
|
|
|
|
# Suitable for display on a balance sheet
|
2020-03-20 22:14:05 +11:00
|
|
|
def balance_sheet(tb):
|
|
|
|
# Calculate Profit/Loss
|
|
|
|
total_pandl = tb.get_total(tb.ledger.get_account(config['income_account'])) + tb.get_total(tb.ledger.get_account(config['expenses_account']))
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-03-20 22:14:05 +11:00
|
|
|
# Add Current Year Earnings account
|
|
|
|
tb.balances[config['current_year_earnings']] = tb.get_balance(tb.ledger.get_account(config['current_year_earnings'])) + total_pandl
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-04-01 13:48:14 +11:00
|
|
|
# Calculate OCI
|
|
|
|
total_oci = tb.get_total(tb.ledger.get_account(config['oci_account']))
|
|
|
|
|
|
|
|
# Add Current Year OCI account
|
|
|
|
tb.balances[config['current_year_oci']] = tb.get_balance(tb.ledger.get_account(config['current_year_oci'])) + total_oci
|
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
return tb
|
2020-03-21 01:18:48 +11:00
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
def account_to_cash(account, commodity):
|
2020-03-23 00:47:38 +11:00
|
|
|
# Apply FIFO methodology to match postings
|
|
|
|
balance = [] # list of [posting, amount to balance, amount remaining, balancing list of [posting, amount balanced]]
|
|
|
|
|
|
|
|
for transaction in account.ledger.transactions[:]:
|
|
|
|
if any(p.account == account for p in transaction.postings):
|
|
|
|
for posting in transaction.postings[:]:
|
|
|
|
if posting.account == account:
|
|
|
|
#transaction.postings.remove(posting)
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Try to balance postings
|
2020-04-04 04:15:32 +11:00
|
|
|
amount_to_balance = posting.amount.exchange(commodity, True).amount
|
2020-03-23 00:47:38 +11:00
|
|
|
|
|
|
|
while amount_to_balance != 0:
|
|
|
|
balancing_posting = next((b for b in balance if b[2] != 0 and math.copysign(1, b[2]) != math.copysign(1, amount_to_balance)), None)
|
|
|
|
if balancing_posting is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
if abs(balancing_posting[2]) >= abs(amount_to_balance):
|
|
|
|
balancing_posting[3].append([posting, amount_to_balance])
|
|
|
|
balancing_posting[2] += amount_to_balance
|
|
|
|
amount_to_balance = Decimal(0)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
balancing_posting[3].append([posting, -balancing_posting[2]])
|
|
|
|
amount_to_balance += balancing_posting[2]
|
|
|
|
balancing_posting[2] = Decimal(0)
|
|
|
|
|
|
|
|
if amount_to_balance != 0:
|
|
|
|
# New unbalanced remainder
|
|
|
|
balance.append([posting, amount_to_balance, amount_to_balance, []])
|
2020-03-21 01:18:48 +11:00
|
|
|
|
2020-03-23 00:47:38 +11:00
|
|
|
transaction.postings = []
|
|
|
|
|
|
|
|
# Finalise balanced postings
|
|
|
|
for orig_posting, amount_to_balance, amount_remaining, balancing_postings in balance:
|
2020-04-04 04:15:32 +11:00
|
|
|
posting = Posting(orig_posting.transaction, orig_posting.account, Amount(amount_to_balance, commodity))
|
2020-03-23 00:47:38 +11:00
|
|
|
posting.transaction.postings.append(posting)
|
|
|
|
|
|
|
|
for balancing_posting, amount_balanced in balancing_postings:
|
2020-04-04 04:15:32 +11:00
|
|
|
posting.transaction.postings.append(Posting(posting.transaction, balancing_posting.account, Amount(amount_balanced, commodity)))
|
2020-03-21 01:18:48 +11:00
|
|
|
|
2020-03-23 00:47:38 +11:00
|
|
|
if balancing_posting in balancing_posting.transaction.postings:
|
|
|
|
balancing_posting.transaction.postings.remove(balancing_posting)
|
|
|
|
|
|
|
|
if amount_remaining != 0:
|
2020-03-23 20:23:31 +11:00
|
|
|
if posting.account.is_asset:
|
2020-03-23 00:47:38 +11:00
|
|
|
# Cash - charge any unbalanced remainder to Other Income
|
2020-04-04 04:15:32 +11:00
|
|
|
posting.transaction.postings.append(Posting(posting.transaction, account.ledger.get_account(config['cash_other_income']), Amount(-amount_remaining, commodity)))
|
2020-03-21 01:18:48 +11:00
|
|
|
else:
|
2020-03-23 00:47:38 +11:00
|
|
|
# Liabilities, etc. - discard any unbalanced remainder
|
|
|
|
posting.amount.amount -= amount_remaining
|
|
|
|
|
|
|
|
# Adjust (in place) a ledger to convert accounting to a cash basis
|
2020-04-04 04:15:32 +11:00
|
|
|
def ledger_to_cash(ledger, commodity):
|
2020-03-23 00:47:38 +11:00
|
|
|
for account in list(ledger.accounts.values()):
|
|
|
|
if not (account.is_cash or account.is_income or account.is_expense or account.is_equity):
|
2020-04-04 04:15:32 +11:00
|
|
|
account_to_cash(account, commodity)
|
2020-03-21 01:18:48 +11:00
|
|
|
|
|
|
|
return ledger
|
2020-04-03 22:34:01 +11:00
|
|
|
|
|
|
|
# Summarise related transactions
|
2020-05-01 02:30:51 +10:00
|
|
|
def account_flows(ledger, date, pstart, accounts, related, label=None):
|
2020-04-03 22:34:01 +11:00
|
|
|
transactions = [t for t in ledger.transactions if any(p.account in accounts for p in t.postings) and t.date <= date and t.date >= pstart]
|
|
|
|
|
2020-05-01 02:30:51 +10:00
|
|
|
tb = TrialBalance(ledger, date, pstart, label=label)
|
2020-04-03 22:34:01 +11:00
|
|
|
|
|
|
|
for transaction in transactions:
|
|
|
|
for posting in transaction.postings:
|
2020-04-03 23:38:08 +11:00
|
|
|
if (posting.account in accounts) is related:
|
2020-04-03 22:34:01 +11:00
|
|
|
continue
|
|
|
|
|
|
|
|
tb.balances[posting.account.name] = tb.get_balance(posting.account) - posting.amount
|
|
|
|
|
|
|
|
return tb
|