From 1235abadd03ac5bf6302494b8514cf9934e4520f Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Mon, 28 Oct 2024 21:17:53 -0500 Subject: [PATCH] fix:properly scoped dividend counts --- app/Models/Holding.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/Models/Holding.php b/app/Models/Holding.php index 5f6eb14..c3f5d16 100644 --- a/app/Models/Holding.php +++ b/app/Models/Holding.php @@ -85,15 +85,19 @@ class Holding extends Model THEN transactions.quantity ELSE 0 END ) AS sold") - ->selectRaw('SUM( - (CASE WHEN transaction_type = "BUY" + ->selectRaw("SUM( + (CASE WHEN transaction_type = 'BUY' + AND transactions.symbol = dividends.symbol + AND transactions.portfolio_id = '$this->portfolio_id' AND date(transactions.date) <= date(dividends.date) THEN transactions.quantity ELSE 0 END - - CASE WHEN transaction_type = "SELL" + - CASE WHEN transaction_type = 'SELL' + AND transactions.symbol = dividends.symbol + AND transactions.portfolio_id = '$this->portfolio_id' AND date(transactions.date) <= date(dividends.date) THEN transactions.quantity ELSE 0 END) * dividends.dividend_amount - ) AS total_received') + ) AS total_received") ->join('transactions', 'transactions.symbol', 'dividends.symbol') ->groupBy(['dividends.symbol','dividends.date','dividends.dividend_amount']) ->orderBy('dividends.date', 'DESC')