diff --git a/app/Exports/Sheets/PortfoliosSheet.php b/app/Exports/Sheets/PortfoliosSheet.php index f352a05..22e70a8 100644 --- a/app/Exports/Sheets/PortfoliosSheet.php +++ b/app/Exports/Sheets/PortfoliosSheet.php @@ -12,7 +12,7 @@ class PortfoliosSheet implements FromCollection, WithHeadings, WithTitle public function headings(): array { return [ - 'ID', + 'Portfolio ID', 'Title', 'Notes', 'Wishlist', diff --git a/app/Imports/BackupImport.php b/app/Imports/BackupImport.php index c722a44..00dfe1a 100644 --- a/app/Imports/BackupImport.php +++ b/app/Imports/BackupImport.php @@ -21,7 +21,7 @@ class BackupImport implements WithMultipleSheets return [ 'Portfolios' => new PortfoliosSheet, 'Transactions' => new TransactionsSheet, - // 'Daily Changes' => new DailyChangesSheet, + 'Daily Changes' => new DailyChangesSheet, ]; // Can listen for AfterSheet to run clean up afterwards diff --git a/app/Imports/Sheets/DailyChangesSheet.php b/app/Imports/Sheets/DailyChangesSheet.php index e9af765..1b242dc 100644 --- a/app/Imports/Sheets/DailyChangesSheet.php +++ b/app/Imports/Sheets/DailyChangesSheet.php @@ -8,19 +8,26 @@ use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\WithHeadingRow; +use Maatwebsite\Excel\Concerns\WithValidation; -class DailyChangesSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows +class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows { // use Importable; public function collection(Collection $dailyChanges) { - foreach ($dailyChanges as $dailyChange) { - if ($dailyChange['user'] != auth()->user()->id) { + $portfolios = auth()->user()->portfolios->pluck('id'); + + $dailyChanges->pluck('portfolio_id')->unique()->each(function($portfolio) use ($portfolios) { - throw new Exception('Can\'t do that.'); + if (!$portfolios->contains($portfolio)) { + + throw new Exception('You do not have permission to access that portfolio.'); } + }); + + foreach ($dailyChanges as $dailyChange) { DailyChange::updateOrCreate([ 'date' => $dailyChange['date'], @@ -37,4 +44,18 @@ class DailyChangesSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows ]); } } + + public function rules(): array + { + return [ + 'portfolio_id' => ['required'], + 'date' => ['required', 'date'], + 'total_market_value' => ['sometimes', 'nullable', 'numeric'], + 'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'], + 'total_gain' => ['sometimes', 'nullable', 'numeric'], + 'total_dividends' => ['sometimes', 'nullable', 'min:0', 'numeric'], + 'realized_gains' => ['sometimes', 'nullable', 'numeric'], + 'annotation' => ['sometimes', 'nullable', 'string'], + ]; + } } diff --git a/app/Imports/Sheets/PortfoliosSheet.php b/app/Imports/Sheets/PortfoliosSheet.php index 59b25a2..a963634 100644 --- a/app/Imports/Sheets/PortfoliosSheet.php +++ b/app/Imports/Sheets/PortfoliosSheet.php @@ -3,12 +3,14 @@ namespace App\Imports\Sheets; use App\Models\Portfolio; +use Illuminate\Validation\Rule; use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\WithHeadingRow; +use Maatwebsite\Excel\Concerns\WithValidation; -class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows +class PortfoliosSheet implements ToCollection, WithValidation, WithHeadingRow, SkipsEmptyRows { // use Importable; @@ -17,12 +19,12 @@ class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows foreach ($portfolios as $portfolio) { auth()->user()->portfolios() - ->where(['id' => $portfolio['id']]) + ->where(['id' => $portfolio['portfolio_id']]) ->orWhere(['title' => $portfolio['title']]) ->firstOr(function () use ($portfolio) { return Portfolio::make()->forceFill([ - 'id' => $portfolio['id'] ?? null, + 'id' => $portfolio['portfolio_id'] ?? null, 'title' => $portfolio['title'], 'wishlist' => $portfolio['wishlist'] ?? false, 'notes' => $portfolio['notes'], @@ -30,4 +32,14 @@ class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows }); } } + + public function rules(): array + { + return [ + 'portfolio_id' => ['sometimes', 'nullable'], + 'title' => ['required', 'string'], + 'wishlist' => ['sometimes', 'nullable', 'boolean'], + 'notes' => ['sometimes', 'nullable', 'string'], + ]; + } } diff --git a/app/Imports/Sheets/TransactionsSheet.php b/app/Imports/Sheets/TransactionsSheet.php index 6ad02ff..310d443 100644 --- a/app/Imports/Sheets/TransactionsSheet.php +++ b/app/Imports/Sheets/TransactionsSheet.php @@ -7,15 +7,16 @@ use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\WithHeadingRow; +use Maatwebsite\Excel\Concerns\WithValidation; use Maatwebsite\Excel\Concerns\WithChunkReading; -class TransactionsSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows, WithChunkReading +class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithChunkReading { // use Importable; public function collection(Collection $transactions) { - foreach ($transactions as $transaction) { + foreach ($transactions->sortBy('date') as $transaction) { Transaction::where('id', $transaction['transaction_id']) ->firstOr(function () use ($transaction) { @@ -35,6 +36,21 @@ class TransactionsSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows, } } + public function rules(): array + { + return [ + 'transaction_id' => ['sometimes', 'nullable'], + 'symbol' => ['required', 'string'], + 'portfolio_id' => ['required', 'exists:portfolios,id'], + 'quantity' => ['required', 'min:0', 'numeric'], + 'transaction_type' => ['required', 'in:BUY,SELL'], + 'date' => ['required', 'date'], + 'quantity' => ['required', 'min:0', 'numeric'], + 'cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'], + 'sale_price' => ['sometimes', 'nullable', 'min:0', 'numeric'], + ]; + } + public function chunkSize(): int { return 500; diff --git a/resources/views/livewire/import-portfolios-field.blade.php b/resources/views/livewire/import-portfolios-field.blade.php index 5b85a5f..49bcc9e 100644 --- a/resources/views/livewire/import-portfolios-field.blade.php +++ b/resources/views/livewire/import-portfolios-field.blade.php @@ -20,7 +20,14 @@ new class extends Component { $this->validate(); - $import = (new BackupImport)->import($this->file); + try { + + $import = (new BackupImport)->import($this->file); + + } catch (\Throwable $e) { + dd($e); + return $this->error($e->getMessage()); + } $this->success(__('Successfully imported!'));