Files
investbrain/app/Imports/Sheets/DailyChangesSheet.php
T
2024-08-29 21:39:59 -05:00

62 lines
2.2 KiB
PHP

<?php
namespace App\Imports\Sheets;
use App\Models\DailyChange;
use Exception;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows
{
// use Importable;
public function collection(Collection $dailyChanges)
{
$portfolios = auth()->user()->portfolios->pluck('id');
$dailyChanges->pluck('portfolio_id')->unique()->each(function($portfolio) use ($portfolios) {
if (!$portfolios->contains($portfolio)) {
throw new Exception('You do not have permission to access that portfolio.');
}
});
foreach ($dailyChanges as $dailyChange) {
DailyChange::updateOrCreate([
'date' => $dailyChange['date'],
'portfolio_id' => $dailyChange['portfolio_id'],
],[
'portfolio_id' => $dailyChange['portfolio_id'],
'date' => $dailyChange['date'],
'total_market_value' => $dailyChange['total_market_value'],
'total_cost_basis' => $dailyChange['total_cost_basis'],
'total_gain' => $dailyChange['total_gain'],
'total_dividends' => $dailyChange['total_dividends'],
'realized_gains' => $dailyChange['realized_gains'],
'annotation' => $dailyChange['annotation'],
]);
}
}
public function rules(): array
{
return [
'portfolio_id' => ['required'],
'date' => ['required', 'date'],
'total_market_value' => ['sometimes', 'nullable', 'numeric'],
'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'],
'total_gain' => ['sometimes', 'nullable', 'numeric'],
'total_dividends' => ['sometimes', 'nullable', 'min:0', 'numeric'],
'realized_gains' => ['sometimes', 'nullable', 'numeric'],
'annotation' => ['sometimes', 'nullable', 'string'],
];
}
}