wip
improve import / export flow and clean up relationships
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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']])
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user