Skip to content

Commit

Permalink
chore: fix seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhsan3adi committed Jul 8, 2024
1 parent e06f035 commit 186f433
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
6 changes: 5 additions & 1 deletion app/Database/Seeds/FineSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Database\Seeds;

use App\Models\LoanModel;
use CodeIgniter\Database\Seeder;
use CodeIgniter\I18n\Time;

class FineSeeder extends Seeder
{
Expand All @@ -19,7 +21,9 @@ public function run()
'loan_id' => 4,
'amount_paid' => 15000,
'fine_amount' => 15000,
'paid_at' => '2023-08-24 09:00:00'
'paid_at' => Time::parse((new LoanModel)->find(4)['return_date'])
->addDays(rand(0, 30))
->toDateTimeString()
],
];

Expand Down
39 changes: 27 additions & 12 deletions app/Database/Seeds/LoanSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;
use CodeIgniter\I18n\Time;

class LoanSeeder extends Seeder
{
Expand All @@ -15,42 +16,56 @@ public function run()
'quantity' => 1,
'member_id' => 1,
'uid' => sha1('1'),
'loan_date' => '2023-08-21',
'due_date' => '2023-08-28',
'return_date' => null,
],
[
'id' => 2,
'book_id' => 4,
'quantity' => 1,
'member_id' => 2,
'uid' => sha1('2'),
'loan_date' => '2023-08-13',
'due_date' => '2023-08-20',
'return_date' => null,
],
[
'id' => 3,
'book_id' => 2,
'quantity' => 5,
'member_id' => 3,
'uid' => sha1('3'),
'loan_date' => '2023-08-13',
'due_date' => '2023-08-20',
'return_date' => '2023-08-24',
],
[
'id' => 4,
'book_id' => 1,
'quantity' => 1,
'member_id' => 4,
'uid' => sha1('4'),
'loan_date' => '2023-08-7',
'due_date' => '2023-08-21',
'return_date' => '2023-08-24',
],
];

$data = array_map(function (array $loan) {
$loanDate = Time::now()->subDays(rand(31, 40))->subMinutes(rand(0, 240));
$dueDate = (clone $loanDate)->addDays(rand(7, 30))->addMinutes(rand(0, 240));
$returnDate = (clone $dueDate)->subDays(rand(0, 6))->subMinutes(rand(0, 240));
$lateReturnDate = (clone $dueDate)
->addDays(rand(1, 30))
->addMinutes(rand(0, 240))
->toDateTimeString();

$loan['loan_date'] = $loanDate->toDateTimeString();

$loan['due_date'] = $dueDate->toDateTimeString();
if ($loan['id'] == 2) {
$loan['due_date'] = Time::now()->addDays(rand(2, 14))->toDateTimeString();
}

$loan['return_date'] = null;
if ($loan['id'] == 1) {
$loan['return_date'] = $returnDate;
} else if ($loan['id'] > 2) {
$loan['return_date'] = $lateReturnDate;
}

return $loan;
}, $data);

$this->db->table('loans')->insertBatch($data);

$this->call('FineSeeder');
Expand Down
11 changes: 11 additions & 0 deletions app/Database/Seeds/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder as DatabaseSeeder;
use CodeIgniter\Model;

class Seeder extends DatabaseSeeder
{
public function run()
{
$superadminExists = (new Model)
->setTable('auth_groups_users')
->where('group', 'superadmin')
->first();

if (!$superadminExists) {
// run superadmin seeder
$this->call('SuperAdminSeeder');
}

// run category, rack, book & bookstock seeder
$this->call('BookSeeder');

Expand Down

0 comments on commit 186f433

Please sign in to comment.