190 lines
5.7 KiB
PHP
190 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Billing;
|
|
use App\Models\TrRegistrasi;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class BillingController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$billings = Billing::with('registrasi')->orderBy('TanggalBilling', 'desc')->paginate(10);
|
|
return view('admin.billing.index', compact('billings'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$registrasis = TrRegistrasi::whereDoesntHave('billing')->get();
|
|
return view('admin.billing.Input-form', compact('registrasis'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'IdRegistrasi' => 'required|exists:tr_registrasi,IdRegistrasi',
|
|
'TanggalBilling' => 'required|date',
|
|
'Dibayar' => 'required|numeric|min:0',
|
|
'Keterangan' => 'nullable|string|max:255'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()
|
|
->withErrors($validator)
|
|
->withInput();
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$billing = new Billing();
|
|
$billing->IdRegistrasi = $request->IdRegistrasi;
|
|
$billing->TanggalBilling = $request->TanggalBilling;
|
|
$billing->Dibayar = $request->Dibayar;
|
|
$billing->Keterangan = $request->Keterangan;
|
|
|
|
// Menghitung total biaya secara otomatis berdasarkan transaksi
|
|
$billing->TotalBiaya = 0; // Nilai awal
|
|
$billing->save();
|
|
|
|
// Menggunakan method dari model untuk menghitung total biaya
|
|
$billing->TotalBiaya = $billing->calculateTotalBiaya();
|
|
|
|
// Update sisa dan status pembayaran
|
|
$billing->updateSisa();
|
|
|
|
DB::commit();
|
|
return redirect()->route('billing.show', $billing->IdBilling)
|
|
->with('success', 'Billing berhasil dibuat.');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return redirect()->back()
|
|
->with('error', 'Terjadi kesalahan: ' . $e->getMessage())
|
|
->withInput();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
$billing = Billing::with('registrasi')->findOrFail($id);
|
|
$transaksis = $billing->getTransaksiDetails();
|
|
|
|
return view('admin.billing.show-form', compact('billing', 'transaksis'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
$billing = Billing::findOrFail($id);
|
|
return view('billing.edit', compact('billing'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'TanggalBilling' => 'required|date',
|
|
'Dibayar' => 'required|numeric|min:0',
|
|
'Keterangan' => 'nullable|string|max:255'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()
|
|
->withErrors($validator)
|
|
->withInput();
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$billing = Billing::findOrFail($id);
|
|
$billing->TanggalBilling = $request->TanggalBilling;
|
|
$billing->Dibayar = $request->Dibayar;
|
|
$billing->Keterangan = $request->Keterangan;
|
|
|
|
// Recalculate total biaya (jika ada perubahan transaksi)
|
|
$billing->TotalBiaya = $billing->calculateTotalBiaya();
|
|
|
|
// Update sisa dan status pembayaran
|
|
$billing->updateSisa();
|
|
|
|
DB::commit();
|
|
return redirect()->route('billing.show', $billing->IdBilling)
|
|
->with('success', 'Billing berhasil diperbarui.');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return redirect()->back()
|
|
->with('error', 'Terjadi kesalahan: ' . $e->getMessage())
|
|
->withInput();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
try {
|
|
$billing = Billing::findOrFail($id);
|
|
$billing->delete();
|
|
|
|
return redirect()->route('billing.index')
|
|
->with('success', 'Billing berhasil dihapus.');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()
|
|
->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Process additional payment for billing.
|
|
*/
|
|
public function processPayment(Request $request, string $id)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'jumlah_bayar' => 'required|numeric|min:1',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()
|
|
->withErrors($validator)
|
|
->withInput();
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$billing = Billing::findOrFail($id);
|
|
$billing->Dibayar += $request->jumlah_bayar;
|
|
|
|
// Update sisa dan status pembayaran
|
|
$billing->updateSisa();
|
|
|
|
DB::commit();
|
|
return redirect()->route('billing.show', $billing->IdBilling)
|
|
->with('success', 'Pembayaran berhasil ditambahkan.');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return redirect()->back()
|
|
->with('error', 'Terjadi kesalahan: ' . $e->getMessage())
|
|
->withInput();
|
|
}
|
|
}
|
|
}
|