2024-08-07 18:29:23 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Imports\Sheets;
|
|
|
|
|
|
2024-10-28 18:35:14 -05:00
|
|
|
use App\Imports\ValidatesPortfolioAccess;
|
2024-08-29 23:36:44 -05:00
|
|
|
use App\Models\DailyChange;
|
2024-10-24 14:48:24 -05:00
|
|
|
use App\Models\BackupImport;
|
2024-10-18 21:59:35 -05:00
|
|
|
use Illuminate\Support\Carbon;
|
2024-10-28 18:35:14 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2024-10-24 14:48:24 -05:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Maatwebsite\Excel\Events\BeforeSheet;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
2024-10-28 18:35:14 -05:00
|
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
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-07 18:29:23 -05:00
|
|
|
|
2024-10-28 18:35:14 -05:00
|
|
|
class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithEvents
|
2024-08-07 18:29:23 -05:00
|
|
|
{
|
2024-10-28 18:35:14 -05:00
|
|
|
use ValidatesPortfolioAccess;
|
|
|
|
|
|
2024-10-24 14:48:24 -05:00
|
|
|
public function __construct(
|
|
|
|
|
public BackupImport $backupImport
|
|
|
|
|
) { }
|
2024-08-30 20:22:28 -05:00
|
|
|
|
2024-10-24 14:48:24 -05:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function registerEvents(): array
|
2024-08-07 18:29:23 -05:00
|
|
|
{
|
2024-10-24 14:48:24 -05:00
|
|
|
return [
|
|
|
|
|
BeforeSheet::class => function(BeforeSheet $event) {
|
|
|
|
|
DB::commit();
|
|
|
|
|
$this->backupImport->update([
|
|
|
|
|
'message' => __('Importing daily changes...'),
|
|
|
|
|
]);
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 18:35:14 -05:00
|
|
|
public function collection(Collection $dailyChanges)
|
2024-10-24 14:48:24 -05:00
|
|
|
{
|
2024-10-28 18:35:14 -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'],
|
|
|
|
|
'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',
|
|
|
|
|
'date'
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
});
|
2024-10-24 14:48:24 -05:00
|
|
|
}
|
2024-10-18 21:59:35 -05:00
|
|
|
|
2024-10-28 18:35:14 -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 [
|
2024-10-28 19:20:52 -05: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
|
|
|
}
|