@php /** * Self-contained invoices panel — embed inside any store dashboard's tab * content. Pass $panelSource = 'own' | 'woocommerce' | 'shopify'. * Computes its own data so no host controller change is needed. Sub-views * (list / detail / settings) switch on ?iv= WITHOUT leaving the dashboard — * links preserve the current URL via fullUrlWithQuery(). */ use App\Support\MoneyFormat; $panelSource = $panelSource ?? 'own'; $__ws = (int) (auth()->user()->current_workspace_id ?? 0); $__iv = request('iv'); $__url = fn ($iv) => request()->fullUrlWithQuery(['iv' => $iv]); @endphp @if ($__iv === 'settings') {{-- Settings — rendered IN the dashboard tab --}}
{{ __('Back to invoices') }} @if (session('success'))
{{ session('success') }}
@endif @if (session('error') || $errors->any())
{{ session('error') ?: $errors->first() }}
@endif @include('user.invoices._settings_form')
@elseif (is_numeric($__iv) && ($__inv = \App\Models\Invoice::forWorkspace($__ws)->find((int) $__iv))) {{-- Invoice detail — rendered IN the dashboard tab --}}
{{ __('Back to invoices') }} @if (session('success'))
{{ session('success') }}
@endif @if (session('error'))
{{ session('error') }}
@endif @include('user.invoices._detail', ['invoice' => $__inv])
@else @php $__ws = (int) (auth()->user()->current_workspace_id ?? 0); $__set = \App\Models\InvoiceSetting::forWorkspace($__ws); $__invQ = \App\Models\Invoice::forWorkspace($__ws)->orderByDesc('id'); $__invQ->when($panelSource === 'own', fn ($q) => $q->whereIn('source', ['own', 'manual']), fn ($q) => $q->where('source', $panelSource)); $__invoices = $__invQ->limit(50)->get(); $__storeSources = $panelSource === 'own' ? ['own', 'storefront', 'waba', 'twilio', 'whatsapp_ai', 'manual'] : [$panelSource]; $__orders = \App\Models\WaOrder::where('workspace_id', $__ws)->whereIn('status', ['paid', 'completed']) ->whereIn('source', $__storeSources)->whereDoesntHave('invoice')->orderByDesc('id')->limit(20)->get(); $__isWaba = str_starts_with((string) $__set->send_sender, 'waba:'); $__autoKey = 'auto_send_'.($panelSource === 'own' ? 'own' : $panelSource); $__checklist = [ ['label' => __('Invoices enabled'), 'ok' => (bool) $__set->enabled], ['label' => __('Company name & tax number set'), 'ok' => filled($__set->seller_name)], ['label' => __('WhatsApp sender connected'), 'ok' => filled($__set->send_sender)], ['label' => $__isWaba ? __('Meta template approved') : __('Delivery ready'), 'ok' => ! $__isWaba || $__set->template_status === 'approved'], ['label' => __('Auto-send on for this store'), 'ok' => (bool) ($__set->$__autoKey ?? false)], ]; $__allOk = collect($__checklist)->every(fn ($c) => $c['ok']); @endphp
{{ __('Invoices') }}

{{ __('A numbered PDF is generated per paid order and delivered on WhatsApp.') }}

{{ __('Invoice settings') }}
@unless ($__allOk)
{{ __('Finish setup to auto-send') }}
{{ __('Open settings') }}
@endunless @if ($__orders->isNotEmpty())
{{ __('Paid orders without an invoice') }}
@foreach ($__orders as $o)
@csrf
@endforeach
@endif
@if ($__invoices->isEmpty())
{{ __('No invoices yet. Generate one from a paid order above, or enable auto-send in settings.') }}
@else
@foreach ($__invoices as $inv) @php $sc = match($inv->send_status){ 'sent'=>'bg-wa-green/15 text-wa-deep','send_failed'=>'bg-accent-coral/10 text-accent-coral','ready','rendering','pending'=>'bg-accent-amber/20 text-[#7B5A14]',default=>'bg-paper-100 text-ink-600' }; @endphp @endforeach
{{ __('Number') }}{{ __('Customer') }}{{ __('Type') }}{{ __('Total') }}{{ __('Delivery') }}
{{ $inv->invoice_number }} {{ $inv->buyer_name ?: '—' }} {{ str_replace('_',' ',$inv->doc_type) }} {{ $inv->total_display }} {{ str_replace('_',' ',$inv->send_status) }} PDF
@endif
{{ __('How it works:') }} {{ __('Set company details + tax number, logo & signature in settings → connect a WhatsApp sender and create the invoice template → turn on auto-send → every paid order becomes a numbered PDF delivered on WhatsApp. WABA uses a button-to-PDF template (no 24h limit); Unofficial sends the file directly.') }}
@endif