@extends('crm.layouts.master') @section('title', $lead->fname . ' Portfolio') @section('page-css') @endsection @section('main-content') {{-- ── Page header ── --}}

{{ $lead->fname }} {{ $lead->lname }}

@if($canCreateDeposit) @endif ← Lead Profile
@if(session('success')) @endif
Automated leveraged trading
Consent: {{ $automationSetting->client_consented?'Yes':'No' }} · Admin approval: {{ $automationSetting->admin_approved?'Yes':'No' }} · Sandbox: {{ $automationSetting->sandbox_enabled?'On':'Off' }}
@csrf @method('PUT')
@if(session('status')) @endif @if(session('error')) @endif @if($errors->any() && session()->hasOldInput('side')) @endif {{-- ══════════════════════════════════════════════ KPI STRIP — PACES finance style ══════════════════════════════════════════════ --}} @php /* Build SVG sparkline from an array of floats */ $mkSparkline = function(array $vals, string $stroke) { $w = 220; $h = 52; $pad = 3; if (count($vals) < 2) { $mid = $h / 2; return ""; } $min = min($vals); $max = max($vals); $range = ($max - $min) ?: 1; $n = count($vals) - 1; $pts = []; foreach ($vals as $i => $v) { $x = round($i / $n * $w, 2); $y = round($h - $pad - (($v - $min) / $range) * ($h - $pad * 2), 2); $pts[] = "$x,$y"; } $d = 'M ' . implode(' L ', $pts); // Fill area under curve $fill = "M 0,{$h} L " . implode(' L ', $pts) . " L {$w},{$h} Z"; $colorRgb = $stroke === '#0d6efd' ? '13,110,253' : ($stroke === '#198754' ? '25,135,84' : ($stroke === '#fd7e14' ? '253,126,20' : '108,117,125')); return " "; }; /* Price history for first open position → used for "Position Value" sparkline */ $firstPos = $portfolio['positions']->first(); $posSparkVals = []; if ($firstPos) { $hist = $priceHistories[$firstPos['position']->symbol_id] ?? collect(); $posSparkVals = $hist->pluck('price')->map(fn($v) => (float)$v)->values()->toArray(); /* Scale to USD position value (multiply by qty) */ $qty = (float) $firstPos['position']->quantity; $posSparkVals = array_map(fn($p) => $p * $qty, $posSparkVals); } /* Equity sparkline = position value curve shifted by totalCash */ $eqSparkVals = array_map(fn($v) => $v + (float)$portfolio['totalCash'], $posSparkVals); $ks = $lead->kyc_status; $kycColor = $ks === 'approved' ? '#198754' : ($ks === 'rejected' ? '#dc3545' : '#fd7e14'); $kycLabel = ucwords(str_replace('_', ' ', $ks)); @endphp
{{-- ① Total Cash --}}
USD Cash
${{ number_format($portfolio['totalCash'], 2) }}
Trading available: ${{ number_format($portfolio['totalAvailable'], 2) }}
{!! $mkSparkline(array_fill(0, 10, (float)$portfolio['totalCash']), '#198754') !!}
{{-- ② Non-withdrawable trading credit --}}
USD Credit
${{ number_format($portfolio['creditBalance'], 2) }}
Trading only · not withdrawable
{!! $mkSparkline(array_fill(0, 10, (float)$portfolio['creditBalance']), '#0d6efd') !!}
{{-- ③ Position Value --}}
Position Value
${{ number_format($valuation['position_market_value'], 2) }}
{{ $valuation['counts']['total'] }} open position{{ $valuation['counts']['total'] !== 1 ? 's' : '' }}
{!! $mkSparkline($posSparkVals, '#fd7e14') !!}
{{-- ③ Total Equity --}}
Total Equity
${{ number_format($valuation['equity'], 2) }}
Cash + Positions
{!! $mkSparkline($eqSparkVals, '#0d6efd') !!}
{{-- ④ KYC Status --}}
KYC Status
{{ $kycLabel }}
Client #{{ $lead->id }}
{!! $mkSparkline([], '#6c757d') !!}
Position mix: {{ $valuation['counts']['regular'] }} asset {{ $valuation['counts']['leveraged'] }} leveraged {{ $valuation['counts']['prediction'] }} prediction {{ $valuation['counts']['binary'] }} binary @if(!$valuation['is_complete'])Some values use delayed or fallback pricing.@endif
{{-- ══════════════════════════════════════════════ USD WALLET STRIP ══════════════════════════════════════════════ --}}
Wallets: @foreach($portfolio['wallets'] as $currency => $wallet)
{{ $currency }} {{ number_format($wallet->balance, 2) }} @if(($lead->main_currency ?? 'USD') === $currency) Main @endif
@endforeach
Account currency: USD
{{-- ══════════════════════════════════════════════ OPEN POSITIONS — full width ══════════════════════════════════════════════ --}}
Asset Positions
{{ $portfolio['positions']->count() }} position{{ $portfolio['positions']->count() !== 1 ? 's' : '' }}
@if($portfolio['positions']->isEmpty())
No open positions.
@else
@foreach($portfolio['positions'] as $row) @php $symId = $row['position']->symbol_id; $history = $priceHistories[$symId] ?? collect(); $pnlPct = ($row['position']->avg_price > 0 && $row['latest_price'] > 0) ? (($row['latest_price'] - $row['position']->avg_price) / $row['position']->avg_price) * 100 : 0; @endphp @endforeach
Symbol Qty Avg Price Value P&L Action
{{ $row['symbol']->name ?? '—' }} {{ $row['position']->currency ?? 'USD' }}
{{ $row['symbol']->tv_symbol ?? '' }}
{{ rtrim(rtrim(number_format($row['position']->quantity, 8), '0'), '.') }} {{ number_format($row['position']->avg_price, 2) }} {{ $row['latest_price'] > 0 ? number_format($row['latest_price'], $row['symbol']->decimal_places ?? 2) : '—' }}Updating USD {{ number_format($row['current_value'], 2) }} @if($row['latest_price'] > 0) {{ ($row['unrealised_pnl'] >= 0 ? '+' : '') . number_format($row['unrealised_pnl'], 2) }}
{{ ($pnlPct >= 0 ? '+' : '') . number_format($pnlPct, 2) }}%
@else — @endif
@if($row['latest_price'] > 0) @endif
@endif
{{-- /row open-positions --}}
All Trades
{{ $allHistory->total() }} position{{ $allHistory->total() === 1 ? '' : 's' }}
@include('shared.trade-history-timeline', ['history' => $allHistory])
@if($valuation['leveraged_rows']->isNotEmpty())
Leveraged Positions
{{ $valuation['counts']['leveraged'] }} open
@foreach($valuation['leveraged_rows'] as $row) @php $position = $row['position']; $decimals = $position->symbol?->decimal_places ?? 4; $units = $position->entry_price > 0 ? $position->exposure / $position->entry_price : 0; @endphp @endforeach
InstrumentDirectionLeverageUnitsMarginExposureEntryOpened (UTC)CurrentStop LossTake ProfitP&LValuePriceAction
{{ $position->symbol?->name ?? 'Unknown' }}
{{ $position->symbol?->display_symbol ?? $position->symbol?->symbol }}
{{ strtoupper($position->direction) }}{{ $position->leverage }}×{{ rtrim(rtrim(number_format($units, 8, '.', ''), '0'), '.') }}USD {{ number_format($position->margin, 2) }}@if((float)$position->credit_margin > 0)Credit {{ number_format($position->credit_margin, 2) }}@endifUSD {{ number_format($position->exposure, 2) }}{{ number_format($position->entry_price, $decimals) }}{{ $position->opened_at?->utc()->format('Y-m-d H:i') ?? '—' }}{{ number_format($row['latest'], $decimals) }}{{ $position->stop_loss !== null ? number_format($position->stop_loss, $decimals) : '—' }}{{ $position->take_profit !== null ? number_format($position->take_profit, $decimals) : '—' }}{{ $row['pnl'] >= 0 ? '+' : '' }}USD {{ number_format($row['pnl'], 2) }}USD {{ number_format($row['value'], 2) }}{{ ucfirst($row['priceStatus']) }}
@csrf
@endif
Leveraged Position History
{{ $leveragedHistory->count() }} completed
@forelse($leveragedHistory as $position) @php $decimals = $position->symbol?->decimal_places ?? 4; $units = $position->entry_price > 0 ? $position->exposure / $position->entry_price : 0; @endphp @empty @endforelse
InstrumentDirectionStatusLeverageUnitsEntryCloseOpened (UTC)Closed (UTC)MarginRealized P&LAction
{{ $position->symbol?->name ?? 'Unknown' }}
{{ $position->symbol?->display_symbol ?? $position->symbol?->symbol }}
{{ strtoupper($position->direction) }}{{ ucfirst($position->status) }}{{ $position->leverage }}×{{ rtrim(rtrim(number_format($units, 8, '.', ''), '0'), '.') }}{{ number_format($position->entry_price, $decimals) }}{{ number_format($position->close_price, $decimals) }}{{ $position->opened_at?->utc()->format('Y-m-d H:i') ?? '—' }}{{ $position->closed_at?->utc()->format('Y-m-d H:i') ?? '—' }}USD {{ number_format($position->margin, 2) }}{{ (float)$position->realized_pnl >= 0 ? '+' : '' }}USD {{ number_format($position->realized_pnl, 2) }}
No completed leveraged positions yet.
@if($valuation['prediction_rows']->isNotEmpty())
Prediction-Market Positions
{{ $valuation['counts']['prediction'] }} open
@foreach($valuation['prediction_rows'] as $row)@php $pnl=(float)($row['value']??0)-(float)($row['cost']??0); @endphp @endforeach
MarketOutcomeSharesAverageCurrentCostValueP&LPrice
{{ $row['position']->question }}{{ $row['position']->outcome }}{{ rtrim(rtrim(number_format($row['shares'], 8), '0'), '.') }}{{ number_format($row['average'] * 100, 2) }}¢{{ number_format($row['latest'] * 100, 2) }}¢USD {{ number_format($row['cost'] ?? 0, 2) }}USD {{ number_format($row['value'] ?? 0, 2) }}{{ $pnl >= 0 ? '+' : '' }}USD {{ number_format($pnl, 2) }}{{ ucfirst($row['priceStatus']) }}
@endif @if($valuation['binary_rows']->isNotEmpty())
Open Binary Options
{{ $valuation['counts']['binary'] }} open
@foreach($valuation['binary_rows'] as $row)@endforeach
InstrumentDirectionStrikeStake at CostOpenedExpiresStatus
{{ $row['option']->symbol?->name ?? 'Unknown' }}{{ strtoupper($row['option']->direction) }}{{ number_format($row['option']->strike_price, $row['option']->symbol?->decimal_places ?? 4) }}USD {{ number_format($row['stake'] ?? 0, 2) }}{{ $row['option']->created_at?->format('M j, Y H:i') ?? '—' }}{{ $row['option']->expires_at?->format('M j, Y H:i') ?? '—' }}Open
@endif {{-- ══════════════════════════════════════════════ DEPOSITS + TRADE HISTORY — side by side ══════════════════════════════════════════════ --}}
{{-- ── COL 1: Deposits (col-lg-6) ── --}}
Deposits
@if($canCreateDeposit) @endif
@foreach($deposits as $dep) @endforeach
Amt CCY Status Date
{{ number_format($dep->amount, 2) }} @if($dep->credited_usd_amount !== null && $dep->currency !== 'USD')
USD {{ number_format($dep->credited_usd_amount, 2) }} credited
@endif
{{ $dep->currency }} {{ ucfirst($dep->status) }} {{ $dep->created_at->format('d M y') }} @if($dep->status === 'pending' && in_array(Auth::user()->access_level, ['Admin','Office Manager']))
@csrf
@csrf
@endif @if(Auth::user()->access_level === 'Admin')
@csrf @method('DELETE')
@endif
{{-- ── COL 2: Trade History (col-lg-6) ── --}}
Trade History
@foreach($trades as $trade) @endforeach
Symbol Side Qty Price Date
{{ $trade->symbol->name ?? '—' }} {{ strtoupper($trade->side) }} {{ rtrim(rtrim(number_format($trade->quantity, 8), '0'), '.') }} {{ number_format($trade->price, 2) }} {{ $trade->traded_at->format('d M y') }}
{{-- /row --}} {{-- ══════════════════════════════════════════════ MODALS (unchanged) ══════════════════════════════════════════════ --}} {{-- Leveraged Position Adjustment Modal --}} {{-- Close Position Modal --}} {{-- Adjust Position Modal --}} {{-- Execute Trade Modal --}} {{-- Admin-only immediate USD deposit modal --}} @if($canCreateDeposit) @endif @endsection @section('page-js') @endsection