remove mysql-specific aliases
This commit is contained in:
+26
-12
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Models\Holding;
|
||||||
|
use App\Models\MarketData;
|
||||||
|
use App\Models\Transaction;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use App\Interfaces\MarketData\MarketDataInterface;
|
use App\Interfaces\MarketData\MarketDataInterface;
|
||||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||||
@@ -93,18 +97,28 @@ class Dividend extends Model
|
|||||||
$symbol = $dividend_data->last()->symbol;
|
$symbol = $dividend_data->last()->symbol;
|
||||||
|
|
||||||
// group by holdings
|
// group by holdings
|
||||||
$dividends = self::where([
|
$dividends = self::select('holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount')
|
||||||
'dividends.symbol' => $dividend_data->last()->symbol,
|
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||||
])
|
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
|
||||||
->select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
->leftJoin(DB::raw('(SELECT symbol, portfolio_id, COALESCE(SUM(quantity), 0) AS total_purchased
|
||||||
->selectRaw('@purchased:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "BUY" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `purchased`')
|
FROM transactions
|
||||||
->selectRaw('@sold:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "SELL" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `sold`')
|
WHERE transaction_type = "BUY"
|
||||||
->selectRaw('@owned:=(@purchased - @sold) AS `owned`')
|
GROUP BY symbol, portfolio_id) AS purchases'), function($join) {
|
||||||
->selectRaw('@dividends_received:=(@owned * dividends.dividend_amount) AS `dividends_received`')
|
$join->on('purchases.symbol', 'dividends.symbol')
|
||||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
->on('purchases.portfolio_id', 'holdings.portfolio_id');
|
||||||
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
|
})
|
||||||
->groupBy(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
->leftJoin(DB::raw('(SELECT symbol, portfolio_id, COALESCE(SUM(quantity), 0) AS total_sold
|
||||||
->get();
|
FROM transactions
|
||||||
|
WHERE transaction_type = "SELL"
|
||||||
|
GROUP BY symbol, portfolio_id) AS sales'), function($join) {
|
||||||
|
$join->on('sales.symbol', 'dividends.symbol')
|
||||||
|
->on('sales.portfolio_id', 'holdings.portfolio_id');
|
||||||
|
})
|
||||||
|
->selectRaw('COALESCE(purchases.total_purchased, 0) - COALESCE(sales.total_sold, 0) AS owned')
|
||||||
|
->selectRaw('(COALESCE(purchases.total_purchased, 0) - COALESCE(sales.total_sold, 0)) * dividends.dividend_amount AS dividends_received')
|
||||||
|
->where('dividends.symbol', $dividend_data->last()->symbol)
|
||||||
|
->groupBy('holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount')
|
||||||
|
->get();
|
||||||
|
|
||||||
// iterate through holdings and update
|
// iterate through holdings and update
|
||||||
Holding::where(['symbol' => $symbol])
|
Holding::where(['symbol' => $symbol])
|
||||||
|
|||||||
+30
-19
@@ -7,6 +7,7 @@ use App\Models\Dividend;
|
|||||||
use App\Models\Portfolio;
|
use App\Models\Portfolio;
|
||||||
use App\Models\MarketData;
|
use App\Models\MarketData;
|
||||||
use App\Models\Transaction;
|
use App\Models\Transaction;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
@@ -154,13 +155,13 @@ class Holding extends Model
|
|||||||
|
|
||||||
public function scopeWithPortfolioMetrics($query)
|
public function scopeWithPortfolioMetrics($query)
|
||||||
{
|
{
|
||||||
return $query->selectRaw('COALESCE(SUM(holdings.dividends_earned),0) AS total_dividends_earned')
|
return $query->selectRaw('COALESCE(SUM(holdings.dividends_earned), 0) AS total_dividends_earned')
|
||||||
->selectRaw('COALESCE(SUM(holdings.realized_gain_dollars),0) AS realized_gain_dollars')
|
->selectRaw('COALESCE(SUM(holdings.realized_gain_dollars), 0) AS realized_gain_dollars')
|
||||||
->selectRaw('@total_market_value:=COALESCE(SUM(holdings.quantity * market_data.market_value),0) AS total_market_value')
|
->selectRaw('COALESCE(SUM(holdings.quantity * market_data.market_value), 0) AS total_market_value')
|
||||||
->selectRaw('@sum_total_cost_basis:=COALESCE(SUM(holdings.total_cost_basis),0) AS total_cost_basis')
|
->selectRaw('COALESCE(SUM(holdings.total_cost_basis), 0) AS total_cost_basis')
|
||||||
->selectRaw('@total_gain_dollars:=COALESCE((@total_market_value - @sum_total_cost_basis),0) AS total_gain_dollars')
|
->selectRaw('COALESCE(SUM(holdings.quantity * market_data.market_value), 0) - COALESCE(SUM(holdings.total_cost_basis), 0) AS total_gain_dollars')
|
||||||
// ->selectRaw('COALESCE((@total_gain_dollars / @sum_total_cost_basis) * 100,0) AS total_gain_percent')
|
// ->selectRaw('COALESCE((@total_gain_dollars / @sum_total_cost_basis) * 100,0) AS total_gain_percent')
|
||||||
->join('market_data', 'market_data.symbol', 'holdings.symbol');
|
->join('market_data', 'market_data.symbol', '=', 'holdings.symbol');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function syncTransactionsAndDividends()
|
public function syncTransactionsAndDividends()
|
||||||
@@ -181,18 +182,28 @@ class Holding extends Model
|
|||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
// pull dividend data joined with holdings/transactions
|
// pull dividend data joined with holdings/transactions
|
||||||
$dividends = Dividend::where([
|
$dividends = Dividend::select('holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount')
|
||||||
'dividends.symbol' => $this->symbol,
|
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||||
])
|
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
|
||||||
->select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
->leftJoin(DB::raw('(SELECT symbol, portfolio_id, COALESCE(SUM(quantity), 0) AS total_purchased
|
||||||
->selectRaw('@purchased:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "BUY" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `purchased`')
|
FROM transactions
|
||||||
->selectRaw('@sold:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "SELL" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `sold`')
|
WHERE transaction_type = "BUY"
|
||||||
->selectRaw('@owned:=(@purchased - @sold) AS `owned`')
|
GROUP BY symbol, portfolio_id) AS purchases'), function($join) {
|
||||||
->selectRaw('@dividends_received:=(@owned * dividends.dividend_amount) AS `dividends_received`')
|
$join->on('purchases.symbol', 'dividends.symbol')
|
||||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
->on('purchases.portfolio_id', 'holdings.portfolio_id');
|
||||||
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
|
})
|
||||||
->groupBy(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
->leftJoin(DB::raw('(SELECT symbol, portfolio_id, COALESCE(SUM(quantity), 0) AS total_sold
|
||||||
->get();
|
FROM transactions
|
||||||
|
WHERE transaction_type = "SELL"
|
||||||
|
GROUP BY symbol, portfolio_id) AS sales'), function($join) {
|
||||||
|
$join->on('sales.symbol', 'dividends.symbol')
|
||||||
|
->on('sales.portfolio_id', 'holdings.portfolio_id');
|
||||||
|
})
|
||||||
|
->selectRaw('COALESCE(purchases.total_purchased, 0) - COALESCE(sales.total_sold, 0) AS owned')
|
||||||
|
->selectRaw('(COALESCE(purchases.total_purchased, 0) - COALESCE(sales.total_sold, 0)) * dividends.dividend_amount AS dividends_received')
|
||||||
|
->where('dividends.symbol', $this->symbol)
|
||||||
|
->groupBy('holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount')
|
||||||
|
->get();
|
||||||
|
|
||||||
// update holding
|
// update holding
|
||||||
$this->fill([
|
$this->fill([
|
||||||
|
|||||||
Reference in New Issue
Block a user