Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fine per day settings #12

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions app/Controllers/Loans/FineSettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Controllers\Loans;

use App\Models\FinesPerDayModel;
use CodeIgniter\RESTful\ResourceController;

class FineSettingsController extends ResourceController
{
public function index()
{
return view('fines/settings', [
'fine' => 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');
}
}
}
3 changes: 2 additions & 1 deletion app/Controllers/Loans/ReturnsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateFinesPerDayTable extends Migration
{
public function up()
{
$this->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');
}
}
63 changes: 63 additions & 0 deletions app/Models/FinesPerDayModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Models;

use CodeIgniter\Model;

class FinesPerDayModel extends Model
{
protected $table = 'fines_per_day';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';

protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'amount',
];

protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;

protected array $casts = [];
protected array $castHandlers = [];

// Dates
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';

// Validation
protected $validationRules = [
'amount' => '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);
}
}
45 changes: 45 additions & 0 deletions app/Views/fines/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?= $this->extend('layouts/admin_layout') ?>

<?= $this->section('head') ?>
<title>Pengaturan Denda</title>
<?= $this->endSection() ?>

<?= $this->section('content') ?>
<?php

if (session()->getFlashdata('msg')) : ?>
<div class="pb-2">
<div class="alert <?= (session()->getFlashdata('error') ?? false) ? 'alert-danger' : 'alert-success'; ?> alert-dismissible fade show" role="alert">
<?= session()->getFlashdata('msg') ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</div>
<?php endif; ?>

<div class="card">
<div class="card-body">
<h5 class="card-title fw-semibold mb-4">Pengaturan Denda</h5>
<form action="<?= base_url('admin/fines/settings/' . $fine['id']); ?>" method="post">
<?= csrf_field(); ?>
<input type="hidden" name="_method" value="PATCH">
<div class="row">
<div class="col-12 col-md-6">
<label for="amount" class="form-label">Nilai denda per hari (Rp)</label>
<div class="input-group">
<input type="number" class="form-control <?php if ($validation->hasError('amount')) : ?>is-invalid<?php endif ?>" id="amount" name="amount" value="<?= $oldInput['amount'] ?? $fine['amount'] ?? ''; ?>" placeholder="1000" required>
<button type="submit" class="btn btn-primary">Simpan</button>
</div>
<?php if ($validation->hasError('amount')) : ?>
<span class="text-danger small">
<?= $validation->getError('amount'); ?>
</span>
<?php endif; ?>
<div class="form-text mt-3">
Minimal Rp1000.
</div>
</div>
</div>
</form>
</div>
</div>
<?= $this->endSection() ?>
19 changes: 16 additions & 3 deletions app/Views/layouts/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@
<div class="navbar-collapse justify-content-end px-0" id="navbarNav">
<ul class="navbar-nav flex-row ms-auto align-items-center justify-content-end gap-2" id="headerCollapse">
<li class="nav-item" id="navBtn">
<a href=" <?= base_url('admin/loans/new/members/search'); ?>" target="_blank" class="btn btn-primary">Ajukan peminjaman</a>
<a href=" <?= base_url('admin/loans/new/members/search'); ?>" target="_blank" class="btn btn-primary">
Ajukan peminjaman
</a>
</li>
<li class="nav-item" id="navBtn">
<a href="<?= base_url('admin/returns/new/search'); ?>" target="_blank" class="btn btn-outline-primary">Pengembalian buku</a>
<a href="<?= base_url('admin/returns/new/search'); ?>" class="btn btn-outline-primary">
Pengembalian buku
</a>
</li>
<li class="nav-item" id="navBtn">
<a href="<?= base_url('admin/fines/returns/search'); ?>" target="_blank" class="btn btn-outline-warning">Bayar denda</a>
<a href="<?= base_url('admin/fines/returns/search'); ?>" class="btn btn-outline-warning">
Bayar denda
</a>
</li>
<?php if (auth()->user()->inGroup('superadmin')) : ?>
<li class="nav-item" id="navBtn">
<a href=" <?= base_url('admin/fines/settings'); ?>" class="btn btn-outline-danger">
Pengaturan Denda
</a>
</li>
<?php endif; ?>
<li class="nav-item dropdown">
<a class="nav-link nav-icon-hover position-relative" href="javascript:void(0)" id="drop2" data-bs-toggle="dropdown" aria-expanded="false">
<img alt="" width="35" height="35" class="rounded-circle border border-primary" style="background-color: white;">
Expand Down
3 changes: 2 additions & 1 deletion app/Views/returns/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<?= $this->section('content') ?>
<?php

use App\Models\FinesPerDayModel;
use CodeIgniter\I18n\Time;

$now = Time::now(locale: 'id');
Expand Down Expand Up @@ -100,7 +101,7 @@
</div>
</div>
<?php if ($isLate) :
$finePerDay = intval(getenv('amountFinesPerDay'));
$finePerDay = FinesPerDayModel::getAmount();
$totalFine = abs($daysLate) * $loan['quantity'] * $finePerDay;
?>
<h5 class="card-title fw-semibold my-3">Denda</h5>
Expand Down