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

89 lines
2.6 KiB
PHP
Raw Normal View History

2024-08-07 18:29:23 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-08-07 18:29:23 -05:00
namespace App\Imports\Sheets;
use App\Imports\ValidatesPortfolioAccess;
2024-10-24 14:48:24 -05:00
use App\Models\BackupImport;
2025-01-28 17:14:49 -06:00
use App\Models\DailyChange;
2024-10-18 21:59:35 -05:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
2024-10-24 14:48:24 -05:00
use Illuminate\Support\Facades\DB;
2024-08-07 18:29:23 -05:00
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
2025-01-28 17:14:49 -06:00
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
2024-08-07 18:29:23 -05:00
use Maatwebsite\Excel\Concerns\WithHeadingRow;
2024-08-29 21:39:59 -05:00
use Maatwebsite\Excel\Concerns\WithValidation;
2025-01-28 17:14:49 -06:00
use Maatwebsite\Excel\Events\BeforeSheet;
2024-08-07 18:29:23 -05:00
2025-01-28 17:14:49 -06:00
class DailyChangesSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadingRow, WithValidation
2024-08-07 18:29:23 -05:00
{
use ValidatesPortfolioAccess;
2024-10-24 14:48:24 -05:00
public function __construct(
public BackupImport $backupImport
2025-01-28 17:14:49 -06:00
) {}
2024-08-30 20:22:28 -05:00
2024-10-24 14:48:24 -05:00
public function registerEvents(): array
2024-08-07 18:29:23 -05:00
{
2024-10-24 14:48:24 -05:00
return [
2025-01-28 17:14:49 -06:00
BeforeSheet::class => function (BeforeSheet $event) {
2024-10-24 14:48:24 -05:00
DB::commit();
$this->backupImport->update([
'message' => __('Preparing to import daily changes...'),
2024-10-24 14:48:24 -05:00
]);
DB::beginTransaction();
2025-01-28 17:14:49 -06:00
},
2024-10-24 14:48:24 -05:00
];
}
2025-01-28 17:14:49 -06:00
public function collection(Collection $dailyChanges)
2024-10-24 14:48:24 -05:00
{
$totalBatches = count($dailyChanges) / $this->batchSize();
$dailyChanges->chunk($this->batchSize())->each(function ($chunk, $index) use ($totalBatches) {
$this->validatePortfolioAccess($chunk);
$this->backupImport->update([
'message' => __('Importing daily changes (Batch :currentBatch of :totalBatches)...', ['currentBatch' => $index + 1, 'totalBatches' => $totalBatches]),
]);
// have to cast to native values
$chunk = $chunk->map(function ($dailyChange) {
return [
'annotation' => $dailyChange['annotation'],
'portfolio_id' => $dailyChange['portfolio_id'],
2025-04-09 19:25:15 -05:00
'date' => Carbon::parse($dailyChange['date'])->toDateString(),
];
});
DailyChange::upsert(
$chunk->toArray(),
['portfolio_id', 'date'],
[
'annotation',
'portfolio_id',
2025-01-28 17:14:49 -06:00
'date',
]
);
});
2024-10-24 14:48:24 -05:00
}
2024-10-18 21:59:35 -05:00
public function batchSize(): int
2024-10-24 14:48:24 -05:00
{
2024-10-28 21:11:08 -05:00
return 500;
2024-08-07 18:29:23 -05:00
}
2024-08-29 21:39:59 -05:00
public function rules(): array
{
return [
2025-01-28 17:14:49 -06:00
'portfolio_id' => ['required', 'uuid'],
2024-08-29 21:39:59 -05:00
'date' => ['required', 'date'],
'annotation' => ['sometimes', 'nullable', 'string'],
];
}
2024-08-07 18:29:23 -05:00
}