@extends('dashboard.layouts.app') @section('content') @include('components.alert') @php $isEdit = isset($purchase); $requestedOrderType = request('type') === 'purchase' || request('type') === 'po' ? 'purchase' : 'sale'; $initialInvoiceType = old('invoice_type', $purchase->invoice_type ?? $requestedOrderType); $initialPartyType = old('party_type', $purchase->party_type ?? ($initialInvoiceType === 'purchase' ? 'company' : 'student')); $initialPartyType = $initialPartyType === 'employee' ? 'staff' : $initialPartyType; $isSaleOrder = $initialInvoiceType === 'sale'; $isStudentSale = $isSaleOrder && $initialPartyType === 'student'; $linkedStudentInvoices = collect(); $linkedPurchaseInvoices = collect(); if ($isEdit) { $linkedStudentInvoices = $purchase->studentInvoices ->merge($purchase->details->pluck('studentInvoice')->filter()) ->when($purchase->studentInvoice, fn ($invoices) => $invoices->push($purchase->studentInvoice)) ->unique('id') ->values(); $linkedPurchaseInvoices = $purchase->purchaseInvoices->when( $purchase->purchaseInvoice, fn ($invoices) => $invoices->push($purchase->purchaseInvoice) )->unique('id')->values(); } $hasInvoice = $isEdit && ( $purchase->purchase_invoice_id || $purchase->student_invoice_id || $purchase->details->contains(fn ($detail) => !empty($detail->student_invoice_id)) || $linkedStudentInvoices->isNotEmpty() || $linkedPurchaseInvoices->isNotEmpty() ); $canAddOrderInvoice = $isEdit && $isSaleOrder && !$isStudentSale && !$hasInvoice && $linkedPurchaseInvoices->isEmpty(); $canAddPurchaseInvoice = $isEdit && !$isSaleOrder && !$hasInvoice && $linkedPurchaseInvoices->isEmpty(); $statusClass = $hasInvoice ? 'ui-pill--success' : 'ui-pill--warn'; $statusLabel = $hasInvoice ? 'Invoiced' : 'Pending'; $selectedStaffIds = old('staff_ids', $isEdit ? $purchase->requestedStaffs->pluck('id')->all() : []); $selectedVendorIds = old('vendor_ids', $isEdit ? $purchase->vendors->pluck('id')->all() : []); $selectedStaffPartyId = old('staff_party_id'); if (!$selectedStaffPartyId && $isEdit && $initialPartyType === 'staff') { $selectedStaffPartyId = ($staffs ?? collect())->first(function ($staff) use ($purchase) { $staffName = trim((string) ($staff->full_name ?: trim(($staff->first_name ?? '') . ' ' . ($staff->last_name ?? '')))); return $staffName !== '' && $staffName === trim((string) $purchase->name); })?->id; } $oldDetails = old('details', $isEdit ? $purchase->details->map(fn ($detail) => [ 'product_name' => $detail->product_name, 'category_name' => $detail->category_name, 'booking_id' => $detail->booking_id, 'course_id' => $detail->course_id, 'serial_number' => $detail->serial_number, 'part_no' => $detail->part_no, 'price' => $detail->price, 'quantity' => $detail->quantity, 'tax_rate' => $detail->tax_rate, ])->all() : [ ['product_name' => '', 'price' => '', 'quantity' => 1, 'tax_rate' => 14], ['product_name' => '', 'price' => '', 'quantity' => 1, 'tax_rate' => 14], ]); $selectedBookingIds = collect($oldDetails)->pluck('booking_id')->filter()->values()->all(); $backRoute = $initialInvoiceType === 'purchase' ? route('purchase.orders.index') : route('purchase.index'); $backLabel = $initialInvoiceType === 'purchase' ? 'Back to Purchase Orders' : 'Back to Sales Orders'; @endphp
{{ $backLabel }}
{{ $isEdit ? 'Update Order' : ($initialInvoiceType === 'purchase' ? 'New Purchase Order' : 'New Sales Order') }}

