manual payment semi final be

This commit is contained in:
alealien666 2025-02-19 22:38:22 +07:00
parent 4a765b28d9
commit c91ceae657
2 changed files with 60 additions and 23 deletions

View File

@ -4,6 +4,7 @@
use App\Models\DetailPayment;
use App\Models\Payment;
use App\Models\PaymentType;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
@ -13,39 +14,74 @@ public function generateAutoBill()
{
$currentMonth = Carbon::now()->month;
$currentYear = Carbon::now()->year;
$previousMonth = Carbon::now()->subMonth()->month;
$previousYear = Carbon::now()->subMonth()->year;
DB::beginTransaction();
try {
$previousPayments = Payment::with('detailPayments')
->whereHas('detailPayments', function ($query) {
$query->where('payment_month', Carbon::now()->subMonth()->month)
->where('payment_year', Carbon::now()->subMonth()->year);
})->get();
$santriPayments = Payment::with('detailPayments')->get();
foreach ($previousPayments as $prevPay) {
$existingBill = DetailPayment::where('payment_id', $prevPay->id)
->where('payment_month', $currentMonth)
->where('payment_year', $currentYear)->exists();
}
foreach ($santriPayments as $prevPay) {
$existingPayment = Payment::where('santri_id', $prevPay->santri_id)
->whereMonth('created_at', $currentMonth)
->whereYear('created_at', $currentYear)
->first();
if (!$existingBill) {
$previousDetail = $prevPay->detailPayments->last();
DetailPayment::create([
'payment_id' => $prevPay->id,
'payment_type_id' => $previousDetail->payment_type_id,
'amount' => $previousDetail->amount,
'status' => 'unpaid',
'payment_month' => $currentMonth,
'payment_year' => $currentYear,
'penalty' => null,
]);
if (!$existingPayment) {
$newPayment = Payment::create([
'payment_status' => 'pending',
'amount_payment' => 0,
'santri_id' => $prevPay->santri_id,
'wallet_id' => $prevPay->wallet_id,
'bank' => null,
'no_va' => null,
'expired_at' => null,
'payment_method' => null,
]);
} else {
$newPayment = $existingPayment;
}
$previousPayment = Payment::where('santri_id', $prevPay->santri_id)
->whereMonth('created_at', $previousMonth)
->whereYear('created_at', $previousYear)
->first();
if (!$previousPayment) {
continue;
}
$previousDetails = $previousPayment->detailPayments;
foreach ($previousDetails as $previousDetail) {
$existingBill = DetailPayment::where('payment_id', $newPayment->id)
->where('type_id', $previousDetail->type_id)
->where('payment_month', $currentMonth)
->where('payment_year', $currentYear)
->exists();
if (!$existingBill) {
$paymentType = PaymentType::find($previousDetail->type_id);
$amount = $paymentType ? $paymentType->amount : 0;
DetailPayment::create([
'payment_id' => $newPayment->id,
'type_id' => $previousDetail->type_id,
'amount' => $amount,
'status' => 'unpaid',
'payment_month' => $currentMonth,
'payment_year' => $currentYear,
'penalty' => null,
]);
}
}
}
DB::commit();
return ['message' => 'berhasil menambah data'];
return ['message' => 'Berhasil menambah data'];
} catch (\Exception $e) {
DB::rollBack();
return ['error' => 'gagal menambah data' . $e->getMessage()];
return ['error' => 'Gagal menambah data: ' . $e->getMessage()];
}
}
}

View File

@ -14,6 +14,7 @@ public function up(): void
Schema::create('payment_types', function (Blueprint $table) {
$table->id();
$table->string('payment_type');
$table->float('amount');
$table->timestamps();
});
}