Skip to content

Commit

Permalink
Removed pandas support
Browse files Browse the repository at this point in the history
  • Loading branch information
sattyamjjain committed Apr 28, 2022
1 parent 226e2cb commit 3844bed
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 25 deletions.
49 changes: 25 additions & 24 deletions demo/personal_finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,39 @@
assert ledger.get_account_balance('LOANS') == 1000
assert ledger.get_account_balance('SAVINGS_BANK') == 4000

# get pandas dataframe
ledger.get_df()
ledger.get_ledger()
'''
sl_no account dr_amount ... date narration key
0 0 SAVINGS_BANK 20000 ... 2022-04-30 March salary person1
1 1 SALARY 0 ... 2022-04-30 March salary person1
2 2 MUTUAL_FUNDS 10000 ... 2022-05-01 Invest in NIFTY 50 Index person1
3 3 SAVINGS_BANK 0 ... 2022-05-01 Invest in NIFTY 50 Index person1
4 4 CAR_EMI 5000 ... 2022-05-05 5th EMI person1
5 5 SAVINGS_BANK 0 ... 2022-05-05 5th EMI person1
6 6 LOANS 3000 ... 2022-05-05 Lend to Kalyan person1
7 7 SAVINGS_BANK 0 ... 2022-05-05 Lend to Kalyan person1
8 8 SAVINGS_BANK 2000 ... 2022-05-15 Partial payback person1
9 9 LOANS 0 ... 2022-05-15 Partial payback person1
[
{'sl_no': 0, 'account': 'SAVINGS_BANK', 'dr_amount': 20000, 'cr_amount': 0, 'date': datetime.datetime(2022, 4, 30, 0, 0), 'narration': 'March salary', 'key': 'person1'},
{'sl_no': 1, 'account': 'SALARY', 'dr_amount': 0, 'cr_amount': 20000, 'date': datetime.datetime(2022, 4, 30, 0, 0), 'narration': 'March salary', 'key': 'person1'},
{'sl_no': 2, 'account': 'MUTUAL_FUNDS', 'dr_amount': 10000, 'cr_amount': 0, 'date': datetime.datetime(2022, 5, 1, 0, 0), 'narration': 'Invest in NIFTY 50 Index', 'key': 'person1'},
{'sl_no': 3, 'account': 'SAVINGS_BANK', 'dr_amount': 0, 'cr_amount': 10000, 'date': datetime.datetime(2022, 5, 1, 0, 0), 'narration': 'Invest in NIFTY 50 Index', 'key': 'person1'},
{'sl_no': 4, 'account': 'CAR_EMI', 'dr_amount': 5000, 'cr_amount': 0, 'date': datetime.datetime(2022, 5, 5, 0, 0), 'narration': '5th EMI', 'key': 'person1'},
{'sl_no': 5, 'account': 'SAVINGS_BANK', 'dr_amount': 0, 'cr_amount': 5000, 'date': datetime.datetime(2022, 5, 5, 0, 0), 'narration': '5th EMI', 'key': 'person1'},
{'sl_no': 6, 'account': 'LOANS', 'dr_amount': 3000, 'cr_amount': 0, 'date': datetime.datetime(2022, 5, 5, 0, 0), 'narration': 'Lend to Kalyan', 'key': 'person1'},
{'sl_no': 7, 'account': 'SAVINGS_BANK', 'dr_amount': 0, 'cr_amount': 3000, 'date': datetime.datetime(2022, 5, 5, 0, 0), 'narration': 'Lend to Kalyan', 'key': 'person1'},
{'sl_no': 8, 'account': 'SAVINGS_BANK', 'dr_amount': 2000, 'cr_amount': 0, 'date': datetime.datetime(2022, 5, 15, 0, 0), 'narration': 'Partial payback', 'key': 'person1'},
{'sl_no': 9, 'account': 'LOANS', 'dr_amount': 0, 'cr_amount': 2000, 'date': datetime.datetime(2022, 5, 15, 0, 0), 'narration': 'Partial payback', 'key': 'person1'}
]
[10 rows x 7 columns]
'''

