wip
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Imports\Sheets;
|
||||
|
||||
use App\Models\Holding;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
@@ -12,28 +13,40 @@ use Maatwebsite\Excel\Concerns\WithChunkReading;
|
||||
|
||||
class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithChunkReading
|
||||
{
|
||||
// use Importable;
|
||||
|
||||
public function collection(Collection $transactions)
|
||||
{
|
||||
foreach ($transactions->sortBy('date') as $transaction) {
|
||||
Transaction::withoutEvents(function () use ($transactions) {
|
||||
|
||||
Transaction::where('id', $transaction['transaction_id'])
|
||||
->firstOr(function () use ($transaction) {
|
||||
foreach ($transactions->sortBy('date') as $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();
|
||||
});
|
||||
}
|
||||
$transaction = 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();
|
||||
});
|
||||
|
||||
Holding::firstOrCreate([
|
||||
'symbol' => $transaction['symbol'],
|
||||
'portfolio_id' => $transaction['portfolio_id'],
|
||||
], [
|
||||
'quantity' => 0,
|
||||
'average_cost_basis' => 0,
|
||||
'total_cost_basis' => 0,
|
||||
'realized_gain_dollars' => 0,
|
||||
'dividends_earned' => 0
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
|
||||
Reference in New Issue
Block a user