{{-- resources/views/admin/sales_invoices/edit.blade.php --}} @extends('admin.layouts.master') @section('title', 'تعديل فاتورة مبيعات') @section('css') @endsection @section('content') @php // ====== Safe defaults (لو الكنترولر مش باعتهم) ====== $today = now()->timezone('Africa/Cairo')->toDateString(); $warehouses = $warehouses ?? collect(); $treasuries = $treasuries ?? collect(); $terminals = $terminals ?? collect(); $openShiftTreasuryId = $openShiftTreasuryId ?? null; $openShiftTreasuryName = $openShiftTreasuryName ?? null; // ====== Pre-fill payments from invoice (لو invoice->payments موجودة) ====== $invoice->loadMissing(['customer','items.item','payments']); $invPayCash = (float) ($invoice->payments?->where('method','cash')->sum('amount') ?? 0); $invPayCard = (float) ($invoice->payments?->where('method','card')->sum('amount') ?? 0); $invPayWallet = (float) ($invoice->payments?->where('method','wallet')->sum('amount') ?? 0); $defaultPayCash = (float) old('payment.cash', $invPayCash); $defaultPayCard = (float) old('payment.card', $invPayCard); $defaultPayWallet = (float) old('payment.wallet', $invPayWallet); // ====== Global UI defaults ====== $defaultGlobalDiscType = old('global_discount_type', 'amount'); $defaultGlobalDiscValue = (float) old('global_discount_value', 0); // VAT default for new lines $defaultVatRate = (float) old('global_vat_rate', 14); // ====== IMPORTANT: Prepare items array in PHP to avoid Blade ParseError ====== $itemsData = $invoice->items->map(function ($it) { $qty = (float) ($it->quantity ?? 0); $price = (float) ($it->price ?? 0); $gross = $qty * $price; // لو اسم الخصم عندك مختلف غيّره هنا $discAmount = (float) ($it->discount ?? 0); $base = max(0, $gross - $discAmount); // لو اسم vat عندك مختلف غيّره هنا $vatAmount = (float) ($it->vat ?? 0); $vatRate = 0.0; if ($base > 0.0001 && $vatAmount > 0) { $vatRate = round(($vatAmount / $base) * 100, 4); } return [ 'item_id' => (int) $it->item_id, 'item_text' => $it->item->name ?? ('Item#' . $it->item_id), 'qty' => round($qty, 4), 'price' => round($price, 4), 'discount_value' => round($discAmount, 4), 'discount_type' => 'amount', 'vat_rate' => round($vatRate, 4), 'cost_price' => round((float) ($it->cost_price ?? 0), 4), ]; })->values()->toArray(); @endphp {{-- Top Bar --}}
@include('admin.Alerts') @if ($errors->any())