wip
This commit is contained in:
@@ -2,12 +2,10 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Dividend;
|
||||
use App\Models\Holding;
|
||||
use App\Models\MarketData;
|
||||
use App\Models\Dividend;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
|
||||
class RefreshHoldingData extends Command
|
||||
{
|
||||
@@ -48,44 +46,7 @@ class RefreshHoldingData extends Command
|
||||
foreach ($holdings as $holding) {
|
||||
$this->line('Refreshing ' . $holding->symbol);
|
||||
|
||||
$query = Transaction::where([
|
||||
'portfolio_id' => $holding->portfolio_id,
|
||||
'symbol' => $holding->symbol,
|
||||
])->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN quantity ELSE 0 END) AS `qty_purchases`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN quantity ELSE 0 END) AS `qty_sales`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN (quantity * cost_basis) ELSE 0 END) AS `cost_basis`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN ((sale_price - cost_basis) * quantity) ELSE 0 END) AS `realized_gains`')
|
||||
->first();
|
||||
|
||||
$total_quantity = $query->qty_purchases - $query->qty_sales;
|
||||
$average_cost_basis = $query->qty_purchases > 0
|
||||
? $query->cost_basis / $query->qty_purchases
|
||||
: 0;
|
||||
|
||||
// pull dividend data joined with holdings/transactions
|
||||
$dividends = Dividend::where([
|
||||
'dividends.symbol' => $holding->symbol,
|
||||
])
|
||||
->select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
||||
->selectRaw('@purchased:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "BUY" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `purchased`')
|
||||
->selectRaw('@sold:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "SELL" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `sold`')
|
||||
->selectRaw('@owned:=(@purchased - @sold) AS `owned`')
|
||||
->selectRaw('@dividends_received:=(@owned * dividends.dividend_amount) AS `dividends_received`')
|
||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
|
||||
->groupBy(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
||||
->get();
|
||||
|
||||
// update holding
|
||||
$holding->fill([
|
||||
'quantity' => $total_quantity,
|
||||
'average_cost_basis' => $average_cost_basis,
|
||||
'total_cost_basis' => $total_quantity * $average_cost_basis,
|
||||
'realized_gain_dollars' => $query->realized_gains,
|
||||
'dividends_earned' => $dividends->where('portfolio_id', $holding->portfolio_id)->sum('dividends_received')
|
||||
]);
|
||||
|
||||
$holding->save();
|
||||
$holding->sync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Exports\Sheets;
|
||||
|
||||
use App\Models\Portfolio;
|
||||
use App\Models\Transaction;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
@@ -13,7 +12,7 @@ class TransactionsSheet implements FromCollection, WithHeadings, WithTitle
|
||||
public function __construct(
|
||||
public bool $empty = false
|
||||
) { }
|
||||
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Imports\Sheets\SplitsSheet;
|
||||
use App\Imports\Sheets\DividendsSheet;
|
||||
use App\Imports\Sheets\MarketDataSheet;
|
||||
@@ -10,6 +11,7 @@ use Illuminate\Support\Facades\Artisan;
|
||||
use Maatwebsite\Excel\Events\AfterSheet;
|
||||
use App\Imports\Sheets\DailyChangesSheet;
|
||||
use App\Imports\Sheets\TransactionsSheet;
|
||||
use Maatwebsite\Excel\Events\BeforeSheet;
|
||||
use Maatwebsite\Excel\Concerns\Importable;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use App\Console\Commands\RefreshHoldingData;
|
||||
@@ -26,9 +28,9 @@ class BackupImport implements WithMultipleSheets, WithEvents
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
|
||||
// BeforeSheet::class => DB::commit(),
|
||||
// AfterSheet::class => Artisan::queue(RefreshHoldingData::class),
|
||||
AfterSheet::class => Artisan::call(RefreshHoldingData::class)
|
||||
// AfterSheet::class => Artisan::call(RefreshHoldingData::class)
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use Exception;
|
||||
|
||||
trait ValidatesPortfolioPermissions {
|
||||
|
||||
public function validatePortfolioPermissions($collection)
|
||||
{
|
||||
$portfolios = auth()->user()->portfolios->pluck('id');
|
||||
|
||||
$collection->pluck('portfolio_id')->unique()->each(function($portfolio) use ($portfolios) {
|
||||
|
||||
if (!$portfolios->contains($portfolio)) {
|
||||
|
||||
throw new Exception('You do not have permission to access that portfolio.');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -164,6 +164,49 @@ class Holding extends Model
|
||||
// ->selectRaw('COALESCE((@total_gain_dollars / @sum_total_cost_basis) * 100,0) AS total_gain_percent')
|
||||
->join('market_data', 'market_data.symbol', 'holdings.symbol');
|
||||
}
|
||||
|
||||
public function sync()
|
||||
{
|
||||
// pull existing transaction data
|
||||
$query = Transaction::where([
|
||||
'portfolio_id' => $this->portfolio_id,
|
||||
'symbol' => $this->symbol,
|
||||
])->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN quantity ELSE 0 END) AS `qty_purchases`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN quantity ELSE 0 END) AS `qty_sales`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN (quantity * cost_basis) ELSE 0 END) AS `cost_basis`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN ((sale_price - cost_basis) * quantity) ELSE 0 END) AS `realized_gains`')
|
||||
->first();
|
||||
|
||||
$total_quantity = $query->qty_purchases - $query->qty_sales;
|
||||
$average_cost_basis = $query->qty_purchases > 0
|
||||
? $query->cost_basis / $query->qty_purchases
|
||||
: 0;
|
||||
|
||||
// pull dividend data joined with holdings/transactions
|
||||
$dividends = Dividend::where([
|
||||
'dividends.symbol' => $this->symbol,
|
||||
])
|
||||
->select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
||||
->selectRaw('@purchased:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "BUY" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `purchased`')
|
||||
->selectRaw('@sold:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "SELL" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `sold`')
|
||||
->selectRaw('@owned:=(@purchased - @sold) AS `owned`')
|
||||
->selectRaw('@dividends_received:=(@owned * dividends.dividend_amount) AS `dividends_received`')
|
||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
|
||||
->groupBy(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
||||
->get();
|
||||
|
||||
// update holding
|
||||
$this->fill([
|
||||
'quantity' => $total_quantity,
|
||||
'average_cost_basis' => $average_cost_basis,
|
||||
'total_cost_basis' => $total_quantity * $average_cost_basis,
|
||||
'realized_gain_dollars' => $query->realized_gains,
|
||||
'dividends_earned' => $dividends->where('portfolio_id', $this->portfolio_id)->sum('dividends_received')
|
||||
]);
|
||||
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,10 @@ class Transaction extends Model
|
||||
|
||||
$transaction->syncHolding();
|
||||
|
||||
$transaction->refreshMarketData();
|
||||
|
||||
$transaction->syncDividendsToHolding();
|
||||
|
||||
cache()->tags(['metrics', auth()->user()->id])->flush();
|
||||
});
|
||||
|
||||
@@ -174,33 +178,6 @@ class Transaction extends Model
|
||||
'total_cost_basis' => $this->quantity * $this->cost_basis,
|
||||
]);
|
||||
|
||||
// pull existing transaction data
|
||||
$query = self::where([
|
||||
'portfolio_id' => $this->portfolio_id,
|
||||
'symbol' => $this->symbol,
|
||||
])->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN quantity ELSE 0 END) AS `qty_purchases`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN quantity ELSE 0 END) AS `qty_sales`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN (quantity * cost_basis) ELSE 0 END) AS `cost_basis`')
|
||||
->selectRaw('SUM(CASE WHEN transaction_type = "SELL" THEN ((sale_price - cost_basis) * quantity) ELSE 0 END) AS `realized_gains`')
|
||||
->first();
|
||||
|
||||
$total_quantity = $query->qty_purchases - $query->qty_sales;
|
||||
$average_cost_basis = $query->qty_purchases > 0
|
||||
? $query->cost_basis / $query->qty_purchases
|
||||
: 0;
|
||||
|
||||
// update holding
|
||||
$holding->fill([
|
||||
'quantity' => $total_quantity,
|
||||
'average_cost_basis' => $average_cost_basis,
|
||||
'total_cost_basis' => $total_quantity * $average_cost_basis,
|
||||
'realized_gain_dollars' => $query->realized_gains,
|
||||
]);
|
||||
|
||||
$holding->save();
|
||||
|
||||
$this->refreshMarketData();
|
||||
|
||||
$this->syncDividendsToHolding();
|
||||
$holding->sync();
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,8 @@
|
||||
"Cancel": "Cancel",
|
||||
"Save": "Save",
|
||||
"Close": "Close",
|
||||
"or": "or",
|
||||
"and": "and",
|
||||
|
||||
"Hang on! You're doing that too much.": "Hang on! You're doing that too much.",
|
||||
"Delete Account": "Delete Account",
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
"Cancel": "Cancelar",
|
||||
"Save": "Guardar",
|
||||
"Close": "Cerrar",
|
||||
"or": "o",
|
||||
"and": "y",
|
||||
|
||||
"Hang on! You're doing that too much.": "¡Por favor espere un momento!",
|
||||
"Delete Account": "Eliminar Cuenta",
|
||||
|
||||
@@ -38,7 +38,11 @@
|
||||
|
||||
@if ($user->portfolios->isEmpty())
|
||||
<div class="flex justify-center items-center h-[100px] mb-8">
|
||||
<x-button label="{{ __('Create your first portfolio!') }}" class="btn-primary" link="{{ route('portfolio.create') }}" />
|
||||
|
||||
<x-button label="{{ __('Import / Export Data') }}" class="btn-primary btn-outline mr-6" link="{{ route('import-export') }}" />
|
||||
<span>{{ __('or') }}</span>
|
||||
<x-button label="{{ __('Create your first portfolio!') }}" class="btn-primary ml-6" link="{{ route('portfolio.create') }}" />
|
||||
|
||||
</div>
|
||||
@endif
|
||||
|
||||
|
||||
@@ -40,7 +40,6 @@ new class extends Component {
|
||||
public function holdings(): Collection
|
||||
{
|
||||
|
||||
// dd(Holding::toSql());
|
||||
$holdings = $this->portfolio
|
||||
->holdings()
|
||||
->withCount(['transactions as num_transactions' => function($query) {
|
||||
|
||||
@@ -29,7 +29,7 @@ new class extends Component {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$this->success(__('Successfully imported!'));
|
||||
$this->success(__('Successfully imported!'), redirectTo: route('dashboard'));
|
||||
}
|
||||
|
||||
public function downloadTemplate()
|
||||
@@ -58,8 +58,12 @@ new class extends Component {
|
||||
</x-slot>
|
||||
|
||||
<x-slot name="actions">
|
||||
|
||||
<x-forms.action-message class="me-3" on="saved">
|
||||
{{ __('Saved.') }}
|
||||
</x-forms.action-message>
|
||||
|
||||
<x-button type="submit">
|
||||
<x-button type="submit" spinner="import">
|
||||
{{ __('Import') }}
|
||||
</x-button>
|
||||
</x-slot>
|
||||
|
||||
Reference in New Issue
Block a user