Skip to content

Commit

Permalink
Fixed outdated code in LedgerTransaction classes that should protect …
Browse files Browse the repository at this point in the history
…from empty bank/broker.
  • Loading branch information
titov-vv committed Feb 6, 2024
1 parent 67d9cab commit c95907b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 86 deletions.
35 changes: 15 additions & 20 deletions jal/db/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,8 @@ def update_tax(self, new_tax) -> None: # FIXME method should take Decimal valu
[(":dividend_id", self._oid), (":tax", new_tax)], commit=True)

def processLedger(self, ledger):
if self._broker is None:
raise LedgerError(
self.tr("Can't process dividend as bank isn't set for investment account: ") + self._account_name)
if not self._broker:
raise LedgerError(self.tr("Can't process dividend as bank isn't set for investment account: ") + self._account_name)
if self._subtype == Dividend.StockDividend or self._subtype == Dividend.StockVesting:
self.processStockDividendOrVesting(ledger)
return
Expand All @@ -592,8 +591,7 @@ def processLedger(self, ledger):
else: # This branch is valid for accrued bond interest payments for bond buying trades
ledger.appendTransaction(self, BookAccount.Costs, -self._amount, category=category, peer=self._broker)
if self._tax:
ledger.appendTransaction(self, BookAccount.Costs, self._tax,
category=PredefinedCategory.Taxes, peer=self._broker)
ledger.appendTransaction(self, BookAccount.Costs, self._tax, category=PredefinedCategory.Taxes, peer=self._broker)

def processStockDividendOrVesting(self, ledger):
asset_amount = ledger.getAmount(BookAccount.Assets, self._account.id(), self._asset.id())
Expand All @@ -605,8 +603,7 @@ def processStockDividendOrVesting(self, ledger):
asset_id=self._asset.id(), value=self._amount * self.price())
if self._tax:
ledger.appendTransaction(self, BookAccount.Money, -self._tax)
ledger.appendTransaction(self, BookAccount.Costs, self._tax,
category=PredefinedCategory.Taxes, peer=self._broker)
ledger.appendTransaction(self, BookAccount.Costs, self._tax, category=PredefinedCategory.Taxes, peer=self._broker)

def processBondAmortization(self, ledger):
operation_value = (self._amount - self._tax)
Expand All @@ -615,8 +612,7 @@ def processBondAmortization(self, ledger):
if credit_returned < operation_value:
ledger.appendTransaction(self, BookAccount.Money, operation_value - credit_returned)
if self._tax:
ledger.appendTransaction(self, BookAccount.Costs, self._tax,
category=PredefinedCategory.Taxes, peer=self._broker)
ledger.appendTransaction(self, BookAccount.Costs, self._tax, category=PredefinedCategory.Taxes, peer=self._broker)
ledger.appendTransaction(self, BookAccount.Assets, Decimal('0'), asset_id=self._asset.id(), value=-self._amount)


Expand Down Expand Up @@ -723,9 +719,8 @@ def accrued_interest(self,currency_id: int = 0) -> Decimal:
return value

def processLedger(self, ledger):
if self._broker is None:
raise LedgerError(
self.tr("Can't process trade as bank isn't set for investment account: ") + self._account_name)
if not self._broker:
raise LedgerError(self.tr("Can't process trade as bank isn't set for investment account: ") + self._account_name)
deal_sign = Decimal('1.0').copy_sign(self._qty) # 1 is buy and -1 is sell operation
qty = abs(self._qty)
trade_value = self._price * qty + deal_sign * self._fee
Expand Down Expand Up @@ -753,8 +748,7 @@ def processLedger(self, ledger):
ledger.appendTransaction(self, BookAccount.Assets, deal_sign * (qty - processed_qty),
asset_id=self._asset.id(), value=deal_sign * (qty - processed_qty) * self._price)
if self._fee:
ledger.appendTransaction(self, BookAccount.Costs, self._fee,
category=PredefinedCategory.Fees, peer=self._broker)
ledger.appendTransaction(self, BookAccount.Costs, self._fee, category=PredefinedCategory.Fees, peer=self._broker)


