2020-03-20 21:43:05 +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/>.
|
|
|
|
|
|
|
|
from .config import config
|
|
|
|
|
|
|
|
from decimal import Decimal
|
2020-04-15 11:11:46 +10:00
|
|
|
from enum import Enum
|
2020-03-20 21:43:05 +11:00
|
|
|
import functools
|
|
|
|
|
|
|
|
class Ledger:
|
|
|
|
def __init__(self, date):
|
|
|
|
self.date = date
|
|
|
|
|
|
|
|
self.root_account = Account(self, '')
|
|
|
|
self.accounts = {}
|
|
|
|
self.transactions = []
|
|
|
|
|
2020-04-04 04:28:43 +11:00
|
|
|
self.commodities = {}
|
2020-03-20 21:43:05 +11:00
|
|
|
self.prices = []
|
|
|
|
|
2020-04-01 14:40:55 +11:00
|
|
|
def clone(self):
|
|
|
|
result = Ledger(self.date)
|
|
|
|
result.root_account = self.root_account
|
|
|
|
result.accounts = self.accounts
|
|
|
|
result.transactions = self.transactions[:]
|
|
|
|
result.prices = self.prices
|
|
|
|
return result
|
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
def get_account(self, name):
|
|
|
|
if name == '':
|
|
|
|
return self.root_account
|
|
|
|
|
|
|
|
if name in self.accounts:
|
|
|
|
return self.accounts[name]
|
|
|
|
|
|
|
|
account = Account(self, name)
|
|
|
|
if account.parent:
|
|
|
|
account.parent.children.append(account)
|
|
|
|
self.accounts[name] = account
|
|
|
|
|
|
|
|
return account
|
|
|
|
|
2020-04-04 04:28:43 +11:00
|
|
|
def get_commodity(self, name):
|
|
|
|
return self.commodities[name]
|
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
def get_price(self, commodity_from, commodity_to, date):
|
|
|
|
prices = [p for p in self.prices if p[1] == commodity_from.name and p[2].commodity == commodity_to and p[0].date() <= date.date()]
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
if not prices:
|
2020-04-04 04:15:32 +11:00
|
|
|
raise Exception('No price information for {} to {} at {:%Y-%m-%d}'.format(commodity_from, commodity_to, date))
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
return max(prices, key=lambda p: p[0])[2]
|
|
|
|
|
|
|
|
class Transaction:
|
2020-03-29 21:46:55 +11:00
|
|
|
def __init__(self, ledger, id, date, description, code=None):
|
2020-03-20 21:43:05 +11:00
|
|
|
self.ledger = ledger
|
|
|
|
self.id = id
|
|
|
|
self.date = date
|
|
|
|
self.description = description
|
2020-03-29 21:46:55 +11:00
|
|
|
self.code = code
|
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
self.postings = []
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Transaction {} "{}">'.format(self.id, self.description)
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
result = ['{:%Y-%m-%d} {}'.format(self.date, self.description)]
|
|
|
|
for posting in self.postings:
|
|
|
|
result.append(' {} {}'.format(posting.account.name, posting.amount.tostr(False)))
|
|
|
|
return '\n'.join(result)
|
|
|
|
|
|
|
|
class Posting:
|
2020-04-15 11:11:46 +10:00
|
|
|
class State(Enum):
|
|
|
|
UNCLEARED = 0
|
|
|
|
CLEARED = 1
|
|
|
|
PENDING = 2
|
|
|
|
|
|
|
|
def __init__(self, transaction, account, amount, comment=None, state=State.UNCLEARED):
|
2020-03-20 21:43:05 +11:00
|
|
|
self.transaction = transaction
|
|
|
|
self.account = account
|
|
|
|
self.amount = Amount(amount)
|
2020-03-29 21:46:55 +11:00
|
|
|
self.comment = comment
|
2020-04-15 11:11:46 +10:00
|
|
|
self.state = state
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Posting "{}" {}>'.format(self.account.name, self.amount.tostr(False))
|
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
def exchange(self, commodity, date):
|
2020-04-04 04:28:43 +11:00
|
|
|
if self.amount.commodity.name == commodity.name:
|
2020-03-20 21:43:05 +11:00
|
|
|
return Amount(self.amount)
|
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
return self.amount.exchange(commodity, True) # Cost basis
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
class Account:
|
|
|
|
def __init__(self, ledger, name):
|
|
|
|
self.ledger = ledger
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
self.children = []
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Account {}>'.format(self.name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def bits(self):
|
|
|
|
return self.name.split(':')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parent(self):
|
|
|
|
if self.name == '':
|
|
|
|
return None
|
|
|
|
return self.ledger.get_account(':'.join(self.bits[:-1]))
|
|
|
|
|
|
|
|
def matches(self, part):
|
|
|
|
if self.name == part or self.name.startswith(part + ':'):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_income(self):
|
|
|
|
return self.matches(config['income_account'])
|
|
|
|
@property
|
|
|
|
def is_expense(self):
|
|
|
|
return self.matches(config['expenses_account'])
|
|
|
|
@property
|
|
|
|
def is_equity(self):
|
|
|
|
return self.matches(config['equity_account'])
|
|
|
|
@property
|
|
|
|
def is_asset(self):
|
|
|
|
return self.matches(config['assets_account'])
|
|
|
|
@property
|
|
|
|
def is_liability(self):
|
|
|
|
return self.matches(config['liabilities_account'])
|
2020-03-21 01:18:48 +11:00
|
|
|
@property
|
|
|
|
def is_cash(self):
|
|
|
|
# Is this a cash asset?
|
|
|
|
return any(self.matches(a) for a in config['cash_asset_accounts'])
|
2020-04-01 13:48:14 +11:00
|
|
|
@property
|
|
|
|
def is_oci(self):
|
|
|
|
return self.matches(config['oci_account'])
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_cost(self):
|
|
|
|
return self.is_income or self.is_expense or self.is_equity
|
|
|
|
@property
|
|
|
|
def is_market(self):
|
|
|
|
return self.is_asset or self.is_liability
|
|
|
|
|
|
|
|
class Amount:
|
2020-04-04 04:15:32 +11:00
|
|
|
def __init__(self, amount, commodity=None):
|
2020-03-20 21:43:05 +11:00
|
|
|
if isinstance(amount, Amount):
|
|
|
|
self.amount = amount.amount
|
2020-04-04 04:15:32 +11:00
|
|
|
self.commodity = amount.commodity
|
|
|
|
elif commodity is None:
|
|
|
|
raise TypeError('commodity is required')
|
2020-03-20 21:43:05 +11:00
|
|
|
else:
|
|
|
|
self.amount = Decimal(amount)
|
2020-04-04 04:15:32 +11:00
|
|
|
self.commodity = commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
def tostr(self, round=True):
|
2020-04-04 04:15:32 +11:00
|
|
|
if self.commodity.is_prefix:
|
|
|
|
amount_str = ('{}{:.2f}' if round else '{}{}').format(self.commodity.name, self.amount)
|
2020-03-20 21:43:05 +11:00
|
|
|
else:
|
2020-04-04 04:15:32 +11:00
|
|
|
amount_str = ('{:.2f} {}' if round else '{} {}').format(self.amount, self.commodity.name)
|
2020-03-20 21:43:05 +11:00
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
if self.commodity.price:
|
|
|
|
return '{} {{{}}}'.format(amount_str, self.commodity.price.tostr(round))
|
2020-03-20 21:43:05 +11:00
|
|
|
return amount_str
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Amount {}>'.format(self.tostr(False))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.tostr()
|
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
def compatible_commodity(func):
|
2020-03-20 21:43:05 +11:00
|
|
|
@functools.wraps(func)
|
|
|
|
def wrapped(self, other):
|
|
|
|
if isinstance(other, Amount):
|
2020-04-04 04:15:32 +11:00
|
|
|
if other.commodity != self.commodity:
|
|
|
|
raise TypeError('Cannot combine Amounts of commodity {} and {}'.format(self.commodity.name, other.commodity.name))
|
2020-03-20 21:43:05 +11:00
|
|
|
other = other.amount
|
|
|
|
elif other != 0:
|
|
|
|
raise TypeError('Cannot combine Amount with non-zero number')
|
|
|
|
return func(self, other)
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
def __neg__(self):
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(-self.amount, self.commodity)
|
2020-03-23 17:17:10 +11:00
|
|
|
def __abs__(self):
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(abs(self.amount), self.commodity)
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2020-03-23 17:17:10 +11:00
|
|
|
if isinstance(other, Amount):
|
|
|
|
if self.amount == 0 and other.amount == 0:
|
|
|
|
return True
|
2020-04-04 04:15:32 +11:00
|
|
|
if other.commodity != self.commodity:
|
2020-03-23 17:17:10 +11:00
|
|
|
return False
|
|
|
|
return self.amount == other.amount
|
|
|
|
|
|
|
|
if other == 0:
|
|
|
|
return self.amount == 0
|
|
|
|
|
|
|
|
raise TypeError('Cannot compare Amount with non-zero number')
|
2020-04-04 04:15:32 +11:00
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __ne__(self, other):
|
|
|
|
return self.amount != other
|
2020-04-04 04:15:32 +11:00
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __gt__(self, other):
|
|
|
|
return self.amount > other
|
2020-04-04 04:15:32 +11:00
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __ge__(self, other):
|
|
|
|
return self.amount >= other
|
2020-04-04 04:15:32 +11:00
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __lt__(self, other):
|
|
|
|
return self.amount < other
|
2020-04-04 04:15:32 +11:00
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __le__(self, other):
|
|
|
|
return self.amount <= other
|
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __add__(self, other):
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(self.amount + other, self.commodity)
|
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __radd__(self, other):
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(other + self.amount, self.commodity)
|
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __sub__(self, other):
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(self.amount - other, self.commodity)
|
|
|
|
@compatible_commodity
|
2020-03-20 21:43:05 +11:00
|
|
|
def __rsub__(self, other):
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(other - self.amount, self.commodity)
|
2020-03-20 21:43:05 +11:00
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
def exchange(self, commodity, is_cost, price=None):
|
2020-04-04 04:28:43 +11:00
|
|
|
if self.commodity.name == commodity.name:
|
2020-03-20 21:43:05 +11:00
|
|
|
return Amount(self)
|
|
|
|
|
2020-04-04 04:28:43 +11:00
|
|
|
if is_cost and self.commodity.price and self.commodity.price.commodity.name == commodity.name:
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(self.amount * self.commodity.price.amount, commodity)
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
if price:
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(self.amount * price.amount, commodity)
|
2020-03-20 21:43:05 +11:00
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
raise TypeError('Cannot exchange {} to {}'.format(self.commodity, commodity))
|
2020-03-23 02:00:00 +11:00
|
|
|
|
|
|
|
@property
|
|
|
|
def near_zero(self):
|
|
|
|
if abs(self.amount) < 0.005:
|
|
|
|
return True
|
|
|
|
return False
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
class Balance:
|
|
|
|
def __init__(self, amounts=None):
|
|
|
|
self.amounts = amounts or []
|
|
|
|
|
|
|
|
def tidy(self):
|
|
|
|
new_amounts = []
|
|
|
|
for amount in self.amounts:
|
2020-04-04 04:15:32 +11:00
|
|
|
new_amount = next((a for a in new_amounts if a.commodity == amount.commodity), None)
|
2020-03-20 21:43:05 +11:00
|
|
|
return Balance(new_amounts)
|
|
|
|
|
2020-03-23 17:17:10 +11:00
|
|
|
def clean(self):
|
|
|
|
return Balance([a for a in self.amounts if a != 0])
|
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
def exchange(self, commodity, is_cost, date=None, ledger=None):
|
|
|
|
result = Amount(0, commodity)
|
2020-03-20 21:43:05 +11:00
|
|
|
for amount in self.amounts:
|
2020-04-04 04:28:43 +11:00
|
|
|
if is_cost or amount.commodity.name == commodity.name:
|
2020-04-04 04:15:32 +11:00
|
|
|
result += amount.exchange(commodity, is_cost)
|
2020-03-20 21:43:05 +11:00
|
|
|
else:
|
2020-04-04 04:15:32 +11:00
|
|
|
if any(p[1] == amount.commodity.name for p in ledger.prices):
|
|
|
|
# This commodity has price information
|
2020-04-03 19:34:40 +11:00
|
|
|
# Measured at fair value
|
2020-04-04 04:15:32 +11:00
|
|
|
result += amount.exchange(commodity, is_cost, ledger.get_price(amount.commodity, commodity, date))
|
2020-04-03 19:34:40 +11:00
|
|
|
else:
|
2020-04-04 04:15:32 +11:00
|
|
|
# This commodity has no price information
|
2020-04-03 19:34:40 +11:00
|
|
|
# Measured at historical cost
|
2020-04-04 04:15:32 +11:00
|
|
|
result += amount.exchange(commodity, True)
|
2020-03-20 21:43:05 +11:00
|
|
|
return result
|
|
|
|
|
|
|
|
def __neg__(self):
|
|
|
|
return Balance([-a for a in self.amounts])
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Balance):
|
|
|
|
raise Exception('NYI')
|
|
|
|
elif isinstance(other, Amount):
|
|
|
|
raise Exception('NYI')
|
|
|
|
elif other == 0:
|
2020-03-20 22:14:05 +11:00
|
|
|
return all(a == 0 for a in self.amounts)
|
2020-03-20 21:43:05 +11:00
|
|
|
else:
|
|
|
|
raise TypeError('Cannot compare Balance with non-zero number')
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
new_amounts = [Amount(a) for a in self.amounts]
|
|
|
|
|
|
|
|
if isinstance(other, Balance):
|
|
|
|
for amount in other.amounts:
|
2020-04-04 04:15:32 +11:00
|
|
|
new_amount = next((a for a in new_amounts if a.commodity == amount.commodity), None)
|
2020-03-20 21:43:05 +11:00
|
|
|
if new_amount is None:
|
2020-04-04 04:15:32 +11:00
|
|
|
new_amount = Amount(0, amount.commodity)
|
2020-03-20 21:43:05 +11:00
|
|
|
new_amounts.append(new_amount)
|
|
|
|
new_amount.amount += amount.amount
|
2020-03-23 17:17:10 +11:00
|
|
|
|
|
|
|
#if new_amount == 0:
|
|
|
|
# new_amounts.remove(new_amount)
|
2020-03-20 21:43:05 +11:00
|
|
|
elif isinstance(other, Amount):
|
2020-04-04 04:15:32 +11:00
|
|
|
new_amount = next((a for a in new_amounts if a.commodity == other.commodity), None)
|
2020-03-20 21:43:05 +11:00
|
|
|
if new_amount is None:
|
2020-04-04 04:15:32 +11:00
|
|
|
new_amount = Amount(0, other.commodity)
|
2020-03-20 21:43:05 +11:00
|
|
|
new_amounts.append(new_amount)
|
|
|
|
new_amount.amount += other.amount
|
2020-03-23 17:17:10 +11:00
|
|
|
|
|
|
|
#if new_amount == 0:
|
|
|
|
# new_amounts.remove(new_amount)
|
2020-03-20 22:14:05 +11:00
|
|
|
elif other == 0:
|
|
|
|
pass
|
2020-03-20 21:43:05 +11:00
|
|
|
else:
|
|
|
|
raise Exception('NYI')
|
|
|
|
|
|
|
|
return Balance(new_amounts)
|
2020-03-23 17:17:10 +11:00
|
|
|
|
|
|
|
def __sub__(self, other):
|
|
|
|
return self + (-other)
|
2020-03-20 21:43:05 +11:00
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
class Commodity:
|
2020-03-20 21:43:05 +11:00
|
|
|
def __init__(self, name, is_prefix, price=None):
|
|
|
|
self.name = name
|
|
|
|
self.is_prefix = is_prefix
|
|
|
|
self.price = price
|
|
|
|
|
|
|
|
def __repr__(self):
|
2020-04-04 04:15:32 +11:00
|
|
|
return '<Commodity {} ({})>'.format(self.name, 'prefix' if self.is_prefix else 'suffix')
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2020-04-04 04:15:32 +11:00
|
|
|
if not isinstance(other, Commodity):
|
2020-03-20 21:43:05 +11:00
|
|
|
return False
|
2020-04-04 04:28:43 +11:00
|
|
|
return self.name == other.name and self.price == other.price
|
|
|
|
|
|
|
|
def strip_price(self):
|
|
|
|
return Commodity(self.name, self.is_prefix)
|
2020-03-20 21:43:05 +11:00
|
|
|
|
|
|
|
class TrialBalance:
|
|
|
|
def __init__(self, ledger, date, pstart):
|
|
|
|
self.ledger = ledger
|
|
|
|
self.date = date
|
|
|
|
self.pstart = pstart
|
|
|
|
|
|
|
|
self.balances = {}
|
|
|
|
|
|
|
|
def get_balance(self, account):
|
|
|
|
return self.balances.get(account.name, Balance())
|
|
|
|
|
|
|
|
def get_total(self, account):
|
2020-03-20 22:14:05 +11:00
|
|
|
return self.get_balance(account) + sum((self.get_total(a) for a in account.children), Balance())
|