Files
investbrain/app/Imports/Sheets/DailyChangesSheet.php
T

63 lines
1.9 KiB
PHP
Raw Normal View History

2024-08-07 18:29:23 -05:00
<?php
namespace App\Imports\Sheets;
2024-08-29 23:36:44 -05:00
use App\Models\DailyChange;
2024-08-07 18:29:23 -05:00
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
2024-08-30 20:22:28 -05:00
use App\Imports\ValidatesPortfolioPermissions;
2024-08-07 18:29:23 -05:00
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
2024-08-29 21:39:59 -05:00
use Maatwebsite\Excel\Concerns\WithValidation;
2024-08-29 23:36:44 -05:00
use Maatwebsite\Excel\Concerns\WithChunkReading;
2024-08-07 18:29:23 -05:00
2024-08-29 23:36:44 -05:00
class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithChunkReading
2024-08-07 18:29:23 -05:00
{
2024-08-30 20:22:28 -05:00
use ValidatesPortfolioPermissions;
2024-08-07 18:29:23 -05:00
public function collection(Collection $dailyChanges)
{
2024-08-30 20:22:28 -05:00
$this->validatePortfolioPermissions($dailyChanges);
2024-10-18 21:34:02 -05:00
$chunkSize = 180;
$dailyChanges->chunk($chunkSize)->each(function ($chunk) {
DailyChange::upsert(
$chunk->toArray(),
[
'portfolio_id',
'date'
],
[
'total_market_value',
'total_cost_basis',
'total_gain',
'total_dividends_earned',
'realized_gains',
'annotation'
]
);
});
2024-08-07 18:29:23 -05:00
}
2024-08-29 21:39:59 -05:00
public function rules(): array
{
return [
2024-08-30 20:22:28 -05:00
'portfolio_id' => ['required', 'exists:portfolios,id'],
2024-08-29 21:39:59 -05:00
'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'],
];
}
2024-08-29 23:36:44 -05:00
public function chunkSize(): int
{
return 500;
}
2024-08-07 18:29:23 -05:00
}