# ----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1181,8 +1175,7 @@ def processLedger(self, ledger):
ledger.appendTransaction(self, BookAccount.Assets, -processed_qty,
asset_id=self._asset.id(), value=-processed_value)
if self._subtype == CorporateAction.Delisting: # Map value to costs and exit - nothing more for delisting
ledger.appendTransaction(self, BookAccount.Costs, processed_value,
category=PredefinedCategory.Profit, peer=self._broker)
ledger.appendTransaction(self, BookAccount.Costs, processed_value, category=PredefinedCategory.Profit, peer=self._broker)
return
# Process assets after corporate action
query = self._exec("SELECT asset_id, qty, value_share FROM action_results WHERE action_id=:oid",
Expand All @@ -1191,8 +1184,7 @@ def processLedger(self, ledger):
asset, qty, share = self._read_record(query, cast=[JalAsset, Decimal, Decimal])
if asset.type() == PredefinedAsset.Money:
ledger.appendTransaction(self, BookAccount.Money, qty)
ledger.appendTransaction(self, BookAccount.Incomes, -qty,
category=PredefinedCategory.Interest, peer=self._broker)
ledger.appendTransaction(self, BookAccount.Incomes, -qty, category=PredefinedCategory.Interest, peer=self._broker)
else:
value = share * processed_value
price = value / qty
Expand Down Expand Up @@ -1248,6 +1240,7 @@ def __init__(self, operation_id=None, display_type=None):
self._amount = Decimal(self._data['amount'])
self._icon = JalIcon[icons[self._action]]
self._oname = f'{DepositActions().get_name(self._action)}'
self._bank = self._account.organization()

def _get_deposit_amount(self) -> Decimal:
amount = Decimal('0')
Expand Down Expand Up @@ -1292,6 +1285,8 @@ def amount(self) -> Decimal:
return self._amount

def processLedger(self, ledger):
if not self._bank:
raise LedgerError(self.tr("Can't process deposit as bank isn't set for account: ") + self._account_name)
if self._action in [DepositActions.Opening, DepositActions.TopUp, DepositActions.Closing, DepositActions.PartialWithdrawal]:
amount = self._get_deposit_amount() if self._action == DepositActions.Closing else self._amount
if self._action in [DepositActions.Opening, DepositActions.TopUp]:
Expand All @@ -1307,10 +1302,10 @@ def processLedger(self, ledger):
elif self._action == DepositActions.TaxWithheld:
ledger.appendTransaction(self, BookAccount.Savings, -self._amount)
ledger.appendTransaction(self, BookAccount.Costs, self._amount,
category=PredefinedCategory.Taxes, peer=self._account.organization())
category=PredefinedCategory.Taxes, peer=self._bank)
elif self._action == DepositActions.InterestAccrued:
ledger.appendTransaction(self, BookAccount.Savings, self._amount)
ledger.appendTransaction(self, BookAccount.Incomes, -self._amount,
category=PredefinedCategory.Interest, peer=self._account.organization())
category=PredefinedCategory.Interest, peer=self._bank)
else:
assert False, "Not implemented deposit action"
74 changes: 41 additions & 33 deletions jal/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,57 +620,57 @@
<context>
<name>CorporateAction</name>
<message>
<location filename="../db/operations.py" line="1032"/>
<location filename="../db/operations.py" line="1026"/>
<source>UNDEFINED</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1033"/>
<location filename="../db/operations.py" line="1027"/>
<source>Symbol change</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1034"/>
<location filename="../db/operations.py" line="1028"/>
<source>Split</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1035"/>
<location filename="../db/operations.py" line="1029"/>
<source>Spin-off</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1036"/>
<location filename="../db/operations.py" line="1030"/>
<source>Merger</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1039"/>
<location filename="../db/operations.py" line="1033"/>
<source>Delisting</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1140"/>
<location filename="../db/operations.py" line="1134"/>
<source>Asset isn&apos;t a part of corporate action results: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1159"/>
<location filename="../db/operations.py" line="1153"/>
<source>Corporate action type isn&apos;t defined. Date: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1164"/>
<location filename="../db/operations.py" line="1158"/>
<source>Asset amount is not enough for corporate action processing. Date: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1168"/>
<location filename="../db/operations.py" line="1162"/>
<source>Unhandled case: Corporate action covers not full open position. Date: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="1177"/>
<location filename="../db/operations.py" line="1171"/>
<source>Results value of corporate action doesn&apos;t match 100% of initial asset value. </source>
<translation type="unfinished"></translation>
</message>
Expand Down Expand Up @@ -1152,17 +1152,17 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="568"/>
<location filename="../db/operations.py" line="567"/>
<source>Can&apos;t process dividend as bank isn&apos;t set for investment account: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="580"/>
<location filename="../db/operations.py" line="579"/>
<source>Unsupported dividend type.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="602"/>
<location filename="../db/operations.py" line="600"/>
<source>Not supported action: stock dividend or vesting closes short trade.</source>
<translation type="unfinished"></translation>
</message>
Expand Down Expand Up @@ -1370,17 +1370,17 @@
<translation></translation>
</message>
<message>
<location filename="../db/holdings_model.py" line="178"/>
<location filename="../db/holdings_model.py" line="177"/>
<source>Last quote date: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/holdings_model.py" line="280"/>
<location filename="../db/holdings_model.py" line="279"/>
<source>N/A</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/holdings_model.py" line="302"/>
<location filename="../db/holdings_model.py" line="301"/>
<source>Money</source>
<translation type="unfinished"></translation>
</message>
Expand All @@ -1395,7 +1395,7 @@
<translation></translation>
</message>
<message>
<location filename="../db/holdings_model.py" line="141"/>
<location filename="../db/holdings_model.py" line="142"/>
<source>Exp:</source>
<translation></translation>
</message>
Expand Down Expand Up @@ -5428,6 +5428,14 @@ Please select an account for import:</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>TermDeposit</name>
<message>
<location filename="../db/operations.py" line="1289"/>
<source>Can&apos;t process deposit as bank isn&apos;t set for account: </source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>TermDepositOperation</name>
<message>
Expand Down Expand Up @@ -5563,17 +5571,17 @@ Please select an account for import:</source>
<context>
<name>Trade</name>
<message>
<location filename="../db/operations.py" line="663"/>
<location filename="../db/operations.py" line="659"/>
<source>Sell</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="667"/>
<location filename="../db/operations.py" line="663"/>
<source>Buy</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="728"/>
<location filename="../db/operations.py" line="723"/>
<source>Can&apos;t process trade as bank isn&apos;t set for investment account: </source>
<translation type="unfinished"></translation>
</message>
Expand Down Expand Up @@ -5665,62 +5673,62 @@ Please select an account for import:</source>
<context>
<name>Transfer</name>
<message>
<location filename="../db/operations.py" line="791"/>
<location filename="../db/operations.py" line="785"/>
<source>Outgoing transfer</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="792"/>
<location filename="../db/operations.py" line="786"/>
<source>Incoming transfer</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="793"/>
<location filename="../db/operations.py" line="787"/>
<source>Transfer fee</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="794"/>
<location filename="../db/operations.py" line="788"/>
<source>Outgoing asset transfer</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="795"/>
<location filename="../db/operations.py" line="789"/>
<source>Incoming asset transfer</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="796"/>
<location filename="../db/operations.py" line="790"/>
<source>Asset transfer fee</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="865"/>
<location filename="../db/operations.py" line="859"/>
<source>Cost basis:</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="880"/>
<location filename="../db/operations.py" line="874"/>
<source>Error. Zero rate</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="938"/>
<location filename="../db/operations.py" line="932"/>
<source>Can&apos;t collect fee from the account &apos;{}&apos; ({}) as organization isn&apos;t set for it. Date: {}</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="961"/>
<location filename="../db/operations.py" line="955"/>
<source>Asset amount is not enough for asset transfer processing. Date: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="966"/>
<location filename="../db/operations.py" line="960"/>
<source>Processed asset amount is less than transfer amount. Date: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../db/operations.py" line="979"/>
<location filename="../db/operations.py" line="973"/>
<source>Asset withdrawal not found for transfer.</source>
<translation type="unfinished"></translation>
</message>
Expand Down
Binary file modified jal/languages/ru.qm
Binary file not shown.
Loading

0 comments on commit c95907b

Please sign in to comment.