wip
This commit is contained in:
@@ -6,6 +6,7 @@ use Exception;
|
||||
use App\Models\DailyChange;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use App\Imports\ValidatesPortfolioPermissions;
|
||||
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
@@ -13,44 +14,34 @@ use Maatwebsite\Excel\Concerns\WithChunkReading;
|
||||
|
||||
class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithChunkReading
|
||||
{
|
||||
use ValidatesPortfolioPermissions;
|
||||
|
||||
public function collection(Collection $dailyChanges)
|
||||
{
|
||||
$this->validatePortfolioPermissions($dailyChanges);
|
||||
|
||||
$portfolios = auth()->user()->portfolios->pluck('id');
|
||||
|
||||
$dailyChanges->pluck('portfolio_id')->unique()->each(function($portfolio) use ($portfolios) {
|
||||
foreach ($dailyChanges as $dailyChange) {
|
||||
|
||||
if (!$portfolios->contains($portfolio)) {
|
||||
|
||||
throw new Exception('You do not have permission to access that portfolio.');
|
||||
}
|
||||
});
|
||||
|
||||
DailyChange::withoutEvents(function () use ($dailyChanges) {
|
||||
|
||||
foreach ($dailyChanges as $dailyChange) {
|
||||
|
||||
DailyChange::updateOrCreate([
|
||||
'date' => $dailyChange['date'],
|
||||
'portfolio_id' => $dailyChange['portfolio_id'],
|
||||
],[
|
||||
'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'],
|
||||
]);
|
||||
}
|
||||
});
|
||||
DailyChange::updateOrCreate([
|
||||
'date' => $dailyChange['date'],
|
||||
'portfolio_id' => $dailyChange['portfolio_id'],
|
||||
],[
|
||||
'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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'portfolio_id' => ['required'],
|
||||
'portfolio_id' => ['required', 'exists:portfolios,id'],
|
||||
'date' => ['required', 'date'],
|
||||
'total_market_value' => ['sometimes', 'nullable', 'numeric'],
|
||||
'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'],
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
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;
|
||||
@@ -14,24 +13,19 @@ class PortfoliosSheet implements ToCollection, WithValidation, WithHeadingRow, S
|
||||
{
|
||||
public function collection(Collection $portfolios)
|
||||
{
|
||||
Portfolio::withoutEvents(function () use ($portfolios) {
|
||||
|
||||
foreach ($portfolios as $portfolio) {
|
||||
foreach ($portfolios as $portfolio) {
|
||||
|
||||
Portfolio::unguard();
|
||||
|
||||
auth()->user()->portfolios()
|
||||
->where(['id' => $portfolio['portfolio_id']])
|
||||
->orWhere(['title' => $portfolio['title']])
|
||||
->firstOr(function () use ($portfolio) {
|
||||
|
||||
return Portfolio::make()->forceFill([
|
||||
'id' => $portfolio['portfolio_id'] ?? null,
|
||||
'title' => $portfolio['title'],
|
||||
'wishlist' => $portfolio['wishlist'] ?? false,
|
||||
'notes' => $portfolio['notes'],
|
||||
])->save();
|
||||
});
|
||||
}
|
||||
});
|
||||
Portfolio::updateOrCreate([
|
||||
'id' => $portfolio['portfolio_id']
|
||||
], [
|
||||
'id' => $portfolio['portfolio_id'] ?? null,
|
||||
'title' => $portfolio['title'],
|
||||
'wishlist' => $portfolio['wishlist'] ?? false,
|
||||
'notes' => $portfolio['notes'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
namespace App\Imports\Sheets;
|
||||
|
||||
use App\Models\Holding;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use App\Imports\ValidatesPortfolioPermissions;
|
||||
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
@@ -13,16 +13,20 @@ use Maatwebsite\Excel\Concerns\WithChunkReading;
|
||||
|
||||
class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation, SkipsEmptyRows, WithChunkReading
|
||||
{
|
||||
use ValidatesPortfolioPermissions;
|
||||
|
||||
public function collection(Collection $transactions)
|
||||
{
|
||||
$this->validatePortfolioPermissions($transactions);
|
||||
|
||||
Transaction::withoutEvents(function () use ($transactions) {
|
||||
|
||||
foreach ($transactions->sortBy('date') as $transaction) {
|
||||
|
||||
$transaction = Transaction::where('id', $transaction['transaction_id'])
|
||||
Transaction::where('id', $transaction['transaction_id'])
|
||||
->firstOr(function () use ($transaction) {
|
||||
|
||||
return Transaction::make()->forceFill([
|
||||
$transaction = Transaction::make()->forceFill([
|
||||
'id' => $transaction['transaction_id'],
|
||||
'symbol' => $transaction['symbol'],
|
||||
'portfolio_id' => $transaction['portfolio_id'],
|
||||
@@ -32,21 +36,16 @@ class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation,
|
||||
'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
|
||||
]);
|
||||
$transaction->save();
|
||||
|
||||
return $transaction;
|
||||
})
|
||||
->syncHolding();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
|
||||
Reference in New Issue
Block a user