Feat: Adds multi currency support (#88)

This commit is contained in:
hackerESQ
2025-04-09 19:25:15 -05:00
committed by GitHub
parent 6d6f968f42
commit eae345f243
100 changed files with 17735 additions and 35761 deletions
@@ -0,0 +1,41 @@
<?php
use App\Models\Holding;
use Livewire\Volt\Component;
new class extends Component
{
// props
public Holding $holding;
protected $listeners = [
'transaction-updated' => '$refresh',
'transaction-saved' => '$refresh',
];
// methods
}; ?>
<div>
@foreach ($holding->dividends->take(5) as $dividend)
<x-list-item :item="$dividend">
<x-slot:value>
@php
$owned = ($dividend->purchased - $dividend->sold);
@endphp
{{ Number::currency($dividend->dividend_amount, $holding->market_data->currency) }}
x {{ $owned }}
= {{ Number::currency($owned * $dividend->dividend_amount, $holding->market_data->currency) }}
</x-slot:value>
<x-slot:sub-value>
<span title="{{ __('Ex Dividend Date') }}">{{ $dividend->date->format('F d, Y') }}</span>
</x-slot:sub-value>
</x-list-item>
@endforeach
</div>
@@ -0,0 +1,61 @@
<?php
use App\Models\Holding;
use Livewire\Volt\Component;
new class extends Component
{
// props
public Holding $holding;
protected $listeners = [
'transaction-updated' => '$refresh',
'transaction-saved' => '$refresh',
];
// methods
}; ?>
<div>
<div class="font-bold text-2xl py-1 flex items-center">
{{ Number::currency($holding->market_data->market_value ?? 0, $holding->market_data->currency) }}
<x-gain-loss-arrow-badge
:cost-basis="$holding->average_cost_basis"
:market-value="$holding->market_data->market_value_base"
/>
</div>
<p>
<span class="font-bold">{{ __('Quantity Owned') }}: </span>
{{ $holding->quantity }}
</p>
<p>
<span class="font-bold">{{ __('Average Cost Basis') }}: </span>
{{ Number::currency($holding->average_cost_basis ?? 0, $holding->market_data->currency) }}
</p>
<p>
<span class="font-bold">{{ __('Total Cost Basis') }}: </span>
{{ Number::currency($holding->total_cost_basis ?? 0, $holding->market_data->currency) }}
</p>
<p>
<span class="font-bold">{{ __('Realized Gain/Loss') }}: </span>
{{ Number::currency($holding->realized_gain_dollars ?? 0, $holding->market_data->currency) }}
</p>
<p>
<span class="font-bold">{{ __('Dividends Earned') }}: </span>
{{ Number::currency($holding->dividends_earned ?? 0, $holding->market_data->currency) }}
</p>
<p class="pt-2 text-sm" title="{{ \Carbon\Carbon::parse($holding->market_data->updated_at)->toIso8601String() }}">
{{ __('Last Refreshed') }}:
{{ !is_null($holding->market_data->updated_at)
? \Carbon\Carbon::parse($holding->market_data->updated_at)->diffForHumans()
: '' }}
</p>
</div>
@@ -0,0 +1,65 @@
<?php
use App\Models\Holding;
use Illuminate\Support\Collection;
use Livewire\Attributes\{Computed};
use Livewire\Volt\Component;
use Mary\Traits\Toast;
use Illuminate\Validation\Rule;
new class extends Component {
use Toast;
// props
public Holding $holding;
public Bool $reinvest_dividends = false;
// methods
public function rules()
{
return [
'reinvest_dividends' => ['required', 'boolean'],
];
}
public function mount()
{
$this->reinvest_dividends = $this->holding?->reinvest_dividends ?? false;
}
public function save()
{
$this->holding->update($this->validate());
$this->success(__('Holding options saved'));
$this->dispatch('toggle-holding-options');
}
}; ?>
<div class="" x-data="{ }"> {{-- grid lg:grid-cols-4 gap-10 --}}
<x-ib-form wire:submit="save" class=""> {{-- col-span-3 --}}
<x-toggle
label="{{ __('Reinvest Dividends') }}"
wire:model="reinvest_dividends"
right
hint="{{ __('Automatically generate buy transactions for any dividends earned.') }}"
/>
<x-slot:actions>
<x-button
label="{{ __('Save') }}"
type="submit"
icon="o-paper-airplane"
class="btn-primary"
spinner="save"
/>
</x-slot:actions>
</x-ib-form>
</div>
@@ -0,0 +1,103 @@
<?php
use App\Models\Portfolio;
use Illuminate\Support\Collection;
use Livewire\Volt\Component;
use App\Models\Currency;
new class extends Component
{
// props
public Portfolio $portfolio;
public array $sortBy = ['column' => 'symbol', 'direction' => 'asc'];
public array $headers;
public function mount()
{
$this->headers = [
['key' => 'symbol', 'label' => __('Symbol')],
['key' => 'market_data_name', 'label' => __('Name'), 'sortable' => true, 'class' => 'hidden md:table-cell'],
['key' => 'quantity', 'label' => __('Quantity')],
['key' => 'average_cost_basis', 'label' => __('Average Cost Basis')],
['key' => 'total_cost_basis', 'label' => __('Total Cost Basis'), 'class' => 'hidden md:table-cell'],
['key' => 'market_data_market_value', 'label' => __('Market Value')],
['key' => 'total_market_value', 'label' => __('Total Market Value'), 'class' => 'hidden md:table-cell'],
['key' => 'market_gain_dollars', 'label' => __('Market Gain/Loss')],
['key' => 'market_gain_percent', 'label' => __('Market Gain/Loss'), 'class' => 'hidden md:table-cell'],
['key' => 'realized_gain_dollars', 'label' => __('Realized Gain/Loss')],
['key' => 'dividends_earned', 'label' => __('Dividends Earned')],
['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'],
['key' => 'num_transactions', 'label' => __('Number of Transactions')],
['key' => 'market_data_updated_at', 'label' => __('Last Refreshed')],
];
}
public function holdings(): Collection
{
$holdings = $this->portfolio
->holdings()
->withCount(['transactions as num_transactions' => function ($query) {
return $query->whereRaw('transactions.symbol = holdings.symbol');
}])
->orderBy(...array_values($this->sortBy))
// ->where('holdings.quantity', '>', 0)
->get();
return $holdings;
}
public function goToHolding($holding)
{
return $this->redirect(route('holding.show', ['portfolio' => $holding['portfolio_id'], 'symbol' => $holding['symbol']]));
}
}; ?>
<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, $row->market_data->currency) }}
@endscope
@scope('cell_total_cost_basis', $row)
{{ Number::currency($row->total_cost_basis ?? 0, $row->market_data->currency) }}
@endscope
@scope('cell_realized_gain_dollars', $row)
{{ Number::currency($row->realized_gain_dollars ?? 0, $row->market_data->currency) }}
@endscope
@scope('cell_market_gain_dollars', $row)
{{ Number::currency($row->market_gain_dollars ?? 0, $row->market_data->currency) }}
@endscope
@scope('cell_market_gain_percent', $row)
<x-gain-loss-arrow-badge
:cost-basis="$row->average_cost_basis"
:market-value="$row->market_data->market_value"
/>
@endscope
@scope('cell_market_data_market_value', $row)
{{ Number::currency($row->market_data_market_value ?? 0, $row->market_data->currency) }}
@endscope
@scope('cell_market_data_fifty_two_week_low', $row)
{{ Number::currency($row->market_data_fifty_two_week_low ?? 0, $row->market_data->currency) }}
@endscope
@scope('cell_market_data_fifty_two_week_high', $row)
{{ Number::currency($row->market_data_fifty_two_week_high ?? 0, $row->market_data->currency) }}
@endscope
@scope('cell_total_market_value', $row)
{{ Number::currency($row->total_market_value ?? 0, $row->market_data->currency) }}
@endscope
@scope('cell_dividends_earned', $row)
{{ Number::currency($row->dividends_earned ?? 0, $row->market_data->currency) }}
@endscope
@scope('cell_market_data_updated_at', $row)
{{ \Carbon\Carbon::parse($row->market_data_updated_at)->diffForHumans() }}
@endscope
</x-table>
+24 -14
View File
@@ -1,3 +1,5 @@
@use('App\Models\Currency')
<x-app-layout>
<div x-data>
@@ -67,48 +69,56 @@
<x-ib-card title="{{ __('Fundamentals') }}" class="md:col-span-4">
@if(!empty($holding->market_data->market_cap))
<p>
<span class="font-bold">{{ __('Market Cap') }}: </span>
${{ Number::forHumans($holding->market_data->market_cap ?? 0) }}
{{ Currency::forHumans($holding->market_data->market_cap, $holding->market_data->currency) }}
</p>
@endif
@if(!empty($holding->market_data->forward_pe))
<p>
<span class="font-bold">{{ __('Forward PE') }}: </span>
{{ $holding->market_data->forward_pe }}
</p>
@endif
@if(!empty($holding->market_data->trailing_pe))
<p>
<span class="font-bold">{{ __('Trailing PE') }}: </span>
{{ $holding->market_data->trailing_pe }}
</p>
@endif
<p>
<span class="font-bold">{{ __('Book Value') }}: </span>
{{ $holding->market_data->book_value }}
</p>
@if(!empty($holding->market_data->book_value))
<p>
<span class="font-bold">{{ __('Book Value') }}: </span>
{{ Number::currency($holding->market_data->book_value, $holding->market_data->currency) }}
</p>
@endif
<p>
<span class="font-bold">{{ __('52 week') }}: </span>
<x-fifty-two-week-range
:low="$holding->market_data->fifty_two_week_low"
:high="$holding->market_data->fifty_two_week_high"
:current="$holding->market_data->market_value"
/>
<x-fifty-two-week-range :market-data="$holding->market_data" />
</p>
@if(!empty($holding->market_data->dividend_yield))
<p>
<span class="font-bold">{{ __('Dividend Yield') }}: </span>
{{ Number::percentage(
$holding->market_data->dividend_yield ?? 0,
$holding->market_data->dividend_yield,
$holding->market_data->dividend_yield < 1 ? 2 : 0
) }}
</p>
@endif
@if(!empty($holding->market_data->last_dividend_date))
<p>
<span class="font-bold">{{ __('Last Dividend Paid') }}: </span>
{{ $holding->market_data?->last_dividend_date?->format('F d, Y') ?? '' }}
{{ $holding->market_data->last_dividend_date->format('F d, Y') }}
</p>
@endif
</x-ib-card>
@@ -164,7 +174,7 @@
</x-ib-card>
@if(config('services.ai_chat_enabled'))
{{-- // TODO: add to system prompt:
{{-- // todo: add to system prompt:
// Additionally, here is some recent news about {$this->holding->symbol}:
// And their latest SEC filings: --}}
@livewire('ai-chat-window', [
@@ -209,7 +219,7 @@
* 52 week high: {$holding->market_data->fifty_two_week_high}
* Dividend yield: {$holding->market_data->dividend_yield}
This data is current as of today's date: " . now()->format('Y-m-d') . ". Based on this current market data, quantity owned, and average cost basis, you should determine if the {$holding->symbol} holding is making or losing money.
This data is current as of today's date: " . now()->toDateString() . ". Based on this current market data, quantity owned, and average cost basis, you should determine if the {$holding->symbol} holding is making or losing money.
Below is the question from the investor. Considering these facts, provide a concise response to the following question (give a direct response). Limit your response to no more than 75 words and consider using a common decision framework. Use github style markdown for any formatting:"
])