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-10-18 21:59:35 -05:00
|
|
|
use Illuminate\Support\Carbon;
|
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:59:35 -05:00
|
|
|
$chunkSize = 500;
|
2024-10-18 21:34:02 -05:00
|
|
|
|
|
|
|
|
$dailyChanges->chunk($chunkSize)->each(function ($chunk) {
|
2024-10-18 21:59:35 -05:00
|
|
|
|
|
|
|
|
// have to manually format dates since we're doing a raw upsert
|
|
|
|
|
$chunk = $chunk->map(function ($daily) {
|
|
|
|
|
|
|
|
|
|
$daily['date'] = Carbon::parse($daily['date'])->format('Y-m-d');
|
|
|
|
|
|
|
|
|
|
return $daily;
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-18 21:34:02 -05:00
|
|
|
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'],
|
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-29 23:36:44 -05:00
|
|
|
|
|
|
|
|
public function chunkSize(): int
|
|
|
|
|
{
|
|
|
|
|
return 500;
|
|
|
|
|
}
|
2024-08-07 18:29:23 -05:00
|
|
|
}
|