wip
This commit is contained in:
+51
-8
@@ -2,7 +2,11 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Split;
|
||||
use App\Models\Dividend;
|
||||
use App\Models\Portfolio;
|
||||
use App\Models\MarketData;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -53,7 +57,8 @@ class Holding extends Model
|
||||
*/
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol')
|
||||
->where('transactions.portfolio_id', $this->portfolio_id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +68,44 @@ class Holding extends Model
|
||||
*/
|
||||
public function dividends()
|
||||
{
|
||||
return $this->hasMany(Dividend::class, 'symbol', 'symbol');
|
||||
|
||||
|
||||
|
||||
return $this->hasMany(Dividend::class, 'symbol', 'symbol')
|
||||
->select([
|
||||
'dividends.symbol',
|
||||
'dividends.date',
|
||||
'dividends.dividend_amount',
|
||||
])
|
||||
->selectRaw("SUM(
|
||||
CASE WHEN transaction_type = 'BUY'
|
||||
AND transactions.symbol = dividends.symbol
|
||||
AND transactions.portfolio_id = '$this->portfolio_id'
|
||||
AND dividends.date >= transactions.date
|
||||
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'
|
||||
AND dividends.date >= transactions.date
|
||||
THEN transactions.quantity
|
||||
ELSE 0 END
|
||||
) AS sold")
|
||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||
->groupBy([
|
||||
'dividends.symbol',
|
||||
'dividends.date',
|
||||
'dividends.dividend_amount',
|
||||
])
|
||||
->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'");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +125,8 @@ class Holding extends Model
|
||||
*/
|
||||
public function splits()
|
||||
{
|
||||
return $this->hasMany(Split::class, 'symbol', 'symbol');
|
||||
return $this->hasMany(Split::class, 'symbol', 'symbol')
|
||||
->orderBy('date', 'DESC');
|
||||
}
|
||||
|
||||
public function scopePortfolio($query, $portfolio)
|
||||
@@ -91,6 +134,11 @@ class Holding extends Model
|
||||
return $query->where('portfolio_id', $portfolio);
|
||||
}
|
||||
|
||||
public function scopeSymbol($query, $symbol)
|
||||
{
|
||||
return $query->where('symbol', $symbol);
|
||||
}
|
||||
|
||||
public function scopeWithoutWishlists($query) {
|
||||
return $query->join('portfolios', 'portfolios.id', 'holdings.portfolio_id')
|
||||
->where('portfolios.wishlist', 0);
|
||||
@@ -115,11 +163,6 @@ class Holding extends Model
|
||||
->join('market_data', 'market_data.symbol', 'holdings.symbol');
|
||||
}
|
||||
|
||||
public function scopeSymbol($query, $symbol)
|
||||
{
|
||||
return $query->where('symbol', $symbol);
|
||||
}
|
||||
|
||||
// public function refreshDividends()
|
||||
// {
|
||||
// return Dividend::getDividendData($this->attributes['symbol']);
|
||||
|
||||
@@ -25,6 +25,15 @@ class MarketData extends Model
|
||||
'market_cap'
|
||||
];
|
||||
|
||||
protected $attributes = [
|
||||
'market_value' => 0,
|
||||
'fifty_two_week_high' => 0,
|
||||
'fifty_two_week_low' => 0,
|
||||
'forward_pe' => 0,
|
||||
'trailing_pe' => 0,
|
||||
'market_cap' => 0
|
||||
];
|
||||
|
||||
public static function setSplitsHoldingSynced($symbol)
|
||||
{
|
||||
$market_data = self::where('symbol', $symbol)->get()->first();
|
||||
|
||||
@@ -126,14 +126,14 @@ class Transaction extends Model
|
||||
*/
|
||||
public function ensureCostBasisIsAddedToSale()
|
||||
{
|
||||
$holding = Holding::firstOrNew([
|
||||
$average_cost_basis = Transaction::where([
|
||||
'portfolio_id' => $this->portfolio_id,
|
||||
'symbol' => $this->symbol
|
||||
],[
|
||||
'average_cost_basis' => null
|
||||
]);
|
||||
'symbol' => $this->symbol,
|
||||
'transaction_type' => 'BUY',
|
||||
])->whereDate('date', '<=', $this->date)
|
||||
->average('cost_basis');
|
||||
|
||||
$this->cost_basis = $holding->average_cost_basis ?? 0;
|
||||
$this->cost_basis = $average_cost_basis ?? 0;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
+6
-9
@@ -77,15 +77,12 @@ class User extends Authenticatable
|
||||
->withAggregate('market_data', 'fifty_two_week_low')
|
||||
->withAggregate('market_data', 'fifty_two_week_high')
|
||||
->withAggregate('market_data', 'updated_at')
|
||||
->selectRaw('COALESCE(transactions.cost_basis * transactions.quantity, 0) AS total_cost_basis')
|
||||
->selectRaw('COALESCE(market_data.market_value * transactions.quantity, 0) AS total_market_value')
|
||||
->selectRaw('COALESCE((market_data.market_value - transactions.cost_basis) * transactions.quantity, 0) AS market_gain_dollars')
|
||||
->selectRaw('COALESCE(((market_data.market_value - transactions.cost_basis) / transactions.cost_basis), 0) AS market_gain_percent')
|
||||
->selectRaw('
|
||||
CASE
|
||||
WHEN transaction_type = \'SELL\'
|
||||
THEN COALESCE(transactions.sale_price - transactions.cost_basis, 0)
|
||||
ELSE COALESCE(market_data.market_value - transactions.cost_basis, 0)
|
||||
END AS gain_dollars')
|
||||
->join('market_data', 'transactions.symbol', 'market_data.symbol');;
|
||||
}
|
||||
|
||||
public function daily_change(): HasManyDeep
|
||||
{
|
||||
return $this->hasManyDeep(DailyChange::class, ['portfolio_user', Portfolio::class]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user