|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api\V1; |
| 4 | + |
| 5 | +use App\Modules\Finance\Models\PaymentSchedule; |
| 6 | +use App\Modules\Finance\Models\PaymentScheduleItem; |
| 7 | +use App\Modules\Finance\Models\PaymentTerm; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | + |
| 11 | +class PaymentTermApiController extends ApiController |
| 12 | +{ |
| 13 | + // ── Payment Terms ───────────────────────────────────────────────────────── |
| 14 | + |
| 15 | + public function indexTerms(Request $request): JsonResponse |
| 16 | + { |
| 17 | + $tenantId = $this->tenantId($request); |
| 18 | + $terms = PaymentTerm::where('tenant_id', $tenantId) |
| 19 | + ->when($request->boolean('active_only'), fn ($q) => $q->where('is_active', true)) |
| 20 | + ->orderBy('days') |
| 21 | + ->get(); |
| 22 | + |
| 23 | + return $this->success($terms->map(fn ($t) => [ |
| 24 | + 'id' => $t->id, |
| 25 | + 'name' => $t->name, |
| 26 | + 'days' => $t->days, |
| 27 | + 'discount_days' => $t->discount_days, |
| 28 | + 'discount_percent' => $t->discount_percent, |
| 29 | + 'has_early_discount' => $t->has_early_discount, |
| 30 | + 'display_label' => $t->display_label, |
| 31 | + 'description' => $t->description, |
| 32 | + 'is_active' => $t->is_active, |
| 33 | + ])); |
| 34 | + } |
| 35 | + |
| 36 | + public function storeTerm(Request $request): JsonResponse |
| 37 | + { |
| 38 | + $tenantId = $this->tenantId($request); |
| 39 | + |
| 40 | + $data = $request->validate([ |
| 41 | + 'name' => ['required', 'string', 'max:100'], |
| 42 | + 'days' => ['required', 'integer', 'min:0'], |
| 43 | + 'discount_days' => ['nullable', 'integer', 'min:0'], |
| 44 | + 'discount_percent' => ['nullable', 'numeric', 'min:0', 'max:100'], |
| 45 | + 'description' => ['nullable', 'string'], |
| 46 | + ]); |
| 47 | + |
| 48 | + $term = PaymentTerm::create([...$data, 'tenant_id' => $tenantId, 'is_active' => true]); |
| 49 | + |
| 50 | + return $this->success($term, 201); |
| 51 | + } |
| 52 | + |
| 53 | + public function updateTerm(Request $request, PaymentTerm $paymentTerm): JsonResponse |
| 54 | + { |
| 55 | + $data = $request->validate([ |
| 56 | + 'name' => ['sometimes', 'string', 'max:100'], |
| 57 | + 'days' => ['sometimes', 'integer', 'min:0'], |
| 58 | + 'discount_days' => ['nullable', 'integer', 'min:0'], |
| 59 | + 'discount_percent' => ['nullable', 'numeric', 'min:0', 'max:100'], |
| 60 | + 'description' => ['nullable', 'string'], |
| 61 | + 'is_active' => ['boolean'], |
| 62 | + ]); |
| 63 | + |
| 64 | + $paymentTerm->update($data); |
| 65 | + |
| 66 | + return $this->success($paymentTerm->fresh()); |
| 67 | + } |
| 68 | + |
| 69 | + public function destroyTerm(PaymentTerm $paymentTerm): JsonResponse |
| 70 | + { |
| 71 | + $paymentTerm->delete(); |
| 72 | + return $this->success(['message' => 'Payment term deleted.']); |
| 73 | + } |
| 74 | + |
| 75 | + // ── Payment Schedules ───────────────────────────────────────────────────── |
| 76 | + |
| 77 | + public function indexSchedules(Request $request): JsonResponse |
| 78 | + { |
| 79 | + $tenantId = $this->tenantId($request); |
| 80 | + $schedules = PaymentSchedule::where('tenant_id', $tenantId) |
| 81 | + ->when($request->status, fn ($q) => $q->where('status', $request->status)) |
| 82 | + ->with('items') |
| 83 | + ->orderByDesc('created_at') |
| 84 | + ->paginate(20); |
| 85 | + |
| 86 | + return $this->paginated($schedules); |
| 87 | + } |
| 88 | + |
| 89 | + public function storeSchedule(Request $request): JsonResponse |
| 90 | + { |
| 91 | + $tenantId = $this->tenantId($request); |
| 92 | + |
| 93 | + $data = $request->validate([ |
| 94 | + 'name' => ['required', 'string', 'max:255'], |
| 95 | + 'total_amount' => ['required', 'numeric', 'min:0.01'], |
| 96 | + 'currency' => ['nullable', 'string', 'max:3'], |
| 97 | + 'frequency' => ['required', 'string', 'in:weekly,monthly,quarterly,yearly,custom'], |
| 98 | + 'installments' => ['required', 'integer', 'min:1', 'max:120'], |
| 99 | + 'start_date' => ['required', 'date'], |
| 100 | + 'reference_type' => ['nullable', 'string', 'max:50'], |
| 101 | + 'reference_id' => ['nullable', 'integer'], |
| 102 | + 'notes' => ['nullable', 'string'], |
| 103 | + ]); |
| 104 | + |
| 105 | + $schedule = PaymentSchedule::create([ |
| 106 | + ...$data, |
| 107 | + 'tenant_id' => $tenantId, |
| 108 | + 'created_by' => $request->user()->id, |
| 109 | + 'status' => 'active', |
| 110 | + ]); |
| 111 | + |
| 112 | + $schedule->schedule_number = $schedule->generateScheduleNumber(); |
| 113 | + $schedule->save(); |
| 114 | + |
| 115 | + $this->generateInstallments($schedule); |
| 116 | + |
| 117 | + return $this->success($schedule->load('items'), 201); |
| 118 | + } |
| 119 | + |
| 120 | + public function showSchedule(PaymentSchedule $paymentSchedule): JsonResponse |
| 121 | + { |
| 122 | + return $this->success($paymentSchedule->load('items')); |
| 123 | + } |
| 124 | + |
| 125 | + public function markInstallmentPaid(Request $request, PaymentSchedule $paymentSchedule, int $itemId): JsonResponse |
| 126 | + { |
| 127 | + $item = PaymentScheduleItem::where('payment_schedule_id', $paymentSchedule->id) |
| 128 | + ->findOrFail($itemId); |
| 129 | + |
| 130 | + $data = $request->validate([ |
| 131 | + 'paid_date' => ['nullable', 'date'], |
| 132 | + ]); |
| 133 | + |
| 134 | + $item->markPaid($data['paid_date'] ?? null); |
| 135 | + $paymentSchedule->recalculatePaidAmount(); |
| 136 | + |
| 137 | + return $this->success([ |
| 138 | + 'item' => $item->fresh(), |
| 139 | + 'schedule' => $paymentSchedule->fresh(), |
| 140 | + ]); |
| 141 | + } |
| 142 | + |
| 143 | + public function pauseSchedule(PaymentSchedule $paymentSchedule): JsonResponse |
| 144 | + { |
| 145 | + $paymentSchedule->pause(); |
| 146 | + return $this->success($paymentSchedule->fresh()); |
| 147 | + } |
| 148 | + |
| 149 | + public function cancelSchedule(PaymentSchedule $paymentSchedule): JsonResponse |
| 150 | + { |
| 151 | + $paymentSchedule->cancel(); |
| 152 | + return $this->success($paymentSchedule->fresh()); |
| 153 | + } |
| 154 | + |
| 155 | + private function generateInstallments(PaymentSchedule $schedule): void |
| 156 | + { |
| 157 | + $installmentAmount = round((float) $schedule->total_amount / $schedule->installments, 2); |
| 158 | + $startDate = now()->parse($schedule->start_date); |
| 159 | + |
| 160 | + for ($i = 1; $i <= $schedule->installments; $i++) { |
| 161 | + $dueDate = match ($schedule->frequency) { |
| 162 | + 'weekly' => $startDate->copy()->addWeeks($i - 1), |
| 163 | + 'quarterly' => $startDate->copy()->addMonths(($i - 1) * 3), |
| 164 | + 'yearly' => $startDate->copy()->addYears($i - 1), |
| 165 | + default => $startDate->copy()->addMonths($i - 1), // monthly / custom |
| 166 | + }; |
| 167 | + |
| 168 | + PaymentScheduleItem::create([ |
| 169 | + 'payment_schedule_id' => $schedule->id, |
| 170 | + 'installment_number' => $i, |
| 171 | + 'amount' => $i === $schedule->installments |
| 172 | + ? (float) $schedule->total_amount - ($installmentAmount * ($schedule->installments - 1)) |
| 173 | + : $installmentAmount, |
| 174 | + 'due_date' => $dueDate->toDateString(), |
| 175 | + 'status' => 'pending', |
| 176 | + ]); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private function tenantId(Request $request): int |
| 181 | + { |
| 182 | + return app()->has('tenant') ? app('tenant')->id : $request->user()->tenant_id; |
| 183 | + } |
| 184 | +} |
0 commit comments