Files
investbrain/app/Models/Transaction.php
T

194 lines
4.9 KiB
PHP
Raw Normal View History

2024-08-10 13:30:19 -05:00
<?php
namespace App\Models;
use App\Models\MarketData;
2024-08-24 22:19:40 -05:00
use Illuminate\Support\Arr;
2024-08-10 13:30:19 -05:00
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',
2024-09-05 21:21:18 -05:00
'portfolio_id',
2024-08-10 13:30:19 -05:00
'transaction_type',
'quantity',
'cost_basis',
2024-09-09 19:39:38 -05:00
'sale_price',
'split',
2024-10-18 20:46:22 -05:00
'reinvested_dividend'
2024-08-10 13:30:19 -05:00
];
protected $hidden = [];
protected $casts = [
'date' => 'datetime',
'split' => 'boolean',
2024-10-18 20:46:22 -05:00
'reinvested_dividend' => 'boolean'
2024-08-10 13:30:19 -05:00
];
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-30 21:58:38 -05:00
$transaction->syncToHolding();
2024-08-21 20:42:32 -05:00
2024-08-30 20:22:28 -05:00
$transaction->refreshMarketData();
cache()->forget('portfolio-metrics-' . $transaction->portfolio_id);
2024-08-15 21:35:43 -05:00
});
static::deleted(function ($transaction) {
2024-08-30 21:58:38 -05:00
$transaction->syncToHolding();
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);
}
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')
2024-08-28 15:19:20 -05:00
->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', 'transactions.symbol', 'market_data.symbol');
}
2024-08-10 13:30:19 -05:00
public function scopePortfolio($query, $portfolio)
{
return $query->where('portfolio_id', $portfolio);
}
public function scopeSymbol($query, $symbol)
{
return $query->where('symbol', $symbol);
}
2024-09-06 23:15:43 -05:00
public function scopeBuy($query)
{
return $query->where('transaction_type', 'BUY');
}
public function scopeSell($query)
{
return $query->where('transaction_type', 'SELL');
}
public function scopeBeforeDate($query, $date)
{
return $query->whereDate('date', '<=', $date);
2024-09-06 23:15:43 -05:00
}
2024-08-28 22:06:47 -05:00
public function scopeMyTransactions()
{
return $this->whereHas('portfolio', function ($query) {
$query->whereHas('users', function ($query) {
$query->where('id', auth()->id());
});
});
}
2024-08-10 13:30:19 -05:00
public function refreshMarketData()
{
return MarketData::getMarketData($this->attributes['symbol']);
}
2024-08-17 21:33:09 -05:00
/**
* Writes average cost basis to a sale transaction
*
* @return Transaction
*/
public function ensureCostBasisIsAddedToSale()
{
2024-08-27 21:17:54 -05:00
$average_cost_basis = Transaction::where([
2024-08-17 21:33:09 -05:00
'portfolio_id' => $this->portfolio_id,
2024-08-27 21:17:54 -05:00
'symbol' => $this->symbol,
'transaction_type' => 'BUY',
])->whereDate('date', '<=', $this->date)
->average('cost_basis');
2024-08-17 21:33:09 -05:00
2024-08-27 21:17:54 -05:00
$this->cost_basis = $average_cost_basis ?? 0;
2024-08-17 21:33:09 -05:00
return $this;
}
/**
* Syncs the holding related to this transaction
*
* @return void
*/
2024-08-30 21:58:38 -05:00
public function syncToHolding() {
2024-08-24 22:19:40 -05:00
2024-08-29 22:26:04 -05:00
// if symbol name changed, sync previous symbol too
2024-08-24 22:19:40 -05:00
if (Arr::has($this->changes, 'symbol')) {
$temp = new Transaction;
$temp->symbol = $this->original['symbol'];
$temp->portfolio_id = $this->portfolio_id;
2024-08-30 21:58:38 -05:00
$temp->syncToHolding();
2024-08-24 22:19:40 -05:00
}
2024-08-17 21:33:09 -05:00
// get the holding for a symbol and portfolio (or create one)
2024-09-05 18:36:45 -05:00
Holding::firstOrNew([
2024-08-17 21:33:09 -05:00
'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,
2024-09-09 19:39:38 -05:00
'splits_synced_at' => now(),
2024-09-05 18:36:45 -05:00
])->syncTransactionsAndDividends();
2024-08-17 21:33:09 -05:00
}
2024-08-10 13:30:19 -05:00
}