From dbf44c21292156ce5ea1dad9448047fb0ea98e17 Mon Sep 17 00:00:00 2001 From: alealien666 Date: Fri, 21 Feb 2025 04:42:36 +0700 Subject: [PATCH] insyaallah done manual payment fe --- app/Http/Controllers/PaymentController.php | 38 +++- ...1_27_080152_create_payment_types_table.php | 2 +- ...025_01_28_080108_create_payments_table.php | 2 +- resources/js/Components/ModalInput.jsx | 172 ++++++++++++++---- .../list-admin/payment/ManualPayment.jsx | 4 +- 5 files changed, 173 insertions(+), 45 deletions(-) diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 248c4fd..77e6de1 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -5,9 +5,12 @@ use App\Models\Payment; use Illuminate\Http\Request; use App\Models\Santri; +use App\Models\PaymentType; +use App\Models\DetailPayment; use Inertia\Inertia; use App\Services\cekDenda; use App\Services\GenerateMonthlyBill; +use Exception; class PaymentController extends Controller { @@ -16,16 +19,18 @@ public function indexManualPayment(cekDenda $cekDenda, GenerateMonthlyBill $gene $penalty = $cekDenda->applyPenalty(); $bill = $generateMonthlyBill->generateAutoBill(); + $paymentType = PaymentType::pluck('payment_type'); + $nominal = PaymentType::pluck('nominal'); + + $paymentPenalties = DetailPayment::with('paymentType') + ->get() + ->pluck('penalty', 'type_id'); $santri = Santri::with([ 'payments.detailPayments.paymentType', 'user' ])->get(); - // $santri = Payment::with('detailPayments')->get(); - - // dd($santri); - return Inertia::render('list-admin/payment/ManualPayment', [ 'santri' => $santri, 'penalty' => $penalty, @@ -35,11 +40,30 @@ public function indexManualPayment(cekDenda $cekDenda, GenerateMonthlyBill $gene 'nama' => 'text', 'status_santri' => 'text', 'role_santri' => 'text', - 'total_penalty' => 'text', - 'amount' => 'text', + // 'total_penalty' => 'text', + // 'amount_payment' => 'text', + // 'nominal' => 'text', + // 'payment_type' => 'select', + ], + 'options' => [ + 'payment_type' => $paymentType, + 'payment_nominal' => $nominal, + 'payment_penalty' => $paymentPenalties ] ]); } - public function manualPayment() {} + public function manualPayment(Request $request, $id) + { + $request->validate([ + 'amount' => 'required', + 'penalty' => 'nullable' + ], [ + 'amount.required' => 'wajib mengisi nominal pembayaran', + ]); + + try { + } catch (Exception $e) { + } + } } diff --git a/database/migrations/2025_01_27_080152_create_payment_types_table.php b/database/migrations/2025_01_27_080152_create_payment_types_table.php index bad022c..e598ba7 100644 --- a/database/migrations/2025_01_27_080152_create_payment_types_table.php +++ b/database/migrations/2025_01_27_080152_create_payment_types_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('payment_types', function (Blueprint $table) { $table->id(); $table->string('payment_type'); - $table->float('amount'); + $table->float('nominal'); $table->timestamps(); }); } diff --git a/database/migrations/2025_01_28_080108_create_payments_table.php b/database/migrations/2025_01_28_080108_create_payments_table.php index e622327..a9b8584 100644 --- a/database/migrations/2025_01_28_080108_create_payments_table.php +++ b/database/migrations/2025_01_28_080108_create_payments_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('payments', function (Blueprint $table) { $table->id(); $table->enum('payment_status', ['pending', 'failed', 'success']); - $table->float('amount_payment'); + $table->float('amount_payment')->nullable(); $table->String('bank')->nullable(); $table->string('no_va')->nullable(); $table->dateTime('expired_at')->nullable(); diff --git a/resources/js/Components/ModalInput.jsx b/resources/js/Components/ModalInput.jsx index c17593e..afc5549 100644 --- a/resources/js/Components/ModalInput.jsx +++ b/resources/js/Components/ModalInput.jsx @@ -4,9 +4,13 @@ import React, { useState, useEffect } from "react"; const ModalInput = ({ fields, tableName, options, initialData, onClose }) => { const [formData, setFormData] = useState({}); const [errors, setErrors] = useState({}); + const [selectedPayments, setSelectedPayments] = useState([]); + const [paymentDetails, setPaymentDetails] = useState({}); useEffect(() => { - setFormData(initialData || {}) + setFormData(initialData || {}); + setSelectedPayments([]); + setPaymentDetails({}); }, [initialData]); const handleChange = (e) => { @@ -17,41 +21,75 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose }) => { } }; - const handleSubmit = (e) => { - e.preventDefault() - // console.log("tableName:", tableName); + const handlePaymentChange = (e) => { + const value = e.target.value; + if (!selectedPayments.includes(value)) { + setSelectedPayments([...selectedPayments, value]); - const formDataObj = new FormData() - Object.keys(formData).forEach((key) => { - if (key === 'foto' && !(formData[key] instanceof File)) { - return - } - formDataObj.append(key, formData[key]) - }) + const nominal = options.payment_nominal?.[value] || 0; + const penalty = options.payment_penalty?.[value] || 0; - if (initialData) { - Inertia.post(`/update${tableName}/${initialData.id}`, formDataObj, { - forceFormData: true, - onError: (errors) => setErrors(errors), - onSuccess: () => { - document.getElementById('modal_input').checked = false - setFormData({}) - setErrors({}) - onClose({}) - } - }) - } else { - Inertia.post(`/add${tableName}`, formDataObj, { - forceFormData: true, - onError: (errors) => setErrors(errors), - onSuccess: () => { - document.getElementById('modal_input').checked = false - setFormData({}) - setErrors({}) - } - }) + setPaymentDetails({ + ...paymentDetails, + [value]: { + range: 1, + nominal, + penalty, + amount: nominal + penalty, + }, + }); } - } + }; + + const handleRangeChange = (paymentType, newRange) => { + setPaymentDetails((prevDetails) => { + const validRange = newRange && !isNaN(newRange) ? Math.max(1, Number(newRange)) : 1; + const currentDetails = prevDetails[paymentType] || { nominal: 0, penalty: 0 }; + + const newAmount = (currentDetails.nominal + currentDetails.penalty) * validRange; + + return { + ...prevDetails, + [paymentType]: { + ...currentDetails, + range: validRange, + amount: newAmount, // Pastikan amount diperbarui + }, + }; + }); + }; + + + const handleRemovePayment = (paymentType) => { + setSelectedPayments(selectedPayments.filter((p) => p !== paymentType)); + const newDetails = { ...paymentDetails }; + delete newDetails[paymentType]; + setPaymentDetails(newDetails); + }; + + const handleSubmit = (e) => { + e.preventDefault(); + const formDataObj = new FormData(); + + Object.entries(formData).forEach(([key, value]) => { + if (key === 'foto' && !(value instanceof File)) return; + formDataObj.append(key, value); + }); + + formDataObj.append("payments", JSON.stringify(paymentDetails)); + + const url = initialData ? `/update${tableName}/${initialData.id}` : `/add${tableName}`; + Inertia.post(url, formDataObj, { + forceFormData: true, + onError: (errors) => setErrors(errors), + onSuccess: () => { + document.getElementById("modal_input").checked = false; + setFormData({}); + setErrors({}); + if (onClose) onClose({}); + }, + }); + }; return (
@@ -102,7 +140,73 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose }) => {
))} - + +
+ +
+ +
+ +
+ +
+ +
+ + + + ))} +
+ +
+ + diff --git a/resources/js/Pages/list-admin/payment/ManualPayment.jsx b/resources/js/Pages/list-admin/payment/ManualPayment.jsx index 10fb12d..dc20481 100644 --- a/resources/js/Pages/list-admin/payment/ManualPayment.jsx +++ b/resources/js/Pages/list-admin/payment/ManualPayment.jsx @@ -2,12 +2,12 @@ import React, { useState } from 'react'; import { Head } from '@inertiajs/react'; import ModalInput from '@/Components/ModalInput'; -export default function ManualPayment({ santri, penalty, bill, fields }) { +export default function ManualPayment({ santri, penalty, bill, fields, options }) { const [selectedSantri, setSelectedSantri] = useState(null); return (
- setSelectedSantri(null)} /> + setSelectedSantri(null)} /> {santri && santri.length > 0 ? santri.map((item, i) => (

Nis: {item.user.nis}