diff --git a/src/pages/stock/components/PortfolioTab.jsx b/src/pages/stock/components/PortfolioTab.jsx index 2073c8a..27ff895 100644 --- a/src/pages/stock/components/PortfolioTab.jsx +++ b/src/pages/stock/components/PortfolioTab.jsx @@ -96,7 +96,7 @@ const PortfolioTab = ({ pf, asset, handleSell, handleSaveSnapshot }) => ( /> - 평균 매입가 (원) + 평균단가 (원) ( required /> + + 매입가 (원) + + pf.setAddForm((p) => ({ ...p, purchase_price: e.target.value })) + } + placeholder="미입력 시 평균단가로 자동 설정" + /> + ( /> - 평균매입가 + 평균단가 ( } /> + + 매입가 + + pf.setEditForm((p) => ({ + ...p, + purchase_price: Number(e.target.value), + })) + } + /> + ( {formatNumber(item.quantity)} - 매입가 + 평균단가 {formatNumber(item.avg_price)} + + 매입가 + {formatNumber(item.purchase_price ?? item.avg_price)} + 현재가 diff --git a/src/pages/stock/hooks/usePortfolio.js b/src/pages/stock/hooks/usePortfolio.js index 441ba1f..1453adc 100644 --- a/src/pages/stock/hooks/usePortfolio.js +++ b/src/pages/stock/hooks/usePortfolio.js @@ -70,14 +70,19 @@ export default function usePortfolio() { }, [brokerGroups]); const getBrokerSummary = (items) => { - let totalBuy = 0, totalEvalAmt = 0, hasNullPrice = false; + // totalBuy: 요약 표시용 (매입가 purchase_price 기준) + // totalCostBasis: 손익 계산용 (평균단가 avg_price 기준) + let totalBuy = 0, totalCostBasis = 0, totalEvalAmt = 0, hasNullPrice = false; for (const item of items) { - totalBuy += (item.avg_price ?? 0) * (item.quantity ?? 0); + const qty = item.quantity ?? 0; + const purchase = item.purchase_price ?? item.avg_price ?? 0; + totalBuy += purchase * qty; + totalCostBasis += (item.avg_price ?? 0) * qty; if (item.eval_amount != null) totalEvalAmt += item.eval_amount; else hasNullPrice = true; } - const totalProfit = totalEvalAmt - totalBuy; - const totalProfitRate = totalBuy > 0 ? (totalProfit / totalBuy) * 100 : 0; + const totalProfit = totalEvalAmt - totalCostBasis; + const totalProfitRate = totalCostBasis > 0 ? (totalProfit / totalCostBasis) * 100 : 0; return { totalBuy, totalEval: totalEvalAmt, totalProfit, totalProfitRate, hasNullPrice }; }; @@ -108,6 +113,9 @@ export default function usePortfolio() { name: addForm.name.trim(), quantity: Number(addForm.quantity), avg_price: Number(addForm.avg_price), + purchase_price: addForm.purchase_price === '' || addForm.purchase_price == null + ? Number(addForm.avg_price) + : Number(addForm.purchase_price), }); setAddForm({ ...emptyPortfolioForm }); setAddFormOpen(false); @@ -121,7 +129,13 @@ export default function usePortfolio() { const handleEditStart = (item) => { setEditingId(item.id); - const data = { quantity: item.quantity, avg_price: item.avg_price, broker: item.broker, name: item.name }; + const data = { + quantity: item.quantity, + avg_price: item.avg_price, + purchase_price: item.purchase_price ?? item.avg_price, + broker: item.broker, + name: item.name, + }; setEditForm(data); editOrigRef.current = { ...data }; }; diff --git a/src/pages/stock/stockUtils.js b/src/pages/stock/stockUtils.js index 7047519..10e0caa 100644 --- a/src/pages/stock/stockUtils.js +++ b/src/pages/stock/stockUtils.js @@ -95,6 +95,7 @@ export const emptyPortfolioForm = { name: '', quantity: '', avg_price: '', + purchase_price: '', }; /* ── empty sell-history form ─────────────────────────────────────── */