wip
This commit is contained in:
@@ -12,7 +12,7 @@ class DashboardController extends Controller
|
||||
*/
|
||||
public function show(Request $request)
|
||||
{
|
||||
$user = $request->user()->load('portfolios');
|
||||
$user = $request->user()->load(['portfolios', 'holdings', 'transactions']);
|
||||
|
||||
// get portfolio metrics
|
||||
$metrics = cache()->tags(['metrics', 'dashboard', $user->id])->remember(
|
||||
@@ -21,6 +21,7 @@ class DashboardController extends Controller
|
||||
function () {
|
||||
return
|
||||
Holding::query()
|
||||
->myHoldings()
|
||||
->getPortfolioMetrics()
|
||||
->first();
|
||||
}
|
||||
|
||||
@@ -15,12 +15,11 @@ class HoldingController extends Controller
|
||||
public function show(Request $request, Portfolio $portfolio, String $symbol)
|
||||
{
|
||||
|
||||
$holding = $request->user()
|
||||
->holdings()
|
||||
->where([
|
||||
'holdings.portfolio_id' => $portfolio->id,
|
||||
'holdings.symbol' => $symbol
|
||||
])->firstOrFail();
|
||||
|
||||
$holding = Holding::query()
|
||||
->portfolio($portfolio->id)
|
||||
->symbol($symbol)
|
||||
->first();
|
||||
|
||||
$market_data = $holding->market_data;
|
||||
|
||||
|
||||
+51
-8
@@ -2,7 +2,11 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Split;
|
||||
use App\Models\Dividend;
|
||||
use App\Models\Portfolio;
|
||||
use App\Models\MarketData;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -53,7 +57,8 @@ class Holding extends Model
|
||||
*/
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol')
|
||||
->where('transactions.portfolio_id', $this->portfolio_id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +68,44 @@ class Holding extends Model
|
||||
*/
|
||||
public function dividends()
|
||||
{
|
||||
return $this->hasMany(Dividend::class, 'symbol', 'symbol');
|
||||
|
||||
|
||||
|
||||
return $this->hasMany(Dividend::class, 'symbol', 'symbol')
|
||||
->select([
|
||||
'dividends.symbol',
|
||||
'dividends.date',
|
||||
'dividends.dividend_amount',
|
||||
])
|
||||
->selectRaw("SUM(
|
||||
CASE WHEN transaction_type = 'BUY'
|
||||
AND transactions.symbol = dividends.symbol
|
||||
AND transactions.portfolio_id = '$this->portfolio_id'
|
||||
AND dividends.date >= transactions.date
|
||||
THEN transactions.quantity
|
||||
ELSE 0 END
|
||||
) AS purchased")
|
||||
->selectRaw("SUM(
|
||||
CASE WHEN transaction_type = 'SELL'
|
||||
AND transactions.symbol = dividends.symbol
|
||||
AND transactions.portfolio_id = '$this->portfolio_id'
|
||||
AND dividends.date >= transactions.date
|
||||
THEN transactions.quantity
|
||||
ELSE 0 END
|
||||
) AS sold")
|
||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||
->groupBy([
|
||||
'dividends.symbol',
|
||||
'dividends.date',
|
||||
'dividends.dividend_amount',
|
||||
])
|
||||
->orderBy('dividends.date', 'DESC')
|
||||
->where('dividends.date', '>=', function ($query) {
|
||||
$query->selectRaw('min(transactions.date)')
|
||||
->from('transactions')
|
||||
->whereRaw("transactions.portfolio_id = '$this->portfolio_id'")
|
||||
->whereRaw("transactions.symbol = '$this->symbol'");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +125,8 @@ class Holding extends Model
|
||||
*/
|
||||
public function splits()
|
||||
{
|
||||
return $this->hasMany(Split::class, 'symbol', 'symbol');
|
||||
return $this->hasMany(Split::class, 'symbol', 'symbol')
|
||||
->orderBy('date', 'DESC');
|
||||
}
|
||||
|
||||
public function scopePortfolio($query, $portfolio)
|
||||
@@ -91,6 +134,11 @@ class Holding extends Model
|
||||
return $query->where('portfolio_id', $portfolio);
|
||||
}
|
||||
|
||||
public function scopeSymbol($query, $symbol)
|
||||
{
|
||||
return $query->where('symbol', $symbol);
|
||||
}
|
||||
|
||||
public function scopeWithoutWishlists($query) {
|
||||
return $query->join('portfolios', 'portfolios.id', 'holdings.portfolio_id')
|
||||
->where('portfolios.wishlist', 0);
|
||||
@@ -115,11 +163,6 @@ class Holding extends Model
|
||||
->join('market_data', 'market_data.symbol', 'holdings.symbol');
|
||||
}
|
||||
|
||||
public function scopeSymbol($query, $symbol)
|
||||
{
|
||||
return $query->where('symbol', $symbol);
|
||||
}
|
||||
|
||||
// public function refreshDividends()
|
||||
// {
|
||||
// return Dividend::getDividendData($this->attributes['symbol']);
|
||||
|
||||
@@ -25,6 +25,15 @@ class MarketData extends Model
|
||||
'market_cap'
|
||||
];
|
||||
|
||||
protected $attributes = [
|
||||
'market_value' => 0,
|
||||
'fifty_two_week_high' => 0,
|
||||
'fifty_two_week_low' => 0,
|
||||
'forward_pe' => 0,
|
||||
'trailing_pe' => 0,
|
||||
'market_cap' => 0
|
||||
];
|
||||
|
||||
public static function setSplitsHoldingSynced($symbol)
|
||||
{
|
||||
$market_data = self::where('symbol', $symbol)->get()->first();
|
||||
|
||||
@@ -126,14 +126,14 @@ class Transaction extends Model
|
||||
*/
|
||||
public function ensureCostBasisIsAddedToSale()
|
||||
{
|
||||
$holding = Holding::firstOrNew([
|
||||
$average_cost_basis = Transaction::where([
|
||||
'portfolio_id' => $this->portfolio_id,
|
||||
'symbol' => $this->symbol
|
||||
],[
|
||||
'average_cost_basis' => null
|
||||
]);
|
||||
'symbol' => $this->symbol,
|
||||
'transaction_type' => 'BUY',
|
||||
])->whereDate('date', '<=', $this->date)
|
||||
->average('cost_basis');
|
||||
|
||||
$this->cost_basis = $holding->average_cost_basis ?? 0;
|
||||
$this->cost_basis = $average_cost_basis ?? 0;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
+6
-9
@@ -77,15 +77,12 @@ class User extends Authenticatable
|
||||
->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), 0) AS market_gain_percent')
|
||||
->selectRaw('
|
||||
CASE
|
||||
WHEN transaction_type = \'SELL\'
|
||||
THEN COALESCE(transactions.sale_price - transactions.cost_basis, 0)
|
||||
ELSE COALESCE(market_data.market_value - transactions.cost_basis, 0)
|
||||
END AS gain_dollars')
|
||||
->join('market_data', 'transactions.symbol', 'market_data.symbol');;
|
||||
}
|
||||
|
||||
public function daily_change(): HasManyDeep
|
||||
{
|
||||
return $this->hasManyDeep(DailyChange::class, ['portfolio_user', Portfolio::class]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
<x-menu-item title="{{ __('Manage Profile') }}" icon="o-user" link="{{ @route('profile.show') }}" />
|
||||
<x-menu-item title="{{ __('Import / Export Data') }}" icon="o-cloud-arrow-down" link="{{ @route('import-export') }}" />
|
||||
|
||||
<x-section-border />
|
||||
<x-section-border class="py-3" />
|
||||
|
||||
<x-menu-item title="{{ __('Log Out') }}" icon="o-power" onclick="event.preventDefault(); document.getElementById('logout').submit();" />
|
||||
<form id="logout" action="{{ route('logout') }}" method="POST" style="display: none;">
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
<div class="hidden sm:block">
|
||||
<div {{ $attributes->merge(['class' => 'py-6']) }}>
|
||||
<div {{ $attributes->class(['py-6' => !$attributes->has('class')]) }}>
|
||||
|
||||
<div class="border-t border-gray-200 dark:border-gray-700"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
<x-app-layout>
|
||||
<div>
|
||||
<div x-data>
|
||||
|
||||
<x-ib-modal
|
||||
key="new-transaction"
|
||||
title="New Transaction"
|
||||
>
|
||||
@livewire('manage-transaction-form', [
|
||||
'portfolio' => $portfolio,
|
||||
'symbol' => $market_data->symbol,
|
||||
])
|
||||
|
||||
</x-ib-modal>
|
||||
|
||||
<x-ib-toolbar>
|
||||
<x-slot:title>
|
||||
@@ -7,6 +18,16 @@
|
||||
{{ $portfolio->title }}
|
||||
</a> » <span title="{{ __('Holding') }}">{{ $market_data->symbol }}</span>
|
||||
</x-slot:title>
|
||||
|
||||
<x-ib-flex-spacer />
|
||||
|
||||
<div>
|
||||
<x-button
|
||||
label="{{ __('Create Transaction') }}"
|
||||
class="btn-sm btn-primary"
|
||||
@click="$dispatch('toggle-new-transaction')"
|
||||
/>
|
||||
</div>
|
||||
</x-ib-toolbar>
|
||||
|
||||
<div class="mt-6 grid md:grid-cols-9 gap-5">
|
||||
@@ -93,7 +114,8 @@
|
||||
|
||||
@livewire('transactions-list', [
|
||||
'portfolio' => $holding->portfolio,
|
||||
'transactions' => $holding->transactions
|
||||
'transactions' => $holding->transactions,
|
||||
'shouldGoToHolding' => false
|
||||
])
|
||||
|
||||
</x-ib-card>
|
||||
@@ -104,9 +126,7 @@
|
||||
|
||||
<x-list-item :item="$dividend">
|
||||
<x-slot:value>
|
||||
|
||||
Purchased {{$dividend->purchased}}<br>
|
||||
Sold {{$dividend->sold}}<br>
|
||||
|
||||
@php
|
||||
$owned = ($dividend->purchased - $dividend->sold);
|
||||
@endphp
|
||||
|
||||
@@ -11,6 +11,7 @@ new class extends Component {
|
||||
public Collection $transactions;
|
||||
public ?Portfolio $portfolio;
|
||||
public ?Transaction $editingTransaction;
|
||||
public Bool $shouldGoToHolding = true;
|
||||
|
||||
protected $listeners = [
|
||||
'transaction-updated' => '$refresh',
|
||||
@@ -24,6 +25,11 @@ new class extends Component {
|
||||
$this->dispatch('toggle-manage-transaction');
|
||||
}
|
||||
|
||||
public function goToHolding($holding)
|
||||
{
|
||||
return $this->redirect(route('holding.show', ['portfolio' => $holding['portfolio_id'], 'symbol' => $holding['symbol']]));
|
||||
}
|
||||
|
||||
}; ?>
|
||||
|
||||
<div class="">
|
||||
@@ -36,6 +42,12 @@ new class extends Component {
|
||||
class="cursor-pointer"
|
||||
x-data="{ loading: false, timeout: null }"
|
||||
@click="
|
||||
if ($wire.shouldGoToHolding) {
|
||||
|
||||
$wire.goToHolding({{ $transaction }})
|
||||
|
||||
return;
|
||||
}
|
||||
timeout = setTimeout(() => { loading = true }, 200);
|
||||
$wire.showTransactionDialog('{{ $transaction->id }}').then(() => {
|
||||
clearTimeout(timeout);
|
||||
|
||||
@@ -24,12 +24,17 @@ new class extends Component {
|
||||
public array $headers;
|
||||
|
||||
// methods
|
||||
public function showTransactionDialog($transactionId)
|
||||
public function goToHolding($holding)
|
||||
{
|
||||
$this->editingTransaction = Transaction::findOrFail($transactionId);
|
||||
$this->dispatch('toggle-manage-transaction');
|
||||
return $this->redirect(route('holding.show', ['portfolio' => $holding['portfolio_id'], 'symbol' => $holding['symbol']]));
|
||||
}
|
||||
|
||||
// public function showTransactionDialog($transactionId)
|
||||
// {
|
||||
// $this->editingTransaction = Transaction::findOrFail($transactionId);
|
||||
// $this->dispatch('toggle-manage-transaction');
|
||||
// }
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->headers = [
|
||||
@@ -64,7 +69,8 @@ new class extends Component {
|
||||
x-data="{ loadingId: null, timeout: null }"
|
||||
@row-click="
|
||||
timeout = setTimeout(() => { loadingId = $event.detail.id }, 200);
|
||||
$wire.showTransactionDialog($event.detail.id).then(() => {
|
||||
{{-- $wire.showTransactionDialog($event.detail.id).then(() => { --}}
|
||||
$wire.goToHolding($event.detail).then(() => {
|
||||
clearTimeout(timeout);
|
||||
loadingId = null;
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user