{{ $purchase->purchase_number ?? ($initialInvoiceType === 'purchase' ? 'PO-DRAFT' : 'SO-DRAFT') }}

{{ $isEdit ? $statusLabel : 'Draft Mode' }}
@csrf @if($isEdit) @method('PUT') @endif
Order Type & Party

Choose order type, party, dates, and cost center.

@if($isEdit)
{{ $isSaleOrder ? 'Sales Order' : 'Purchase Order' }} Order type is fixed after creation
@endif
Order Items

Add item descriptions, quantities, prices, and VAT.

@foreach ($oldDetails as $index => $detail) @endforeach
Description Qty Price Total
0.00
Totals & Automatic Journal Entry

Preview totals and the balanced journal entry before posting.

Total Before Tax 0.00
VAT 0.00
Amount Due 0.00
@if($isEdit && $isSaleOrder)
Sales Order Invoices

Create linked invoices and review all invoices associated with this sales order.

@if(!$hasInvoice) @endif
@if($isStudentSale)
@forelse($purchase->details as $detail) @empty @endforelse
# Item / Course Booking ID Course ID Linked Invoice Action
{{ $loop->iteration }} {{ $detail->product_name ?: '--' }} {{ $detail->booking_id ?? '--' }} {{ $detail->course_id ?? '--' }} @if($detail->studentInvoice) {{ $detail->studentInvoice->invoice_number }} @else -- @endif @if($detail->booking_id && $detail->course_id) @if($detail->studentInvoice) Open Invoice @else {{ $hasInvoice ? 'Invoice already exists' : 'Use Add Invoice' }} @endif @else Missing booking data @endif
No sales order items found.
@endif
@foreach($linkedStudentInvoices as $invoice) @endforeach @foreach($linkedPurchaseInvoices as $invoice) @endforeach @if($linkedStudentInvoices->isEmpty() && $linkedPurchaseInvoices->isEmpty()) @endif
# Invoice Number Type Status Total Created / Date Action
{{ $loop->iteration }} {{ $invoice->invoice_number }} Sales Invoice {{ ucwords(str_replace('_', ' ', $invoice->payment_status ?? '--')) }} {{ number_format((float) ($invoice->final_amount ?: $invoice->net_price ?: $invoice->total_price ?: 0), 2) }} {{ optional($invoice->created_at)->format('M d, Y') ?: '--' }} Open
{{ $linkedStudentInvoices->count() + $loop->iteration }} {{ $invoice->invoice_number }} Order Invoice {{ ucwords(str_replace('_', ' ', $invoice->status ?? '--')) }} {{ number_format((float) ($invoice->total ?? 0), 2) }} {{ optional($invoice->invoice_date)->format('M d, Y') ?: '--' }} Open
No invoices linked to this sales order yet.
@endif @if($isEdit && !$isSaleOrder)
Purchase Order Invoice

Create and review the vendor invoice linked to this purchase order.

@if(!$hasInvoice) @endif
@forelse($linkedPurchaseInvoices as $invoice) @empty @endforelse
# Invoice Number Status Total Paid Remaining Action
{{ $loop->iteration }} {{ $invoice->invoice_number }} {{ ucwords(str_replace('_', ' ', $invoice->status ?? '--')) }} {{ number_format((float) ($invoice->total ?? 0), 2) }} {{ number_format((float) ($invoice->paid_amount ?? 0), 2) }} {{ number_format((float) ($invoice->remaining_amount ?? 0), 2) }} Open
No invoice linked to this purchase order yet.
@endif
Cancel
@if($isEdit && $isStudentSale) @endif @if($isEdit && $isSaleOrder && !$isStudentSale) @endif @if($isEdit && !$isSaleOrder) @endif
@endsection @push('scripts') @endpush