diff --git a/README.md b/README.md index 5a3161e..3501f33 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,6 @@ - Unduh dan impor kode proyek ini ke dalam direktori proyek anda (htdocs). - Penting ⚠️. Jika belum memiliki file `.env`, salin/rename file `.env.example` menjadi `.env` - (Opsional) Konfigurasi file `.env` untuk mengatur parameter seperti koneksi database dan pengaturan lainnya sesuai dengan lingkungan pengembangan Anda. -- Ubah denda per hari di file `.env` - -``` -# in rupiah -amountFinesPerDay = 1000 -``` - - Penting ⚠️. Install dependencies yang diperlukan dengan cara menjalankan perintah berikut di terminal: ```shell diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 0e0a234..defdbb5 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -55,6 +55,7 @@ $routes->get('fines/returns/search', 'Loans\FinesController::searchReturn'); $routes->get('fines/pay/(:any)', 'Loans\FinesController::pay/$1'); + $routes->resource('fines/settings', ['controller' => 'Loans\FineSettingsController', 'filter' => 'group:superadmin']); $routes->resource('fines', ['controller' => 'Loans\FinesController']); $routes->group('users', ['filter' => 'group:superadmin'], static function (RouteCollection $routes) { diff --git a/app/Controllers/Loans/FineSettingsController.php b/app/Controllers/Loans/FineSettingsController.php new file mode 100644 index 0000000..1e93965 --- /dev/null +++ b/app/Controllers/Loans/FineSettingsController.php @@ -0,0 +1,46 @@ + FinesPerDayModel::get(), + 'validation' => \Config\Services::validation() + ]); + } + + public function show($id = null) + { + return $this->index(); + } + + public function update($id = null) + { + if (!$this->validate([ + 'amount' => 'required|integer|greater_than_equal_to[1000]' + ])) { + $data = [ + 'validation' => \Config\Services::validation(), + 'oldInput' => $this->request->getVar(), + 'fine' => FinesPerDayModel::get(), + ]; + + return view('fines/settings', $data); + } + try { + FinesPerDayModel::updateAmount($this->request->getVar('amount')); + + session()->setFlashdata(['msg' => 'Update fine amount successful']); + return redirect('admin/fines/settings'); + } catch (\Throwable $e) { + session()->setFlashdata(['msg' => $e->getMessage(), 'error' => true]); + return redirect('admin/fines/settings'); + } + } +} diff --git a/app/Controllers/Loans/ReturnsController.php b/app/Controllers/Loans/ReturnsController.php index 352a142..63fb109 100644 --- a/app/Controllers/Loans/ReturnsController.php +++ b/app/Controllers/Loans/ReturnsController.php @@ -5,6 +5,7 @@ use App\Libraries\QRGenerator; use App\Models\BookModel; use App\Models\FineModel; +use App\Models\FinesPerDayModel; use App\Models\LoanModel; use App\Models\MemberModel; use CodeIgniter\Exceptions\PageNotFoundException; @@ -232,7 +233,7 @@ public function create() return redirect()->to('admin/returns/new?loan-uid=' . $loan['uid']); } - $finePerDay = intval(getenv('amountFinesPerDay')); + $finePerDay = FinesPerDayModel::getAmount(); $daysLate = $date->today()->difference($loanDueDate)->getDays(); $totalFine = abs($daysLate) * $loan['quantity'] * $finePerDay; diff --git a/app/Database/Migrations/2024-07-08-045735_CreateFinesPerDayTable.php b/app/Database/Migrations/2024-07-08-045735_CreateFinesPerDayTable.php new file mode 100644 index 0000000..3b0c8e9 --- /dev/null +++ b/app/Database/Migrations/2024-07-08-045735_CreateFinesPerDayTable.php @@ -0,0 +1,40 @@ +forge->addField([ + 'id' => [ + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => true, + 'auto_increment' => true + ], + 'amount' => [ + 'type' => 'INT', + 'constraint' => 11, + 'unsigned' => true, + ], + 'created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL', + 'updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL', + ]); + + $this->forge->addPrimaryKey('id'); + + $this->forge->createTable('fines_per_day', TRUE); + + $this->db->table('fines_per_day')->insert([ + 'amount' => 1000 + ]); + } + + public function down() + { + $this->forge->dropTable('fines_per_day'); + } +} diff --git a/app/Models/FinesPerDayModel.php b/app/Models/FinesPerDayModel.php new file mode 100644 index 0000000..c7f1bf3 --- /dev/null +++ b/app/Models/FinesPerDayModel.php @@ -0,0 +1,63 @@ + 'required|numeric|greater_than_equal_to[1000]', + ]; + protected $validationMessages = []; + protected $skipValidation = false; + protected $cleanValidationRules = true; + + public static function getAmount(): int + { + return intval(self::get()['amount'] ?? 0); + } + + public static function get() + { + return (new FinesPerDayModel)->first(); + } + + public static function updateAmount(int $amount) + { + $current = self::get(); + if (!$current) { + return (new FinesPerDayModel)->insert([ + 'amount' => $amount ?? 1000, + ]); + } + $data = [ + 'amount' => $amount ?? $current['amount'], + ]; + return (new FinesPerDayModel)->update($current['id'], $data); + } +} diff --git a/app/Views/fines/settings.php b/app/Views/fines/settings.php new file mode 100644 index 0000000..a0f0655 --- /dev/null +++ b/app/Views/fines/settings.php @@ -0,0 +1,45 @@ += $this->extend('layouts/admin_layout') ?> + += $this->section('head') ?> +