fix(ledger): keep legacy amount wire type and preserve dividend tax decimals
Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
co-authored by
genichin
parent
fde5b33f05
commit
d3601d32dc
@@ -688,11 +688,14 @@ def record_ledger_trade_v1(
|
||||
|
||||
def _manual_entry_to_response(entry) -> ManualLedgerResponse:
|
||||
action = (entry.action or entry.side or "").upper()
|
||||
amount = (
|
||||
(entry.price_decimal if entry.price_decimal is not None else Decimal(str(entry.price)))
|
||||
if action in {"INVEST", "BUFFER_OUT", "BUFFER_IN", "WITHDRAW", "PROFIT", "DIVIDEND"}
|
||||
else None
|
||||
)
|
||||
amount: float | Decimal | None = None
|
||||
if action == "DIVIDEND":
|
||||
amount = entry.price_decimal if entry.price_decimal is not None else Decimal(str(entry.price))
|
||||
elif action in {"INVEST", "BUFFER_OUT", "BUFFER_IN", "WITHDRAW", "PROFIT"}:
|
||||
amount = float(entry.price)
|
||||
tax_value: float | Decimal = float(entry.tax_amount)
|
||||
if action == "DIVIDEND":
|
||||
tax_value = entry.tax_amount_decimal if entry.tax_amount_decimal is not None else Decimal(str(entry.tax_amount))
|
||||
return ManualLedgerResponse(
|
||||
ledger_entry_id=entry.ledger_entry_id,
|
||||
portfolio_id=entry.portfolio_id,
|
||||
@@ -701,7 +704,7 @@ def _manual_entry_to_response(entry) -> ManualLedgerResponse:
|
||||
quantity=entry.quantity,
|
||||
price=entry.price,
|
||||
fee=entry.fee_amount,
|
||||
tax=entry.tax_amount,
|
||||
tax=tax_value,
|
||||
executed_at=entry.executed_at,
|
||||
note=entry.note,
|
||||
recorded_at=entry.recorded_at,
|
||||
@@ -791,9 +794,14 @@ def _decode_ledger_cursor(cursor: str, *, portfolio_id: str, source: str | None)
|
||||
|
||||
def _ledger_entry_to_history_item(entry) -> LedgerHistoryItem:
|
||||
action = (entry.action or entry.side or "").upper()
|
||||
amount = None
|
||||
if action in {"INVEST", "BUFFER_OUT", "BUFFER_IN", "WITHDRAW", "PROFIT", "DIVIDEND"}:
|
||||
amount: float | Decimal | None = None
|
||||
if action == "DIVIDEND":
|
||||
amount = entry.price_decimal if entry.price_decimal is not None else Decimal(str(entry.price))
|
||||
elif action in {"INVEST", "BUFFER_OUT", "BUFFER_IN", "WITHDRAW", "PROFIT"}:
|
||||
amount = float(entry.price)
|
||||
tax_amount: float | Decimal | None = entry.tax_amount
|
||||
if action == "DIVIDEND":
|
||||
tax_amount = entry.tax_amount_decimal if entry.tax_amount_decimal is not None else Decimal(str(entry.tax_amount))
|
||||
return LedgerHistoryItem(
|
||||
ledger_entry_id=entry.ledger_entry_id,
|
||||
portfolio_id=entry.portfolio_id,
|
||||
@@ -807,7 +815,7 @@ def _ledger_entry_to_history_item(entry) -> LedgerHistoryItem:
|
||||
amount=amount,
|
||||
price=entry.price,
|
||||
fee_amount=entry.fee_amount,
|
||||
tax_amount=entry.tax_amount,
|
||||
tax_amount=tax_amount,
|
||||
note=entry.note,
|
||||
cash_balance_after=entry.cash_balance_after,
|
||||
asset_quantity_after=entry.asset_quantity_after,
|
||||
|
||||
@@ -144,6 +144,7 @@ class LedgerEntry:
|
||||
side: str | None = None
|
||||
fee_amount: float = 0.0
|
||||
tax_amount: float = 0.0
|
||||
tax_amount_decimal: Decimal | None = None
|
||||
executed_at: datetime | None = None
|
||||
recorded_at: datetime | None = None
|
||||
cash_balance_after: float = 0.0
|
||||
|
||||
@@ -703,6 +703,7 @@ class PostgresPortfolioRepository:
|
||||
|
||||
@staticmethod
|
||||
def _row_to_ledger_entry_legacy(row: Any) -> LedgerEntry:
|
||||
raw_tax = row[6]
|
||||
return LedgerEntry(
|
||||
ledger_entry_id=0,
|
||||
portfolio_id=row[0],
|
||||
@@ -713,7 +714,8 @@ class PostgresPortfolioRepository:
|
||||
price=float(row[3]),
|
||||
price_decimal=Decimal(str(row[3])) if row[3] is not None else None,
|
||||
fee_amount=float(row[5]),
|
||||
tax_amount=float(row[6]),
|
||||
tax_amount=float(raw_tax),
|
||||
tax_amount_decimal=Decimal(str(raw_tax)) if raw_tax is not None else None,
|
||||
executed_at=row[1],
|
||||
recorded_at=row[1],
|
||||
cash_balance_after=float(row[7]),
|
||||
@@ -1866,6 +1868,7 @@ class PostgresPortfolioRepository:
|
||||
if row is None:
|
||||
raise ValueError("원장 항목을 찾을 수 없습니다")
|
||||
raw_price = row[6]
|
||||
raw_tax = row[8]
|
||||
return LedgerEntry(
|
||||
ledger_entry_id=int(row[0]),
|
||||
portfolio_id=row[1],
|
||||
@@ -1876,7 +1879,8 @@ class PostgresPortfolioRepository:
|
||||
price=float(raw_price),
|
||||
price_decimal=Decimal(str(raw_price)) if raw_price is not None else None,
|
||||
fee_amount=float(row[7]),
|
||||
tax_amount=float(row[8]),
|
||||
tax_amount=float(raw_tax),
|
||||
tax_amount_decimal=Decimal(str(raw_tax)) if raw_tax is not None else None,
|
||||
executed_at=row[9],
|
||||
recorded_at=row[10],
|
||||
cash_balance_after=float(row[11]),
|
||||
|
||||
+4
-4
@@ -220,11 +220,11 @@ class ManualLedgerResponse(BaseModel):
|
||||
ledger_entry_id: int
|
||||
portfolio_id: str
|
||||
action: str
|
||||
amount: Decimal | None = None
|
||||
amount: float | Decimal | None = None
|
||||
quantity: float = 0.0
|
||||
price: float = 0.0
|
||||
fee: float = 0.0
|
||||
tax: float = 0.0
|
||||
tax: float | Decimal = 0.0
|
||||
executed_at: datetime | None = None
|
||||
note: str = ""
|
||||
recorded_at: datetime | None = None
|
||||
@@ -254,10 +254,10 @@ class LedgerHistoryItem(BaseModel):
|
||||
order_id: str | None = None
|
||||
execution_id: str | None = None
|
||||
quantity: float | None = None
|
||||
amount: Decimal | None = None
|
||||
amount: float | Decimal | None = None
|
||||
price: float | None = None
|
||||
fee_amount: float | None = None
|
||||
tax_amount: float | None = None
|
||||
tax_amount: float | Decimal | None = None
|
||||
note: str | None = None
|
||||
cash_balance_after: float | None = None
|
||||
asset_quantity_after: float | None = None
|
||||
|
||||
Reference in New Issue
Block a user