2024-08-10 13:30:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Models\MarketData;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-08-17 21:33:09 -05:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2024-08-15 21:35:43 -05:00
|
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
2024-08-10 13:30:19 -05:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
|
|
|
|
|
class Transaction extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2024-08-15 21:35:43 -05:00
|
|
|
use HasUuids;
|
2024-08-10 13:30:19 -05:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'symbol',
|
|
|
|
|
'date',
|
|
|
|
|
'transaction_type',
|
|
|
|
|
'quantity',
|
|
|
|
|
'cost_basis',
|
2024-08-17 21:33:09 -05:00
|
|
|
'sale_price'
|
2024-08-10 13:30:19 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $hidden = [];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'date' => 'datetime',
|
|
|
|
|
'split' => 'boolean',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected static function boot()
|
|
|
|
|
{
|
|
|
|
|
parent::boot();
|
|
|
|
|
|
2024-08-15 21:35:43 -05:00
|
|
|
static::saving(function ($transaction) {
|
2024-08-10 13:30:19 -05:00
|
|
|
|
2024-08-15 21:35:43 -05:00
|
|
|
if ($transaction->transaction_type == 'SELL') {
|
|
|
|
|
|
2024-08-17 21:33:09 -05:00
|
|
|
$transaction->ensureCostBasisIsAddedToSale();
|
2024-08-15 21:35:43 -05:00
|
|
|
}
|
2024-08-10 13:30:19 -05:00
|
|
|
});
|
|
|
|
|
|
2024-08-15 21:35:43 -05:00
|
|
|
static::saved(function ($transaction) {
|
|
|
|
|
|
2024-08-17 21:33:09 -05:00
|
|
|
$transaction->syncHolding();
|
2024-08-21 20:42:32 -05:00
|
|
|
|
|
|
|
|
cache()->forget('portfolio-metrics-' . $transaction->portfolio_id);
|
2024-08-15 21:35:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
static::deleted(function ($transaction) {
|
|
|
|
|
|
2024-08-17 21:33:09 -05:00
|
|
|
$transaction->syncHolding();
|
2024-08-21 20:42:32 -05:00
|
|
|
|
|
|
|
|
cache()->forget('portfolio-metrics-' . $transaction->portfolio_id);
|
2024-08-10 13:30:19 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 21:33:09 -05:00
|
|
|
/**
|
|
|
|
|
* Ensure transaction symbol is always upper case
|
|
|
|
|
*/
|
|
|
|
|
protected function symbol(): Attribute
|
2024-08-10 13:30:19 -05:00
|
|
|
{
|
2024-08-17 21:33:09 -05:00
|
|
|
return Attribute::make(
|
|
|
|
|
set: fn (string $value) => strtoupper($value)
|
|
|
|
|
);
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-08-17 21:33:09 -05:00
|
|
|
* Related market data for transaction
|
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 portfolio
|
2024-08-10 13:30:19 -05:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function portfolio()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Portfolio::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function scopePortfolio($query, $portfolio)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('portfolio_id', $portfolio);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function scopeSymbol($query, $symbol)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('symbol', $symbol);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function scopeMyTransactions()
|
|
|
|
|
{
|
|
|
|
|
return $this->whereHas('portfolio', function ($query) {
|
|
|
|
|
return $query->whereRelation('users', 'id', auth()->user()->id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function refreshMarketData()
|
|
|
|
|
{
|
|
|
|
|
return MarketData::getMarketData($this->attributes['symbol']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function syncDividendsToHolding()
|
|
|
|
|
{
|
|
|
|
|
return Dividend::syncHoldings(['symbol' => $this->attributes['symbol']]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function refreshDividends()
|
|
|
|
|
{
|
|
|
|
|
return Dividend::getDividendData($this->attributes['symbol']);
|
|
|
|
|
}
|
2024-08-17 21:33:09 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Writes average cost basis to a sale transaction
|
|
|
|
|
*
|
|
|
|
|
* @return Transaction
|
|
|
|
|
*/
|
|
|
|
|
public function ensureCostBasisIsAddedToSale()
|
|
|
|
|
{
|
|
|
|
|
$holding = Holding::firstOrNew([
|
|
|
|
|
'portfolio_id' => $this->portfolio_id,
|
|
|
|
|
'symbol' => $this->symbol
|
|
|
|
|
],[
|
|
|
|
|
'average_cost_basis' => null
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->cost_basis = $holding->average_cost_basis ?? 0;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Syncs the holding related to this transaction
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function syncHolding() {
|
|
|
|
|
// get the holding for a symbol and portfolio (or create one)
|
|
|
|
|
$holding = Holding::firstOrNew([
|
|
|
|
|
'portfolio_id' => $this->portfolio_id,
|
|
|
|
|
'symbol' => $this->symbol
|
|
|
|
|
], [
|
|
|
|
|
'portfolio_id' => $this->portfolio_id,
|
|
|
|
|
'symbol' => $this->symbol,
|
|
|
|
|
'quantity' => $this->quantity,
|
|
|
|
|
'average_cost_basis' => $this->cost_basis,
|
|
|
|
|
'total_cost_basis' => $this->quantity * $this->cost_basis,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// pull existing transaction data
|
|
|
|
|
$query = self::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`')
|
|
|
|
|
->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN (quantity * cost_basis) ELSE 0 END) AS `cost_basis`')
|
|
|
|
|
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN ((sale_price - cost_basis) * quantity) ELSE 0 END) AS `realized_gains`')
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
$total_quantity = $query->qty_purchases - $query->qty_sales;
|
|
|
|
|
$average_cost_basis = $query->qty_purchases > 0
|
|
|
|
|
? $query->cost_basis / $query->qty_purchases
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
|
|
// update holding
|
|
|
|
|
$holding->fill([
|
|
|
|
|
'quantity' => $total_quantity,
|
|
|
|
|
'average_cost_basis' => $average_cost_basis,
|
|
|
|
|
'total_cost_basis' => $total_quantity * $average_cost_basis,
|
2024-08-21 20:42:32 -05:00
|
|
|
'realized_gain_dollars' => $query->realized_gains,
|
2024-08-17 21:33:09 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$holding->save();
|
|
|
|
|
|
|
|
|
|
// load market data while we're here
|
|
|
|
|
// $this->refreshMarketData();
|
|
|
|
|
|
|
|
|
|
// sync dividends to holding
|
|
|
|
|
// $this->syncDividendsToHolding();
|
|
|
|
|
}
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|