2024-08-10 13:30:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2024-08-27 21:17:54 -05:00
|
|
|
use App\Models\Split;
|
2024-08-10 13:30:19 -05:00
|
|
|
use App\Models\Dividend;
|
2024-08-27 21:17:54 -05:00
|
|
|
use App\Models\Portfolio;
|
|
|
|
|
use App\Models\MarketData;
|
|
|
|
|
use App\Models\Transaction;
|
2024-09-06 19:50:28 -05:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-08-10 13:30:19 -05:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-08-17 18:40:50 -05:00
|
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
2024-08-10 13:30:19 -05:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
|
|
|
|
|
class Holding extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2024-08-17 18:40:50 -05:00
|
|
|
use HasUuids;
|
2024-08-10 13:30:19 -05:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'portfolio_id',
|
|
|
|
|
'symbol',
|
|
|
|
|
'quantity',
|
|
|
|
|
'average_cost_basis',
|
|
|
|
|
'total_cost_basis',
|
2024-08-21 20:42:32 -05:00
|
|
|
'realized_gain_dollars',
|
2024-08-10 13:30:19 -05:00
|
|
|
'dividends_earned',
|
|
|
|
|
'splits_synced_at',
|
2024-10-18 20:46:22 -05:00
|
|
|
'reinvest_dividends'
|
2024-08-10 13:30:19 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'splits_synced_at' => 'datetime',
|
2024-10-18 20:46:22 -05:00
|
|
|
'first_transaction_date' => 'datetime',
|
|
|
|
|
'reinvest_dividends' => 'boolean'
|
2024-08-10 13:30:19 -05:00
|
|
|
];
|
|
|
|
|
|
2024-08-26 22:13:00 -05:00
|
|
|
protected $attributes = [
|
|
|
|
|
'realized_gain_dollars' => 0,
|
|
|
|
|
'dividends_earned' => 0,
|
|
|
|
|
];
|
|
|
|
|
|
2024-08-10 13:30:19 -05:00
|
|
|
/**
|
2024-08-17 21:33:09 -05:00
|
|
|
* Market data for holding
|
2024-08-10 13:30:19 -05:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function market_data()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(MarketData::class, 'symbol', 'symbol');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-17 21:33:09 -05:00
|
|
|
* Related transactions for holding
|
2024-08-10 13:30:19 -05:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function transactions()
|
|
|
|
|
{
|
2024-08-30 20:58:00 -05:00
|
|
|
return $this->hasManyThrough(Transaction::class, Portfolio::class, 'id', 'portfolio_id', 'portfolio_id', 'id')->orderBy('date', 'DESC');
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-17 21:33:09 -05:00
|
|
|
* Related dividends for holding
|
2024-08-10 13:30:19 -05:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function dividends()
|
|
|
|
|
{
|
2024-08-27 21:17:54 -05:00
|
|
|
return $this->hasMany(Dividend::class, 'symbol', 'symbol')
|
2024-08-29 22:26:04 -05:00
|
|
|
->select(['dividends.symbol','dividends.date','dividends.dividend_amount'])
|
2024-08-27 21:17:54 -05:00
|
|
|
->selectRaw("SUM(
|
|
|
|
|
CASE WHEN transaction_type = 'BUY'
|
|
|
|
|
AND transactions.symbol = dividends.symbol
|
|
|
|
|
AND transactions.portfolio_id = '$this->portfolio_id'
|
2024-09-23 15:09:35 -05:00
|
|
|
AND date(dividends.date) >= date(transactions.date)
|
2024-08-27 21:17:54 -05:00
|
|
|
THEN transactions.quantity
|
|
|
|
|
ELSE 0 END
|
|
|
|
|
) AS purchased")
|
|
|
|
|
->selectRaw("SUM(
|
|
|
|
|
CASE WHEN transaction_type = 'SELL'
|
|
|
|
|
AND transactions.symbol = dividends.symbol
|
|
|
|
|
AND transactions.portfolio_id = '$this->portfolio_id'
|
2024-09-23 15:09:35 -05:00
|
|
|
AND date(dividends.date) >= date(transactions.date)
|
2024-08-27 21:17:54 -05:00
|
|
|
THEN transactions.quantity
|
|
|
|
|
ELSE 0 END
|
|
|
|
|
) AS sold")
|
2024-09-23 15:09:35 -05:00
|
|
|
->selectRaw('SUM(
|
|
|
|
|
(CASE WHEN transaction_type = "BUY"
|
|
|
|
|
AND date(transactions.date) <= date(dividends.date)
|
|
|
|
|
THEN transactions.quantity ELSE 0 END
|
|
|
|
|
- CASE WHEN transaction_type = "SELL"
|
|
|
|
|
AND date(transactions.date) <= date(dividends.date)
|
|
|
|
|
THEN transactions.quantity ELSE 0 END)
|
|
|
|
|
* dividends.dividend_amount
|
|
|
|
|
) AS total_received')
|
2024-08-27 21:17:54 -05:00
|
|
|
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
2024-08-29 22:26:04 -05:00
|
|
|
->groupBy(['dividends.symbol','dividends.date','dividends.dividend_amount'])
|
2024-08-27 21:17:54 -05:00
|
|
|
->orderBy('dividends.date', 'DESC')
|
|
|
|
|
->where('dividends.date', '>=', function ($query) {
|
|
|
|
|
$query->selectRaw('min(transactions.date)')
|
|
|
|
|
->from('transactions')
|
|
|
|
|
->whereRaw("transactions.portfolio_id = '$this->portfolio_id'")
|
|
|
|
|
->whereRaw("transactions.symbol = '$this->symbol'");
|
2024-10-20 13:28:40 -05:00
|
|
|
})
|
|
|
|
|
->having('total_received', '>', 0);
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-17 21:33:09 -05:00
|
|
|
* Related portfolio for holding
|
2024-08-10 13:30:19 -05:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function portfolio()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Portfolio::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-17 21:33:09 -05:00
|
|
|
* Related splits for holding
|
2024-08-10 13:30:19 -05:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function splits()
|
|
|
|
|
{
|
2024-08-27 21:17:54 -05:00
|
|
|
return $this->hasMany(Split::class, 'symbol', 'symbol')
|
|
|
|
|
->orderBy('date', 'DESC');
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 15:19:20 -05:00
|
|
|
public function scopeWithMarketData($query)
|
|
|
|
|
{
|
2024-08-28 23:32:01 -05:00
|
|
|
return $query->withAggregate('market_data', 'name')
|
|
|
|
|
->withAggregate('market_data', 'market_value')
|
|
|
|
|
->withAggregate('market_data', 'fifty_two_week_low')
|
|
|
|
|
->withAggregate('market_data', 'fifty_two_week_high')
|
|
|
|
|
->withAggregate('market_data', 'updated_at')
|
|
|
|
|
->join('market_data', 'holdings.symbol', 'market_data.symbol');
|
2024-08-28 15:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-29 18:46:21 -05:00
|
|
|
public function scopeWithPerformance($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
|
|
|
|
->selectRaw('COALESCE((market_data.market_value - holdings.average_cost_basis) * holdings.quantity, 0) AS market_gain_dollars')
|
|
|
|
|
->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis), 0) AS market_gain_percent');
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-10 13:30:19 -05:00
|
|
|
public function scopePortfolio($query, $portfolio)
|
|
|
|
|
{
|
2024-09-11 22:00:37 -05:00
|
|
|
return $query->where('holdings.portfolio_id', $portfolio);
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 21:17:54 -05:00
|
|
|
public function scopeSymbol($query, $symbol)
|
|
|
|
|
{
|
2024-08-28 22:06:47 -05:00
|
|
|
return $query->where('holdings.symbol', $symbol);
|
2024-08-27 21:17:54 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-10 13:30:19 -05:00
|
|
|
public function scopeWithoutWishlists($query) {
|
|
|
|
|
return $query->join('portfolios', 'portfolios.id', 'holdings.portfolio_id')
|
|
|
|
|
->where('portfolios.wishlist', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function scopeMyHoldings($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->whereHas('portfolio', function($query) {
|
|
|
|
|
$query->whereRelation('users', 'id', auth()->user()->id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 18:46:21 -05:00
|
|
|
public function scopeWithPortfolioMetrics($query)
|
2024-08-10 13:30:19 -05:00
|
|
|
{
|
2024-09-06 19:50:28 -05:00
|
|
|
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.quantity * market_data.market_value), 0) AS total_market_value')
|
|
|
|
|
->selectRaw('COALESCE(SUM(holdings.total_cost_basis), 0) AS total_cost_basis')
|
|
|
|
|
->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')
|
|
|
|
|
->join('market_data', 'market_data.symbol', '=', 'holdings.symbol');
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
2024-08-30 20:22:28 -05:00
|
|
|
|
2024-08-30 21:58:38 -05:00
|
|
|
public function syncTransactionsAndDividends()
|
2024-08-30 20:22:28 -05:00
|
|
|
{
|
|
|
|
|
// pull existing transaction data
|
|
|
|
|
$query = Transaction::where([
|
|
|
|
|
'portfolio_id' => $this->portfolio_id,
|
|
|
|
|
'symbol' => $this->symbol,
|
|
|
|
|
])->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN quantity ELSE 0 END) AS `qty_purchases`')
|
|
|
|
|
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN quantity ELSE 0 END) AS `qty_sales`')
|
2024-09-18 22:42:09 -05:00
|
|
|
->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN (quantity * cost_basis) ELSE 0 END) AS `total_cost_basis`')
|
2024-09-18 23:19:11 -05:00
|
|
|
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN (quantity * sale_price) ELSE 0 END) AS `total_sale_price`')
|
2024-08-30 20:22:28 -05:00
|
|
|
->first();
|
|
|
|
|
|
2024-09-18 22:10:02 -05:00
|
|
|
$total_quantity = round($query->qty_purchases - $query->qty_sales, 3);
|
2024-09-06 23:15:43 -05:00
|
|
|
|
|
|
|
|
$average_cost_basis = (
|
|
|
|
|
$query->qty_purchases > 0
|
|
|
|
|
&& $total_quantity > 0
|
|
|
|
|
)
|
2024-09-18 22:42:09 -05:00
|
|
|
? $query->total_cost_basis / $query->qty_purchases
|
2024-09-06 23:15:43 -05:00
|
|
|
: 0;
|
2024-08-30 20:22:28 -05:00
|
|
|
|
|
|
|
|
// update holding
|
|
|
|
|
$this->fill([
|
|
|
|
|
'quantity' => $total_quantity,
|
|
|
|
|
'average_cost_basis' => $average_cost_basis,
|
|
|
|
|
'total_cost_basis' => $total_quantity * $average_cost_basis,
|
2024-09-23 15:09:35 -05:00
|
|
|
'realized_gain_dollars' => $query->qty_purchases > 0 && $query->total_sale_price > 0
|
|
|
|
|
? $query->total_sale_price - ($query->qty_sales * ($query->total_cost_basis / $query->qty_purchases))
|
|
|
|
|
: 0,
|
|
|
|
|
'dividends_earned' => $this->dividends->sum('total_received')
|
2024-08-30 20:22:28 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
|
}
|
2024-08-10 13:30:19 -05:00
|
|
|
|
2024-10-18 20:46:22 -05:00
|
|
|
public function qtyOwned(\Illuminate\Support\Carbon $date = null)
|
|
|
|
|
{
|
|
|
|
|
if ($date == null) $date = now();
|
|
|
|
|
|
|
|
|
|
$transactions = $this->transactions->where('date', '<=', $date);
|
|
|
|
|
|
|
|
|
|
$purchases = $transactions->where('transaction_type', 'BUY')->sum('quantity');
|
|
|
|
|
|
|
|
|
|
$sales = $transactions->where('transaction_type', 'SELL')->sum('quantity');
|
|
|
|
|
|
|
|
|
|
return $purchases - $sales;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 22:00:37 -05:00
|
|
|
public function dailyPerformance(
|
|
|
|
|
\Illuminate\Support\Carbon $start_date = null,
|
|
|
|
|
\Illuminate\Support\Carbon $end_date = null,
|
|
|
|
|
) {
|
|
|
|
|
if ($start_date == null) $start_date = now();
|
|
|
|
|
if ($end_date == null) $end_date = now();
|
|
|
|
|
|
|
|
|
|
$date_interval = "DATE_ADD(date, INTERVAL 1 DAY)";
|
|
|
|
|
|
|
|
|
|
if (config('database.default') === 'sqlite') {
|
2024-09-11 23:04:21 -05:00
|
|
|
|
2024-09-11 22:00:37 -05:00
|
|
|
$date_interval = "date(date, '+1 day')";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DB::table(DB::raw("(
|
|
|
|
|
WITH RECURSIVE date_series AS (
|
|
|
|
|
SELECT '{$start_date->format('Y-m-d')}' AS date
|
|
|
|
|
UNION ALL
|
|
|
|
|
SELECT $date_interval
|
|
|
|
|
FROM date_series
|
|
|
|
|
WHERE date < '{$end_date->format('Y-m-d')}'
|
|
|
|
|
)
|
|
|
|
|
SELECT date_series.date
|
|
|
|
|
FROM date_series
|
|
|
|
|
) as date_series")
|
|
|
|
|
)
|
|
|
|
|
->select([
|
|
|
|
|
'date_series.date',
|
|
|
|
|
DB::raw("
|
2024-09-18 22:10:02 -05:00
|
|
|
ROUND(
|
2024-09-11 22:00:37 -05:00
|
|
|
COALESCE(SUM(CASE WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity ELSE 0 END), 0) -
|
2024-09-18 22:10:02 -05:00
|
|
|
COALESCE(SUM(CASE WHEN transactions.transaction_type = 'SELL' THEN transactions.quantity ELSE 0 END), 0), 3) AS `owned`
|
2024-09-11 22:00:37 -05:00
|
|
|
"),
|
2024-09-15 11:28:13 -05:00
|
|
|
DB::raw("
|
|
|
|
|
COALESCE(CASE
|
|
|
|
|
WHEN (
|
2024-09-18 22:10:02 -05:00
|
|
|
ROUND(
|
2024-09-15 11:28:13 -05:00
|
|
|
COALESCE(SUM(CASE WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity ELSE 0 END), 0) -
|
2024-09-18 22:10:02 -05:00
|
|
|
COALESCE(SUM(CASE WHEN transactions.transaction_type = 'SELL' THEN transactions.quantity ELSE 0 END), 0), 3)
|
2024-09-15 11:28:13 -05:00
|
|
|
) = 0 THEN 0
|
|
|
|
|
ELSE SUM(CASE
|
|
|
|
|
WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity * transactions.cost_basis
|
|
|
|
|
ELSE 0
|
|
|
|
|
END)
|
|
|
|
|
END, 0) AS cost_basis
|
|
|
|
|
"),
|
2024-09-11 22:00:37 -05:00
|
|
|
DB::raw("COALESCE(SUM(CASE WHEN transaction_type = 'SELL' THEN ((sale_price - cost_basis) * quantity) ELSE 0 END), 0) AS `realized_gains`")
|
|
|
|
|
])
|
|
|
|
|
->leftJoin('transactions', function ($join) {
|
|
|
|
|
$join->on(DB::raw('DATE(transactions.date)'), '<=', 'date_series.date')
|
|
|
|
|
->where('transactions.symbol', '=', $this->symbol)
|
|
|
|
|
->where('transactions.portfolio_id', '=', $this->portfolio_id);
|
|
|
|
|
})
|
|
|
|
|
->groupBy('date_series.date')
|
|
|
|
|
->orderBy('date_series.date')
|
|
|
|
|
->get()
|
|
|
|
|
->keyBy('date');
|
|
|
|
|
}
|
|
|
|
|
}
|