|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Finance\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Finance\Models\BatchPayment; |
| 7 | +use App\Modules\Finance\Models\Bill; |
| 8 | +use App\Modules\Finance\Models\BillPayment; |
| 9 | +use App\Modules\Finance\Models\Invoice; |
| 10 | +use App\Modules\Finance\Models\Payment; |
| 11 | +use Illuminate\Http\RedirectResponse; |
| 12 | +use Illuminate\Http\Request; |
| 13 | +use Illuminate\Support\Facades\DB; |
| 14 | +use Inertia\Inertia; |
| 15 | +use Inertia\Response; |
| 16 | + |
| 17 | +class BatchPaymentController extends Controller |
| 18 | +{ |
| 19 | + public function index(): Response |
| 20 | + { |
| 21 | + $this->authorize('viewAny', BatchPayment::class); |
| 22 | + |
| 23 | + $batches = BatchPayment::withCount('payments') |
| 24 | + ->orderByDesc('payment_date') |
| 25 | + ->paginate(25); |
| 26 | + |
| 27 | + return Inertia::render('Finance/BatchPayments/Index', compact('batches')); |
| 28 | + } |
| 29 | + |
| 30 | + public function create(Request $request): Response |
| 31 | + { |
| 32 | + $this->authorize('create', BatchPayment::class); |
| 33 | + |
| 34 | + $type = $request->get('type', 'received'); |
| 35 | + |
| 36 | + if ($type === 'received') { |
| 37 | + $openItems = Invoice::with('contact') |
| 38 | + ->whereIn('status', ['sent', 'partial']) |
| 39 | + ->orderBy('due_date') |
| 40 | + ->get(['id', 'number', 'contact_id', 'due_date', 'currency_code', 'status']); |
| 41 | + } else { |
| 42 | + $openItems = Bill::with('contact') |
| 43 | + ->whereIn('status', ['received', 'partial']) |
| 44 | + ->orderBy('due_date') |
| 45 | + ->get(['id', 'number', 'contact_id', 'due_date', 'currency_code', 'status']); |
| 46 | + } |
| 47 | + |
| 48 | + // Load items and payments for computed totals |
| 49 | + $openItems->load(['items', 'payments']); |
| 50 | + |
| 51 | + return Inertia::render('Finance/BatchPayments/Create', compact('openItems', 'type')); |
| 52 | + } |
| 53 | + |
| 54 | + public function store(Request $request): RedirectResponse |
| 55 | + { |
| 56 | + $this->authorize('create', BatchPayment::class); |
| 57 | + |
| 58 | + $data = $request->validate([ |
| 59 | + 'reference' => 'required|string|max:100|unique:batch_payments,reference', |
| 60 | + 'payment_date' => 'required|date', |
| 61 | + 'payment_method' => 'required|in:bank_transfer,cheque,cash,card,other', |
| 62 | + 'type' => 'required|in:received,made', |
| 63 | + 'notes' => 'nullable|string', |
| 64 | + 'payments' => 'required|array|min:1', |
| 65 | + 'payments.*.id' => 'required|integer', |
| 66 | + 'payments.*.amount' => 'required|numeric|min:0.01', |
| 67 | + ]); |
| 68 | + |
| 69 | + DB::transaction(function () use ($data) { |
| 70 | + $totalAmount = collect($data['payments'])->sum('amount'); |
| 71 | + |
| 72 | + $batch = BatchPayment::create([ |
| 73 | + 'tenant_id' => auth()->user()->tenant_id, |
| 74 | + 'reference' => $data['reference'], |
| 75 | + 'payment_date' => $data['payment_date'], |
| 76 | + 'payment_method' => $data['payment_method'], |
| 77 | + 'type' => $data['type'], |
| 78 | + 'total_amount' => $totalAmount, |
| 79 | + 'notes' => $data['notes'] ?? null, |
| 80 | + ]); |
| 81 | + |
| 82 | + foreach ($data['payments'] as $p) { |
| 83 | + if ($data['type'] === 'received') { |
| 84 | + $invoice = Invoice::with(['items', 'payments'])->findOrFail($p['id']); |
| 85 | + $outstanding = $invoice->total - $invoice->amount_paid; |
| 86 | + $amount = min((float) $p['amount'], $outstanding); |
| 87 | + |
| 88 | + if ($amount <= 0) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + Payment::create([ |
| 93 | + 'tenant_id' => auth()->user()->tenant_id, |
| 94 | + 'invoice_id' => $invoice->id, |
| 95 | + 'amount' => $amount, |
| 96 | + 'payment_date' => $data['payment_date'], |
| 97 | + 'method' => $data['payment_method'], |
| 98 | + 'reference' => $data['reference'], |
| 99 | + 'batch_payment_id' => $batch->id, |
| 100 | + ]); |
| 101 | + |
| 102 | + // Reload to get updated amount_paid |
| 103 | + $invoice->load(['items', 'payments']); |
| 104 | + if ($invoice->amount_due <= 0 && $invoice->canTransitionTo('paid')) { |
| 105 | + $invoice->transitionTo('paid'); |
| 106 | + } elseif ($invoice->amount_paid > 0 && $invoice->canTransitionTo('partial')) { |
| 107 | + $invoice->transitionTo('partial'); |
| 108 | + } |
| 109 | + } else { |
| 110 | + $bill = Bill::with(['items', 'payments'])->findOrFail($p['id']); |
| 111 | + $outstanding = $bill->total - $bill->amount_paid; |
| 112 | + $amount = min((float) $p['amount'], $outstanding); |
| 113 | + |
| 114 | + if ($amount <= 0) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + BillPayment::create([ |
| 119 | + 'tenant_id' => auth()->user()->tenant_id, |
| 120 | + 'bill_id' => $bill->id, |
| 121 | + 'amount' => $amount, |
| 122 | + 'payment_date' => $data['payment_date'], |
| 123 | + 'method' => $data['payment_method'], |
| 124 | + 'reference' => $data['reference'], |
| 125 | + ]); |
| 126 | + |
| 127 | + // Reload to get updated amount_paid |
| 128 | + $bill->load(['items', 'payments']); |
| 129 | + if ($bill->amount_due <= 0 && $bill->canTransitionTo('paid')) { |
| 130 | + $bill->transitionTo('paid'); |
| 131 | + } elseif ($bill->amount_paid > 0 && $bill->canTransitionTo('partial')) { |
| 132 | + $bill->transitionTo('partial'); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + return redirect()->route('finance.batch-payments.index') |
| 139 | + ->with('success', 'Batch payment recorded.'); |
| 140 | + } |
| 141 | + |
| 142 | + public function show(BatchPayment $batchPayment): Response |
| 143 | + { |
| 144 | + $this->authorize('view', $batchPayment); |
| 145 | + |
| 146 | + $batchPayment->load('payments.invoice'); |
| 147 | + |
| 148 | + return Inertia::render('Finance/BatchPayments/Show', compact('batchPayment')); |
| 149 | + } |
| 150 | + |
| 151 | + public function destroy(BatchPayment $batchPayment): RedirectResponse |
| 152 | + { |
| 153 | + $this->authorize('delete', $batchPayment); |
| 154 | + |
| 155 | + $batchPayment->delete(); |
| 156 | + |
| 157 | + return redirect()->route('finance.batch-payments.index') |
| 158 | + ->with('success', 'Batch payment deleted.'); |
| 159 | + } |
| 160 | +} |
0 commit comments