manual payment semi final be
This commit is contained in:
parent
4a765b28d9
commit
c91ceae657
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use App\Models\DetailPayment;
|
use App\Models\DetailPayment;
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
|
use App\Models\PaymentType;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
@ -13,39 +14,74 @@ public function generateAutoBill()
|
||||||
{
|
{
|
||||||
$currentMonth = Carbon::now()->month;
|
$currentMonth = Carbon::now()->month;
|
||||||
$currentYear = Carbon::now()->year;
|
$currentYear = Carbon::now()->year;
|
||||||
|
$previousMonth = Carbon::now()->subMonth()->month;
|
||||||
|
$previousYear = Carbon::now()->subMonth()->year;
|
||||||
|
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
try {
|
try {
|
||||||
$previousPayments = Payment::with('detailPayments')
|
$santriPayments = Payment::with('detailPayments')->get();
|
||||||
->whereHas('detailPayments', function ($query) {
|
|
||||||
$query->where('payment_month', Carbon::now()->subMonth()->month)
|
|
||||||
->where('payment_year', Carbon::now()->subMonth()->year);
|
|
||||||
})->get();
|
|
||||||
|
|
||||||
foreach ($previousPayments as $prevPay) {
|
foreach ($santriPayments as $prevPay) {
|
||||||
$existingBill = DetailPayment::where('payment_id', $prevPay->id)
|
$existingPayment = Payment::where('santri_id', $prevPay->santri_id)
|
||||||
->where('payment_month', $currentMonth)
|
->whereMonth('created_at', $currentMonth)
|
||||||
->where('payment_year', $currentYear)->exists();
|
->whereYear('created_at', $currentYear)
|
||||||
}
|
->first();
|
||||||
|
|
||||||
if (!$existingBill) {
|
if (!$existingPayment) {
|
||||||
$previousDetail = $prevPay->detailPayments->last();
|
$newPayment = Payment::create([
|
||||||
DetailPayment::create([
|
'payment_status' => 'pending',
|
||||||
'payment_id' => $prevPay->id,
|
'amount_payment' => 0,
|
||||||
'payment_type_id' => $previousDetail->payment_type_id,
|
'santri_id' => $prevPay->santri_id,
|
||||||
'amount' => $previousDetail->amount,
|
'wallet_id' => $prevPay->wallet_id,
|
||||||
'status' => 'unpaid',
|
'bank' => null,
|
||||||
'payment_month' => $currentMonth,
|
'no_va' => null,
|
||||||
'payment_year' => $currentYear,
|
'expired_at' => null,
|
||||||
'penalty' => 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();
|
DB::commit();
|
||||||
return ['message' => 'berhasil menambah data'];
|
return ['message' => 'Berhasil menambah data'];
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
DB::rollBack();
|
DB::rollBack();
|
||||||
return ['error' => 'gagal menambah data' . $e->getMessage()];
|
return ['error' => 'Gagal menambah data: ' . $e->getMessage()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ public function up(): void
|
||||||
Schema::create('payment_types', function (Blueprint $table) {
|
Schema::create('payment_types', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->string('payment_type');
|
$table->string('payment_type');
|
||||||
|
$table->float('amount');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue