payment phase 3

This commit is contained in:
alealien666 2025-05-08 15:45:59 +07:00
parent 260942462c
commit 8617fce4aa
2 changed files with 130 additions and 98 deletions

View File

@ -21,8 +21,7 @@ public function indexManualPayment(cekDenda $cekDenda, GenerateMonthlyBill $gene
$penalty = $cekDenda->applyPenalty();
$bill = $generateMonthlyBill->generateAutoBill();
$paymentType = PaymentType::pluck('payment_type');
$nominal = PaymentType::pluck('nominal');
$paymentTypes = PaymentType::get(['id', 'payment_type', 'nominal']);
$paymentPenalties = DetailPayment::with('paymentType')
->get()
@ -30,7 +29,7 @@ public function indexManualPayment(cekDenda $cekDenda, GenerateMonthlyBill $gene
$santri = User::with([
'payments.detailPayments.paymentType',
])->paginate(2);
])->paginate(10);
return Inertia::render('list-admin/payment/ManualPayment', [
'santri' => $santri->items(),
@ -42,8 +41,8 @@ public function indexManualPayment(cekDenda $cekDenda, GenerateMonthlyBill $gene
'status_santri' => ['type' => 'text', 'readonly' => true],
],
'options' => [
'payment_type' => $paymentType,
'payment_nominal' => $nominal,
'payment_type' => $paymentTypes->pluck('payment_type', 'id'),
'payment_nominal' => $paymentTypes->pluck('nominal', 'id'),
'payment_penalty' => $paymentPenalties
]
]);
@ -51,59 +50,120 @@ public function indexManualPayment(cekDenda $cekDenda, GenerateMonthlyBill $gene
public function manualPayment(Request $request, $paymentId)
{
// $request->validate([
// 'items' => 'required|array',
// 'items.*.type_id' => 'required|exists:payment_types,id',
// 'items.*.range' => 'required|integer|min:1'
// ]);
$items = $request->input('items');
$userId = $request->id;
$items = json_decode($request->input('items'), true);
try {
DB::beginTransaction();
$range = (int) $request->input('range');
$userId = $request->id;
$typeId = $request->input('type_id');
// Ambil semua payment type sekaligus
$paymentTypes = PaymentType::pluck('nominal', 'id');
$existingPayment = Payment::where('user_id', $userId)
->where('payment_status', 'pending')
->first();
$unpaidDetails = $existingPayment
? DetailPayment::where('payment_id', $existingPayment->id)
->where('status', 'unpaid')
->orderBy('payment_year')
->orderBy('payment_month')
->get()
: collect();
if ($existingPayment && $unpaidDetails->count() > 0) {
if ($existingPayment) {
$totalAmount = 0;
$toPay = $unpaidDetails->take($range);
$jumlahUnpaid = $toPay->count();
foreach ($toPay as $detail) {
$nominal = PaymentType::where('id', $detail->type_id)->value('nominal') ?? 0;
$penalty = $detail->penalty ?? 0;
$total = $nominal + $penalty;
foreach ($items as $item) {
$typeId = $item['type_id'];
$range = $item['range'];
$nominal = $paymentTypes[$typeId] ?? 0;
$detail->update([
'status' => 'paid',
'amount' => $nominal,
'penalty' => $penalty,
]);
// Ambil unpaid details
$unpaidDetails = DetailPayment::where('payment_id', $existingPayment->id)
->where('status', 'unpaid')
->where('type_id', $typeId)
->orderBy('payment_year')
->orderBy('payment_month')
->get();
$totalAmount += $total;
// Proses unpaid details
$toPay = $unpaidDetails->take($range);
$toPay->each(function ($detail) use (&$totalAmount, $nominal) {
$detail->update([
'status' => 'paid',
'amount' => $nominal,
'penalty' => $detail->penalty ?? 0,
]);
$totalAmount += $nominal + $detail->penalty;
});
// Hitung sisa bulan yang perlu dibuat
$sisa = $range - $toPay->count();
if ($sisa > 0) {
// Ambil bulan/tahun terakhir
$lastDetail = DetailPayment::where('payment_id', $existingPayment->id)
->orderBy('payment_year', 'desc')
->orderBy('payment_month', 'desc')
->first();
$bulan = $lastDetail->payment_month ?? now()->month;
$tahun = $lastDetail->payment_year ?? now()->year;
for ($i = 0; $i < $sisa; $i++) {
$bulan++;
if ($bulan > 12) {
$bulan = 1;
$tahun++;
}
DetailPayment::create([
'payment_id' => $existingPayment->id,
'payment_month' => $bulan,
'payment_year' => $tahun,
'amount' => $nominal,
'penalty' => 0,
'status' => 'paid',
'type_id' => $typeId,
]);
$totalAmount += $nominal;
}
}
}
$sisa = $range - $jumlahUnpaid;
$bulan = $unpaidDetails->last()?->payment_month ?? now()->month;
$tahun = $unpaidDetails->last()?->payment_year ?? now()->year;
// Update payment status
$existingPayment->update([
'amount_payment' => DetailPayment::where('payment_id', $existingPayment->id)->sum('amount'),
'payment_status' => DetailPayment::where('payment_id', $existingPayment->id)
->where('status', 'unpaid')
->exists() ? 'pending' : 'success'
]);
for ($i = 0; $i < $sisa; $i++) {
$bulan++;
if ($bulan > 12) {
$bulan = 1;
$tahun++;
}
DB::commit();
return response()->json(['message' => 'Pembayaran berhasil diupdate']);
}
$nominal = PaymentType::where('id', $typeId)->value('nominal') ?? 0;
// Jika tidak ada existing payment, buat baru
$newPayment = Payment::create([
'payment_status' => 'success',
'amount_payment' => 0,
'user_id' => $userId,
]);
$bulan = now()->month;
$tahun = now()->year;
$totalAmount = 0;
foreach ($items as $item) {
$typeId = $item['type_id'];
$range = $item['range'];
$nominal = $paymentTypes[$typeId] ?? 0;
for ($i = 0; $i < $range; $i++) {
DetailPayment::create([
'payment_id' => $existingPayment->id,
'payment_id' => $newPayment->id,
'payment_month' => $bulan,
'payment_year' => $tahun,
'amount' => $nominal,
@ -113,61 +173,24 @@ public function manualPayment(Request $request, $paymentId)
]);
$totalAmount += $nominal;
}
$existingPayment->update([
'amount_payment' => DetailPayment::where('payment_id', $existingPayment->id)->sum('amount'),
'payment_status' => DetailPayment::where('payment_id', $existingPayment->id)->where('status', 'unpaid')->exists() ? 'pending' : 'success',
]);
DB::commit();
return redirect()->back()->with('success', 'Pembayaran berhasil menggunakan data existing');
}
$newPayment = Payment::create([
'payment_status' => 'success',
'amount_payment' => 0,
'bank' => null,
'no_va' => null,
'expired_at' => null,
'user_id' => $userId,
]);
$bulan = now()->month;
$tahun = now()->year;
$nominal = PaymentType::where('id', $typeId)->value('nominal') ?? 0;
$totalAmount = 0;
for ($i = 0; $i < $range; $i++) {
DetailPayment::create([
'payment_id' => $newPayment->id,
'payment_month' => $bulan,
'payment_year' => $tahun,
'amount' => $nominal,
'penalty' => 0,
'status' => 'paid',
'type_id' => $typeId,
]);
$totalAmount += $nominal;
$bulan++;
if ($bulan > 12) {
$bulan = 1;
$tahun++;
$bulan++;
if ($bulan > 12) {
$bulan = 1;
$tahun++;
}
}
}
$newPayment->update([
'amount_payment' => $totalAmount,
]);
$newPayment->update(['amount_payment' => $totalAmount]);
DB::commit();
dd($newPayment);
return redirect()->back()->with('success', 'Pembayaran baru berhasil dibuat');
dd('berhasil lur');
// return redirect()->back()->with(['success' => 'Pembayaran baru berhasil dibuat']);
} catch (Exception $e) {
DB::rollBack();
return dd('Error:', $e->getMessage());
dd('gagal' . $e->getMessage());
return redirect()->back()->with(['error' => 'gagal' . $e->getMessage()]);
}
}
}

View File

@ -24,16 +24,18 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose, showPaym
// console.log(initialData)
const handlePaymentChange = (e) => {
const value = e.target.value;
if (!selectedPayments.includes(value)) {
setSelectedPayments([...selectedPayments, value]);
const typeId = e.target.value;
console.log(typeId)
const nominal = parseInt(options.payment_nominal?.[value]) || 0;
const penalty = parseInt(options.payment_penalty?.[value]) || 0;
if (!selectedPayments.includes(typeId)) {
setSelectedPayments([...selectedPayments, typeId]);
const nominal = parseInt(options.payment_nominal[typeId]) || 0;
const penalty = parseInt(options.payment_penalty[typeId]) || 0;
setPaymentDetails({
...paymentDetails,
[value]: {
[typeId]: {
range: 1,
nominal,
penalty,
@ -80,9 +82,15 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose, showPaym
});
if (showPayments) {
formDataObj.append('payment_details', JSON.stringify(paymentDetails));
const detailsArray = Object.entries(paymentDetails).map(([type_id, detail]) => ({
...detail,
type_id: parseInt(type_id)
}));
console.log('Payment Details:', detailsArray);
formDataObj.append('items', JSON.stringify(detailsArray));
}
const url = initialData
? `/update${tableName}/${initialData.id}`
: `/add${tableName}`;
@ -183,10 +191,11 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose, showPaym
<option disabled value="">
Pilih Payment Type
</option>
// Di bagian dropdown payment type:
{options.payment_type &&
Object.entries(options.payment_type).map(([key, value]) => (
<option key={key} value={key}>
{value}
Object.entries(options.payment_type).map(([id, name]) => (
<option key={id} value={id}> {/* Gunakan ID sebagai value */}
{name}
</option>
))}
</select>