Files
investbrain/resources/views/livewire/holdings-table.blade.php
T

102 lines
4.0 KiB
PHP
Raw Normal View History

2024-08-17 21:33:09 -05:00
<?php
2024-08-28 23:32:01 -05:00
use App\Models\Holding;
2024-08-17 21:33:09 -05:00
use App\Models\Portfolio;
use App\Models\Transaction;
use Illuminate\Support\Collection;
use Livewire\Volt\Component;
new class extends Component {
// props
public Portfolio $portfolio;
public array $sortBy = ['column' => 'symbol', 'direction' => 'asc'];
public array $headers;
public function mount()
{
$this->headers = [
2024-10-22 12:13:36 -05:00
['key' => 'symbol', 'label' => __('Symbol')],
2024-10-22 12:36:43 -05:00
['key' => 'market_data_name', 'label' => __('Name'), 'sortable' => true, 'class' => 'hidden md:table-cell'],
2024-08-17 21:33:09 -05:00
['key' => 'quantity', 'label' => __('Quantity')],
['key' => 'average_cost_basis', 'label' => __('Average Cost Basis')],
2024-10-22 12:36:43 -05:00
['key' => 'total_cost_basis', 'label' => __('Total Cost Basis'), 'class' => 'hidden md:table-cell'],
2024-08-17 21:33:09 -05:00
['key' => 'market_data_market_value', 'label' => __('Market Value')],
2024-10-22 12:36:43 -05:00
['key' => 'total_market_value', 'label' => __('Total Market Value'), 'class' => 'hidden md:table-cell'],
2024-08-21 20:42:32 -05:00
['key' => 'market_gain_dollars', 'label' => __('Market Gain/Loss')],
2024-10-22 12:36:43 -05:00
['key' => 'market_gain_percent', 'label' => __('Market Gain/Loss'), 'class' => 'hidden md:table-cell'],
2024-08-21 20:42:32 -05:00
['key' => 'realized_gain_dollars', 'label' => __('Realized Gain/Loss')],
2024-08-17 21:33:09 -05:00
['key' => 'dividends_earned', 'label' => __('Dividends Earned')],
2024-10-22 12:36:43 -05:00
['key' => 'market_data_fifty_two_week_low', 'label' => __('52 week low'), 'class' => 'hidden md:table-cell'],
['key' => 'market_data_fifty_two_week_high', 'label' => __('52 week high'), 'class' => 'hidden md:table-cell'],
2024-08-17 21:33:09 -05:00
['key' => 'num_transactions', 'label' => __('Number of Transactions')],
2024-09-05 20:41:07 -05:00
['key' => 'market_data_updated_at', 'label' => __('Last Refreshed')],
2024-08-17 21:33:09 -05:00
];
}
public function holdings(): Collection
{
2024-08-29 20:55:23 -05:00
2024-08-29 18:46:21 -05:00
$holdings = $this->portfolio
->holdings()
->withCount(['transactions as num_transactions' => function($query) {
return $query->whereRaw('transactions.symbol = holdings.symbol');
}])
->orderBy(...array_values($this->sortBy))
2024-09-09 20:50:06 -05:00
// ->where('holdings.quantity', '>', 0)
2024-08-29 18:46:21 -05:00
->get();
return $holdings;
2024-08-17 21:33:09 -05:00
}
2024-08-27 18:12:51 -05:00
public function goToHolding($holding)
{
return $this->redirect(route('holding.show', ['portfolio' => $holding['portfolio_id'], 'symbol' => $holding['symbol']]));
}
2024-08-17 21:33:09 -05:00
}; ?>
2024-10-22 12:13:36 -05:00
<x-table
:headers="$headers"
:rows="$this->holdings()"
:sort-by="$sortBy"
@row-click="$wire.goToHolding($event.detail)"
>
@scope('cell_average_cost_basis', $row)
{{ Number::currency($row->average_cost_basis ?? 0) }}
@endscope
@scope('cell_total_cost_basis', $row)
{{ Number::currency($row->total_cost_basis ?? 0) }}
@endscope
@scope('cell_realized_gain_dollars', $row)
{{ Number::currency($row->realized_gain_dollars ?? 0) }}
@endscope
@scope('cell_market_gain_dollars', $row)
{{ Number::currency($row->market_gain_dollars ?? 0) }}
@endscope
@scope('cell_market_gain_percent', $row)
<x-gain-loss-arrow-badge :percent="$row->market_gain_percent" />
@endscope
@scope('cell_market_data_market_value', $row)
{{ Number::currency($row->market_data_market_value ?? 0) }}
@endscope
@scope('cell_market_data_fifty_two_week_low', $row)
{{ Number::currency($row->market_data_fifty_two_week_low ?? 0) }}
@endscope
@scope('cell_market_data_fifty_two_week_high', $row)
{{ Number::currency($row->market_data_fifty_two_week_high ?? 0) }}
@endscope
@scope('cell_total_market_value', $row)
{{ Number::currency($row->total_market_value ?? 0) }}
@endscope
@scope('cell_dividends_earned', $row)
{{ Number::currency($row->dividends_earned ?? 0) }}
@endscope
@scope('cell_market_data_updated_at', $row)
{{ \Carbon\Carbon::parse($row->market_data_updated_at)->diffForHumans() }}
@endscope
</x-table>