2024-08-10 13:30:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-28 17:33:54 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2024-08-10 13:30:19 -05:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
2025-04-09 19:25:15 -05:00
|
|
|
use App\Actions\ConvertToMarketDataCurrency;
|
|
|
|
|
use App\Actions\CopyToBaseCurrency;
|
|
|
|
|
use App\Actions\EnsureCostBasisAddedToSale;
|
|
|
|
|
use App\Casts\BaseCurrency;
|
|
|
|
|
use App\Traits\HasMarketData;
|
2025-01-28 19:03:06 -06:00
|
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
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;
|
2025-01-28 17:14:49 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2025-01-28 19:03:06 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2025-01-28 17:14:49 -06:00
|
|
|
use Illuminate\Support\Arr;
|
2025-04-09 19:25:15 -05:00
|
|
|
use Illuminate\Support\Facades\Pipeline;
|
2024-08-10 13:30:19 -05:00
|
|
|
|
|
|
|
|
class Transaction extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2025-04-09 19:25:15 -05:00
|
|
|
use HasMarketData;
|
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',
|
2025-04-09 19:25:15 -05:00
|
|
|
'currency',
|
2024-08-10 13:30:19 -05:00
|
|
|
'quantity',
|
|
|
|
|
'cost_basis',
|
2024-09-09 19:39:38 -05:00
|
|
|
'sale_price',
|
|
|
|
|
'split',
|
2025-01-28 17:14:49 -06:00
|
|
|
'reinvested_dividend',
|
2024-08-10 13:30:19 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $hidden = [];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'date' => 'datetime',
|
|
|
|
|
'split' => 'boolean',
|
2025-01-28 17:14:49 -06:00
|
|
|
'reinvested_dividend' => 'boolean',
|
2025-04-09 19:25:15 -05:00
|
|
|
'quantity' => 'float',
|
|
|
|
|
'cost_basis' => 'float',
|
|
|
|
|
'sale_price' => 'float',
|
|
|
|
|
'cost_basis_base' => BaseCurrency::class,
|
|
|
|
|
'sale_price_base' => BaseCurrency::class,
|
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
|
|
|
|
2025-04-09 19:25:15 -05:00
|
|
|
$transaction = Pipeline::send($transaction)
|
|
|
|
|
->through([
|
|
|
|
|
ConvertToMarketDataCurrency::class,
|
|
|
|
|
EnsureCostBasisAddedToSale::class,
|
|
|
|
|
CopyToBaseCurrency::class,
|
|
|
|
|
])
|
|
|
|
|
->then(fn (Transaction $transaction) => $transaction);
|
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
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
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
|
|
|
|
2025-01-28 17:14:49 -06: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 portfolio
|
2024-08-10 13:30:19 -05:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-01-28 19:03:06 -06:00
|
|
|
public function portfolio(): BelongsTo
|
2024-08-10 13:30:19 -05:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Portfolio::class);
|
|
|
|
|
}
|
2024-08-28 15:19:20 -05:00
|
|
|
|
2025-01-28 19:03:06 -06:00
|
|
|
public function scopeWithMarketData($query): Builder
|
2024-08-28 15:19:20 -05:00
|
|
|
{
|
2024-08-28 23:32:01 -05:00
|
|
|
return $query->withAggregate('market_data', 'name')
|
2025-01-28 17:14:49 -06: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-28 15:19:20 -05:00
|
|
|
}
|
2025-01-28 17:14:49 -06:00
|
|
|
|
2025-01-28 19:03:06 -06:00
|
|
|
public function scopePortfolio($query, $portfolio): Builder
|
2024-08-10 13:30:19 -05:00
|
|
|
{
|
|
|
|
|
return $query->where('portfolio_id', $portfolio);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 19:03:06 -06:00
|
|
|
public function scopeSymbol($query, $symbol): Builder
|
2024-08-10 13:30:19 -05:00
|
|
|
{
|
|
|
|
|
return $query->where('symbol', $symbol);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 19:03:06 -06:00
|
|
|
public function scopeBuy($query): Builder
|
2024-09-06 23:15:43 -05:00
|
|
|
{
|
|
|
|
|
return $query->where('transaction_type', 'BUY');
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 19:03:06 -06:00
|
|
|
public function scopeSell($query): Builder
|
2024-09-06 23:15:43 -05:00
|
|
|
{
|
|
|
|
|
return $query->where('transaction_type', 'SELL');
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 19:03:06 -06:00
|
|
|
public function scopeBeforeDate($query, $date): Builder
|
2024-09-06 23:15:43 -05:00
|
|
|
{
|
2024-11-07 18:20:53 -06:00
|
|
|
return $query->whereDate('date', '<=', $date);
|
2024-09-06 23:15:43 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 19:03:06 -06:00
|
|
|
public function scopeMyTransactions(): Builder
|
2024-08-28 22:06:47 -05:00
|
|
|
{
|
|
|
|
|
return $this->whereHas('portfolio', function ($query) {
|
|
|
|
|
$query->whereHas('users', function ($query) {
|
|
|
|
|
$query->where('id', auth()->id());
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 21:33:09 -05:00
|
|
|
/**
|
|
|
|
|
* Syncs the holding related to this transaction
|
|
|
|
|
*/
|
2025-01-28 19:03:06 -06:00
|
|
|
public function syncToHolding(): void
|
2025-01-28 17:14:49 -06:00
|
|
|
{
|
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,
|
2025-01-28 17:14:49 -06:00
|
|
|
'symbol' => $this->symbol,
|
2024-08-17 21:33:09 -05:00
|
|
|
], [
|
|
|
|
|
'portfolio_id' => $this->portfolio_id,
|
|
|
|
|
'symbol' => $this->symbol,
|
|
|
|
|
'quantity' => $this->quantity,
|
2025-04-09 19:25:15 -05:00
|
|
|
'average_cost_basis' => $this->cost_basis_base,
|
|
|
|
|
'total_cost_basis' => $this->quantity * $this->cost_basis_base,
|
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
|
|
|
}
|
2025-01-28 17:14:49 -06:00
|
|
|
}
|