|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Finance\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Finance\Models\PaymentTerm; |
| 7 | +use Illuminate\Http\RedirectResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Inertia\Inertia; |
| 10 | +use Inertia\Response; |
| 11 | + |
| 12 | +class PaymentTermController extends Controller |
| 13 | +{ |
| 14 | + public function index(Request $request): Response |
| 15 | + { |
| 16 | + $this->authorize('viewAny', PaymentTerm::class); |
| 17 | + |
| 18 | + $paymentTerms = PaymentTerm::where('is_active', true) |
| 19 | + ->latest() |
| 20 | + ->paginate(20) |
| 21 | + ->withQueryString(); |
| 22 | + |
| 23 | + return Inertia::render('Finance/PaymentTerms/Index', [ |
| 24 | + 'paymentTerms' => $paymentTerms, |
| 25 | + ]); |
| 26 | + } |
| 27 | + |
| 28 | + public function store(Request $request): RedirectResponse |
| 29 | + { |
| 30 | + $this->authorize('create', PaymentTerm::class); |
| 31 | + |
| 32 | + $validated = $request->validate([ |
| 33 | + 'name' => 'required|string|max:100', |
| 34 | + 'days' => 'required|integer|min:0', |
| 35 | + 'discount_days' => 'nullable|integer|min:0', |
| 36 | + 'discount_percent' => 'nullable|numeric|min:0|max:100', |
| 37 | + 'description' => 'nullable|string', |
| 38 | + 'is_active' => 'boolean', |
| 39 | + ]); |
| 40 | + |
| 41 | + PaymentTerm::create([ |
| 42 | + ...$validated, |
| 43 | + 'tenant_id' => auth()->user()->tenant_id, |
| 44 | + ]); |
| 45 | + |
| 46 | + return redirect()->back()->with('success', 'Payment term created successfully.'); |
| 47 | + } |
| 48 | + |
| 49 | + public function show(PaymentTerm $paymentTerm): Response |
| 50 | + { |
| 51 | + $this->authorize('view', $paymentTerm); |
| 52 | + |
| 53 | + return Inertia::render('Finance/PaymentTerms/Show', [ |
| 54 | + 'paymentTerm' => $paymentTerm, |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + public function update(Request $request, PaymentTerm $paymentTerm): RedirectResponse |
| 59 | + { |
| 60 | + $this->authorize('update', $paymentTerm); |
| 61 | + |
| 62 | + $validated = $request->validate([ |
| 63 | + 'name' => 'required|string|max:100', |
| 64 | + 'days' => 'required|integer|min:0', |
| 65 | + 'discount_days' => 'nullable|integer|min:0', |
| 66 | + 'discount_percent' => 'nullable|numeric|min:0|max:100', |
| 67 | + 'description' => 'nullable|string', |
| 68 | + 'is_active' => 'boolean', |
| 69 | + ]); |
| 70 | + |
| 71 | + $paymentTerm->update($validated); |
| 72 | + |
| 73 | + return redirect()->back()->with('success', 'Payment term updated successfully.'); |
| 74 | + } |
| 75 | + |
| 76 | + public function destroy(PaymentTerm $paymentTerm): RedirectResponse |
| 77 | + { |
| 78 | + $this->authorize('delete', $paymentTerm); |
| 79 | + |
| 80 | + $paymentTerm->delete(); |
| 81 | + |
| 82 | + return redirect()->route('finance.payment-terms.index') |
| 83 | + ->with('success', 'Payment term deleted successfully.'); |
| 84 | + } |
| 85 | +} |
0 commit comments