-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from ikhsan3adi/fine-per-day-settings
Add fine per day settings
- Loading branch information
Showing
9 changed files
with
215 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/Database/Migrations/2024-07-08-045735_CreateFinesPerDayTable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters