Open-source personal finance manager. Self-hosted, privacy-first.
fix(assets): show profit for manual and sold holdings (#806)
Artur Schaefer6db62d76ddbc
diff --git a/frontend/src/lib/asset-profit.test.ts b/frontend/src/lib/asset-profit.test.tsnew file mode 100644index 00000000..8908b319--- /dev/null+++ b/frontend/src/lib/asset-profit.test.ts@@ -0,0 +1,62 @@+import { describe, expect, it } from 'vitest'++import { getAssetProfit } from '@/lib/asset-profit'++const baseAsset = {+ gain_loss: null,+ purchase_price: null,+ realized_gain: null,+ sell_date: null,+ sell_price: null,+ total_invested: null,+ value_count: 0,+}++describe('getAssetProfit', () => {+ it('uses the purchase price for a manually valued asset', () => {+ expect(getAssetProfit({+ ...baseAsset,+ gain_loss: 446.45,+ purchase_price: 3679,+ value_count: 1,+ })).toEqual({+ amount: 446.45,+ percentage: (446.45 / 3679) * 100,+ })+ })++ it('uses the ledger cost basis for a held market asset', () => {+ expect(getAssetProfit({+ ...baseAsset,+ gain_loss: -50,+ purchase_price: 950,+ total_invested: 1000,+ value_count: 1,+ })).toEqual({ amount: -50, percentage: -5 })+ })++ it('hides fallback zero profit until a manual valuation exists', () => {+ expect(getAssetProfit({+ ...baseAsset,+ gain_loss: 0,+ purchase_price: 1000,+ })).toBeNull()+ })++ it('shows the cumulative realized gain for a sold ledger asset', () => {+ expect(getAssetProfit({+ ...baseAsset,+ realized_gain: 249.48,+ sell_date: '2025-11-07',+ })).toEqual({ amount: 249.48, percentage: null })+ })++ it('derives profit for a manually sold asset', () => {+ expect(getAssetProfit({+ ...baseAsset,+ purchase_price: 1000,+ sell_date: '2025-11-07',+ sell_price: 1250,+ })).toEqual({ amount: 250, percentage: 25 })+ })+})diff --git a/frontend/src/lib/asset-profit.ts b/frontend/src/lib/asset-profit.tsnew file mode 100644index 00000000..445d9025--- /dev/null+++ b/frontend/src/lib/asset-profit.ts@@ -0,0 +1,30 @@+import type { Asset } from '@/types'++type ProfitAsset = Pick<+ Asset,+ | 'gain_loss'+ | 'purchase_price'+ | 'realized_gain'+ | 'sell_date'+ | 'sell_price'+ | 'total_invested'+ | 'value_count'+>++export function getAssetProfit(asset: ProfitAsset) {+ const amount = asset.sell_date+ ? asset.realized_gain ?? (+ asset.sell_price != null && asset.purchase_price != null+ ? asset.sell_price - asset.purchase_price+ : null+ )+ : asset.value_count > 0 ? asset.gain_loss : null++ if (amount == null) return null++ const cost = asset.total_invested ?? asset.purchase_price+ return {+ amount,+ percentage: cost ? (amount / cost) * 100 : null,+ }+}diff --git a/frontend/src/pages/assets.tsx b/frontend/src/pages/assets.tsxindex 6cbc80ad..c4906f51 100644--- a/frontend/src/pages/assets.tsx+++ b/frontend/src/pages/assets.tsx@@ -57,6 +57,7 @@ import { usePrivacyMode } from '@/hooks/use-privacy-mode'import { useAuth } from '@/contexts/auth-context'import { useWorkspace } from '@/contexts/workspace-context'import { useCollectionFilter } from '@/contexts/collection-filter-context'+import { getAssetProfit } from '@/lib/asset-profit'import { formatCurrency } from '@/lib/format'// Renders a logo image when one is available, falling back to the asset's@@ -715,10 +716,7 @@ export default function AssetsPage() {const isMarketPriced = asset.valuation_method === 'market_price'const isProviderOwned = isSynced && !isMarketPricedconst hasCost = asset.average_price != null && asset.total_invested != null- const returnPct =- hasCost && asset.gain_loss != null && asset.total_invested- ? (asset.gain_loss / asset.total_invested) * 100- : null+ const profit = getAssetProfit(asset)const pctOfPortfolio =portfolioTotalPrimary > 0 && asset.current_value_primary != null? (asset.current_value_primary / portfolioTotalPrimary) * 100@@ -781,9 +779,16 @@ export default function AssetsPage() {</div>{/* Rentabilidade */}<div className="text-right tabular-nums">- {returnPct != null ? (- <span className={returnPct >= 0 ? 'text-emerald-600' : 'text-rose-500'}>- {returnPct >= 0 ? '+' : ''}{returnPct.toFixed(1)}%+ {profit ? (+ <span className={profit.amount >= 0 ? 'text-emerald-600' : 'text-rose-500'}>+ <span className="block">+ {profit.amount >= 0 ? '+' : ''}{mask(formatCurrency(profit.amount, asset.currency, locale))}+ </span>+ {profit.percentage != null && (+ <span className="block text-[10px]">+ {profit.percentage >= 0 ? '+' : ''}{profit.percentage.toFixed(1)}%+ </span>+ )}</span>) : <span className="text-muted-foreground">—</span>}</div>