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

98 lines
3.2 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' => __('Importing daily changes...'),
]);
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
{
$dailyChanges->chunk($this->batchSize())->each(function ($chunk) {
$this->validatePortfolioAccess($chunk);
// have to cast to native values
$chunk = $chunk->map(function ($dailyChange) {
return [
'total_market_value' => $dailyChange['total_market_value'],
'total_cost_basis' => $dailyChange['total_cost_basis'],
'total_gain' => $dailyChange['total_gain'],
'total_dividends_earned' => $dailyChange['total_dividends_earned'],
'realized_gains' => $dailyChange['realized_gains'],
'annotation' => $dailyChange['annotation'],
'portfolio_id' => $dailyChange['portfolio_id'],
2025-01-28 17:14:49 -06:00
'date' => Carbon::parse($dailyChange['date'])->format('Y-m-d'),
];
});
DailyChange::upsert(
$chunk->toArray(),
['portfolio_id', 'date'],
[
'total_market_value',
'total_cost_basis',
'total_gain',
'total_dividends_earned',
'realized_gains',
'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'],
'total_market_value' => ['sometimes', 'nullable', 'numeric'],
'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'],
'total_gain' => ['sometimes', 'nullable', 'numeric'],
2024-10-18 21:59:35 -05:00
'total_dividends_earned' => ['sometimes', 'nullable', 'min:0', 'numeric'],
2024-08-29 21:39:59 -05:00
'realized_gains' => ['sometimes', 'nullable', 'numeric'],
'annotation' => ['sometimes', 'nullable', 'string'],
];
}
2024-08-07 18:29:23 -05:00
}