{{-- resources/views/admin/sales_invoices/show.blade.php --}} @extends('admin.layouts.master') @section('title', 'عرض فاتورة مبيعات') @section('css') @endsection @section('content') @php $fmt = fn($n, $d=2) => number_format((float)($n ?? 0), $d); $date = fn($v) => $v ? \Carbon\Carbon::parse($v)->format('Y-m-d') : '-'; $status = $invoice->status ?? 'draft'; $statusBadge = match($status){ 'draft' => ['primary', 'Draft'], 'posted' => ['info', 'Posted'], 'paid' => ['success', 'Paid'], 'partial' => ['warn', 'Partial'], 'cancelled' => ['danger', 'Cancelled'], default => ['primary', $status], }; $payType = $invoice->payment_type ?? 'cash'; $payBadge = $payType === 'cash' ? ['success','Cash'] : ['warn','Credit']; $customer = $invoice->customer; $hasReturns = !empty($invoice->returns) && $invoice->returns->count() > 0; $payments = $invoice->payments ?? collect(); // ✅ Journal Entry relation (make sure loaded) $je = $invoice->journalEntry ?? null; $lines = $je?->lines ?? collect(); $jeStatus = $je?->status ?? null; $jeBadge = $jeStatus ? match($jeStatus){ 'posted' => ['success','Posted'], 'draft' => ['warn','Draft'], 'reversed' => ['danger','Reversed'], default => ['primary',$jeStatus], } : null; @endphp {{-- Topbar --}}

عرض فاتورة مبيعات