# get the balance sheet
ledger.get_balance_sheet()
'''
sl_no account dr_amount ... MUTUAL_FUNDS LOANS CAR_EMI
0 0 SAVINGS_BANK 20000 ... 0 0 0
1 1 SALARY 0 ... 0 0 0
2 2 MUTUAL_FUNDS 10000 ... 10000 0 0
3 3 SAVINGS_BANK 0 ... 10000 0 0
4 4 CAR_EMI 5000 ... 10000 0 5000
5 5 SAVINGS_BANK 0 ... 10000 0 5000
6 6 LOANS 3000 ... 10000 3000 5000
7 7 SAVINGS_BANK 0 ... 10000 3000 5000
8 8 SAVINGS_BANK 2000 ... 10000 3000 5000
9 9 LOANS 0 ... 10000 1000 5000
[
{'sl_no': 0, 'account': 'SAVINGS_BANK', 'dr_amount': 20000, 'cr_amount': 0, 'date': datetime.datetime(2022, 4, 30, 0, 0), 'narration': 'March salary', 'key': 'person1', 'SALARY': 0, 'SAVINGS_BANK': 20000, 'MUTUAL_FUNDS': 0, 'LOANS': 0, 'CAR_EMI': 0},
{'sl_no': 1, 'account': 'SALARY', 'dr_amount': 0, 'cr_amount': 20000, 'date': datetime.datetime(2022, 4, 30, 0, 0), 'narration': 'March salary', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 20000, 'MUTUAL_FUNDS': 0, 'LOANS': 0, 'CAR_EMI': 0},
{'sl_no': 2, 'account': 'MUTUAL_FUNDS', 'dr_amount': 10000, 'cr_amount': 0, 'date': datetime.datetime(2022, 5, 1, 0, 0), 'narration': 'Invest in NIFTY 50 Index', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 20000, 'MUTUAL_FUNDS': 10000, 'LOANS': 0, 'CAR_EMI': 0},
{'sl_no': 3, 'account': 'SAVINGS_BANK', 'dr_amount': 0, 'cr_amount': 10000, 'date': datetime.datetime(2022, 5, 1, 0, 0), 'narration': 'Invest in NIFTY 50 Index', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 10000, 'MUTUAL_FUNDS': 10000, 'LOANS': 0, 'CAR_EMI': 0},
{'sl_no': 4, 'account': 'CAR_EMI', 'dr_amount': 5000, 'cr_amount': 0, 'date': datetime.datetime(2022, 5, 5, 0, 0), 'narration': '5th EMI', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 10000, 'MUTUAL_FUNDS': 10000, 'LOANS': 0, 'CAR_EMI': 5000},
{'sl_no': 5, 'account': 'SAVINGS_BANK', 'dr_amount': 0, 'cr_amount': 5000, 'date': datetime.datetime(2022, 5, 5, 0, 0), 'narration': '5th EMI', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 5000, 'MUTUAL_FUNDS': 10000, 'LOANS': 0, 'CAR_EMI': 5000},
{'sl_no': 6, 'account': 'LOANS', 'dr_amount': 3000, 'cr_amount': 0, 'date': datetime.datetime(2022, 5, 5, 0, 0), 'narration': 'Lend to Kalyan', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 5000, 'MUTUAL_FUNDS': 10000, 'LOANS': 3000, 'CAR_EMI': 5000},
{'sl_no': 7, 'account': 'SAVINGS_BANK', 'dr_amount': 0, 'cr_amount': 3000, 'date': datetime.datetime(2022, 5, 5, 0, 0), 'narration': 'Lend to Kalyan', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 2000, 'MUTUAL_FUNDS': 10000, 'LOANS': 3000, 'CAR_EMI': 5000},
{'sl_no': 8, 'account': 'SAVINGS_BANK', 'dr_amount': 2000, 'cr_amount': 0, 'date': datetime.datetime(2022, 5, 15, 0, 0), 'narration': 'Partial payback', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 4000, 'MUTUAL_FUNDS': 10000, 'LOANS': 3000, 'CAR_EMI': 5000},
{'sl_no': 9, 'account': 'LOANS', 'dr_amount': 0, 'cr_amount': 2000, 'date': datetime.datetime(2022, 5, 15, 0, 0), 'narration': 'Partial payback', 'key': 'person1', 'SALARY': 20000, 'SAVINGS_BANK': 4000, 'MUTUAL_FUNDS': 10000, 'LOANS': 1000, 'CAR_EMI': 5000}
]
[10 rows x 12 columns]
'''
Expand Down
33 changes: 33 additions & 0 deletions pyluca/ledger.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
from datetime import datetime
from typing import List, TypedDict
from pyluca.account_config import BalanceType
from pyluca.aging import get_account_aging
from pyluca.journal import Journal


