Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyluca/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def _get_param(
return _get_param(context[next_key], event, accountant, context)
if key.startswith('balance.'):
return Ledger(accountant.journal, accountant.config).get_account_balance(key.replace('balance.', ''))
if key.startswith('opening_balance.'):
return context['opening_balances'][key.replace('opening_balance.', '')]
if hasattr(event, key):
return event.__getattribute__(key)
raise NotImplementedError(f'param {key} not implemented')
Expand Down Expand Up @@ -104,6 +106,7 @@ def _apply_action(

def apply(event: Event, accountant: Accountant, context: dict = None, external_actions: dict = None):
context = context if context else {}
context['opening_balances'] = Ledger(accountant.journal, accountant.config).get_balances()
event_config = accountant.config['actions_config']['on_event'][event.__class__.__name__]
common_actions = accountant.config['actions_config'].get('common_actions', {})
external_actions = external_actions if external_actions else {}
Expand Down
90 changes: 87 additions & 3 deletions pyluca/tests/test_action.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from datetime import datetime
from unittest import TestCase
from pyluca.accountant import Accountant
Expand Down Expand Up @@ -26,13 +27,15 @@
'SAVINGS_BANK': {'type': 'ASSET'},
'MUTUAL_FUNDS': {'type': 'ASSET'},
'LOANS': {'type': 'ASSET'},
'CAR_EMI': {'type': 'EXPENSE'},
'CAR_EMI': {'type': 'LIABILITY'},
'CAR_LOAN': {'type': 'LIABILITY'},
'FREELANCING_INCOME': {'type': 'INCOME'},
'LOANS_PAYBACK': {'type': 'ASSET'},
'RISKY_LOANS': {'type': 'ASSET'},
'MUTUAL_FUNDS_PNL': {'type': 'INCOME'},
'CHARITY': {'type': 'EXPENSE'},
'FIXED_DEPOSIT': {'type': 'ASSET'}
'FIXED_DEPOSIT': {'type': 'ASSET'},
'BORROW': {'type': 'LIABILITY'}
},
'rules': {},
'actions_config': {
Expand Down Expand Up @@ -160,6 +163,48 @@
}
}
]
},
'CarEMIBillEvent': {
'actions': [
{
'dr_account': 'CAR_LOAN',
'cr_account': 'CAR_EMI',
'amount': 'amount',
'narration': 'Car EMI Bill'
}
]
},
'PayCarEMIEvent': {
'actions': [
{
'dr_account': 'CAR_EMI',
'cr_account': 'SAVINGS_BANK',
'amount': {
'type': 'min',
'a': 'amount',
'b': 'opening_balance.SAVINGS_BANK'
},
'narration': 'Paying Car EMI'
},
{
'dr_account': 'CAR_EMI',
'cr_account': 'BORROW',
'amount': {
"type": "-",
"a": {
"type": "min",
"a": "opening_balance.CAR_EMI",
"b": "amount"
},
"b": {
"type": "min",
"a": 'amount',
"b": "opening_balance.SAVINGS_BANK"
}
},
'narration': 'Paying Car EMI'
},
]
}
}
}
Expand Down Expand Up @@ -219,6 +264,14 @@ class FreelancingSalaryEvent(AmountEvent):
pass


class CarEMIBillEvent(AmountEvent):
pass


class PayCarEMIEvent(AmountEvent):
pass


class TestAction(TestCase):
def test_config(self):
config = {
Expand Down Expand Up @@ -329,7 +382,7 @@ def test_sub_narration(self):
self.assertEqual(je.narration, 'Put in fixed deposit for Freelancing salary')

def test_externals(self):
config = {**personal_fin_config}
config = deepcopy(personal_fin_config)
config['actions_config']['on_event']['SalaryEvent']['actions'] = [
*config['actions_config']['on_event']['SalaryEvent']['actions'],
{
Expand Down Expand Up @@ -358,3 +411,34 @@ def __check_balance(acct_name: str, balance: str, date: datetime):
apply(e, accountant, external_actions={'check_balance': __check_balance})

self.assertTrue(local_state['checked'])

def test_opening_balances(self):
accountant = Accountant(Journal(), personal_fin_config, '1')
events = [
SalaryEvent('1', 2000, datetime(2022, 4, 1), datetime(2022, 4, 1)),
CarEMIBillEvent('2', 3000, datetime(2022, 4, 2), datetime(2022, 4, 2))
]
for e in events:
apply(e, accountant)
ledger = Ledger(accountant.journal, accountant.config)
self.assertEqual(ledger.get_account_balance('SALARY'), 2000)
self.assertEqual(ledger.get_account_balance('SAVINGS_BANK'), 2000)
self.assertEqual(ledger.get_account_balance('CAR_EMI'), 3000)
events = [
PayCarEMIEvent('1', 2700, datetime(2022, 4, 3), datetime(2022, 4, 3))
]
for e in events:
apply(e, accountant)
ledger = Ledger(accountant.journal, accountant.config)
self.assertEqual(ledger.get_account_balance('SAVINGS_BANK'), 0)
self.assertEqual(ledger.get_account_balance('CAR_EMI'), 300)
self.assertEqual(ledger.get_account_balance('BORROW'), 700)
events = [
PayCarEMIEvent('1', 300, datetime(2022, 4, 4), datetime(2022, 4, 4))
]
for e in events:
apply(e, accountant)
ledger = Ledger(accountant.journal, accountant.config)
self.assertEqual(ledger.get_account_balance('SAVINGS_BANK'), 0)
self.assertEqual(ledger.get_account_balance('CAR_EMI'), 0)
self.assertEqual(ledger.get_account_balance('BORROW'), 1000)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name='pyluca',
version='1.3.1',
version='1.3.2',
author='datasignstech',
author_email='[email protected]',
description='Double entry accounting system',
Expand Down