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

41 lines
1.3 KiB
PHP
Raw Normal View History

2024-08-07 18:29:23 -05:00
<?php
namespace App\Imports\Sheets;
use App\Models\DailyChange;
use Exception;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class DailyChangesSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
{
// use Importable;
public function collection(Collection $dailyChanges)
{
2024-08-28 22:06:47 -05:00
foreach ($dailyChanges as $dailyChange) {
if ($dailyChange['user'] != auth()->user()->id) {
2024-08-07 18:29:23 -05:00
throw new Exception('Can\'t do that.');
}
DailyChange::updateOrCreate([
2024-08-28 22:06:47 -05:00
'date' => $dailyChange['date'],
'portfolio_id' => $dailyChange['portfolio_id'],
2024-08-07 18:29:23 -05:00
],[
2024-08-28 22:06:47 -05:00
'portfolio_id' => $dailyChange['portfolio_id'],
'date' => $dailyChange['date'],
'total_market_value' => $dailyChange['total_market_value'],
'total_cost_basis' => $dailyChange['total_cost_basis'],
'total_gain' => $dailyChange['total_gain'],
'total_dividends' => $dailyChange['total_dividends'],
'realized_gains' => $dailyChange['realized_gains'],
'annotation' => $dailyChange['annotation'],
2024-08-07 18:29:23 -05:00
]);
}
}
}