add transactions index

This commit is contained in:
hackerESQ
2024-08-26 19:49:43 -05:00
parent 57629ba737
commit 1017463ea7
9 changed files with 149 additions and 8 deletions
@@ -13,7 +13,8 @@ class TransactionController extends Controller
*/ */
public function index() public function index()
{ {
$user = request()->user();
return view('transaction.index'); return view('transaction.index', compact('user'));
} }
} }
+12 -1
View File
@@ -70,7 +70,18 @@ class User extends Authenticatable
public function transactions(): HasManyDeep public function transactions(): HasManyDeep
{ {
return $this->hasManyDeep(Transaction::class, ['portfolio_user', Portfolio::class]); return $this->hasManyDeep(Transaction::class, ['portfolio_user', Portfolio::class])
->withAggregate('market_data', 'name')
->withAggregate('portfolio', 'title')
->withAggregate('market_data', 'market_value')
->withAggregate('market_data', 'fifty_two_week_low')
->withAggregate('market_data', 'fifty_two_week_high')
->withAggregate('market_data', 'updated_at')
->selectRaw('COALESCE(transactions.cost_basis * transactions.quantity, 0) AS total_cost_basis')
->selectRaw('COALESCE(market_data.market_value * transactions.quantity, 0) AS total_market_value')
->selectRaw('COALESCE((market_data.market_value - transactions.cost_basis) * transactions.quantity, 0) AS market_gain_dollars')
->selectRaw('COALESCE(((market_data.market_value - transactions.cost_basis) / transactions.cost_basis) * 100, 0) AS market_gain_percent')
->join('market_data', 'transactions.symbol', 'market_data.symbol');;
} }
public function daily_change(): HasManyDeep public function daily_change(): HasManyDeep
+1
View File
@@ -115,6 +115,7 @@
"Press :key to search": "Press :key to search", "Press :key to search": "Press :key to search",
"Search holdings, portfolios, or anything else...": "Search holdings, portfolios, or anything else...", "Search holdings, portfolios, or anything else...": "Search holdings, portfolios, or anything else...",
"Darn! Nothing found for that search.": "Darn! Nothing found for that search.", "Darn! Nothing found for that search.": "Darn! Nothing found for that search.",
"Portfolio": "Portfolio",
"Portfolios": "Portfolios", "Portfolios": "Portfolios",
"Create Portfolio": "Create Portfolio", "Create Portfolio": "Create Portfolio",
"Transactions": "Transactions", "Transactions": "Transactions",
+1
View File
@@ -115,6 +115,7 @@
"Press :key to search": "Presiona :key para buscar", "Press :key to search": "Presiona :key para buscar",
"Search holdings, portfolios, or anything else...": "Busca participaciones, portafolios, o cualquier otra cosa...", "Search holdings, portfolios, or anything else...": "Busca participaciones, portafolios, o cualquier otra cosa...",
"Darn! Nothing found for that search.": "¡Vaya! No se encontró nada para esa búsqueda.", "Darn! Nothing found for that search.": "¡Vaya! No se encontró nada para esa búsqueda.",
"Portfolio": "Portafolio",
"Portfolios": "Portafolios", "Portfolios": "Portafolios",
"Create Portfolio": "Crear Portafolio", "Create Portfolio": "Crear Portafolio",
"Transactions": "Transacciones", "Transactions": "Transacciones",
@@ -16,7 +16,7 @@
<x-menu-item title="{{ __('Create Portfolio') }}" icon="o-document-plus" link="{{ route('portfolio.create') }}" /> <x-menu-item title="{{ __('Create Portfolio') }}" icon="o-document-plus" link="{{ route('portfolio.create') }}" />
</x-menu-sub> </x-menu-sub>
{{-- <x-menu-item title="{{ __('Transactions') }}" icon="o-banknotes" link="{{ route('transaction.index') }}" /> --}} <x-menu-item title="{{ __('Transactions') }}" icon="o-banknotes" link="{{ route('transaction.index') }}" />
{{-- <x-menu-item title="{{ __('Reporting') }}" icon="o-chart-bar-square" link="####" /> --}} {{-- <x-menu-item title="{{ __('Reporting') }}" icon="o-chart-bar-square" link="####" /> --}}
</x-menu> </x-menu>
@@ -80,9 +80,6 @@ new class extends Component {
@scope('cell_dividends_earned', $row) @scope('cell_dividends_earned', $row)
{{ Number::currency($row->dividends_earned ?? 0) }} {{ Number::currency($row->dividends_earned ?? 0) }}
@endscope @endscope
@scope('cell_dividends_earned', $row)
{{ Number::currency($row->dividends_earned ?? 0) }}
@endscope
@scope('cell_market_data_updated_at', $row) @scope('cell_market_data_updated_at', $row)
{{ \Carbon\Carbon::parse($row->market_data_updated_at)->diffForHumans() }} {{ \Carbon\Carbon::parse($row->market_data_updated_at)->diffForHumans() }}
@endscope @endscope
@@ -62,7 +62,7 @@ new class extends Component {
// $this->transaction->owner_id = auth()->user()->id; // $this->transaction->owner_id = auth()->user()->id;
$this->transaction->save(); $this->transaction->save();
$this->success(__('Transaction updated'), redirectTo: route('portfolio.show', ['portfolio' => $this->portfolio->id])); $this->success(__('Transaction updated'), redirectTo: route('portfolio.show', ['portfolio' => $this->transaction->portfolio_id]));
} }
public function save() public function save()
@@ -0,0 +1,129 @@
<?php
use App\Models\User;
use App\Models\Transaction;
use Illuminate\Support\Collection;
use Livewire\Volt\Component;
use Livewire\WithPagination;
new class extends Component {
use WithPagination;
// props
public User $user;
public ?Transaction $editingTransaction;
public array $sortBy = ['column' => 'date', 'direction' => 'desc'];
public array $headers;
// methods
public function showTransactionDialog($transactionId)
{
$this->editingTransaction = Transaction::findOrFail($transactionId);
$this->dispatch('toggle-manage-transaction');
}
public function mount()
{
$this->headers = [
['key' => 'date', 'label' => __('Date'), 'sortable' => true],
['key' => 'portfolio_title', 'label' => __('Portfolio')],
['key' => 'symbol', 'label' => __('Symbol'), 'class' => ''],
['key' => 'market_data_name', 'label' => __('Name')],
['key' => 'transaction_type', 'label' => __('Type')],
['key' => 'quantity', 'label' => __('Quantity')],
['key' => 'cost_basis', 'label' => __('Cost Basis')],
['key' => 'total_cost_basis', 'label' => __('Total Cost Basis')],
['key' => 'market_data_market_value', 'label' => __('Market Value')],
['key' => 'total_market_value', 'label' => __('Total Market Value')],
// ['key' => 'market_gain_dollars', 'label' => __('Market Gain/Loss')],
// ['key' => 'market_gain_percent', 'label' => __('Market Gain/Loss')],
// ['key' => 'realized_gain_dollars', 'label' => __('Realized Gain/Loss')],
// ['key' => 'dividends_earned', 'label' => __('Dividends Earned')],
// ['key' => 'market_data_updated_at', 'label' => __('Market Data Age')],
];
}
public function transactions()
{
return auth()
->user()
->transactions()
->orderBy(...array_values($this->sortBy))
->paginate(10);
}
}; ?>
<div class="">
<x-table
:headers="$headers"
:rows="$this->transactions()"
x-data="{ loading: false, timeout: null }"
@row-click="
timeout = setTimeout(() => { loading = true }, 200);
$wire.showTransactionDialog($event.detail.id).then(() => {
clearTimeout(timeout);
loading = false;
})
"
:sort-by="$sortBy"
with-pagination
>
@scope('cell_symbol', $row)
{{ $row->symbol }}
<x-loading x-show="loading" x-cloak class="text-gray-400 ml-2" />
@endscope
@scope('cell_date', $row)
{{ $row->date->format('M d, Y') }}
@endscope
@scope('cell_transaction_type', $row)
<x-badge
:value="$row->transaction_type"
class="{{ $row->transaction_type == 'BUY'
? 'badge-success'
: 'badge-error' }} badge-sm mr-3"
/>
@endscope
@scope('cell_cost_basis', $row)
{{ Number::currency($row->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)
{{ Number::percentage($row->market_gain_percent ?? 0) }}
@endscope
@scope('cell_market_data_market_value', $row)
{{ Number::currency($row->market_data_market_value ?? 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>
<x-ib-modal
key="manage-transaction"
title="Manage Transaction"
>
@livewire('manage-transaction-form', [
'transaction' => $editingTransaction,
], key($editingTransaction->id ?? 'new'))
</x-ib-modal>
</div>
+2 -1
View File
@@ -3,6 +3,7 @@
<x-ib-toolbar title="{{ __('All Transactions') }}" /> <x-ib-toolbar title="{{ __('All Transactions') }}" />
{{-- @livewire('manage-transaction-form', ['portfolio' => $portfolio]) --}} @livewire('transactions-table')
</div> </div>
</x-app-layout> </x-app-layout>