class LedgerDict(TypedDict):
key: str
sl_no: int
account: str
dr_amount: float
cr_amount: float
date: datetime
narration: str


class Ledger:
def __init__(self, journal: Journal, config: dict):
self.journal = journal
Expand All @@ -20,6 +32,27 @@ def get_account_balance(self, account: str):
return self.get_account_dr(account) - self.get_account_cr(account)
return self.get_account_cr(account) - self.get_account_dr(account)

def get_ledger(self) -> List[LedgerDict]:
return [_entry.__dict__ for _entry in self.journal.entries]

def get_aging(self, account: str):
return get_account_aging(self.config, self.journal.entries, account, self.journal.entries[-1].date)

def add_account_balance(self, account: str, ledger: List[LedgerDict]):

This comment has been minimized.

Copy link
@pskd73

pskd73 Apr 28, 2022

Contributor

This function is not necessary. get_balance_sheet is the only function that will be exposed

account_type = self.config['accounts'][account]['type']
positive_col, negative_col = 'cr_amount', 'dr_amount'
if self.config['account_types'][account_type]['balance_type'] == BalanceType.DEBIT.value:
positive_col, negative_col = 'dr_amount', 'cr_amount'
balance = 0
for row in ledger:
if row['account'] == account:
balance += row[positive_col]
balance -= row[negative_col]
row[account] = balance
return ledger

def get_balance_sheet(self):
ledger = self.get_ledger()
for acct_name in self.config['accounts'].keys():
ledger = self.add_account_balance(acct_name, ledger)
return ledger
1 change: 1 addition & 0 deletions pyluca/tests/test_accountant.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_base(self):
ledger = Ledger(accountant.journal, accountant.config)
self.assertEqual(ledger.get_account_balance('SAVINGS_BANK'), 20000)
self.assertEqual(ledger.get_account_balance('SALARY'), 20000)
self.assertEqual(len(ledger.get_ledger()), 2)

accountant.enter_journal('MUTUAL_FUNDS', 'SAVINGS_BANK', 10000, datetime(2022, 5, 1), 'ELSS')
ledger = Ledger(accountant.journal, accountant.config)
Expand Down
14 changes: 14 additions & 0 deletions pyluca/tests/test_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ def test_ledger_aging(self):
])
ages = Ledger(journal, account_config).get_aging('SAVINGS_BANK')
self.assertEqual(len(ages), 3)

def test_ledger_balance_sheet(self):
accountant = Accountant(Journal(), account_config, '1')
accountant.enter_journal('SAVINGS_BANK', 'SALARY', 20000, datetime(2022, 4, 30), 'April salary')
accountant.enter_journal('MUTUAL_FUNDS', 'SAVINGS_BANK', 10000, datetime(2022, 5, 1), 'ELSS')
accountant.enter_journal('LOANS', 'SAVINGS_BANK', 5000, datetime(2022, 5, 2), 'Lend to Pramod')
accountant.enter_journal('CAR_EMI', 'SAVINGS_BANK', 3000, datetime(2022, 5, 2), 'EMI 3/48')
ledger = Ledger(accountant.journal, account_config)
last_row = ledger.get_balance_sheet()[-1]
self.assertEqual(last_row['SALARY'], 20000)
self.assertEqual(last_row['SAVINGS_BANK'], 2000)
self.assertEqual(last_row['MUTUAL_FUNDS'], 10000)
self.assertEqual(last_row['LOANS'], 5000)
self.assertEqual(last_row['CAR_EMI'], 3000)
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
pandas==1.1.5

0 comments on commit 3844bed

Please sign in to comment.