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

65 lines
2.0 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;
2024-10-24 14:48:24 -05:00
use App\Models\BackupImport;
2025-01-28 17:14:49 -06:00
use App\Models\Portfolio;
2024-08-07 18:29:23 -05:00
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 PortfoliosSheet implements SkipsEmptyRows, ToCollection, WithEvents, WithHeadingRow, WithValidation
2024-08-07 18:29:23 -05:00
{
2024-10-24 14:48:24 -05:00
public function __construct(
public BackupImport $backupImport
2025-01-28 17:14:49 -06:00
) {}
2024-10-24 14:48:24 -05:00
public function registerEvents(): array
{
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 portfolios...'),
]);
DB::beginTransaction();
2025-01-28 17:14:49 -06:00
},
2024-10-24 14:48:24 -05:00
];
}
2024-08-07 18:29:23 -05:00
public function collection(Collection $portfolios)
{
2024-09-18 20:14:30 -05:00
foreach ($portfolios as $index => $portfolio) {
2024-10-24 14:48:24 -05:00
2024-10-28 20:48:52 -05:00
Portfolio::unguard(); // ensures we can set an owner for the portfolio
2024-08-07 18:29:23 -05:00
2024-10-24 14:48:24 -05:00
$portfolio = Portfolio::fullAccess($this->backupImport->user_id)->updateOrCreate([
2025-01-28 17:14:49 -06:00
'id' => $portfolio['portfolio_id'],
2024-08-30 20:22:28 -05:00
], [
'id' => $portfolio['portfolio_id'] ?? null,
'title' => $portfolio['title'],
'wishlist' => $portfolio['wishlist'] ?? false,
'notes' => $portfolio['notes'],
'owner_id' => $this->backupImport->user_id,
2024-08-30 20:22:28 -05:00
]);
}
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' => ['sometimes', 'nullable', 'uuid'],
2024-08-29 21:39:59 -05:00
'title' => ['required', 'string'],
'wishlist' => ['sometimes', 'nullable', 'boolean'],
'notes' => ['sometimes', 'nullable', 'string'],
];
}
2024-08-07 18:29:23 -05:00
}