Files
investbrain/app/Actions/EnsureCostBasisAddedToSale.php
T

30 lines
753 B
PHP
Raw Normal View History

2025-04-09 19:25:15 -05:00
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Models\Transaction;
use Illuminate\Database\Eloquent\Model;
class EnsureCostBasisAddedToSale
{
public function __invoke(Model $model, callable $next)
{
// cost basis is required for sales to calculate realized gains
if ($model->transaction_type == 'SELL') {
$average_cost_basis = Transaction::where([
'portfolio_id' => $model->portfolio_id,
'symbol' => $model->symbol,
'transaction_type' => 'BUY',
])->whereDate('date', '<=', $model->date)
->average('cost_basis');
$model->cost_basis = $average_cost_basis ?? 0;
}
return $next($model);
}
}