{{ $statusBadge[1] }} {{ $payBadge[1] }} #{{ $invoice->invoice_code ?? $invoice->invoice_number }}
تاريخ: {{ $date($invoice->invoice_date) }} @if($invoice->due_date) — استحقاق: {{ $date($invoice->due_date) }} @endif
رجوع @if(Route::has('sales_invoices.pdf')) PDF @endif {{-- ✅ Create Sales Return --}} @if(Route::has('sales_returns.create_from_invoice')) @php $canReturnStatus = in_array($status, ['posted','paid','partial'], true); // مجموع المرتجعات الفعلية (استبعد الملغي لو total=0) $returnedTotal = (float) ($invoice->returns?->sum('total') ?? 0); $invoiceTotal = (float) ($invoice->total ?? 0); $returnableAmount = max(0, $invoiceTotal - $returnedTotal); $canReturnAmount = $returnableAmount > 0.0001; $canReturn = $canReturnStatus && $canReturnAmount; @endphp @if($canReturn) عمل مرتجع مبيعات (المتاح: {{ number_format($returnableAmount, 2) }}) @elseif($canReturnStatus && !$canReturnAmount) @endif @endif @if($status !== 'cancelled') {{-- ترحيل --}} @if(Route::has('sales_invoices.post') && empty($invoice->journal_entry_id))
@csrf
@endif {{-- إلغاء --}} @if(Route::has('sales_invoices.cancel'))
@csrf
@endif @endif
@include('admin.Alerts')
{{-- LEFT: Details --}}
{{-- Invoice Meta --}}
بيانات الفاتورة
@if(!empty($invoice->journal_entry_id)) JE ID: #{{ $invoice->journal_entry_id }} @else No Journal Entry @endif @if($invoice->posted_at) Posted: {{ \Carbon\Carbon::parse($invoice->posted_at)->format('Y-m-d H:i') }} @endif
رقم الفاتورة
{{ $invoice->invoice_number }}
كود الفاتورة
{{ $invoice->invoice_code ?? '-' }}
الحالة
{{ $statusBadge[1] }}
نوع الدفع
{{ $payBadge[1] }}
العميل
{{ $customer?->name ?? '-' }} @if($customer?->code) — #{{ $customer->code }} @endif
{{ $customer?->phone ? ('📞 '.$customer->phone) : '' }}
تاريخ الفاتورة
{{ $date($invoice->invoice_date) }}
الاستحقاق
{{ $date($invoice->due_date) }}
للآجل فقط غالبًا.
آخر تحديث بواسطة
{{ $invoice->updated_by ?? '-' }}
Posted By: {{ $invoice->posted_by ?? '-' }}
@if($invoice->notes)
ملاحظات
{{ $invoice->notes }}
@endif
{{-- ✅ Journal Entry (Posting) - FULL CARD --}} {{-- Journal Entry (Inline) --}} {{-- All Journal Entries --}}
القيود المرتبطة بالفاتورة
Count: {{ ($journalEntries ?? collect())->count() }}
@if(($journalEntries ?? collect())->count()) @foreach($journalEntries as $row) @php $je = $row['je']; $lines = $je->lines ?? collect(); $jeStatus = $je->status ?? 'posted'; $jeBadge = match($jeStatus){ 'posted' => ['success','Posted'], 'draft' => ['warn','Draft'], 'reversed' => ['danger','Reversed'], default => ['primary',$jeStatus], }; @endphp
{{ $row['label'] }} {{ $je->entry_number ?? ('#'.$je->id) }} {{ $date($je->entry_date) }} {{ $jeBadge[1] }} {{ $je->source ?? '-' }} @if(($row['type'] ?? '') === 'receipt' && !empty($row['payment'])) خزنة: {{ $row['payment']->treasury?->name ?? ('#'.$row['payment']->treasury_id) }} Amount: {{ $fmt($row['payment']->amount, 2) }} Method: {{ $row['payment']->method }} @endif
الوصف
{{ $je->description ?? '-' }}
إجمالي مدين
{{ $fmt($je->total_debit, 2) }}
إجمالي دائن
{{ $fmt($je->total_credit, 2) }}
@forelse($lines as $l) @empty @endforelse
الحساب # مدين دائن Memo
{{ $l->account?->name ?? ('#'.$l->account_id) }}
{{ $l->account?->account_number ? ('#'.$l->account->account_number) : '' }} @if($l->currency_code) {{ $l->currency_code }} @endif @if($l->branch_id) Branch: {{ $l->branch_id }} @endif
#{{ $l->line_no }} {{ $fmt($l->debit, 2) }} {{ $fmt($l->credit, 2) }} {{ $l->memo ?? '-' }}
لا توجد سطور للقيد.
@endforeach @else
لا توجد قيود مرتبطة
قيد الفاتورة يظهر بعد الترحيل (Post)، وقيود التحصيل تظهر بعد تسجيل الدفعات في sales_payments.
@endif
{{-- Items --}}
الأصناف
عدد السطور: {{ $invoice->items?->count() ?? 0 }}
@forelse($invoice->items as $line) @php $it = $line->item; $code = $it?->items_code ?? $it?->code ?? null; $barcode = $it?->barcode ?? null; @endphp @empty @endforelse
الصنف الكود/باركود Qty Price Disc VAT Total
{{ $it?->name ?? ('#'.$line->item_id) }}
{{ $it?->type ? ('نوع: '.$it->type) : '' }}
@if($code) #{{ $code }} @endif @if($code && $barcode) @endif @if($barcode) {{ $barcode }} @endif @if(!$code && !$barcode) - @endif {{ $fmt($line->quantity, 4) }} {{ $fmt($line->price, 4) }} {{ $fmt($line->discount, 4) }} {{ $fmt($line->vat, 4) }} {{ $fmt($line->total, 4) }}
لا توجد أصناف داخل الفاتورة.
* إجمالي السطر عندك في الجدول = (بعد الخصم + VAT) — مطابق لطريقة recalcInvoice.
{{-- Payments --}}
المدفوعات
Count: {{ $payments->count() }} Sum: {{ $fmt($payments->sum('amount'), 2) }}
@if($payments->count())
@foreach($payments as $p) @endforeach
التاريخ الخزنة المبلغ مرجع JE
{{ $date($p->payment_date) }} {{ $p->treasury?->name ?? ('#'.$p->treasury_id) }} {{ $fmt($p->amount, 2) }} {{ $p->reference ?? '-' }} @if($p->journal_entry_id) #{{ $p->journal_entry_id }} @else - @endif
@else
لا توجد مدفوعات مسجلة
لو الفاتورة Cash وبتسجل Split Payment، المفروض يتخزن هنا (sales_payments) علشان يبقى مصدر الحقيقة للحسابات.
@endif
{{-- Returns --}}
المرتجعات
{{ $hasReturns ? 'يوجد مرتجعات' : 'لا يوجد' }}
@if($hasReturns)
@foreach($invoice->returns as $r) @endforeach
# تاريخ المرتجع الإجمالي
#{{ $r->id }} {{ $date($r->return_date) }} {{ $fmt($r->total, 2) }}
@else
@endif
{{-- RIGHT: Totals / Status --}}
الإجماليات
Auto
Subtotal
{{ $fmt($invoice->subtotal, 2) }}
بعد خصم الفاتورة (discount_amount) وقبل VAT.
Discount
{{ $fmt($invoice->discount_amount, 2) }}
خصم على مستوى الفاتورة.
VAT
{{ $fmt($invoice->vat_amount, 2) }}
مجموع VAT من السطور.
Total
{{ $fmt($invoice->total, 2) }}
Total = subtotal + VAT.
Paid
{{ $fmt($invoice->paid_amount, 2) }}
مدفوع (من payments إن وجدت).
Remaining
{{ $fmt($invoice->remaining_amount, 2) }}
متبقي.
الحالة الحالية
{{ $statusBadge[1] }}
نوع الدفع
{{ $payBadge[1] }}
@endsection