Skip to content

Commit

Permalink
Fix for current deposit amount methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
titov-vv committed Jan 24, 2024
1 parent cc90a6a commit e557992
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
15 changes: 7 additions & 8 deletions jal/db/deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ def currency(self) -> JalAsset:

# Return accumulated money for the deposit at given timestamp
def balance(self, timestamp: int) -> Decimal:
balance = self._read(
"WITH last_deposit_amount AS ( "
"SELECT amount_acc, ROW_NUMBER() OVER (PARTITION BY op_type, operation_id ORDER BY id DESC) AS row_no "
"FROM ledger WHERE book_account=:book AND op_type=:type AND operation_id=:id AND timestamp<=:timestamp) "
"SELECT amount_acc FROM last_deposit_amount WHERE row_no=1",
[(":book", BookAccount.Savings), (":type", LedgerTransaction.TermDeposit),
(":id", self._id), (":timestamp", timestamp)])
balance = Decimal('0') if balance is None else Decimal(balance)
balance = Decimal('0')
query = self._exec("SELECT amount FROM ledger "
"WHERE book_account=:book AND op_type=:type AND operation_id=:id AND timestamp<=:timestamp",
[(":book", BookAccount.Savings), (":type", LedgerTransaction.TermDeposit),
(":id", self._id), (":timestamp", timestamp)])
while query.next():
balance += self._read_record(query, cast=[Decimal])
return balance

# Return a timestamp of deposit opening
Expand Down
14 changes: 7 additions & 7 deletions jal/db/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,13 +1239,13 @@ def __init__(self, operation_id=None, display_type=None):
self._oname = f'{DepositActions().get_name(self._action)}'

def _get_deposit_amount(self) -> Decimal:
amount = self._read("SELECT amount_acc FROM ledger "
"WHERE op_type=:op_type AND operation_id=:oid AND "
"book_account=:book AND account_id=:account_id AND timestamp<:timestamp "
"ORDER BY id DESC LIMIT 1",
[(":op_type", self._otype), (":oid", self._oid), (":timestamp", self._timestamp),
(":account_id", self._account.id()), (":book", BookAccount.Savings)])
amount = Decimal('0') if amount is None else Decimal(amount)
amount = Decimal('0')
query = self._exec("SELECT amount FROM ledger WHERE op_type=:op_type AND operation_id=:oid AND "
"book_account=:book AND account_id=:account_id AND timestamp<:timestamp",
[(":op_type", self._otype), (":oid", self._oid), (":timestamp", self._timestamp),
(":account_id", self._account.id()), (":book", BookAccount.Savings)])
while query.next():
amount += self._read_record(query, cast=[Decimal])
return amount

def description(self) -> str:
Expand Down

0 comments on commit e557992

Please sign in to comment.