improve import / export flow and clean up relationships
This commit is contained in:
hackerESQ
2024-08-28 22:06:47 -05:00
parent e684c1f4a3
commit 69c43dc41f
15 changed files with 104 additions and 74 deletions
+3 -1
View File
@@ -20,8 +20,10 @@ class BackupImport implements WithMultipleSheets
{
return [
'Portfolios' => new PortfoliosSheet,
// 'Transactions' => new TransactionsSheet,
'Transactions' => new TransactionsSheet,
// 'Daily Changes' => new DailyChangesSheet,
];
// Can listen for AfterSheet to run clean up afterwards
}
}
+14 -12
View File
@@ -15,23 +15,25 @@ class DailyChangesSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
public function collection(Collection $dailyChanges)
{
foreach ($dailyChanges->sortBy('date') as $row) {
if ($row['user'] != auth()->user()->id) {
foreach ($dailyChanges as $dailyChange) {
if ($dailyChange['user'] != auth()->user()->id) {
throw new Exception('Can\'t do that.');
}
DailyChange::updateOrCreate([
'date' => $row['date'],
'user_id' => $row['user'],
'date' => $dailyChange['date'],
'portfolio_id' => $dailyChange['portfolio_id'],
],[
'user_id' => $row['user'],
'date' => $row['date'],
'total_market_value' => $row['total_market_value'],
'total_cost_basis' => $row['total_cost_basis'],
'total_gain' => $row['total_gain'],
'total_dividends_earned' => $row['total_dividends_earned'],
'realized_gains' => $row['realized_gains'],
'notes' => $row['notes'],
'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'],
]);
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ class PortfoliosSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
public function collection(Collection $portfolios)
{
foreach ($portfolios->sortBy('date') as $portfolio) {
foreach ($portfolios as $portfolio) {
auth()->user()->portfolios()
->where(['id' => $portfolio['id']])
+23 -15
View File
@@ -7,28 +7,36 @@ use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithChunkReading;
class TransactionsSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows
class TransactionsSheet implements ToCollection, WithHeadingRow, SkipsEmptyRows, WithChunkReading
{
// use Importable;
public function collection(Collection $transactions)
{
foreach ($transactions->sortBy('date') as $row) {
foreach ($transactions as $transaction) {
Transaction::updateOrCreate([
'id' => $row['id'],
],[
'id' => $row['id'],
'symbol' => $row['symbol'],
'portfolio_id' => $row['portfolio'],
'transaction_type' => $row['transaction'],
'quantity' => $row['quantity'],
'cost_basis' => $row['cost_basis'] ?? 0,
'sale_price' => $row['sale_price'],
'split' => $row['split'] ?? null,
'date' => $row['date'],
]);
Transaction::where('id', $transaction['transaction_id'])
->firstOr(function () use ($transaction) {
return Transaction::make()->forceFill([
'id' => $transaction['transaction_id'],
'symbol' => $transaction['symbol'],
'portfolio_id' => $transaction['portfolio_id'],
'transaction_type' => $transaction['transaction_type'],
'quantity' => $transaction['quantity'],
'cost_basis' => $transaction['cost_basis'] ?? 0,
'sale_price' => $transaction['sale_price'],
'split' => $transaction['split'] ?? null,
'date' => $transaction['date'],
])->save();
});
}
}
public function chunkSize(): int
{
return 500;
}
}