Files
investbrain/app/Actions/EnsureCostBasisAddedToSale.php
T

36 lines
1.1 KiB
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') {
2025-07-21 20:28:39 -05:00
$cost_basis = Transaction::where([
2025-04-09 19:25:15 -05:00
'portfolio_id' => $model->portfolio_id,
'symbol' => $model->symbol,
'transaction_type' => 'BUY',
])->whereDate('date', '<=', $model->date)
2025-07-21 20:28:39 -05:00
->selectRaw('SUM(transactions.cost_basis * transactions.quantity) as total_cost_basis')
->selectRaw('SUM(transactions.quantity) as total_quantity')
->first();
$average_cost_basis = empty($cost_basis->total_quantity)
? 0
: $cost_basis->total_cost_basis / $cost_basis->total_quantity;
2025-04-09 19:25:15 -05:00
$model->cost_basis = $average_cost_basis ?? 0;
}
return $next($model);
}
}