Skip to content

Commit

Permalink
light cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
edelgm6 committed Jan 22, 2024
1 parent 31e274a commit 48eea6b
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions api/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
from dateutil.relativedelta import relativedelta
from collections import namedtuple
from django.db.models import Sum, Case, When, Value, DecimalField
from api.models import JournalEntryItem, Account, JournalEntryItem
from api.models import JournalEntryItem, Account


class Trend:

def __init__(self, start_date, end_date):
# self.type = type
self.start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
self.end_date = datetime.strptime(end_date, '%Y-%m-%d').date()

Expand Down Expand Up @@ -38,14 +37,15 @@ def get_balances(self):

balance_sheet_start_date = range.start - timedelta(days=1)
balance_sheet_start = BalanceSheet(end_date=balance_sheet_start_date)
cash_flow_statement = CashFlowStatement(income_statement,balance_sheet_start,balance_sheet)
cash_flow_statement = CashFlowStatement(income_statement, balance_sheet_start, balance_sheet)

balances += income_statement.get_balances()
balances += balance_sheet.get_balances()
balances += cash_flow_statement.get_balances()

return balances


class Balance:

def __init__(self, account, amount, account_type, account_sub_type, date=None, type='flow'):
Expand All @@ -56,13 +56,15 @@ def __init__(self, account, amount, account_type, account_sub_type, date=None, t
self.date = date
self.type = type


class Metric:

def __init__(self, name, value, metric_type='total'):
self.name = name
self.value = value
self.metric_type = metric_type


class Statement:

def __init__(self, end_date):
Expand All @@ -77,22 +79,22 @@ def _get_balance_from_aggregates(aggregates, end_date=None, balance_type=None):
debits = aggregate['debit_total']
credits = aggregate['credit_total']

balance = Account.get_balance_from_debit_and_credit(account_type,debits=debits,credits=credits)
account_balance_list.append(Balance(aggregate['account__name'],balance,account_type,aggregate['account__sub_type'],end_date,balance_type))
balance = Account.get_balance_from_debit_and_credit(account_type, debits=debits, credits=credits)
account_balance_list.append(Balance(aggregate['account__name'], balance, account_type, aggregate['account__sub_type'], end_date, balance_type))

sorted_list = sorted(account_balance_list, key=lambda k: k.account)
return sorted_list

def get_balances(self):
if type(self) == IncomeStatement:
ACCOUNT_TYPES = ['income','expense']
ACCOUNT_TYPES = ['income', 'expense']
aggregates = JournalEntryItem.objects.filter(
account__type__in=ACCOUNT_TYPES,
journal_entry__date__gte=self.start_date,
journal_entry__date__lte=self.end_date
)
else:
ACCOUNT_TYPES = ['asset','liability','equity']
ACCOUNT_TYPES = ['asset', 'liability', 'equity']
aggregates = JournalEntryItem.objects.filter(
account__type__in=ACCOUNT_TYPES,
journal_entry__date__lte=self.end_date
Expand Down Expand Up @@ -121,7 +123,7 @@ def get_balances(self):
if type(self) == BalanceSheet:
balance_type = 'stock'

balances = self._get_balance_from_aggregates(aggregates,self.end_date,balance_type)
balances = self._get_balance_from_aggregates(aggregates, self.end_date, balance_type)

# Add accounts without any activity
represented_accounts_names = [balance.account for balance in balances]
Expand Down Expand Up @@ -178,7 +180,7 @@ def __init__(self, income_statement, start_balance_sheet, end_balance_sheet):
self.cash_from_financing_balances = self.get_cash_from_financing_balances()
self.cash_from_investing_balances = self.get_cash_from_investing_balances()
self.net_cash_flow = 0
for balances_list in [self.cash_from_operations_balances,self.cash_from_financing_balances,self.cash_from_investing_balances]:
for balances_list in [self.cash_from_operations_balances, self.cash_from_financing_balances, self.cash_from_investing_balances]:
self.balances += balances_list
self.net_cash_flow += self.get_cash_flow(balances_list)

Expand Down Expand Up @@ -227,7 +229,7 @@ def get_balance_sheet_account_deltas(self):
delta = ending_balance - starting_balance
if account.type == Account.Type.ASSET:
delta = delta * -1
account_deltas.append(Balance(account.name,delta,account.type,account.sub_type,self.end_balance_sheet.end_date))
account_deltas.append(Balance(account.name, delta, account.type, account.sub_type, self.end_balance_sheet.end_date))

return account_deltas

Expand All @@ -236,7 +238,8 @@ def get_cash_from_operations_balances(self):
Balance(
'Realized Net Income',
self.net_income_less_gains_and_losses,
Account.Type.EQUITY,Account.SubType.RETAINED_EARNINGS,
Account.Type.EQUITY,
Account.SubType.RETAINED_EARNINGS,
self.end_balance_sheet.end_date
)
]
Expand Down Expand Up @@ -278,6 +281,7 @@ def get_cash_from_investing_balances(self):

return sorted_balances


class IncomeStatement(Statement):

def __init__(self, end_date, start_date):
Expand Down Expand Up @@ -318,7 +322,7 @@ def get_net_income(self):
return net_income

def get_taxable_income(self):
return sum([balance.amount for balance in self.balances if balance.account_type == Account.Type.INCOME and balance.account_sub_type not in [Account.SubType.UNREALIZED_INVESTMENT_GAINS,Account.SubType.OTHER_INCOME]])
return sum([balance.amount for balance in self.balances if balance.account_type == Account.Type.INCOME and balance.account_sub_type not in [Account.SubType.UNREALIZED_INVESTMENT_GAINS, Account.SubType.OTHER_INCOME]])

def get_unrealized_gains_and_losses(self):
return sum([balance.amount for balance in self.balances if balance.account_sub_type == Account.SubType.UNREALIZED_INVESTMENT_GAINS])
Expand Down Expand Up @@ -348,14 +352,14 @@ def __init__(self, end_date):
self.balances = self.get_balances()
investment_gains_losses, net_retained_earnings = self.get_retained_earnings_values()
self.balances += [
Balance('9000-Net Retained Earnings', net_retained_earnings, Account.Type.EQUITY, Account.SubType.RETAINED_EARNINGS,self.end_date),
Balance('9100-Investment Gains/Losses', investment_gains_losses, Account.Type.EQUITY, Account.SubType.RETAINED_EARNINGS,self.end_date)
Balance('9000-Net Retained Earnings', net_retained_earnings, Account.Type.EQUITY, Account.SubType.RETAINED_EARNINGS, self.end_date),
Balance('9100-Investment Gains/Losses', investment_gains_losses, Account.Type.EQUITY, Account.SubType.RETAINED_EARNINGS, self.end_date)
]
self.summaries = self.get_summaries()
self.metrics = self.get_metrics()

def get_retained_earnings_values(self):
income_statement = IncomeStatement(end_date=self.end_date,start_date='1970-01-01')
income_statement = IncomeStatement(end_date=self.end_date, start_date='1970-01-01')
retained_earnings = income_statement.get_net_income()

investment_gains_losses = sum([balance.amount for balance in income_statement.balances if balance.account_sub_type == Account.SubType.UNREALIZED_INVESTMENT_GAINS])
Expand All @@ -366,7 +370,8 @@ def get_retained_earnings_values(self):
def get_balance(self, account):
try:
balance = [balance.amount for balance in self.balances if balance.account == account.name][0]
except IndexError: # Need this when there is no balance for a given account
# Need this when there is no balance for a given account
except IndexError:
balance = 0
return balance

Expand Down

0 comments on commit 48eea6b

Please sign in to comment.