Skip to content

Commit

Permalink
Perbaikan seeder dan index lodging
Browse files Browse the repository at this point in the history
  • Loading branch information
itsfaqih committed Jul 27, 2020
1 parent 9b7cd2d commit 7b16cb2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/LodgingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function index()
{
return Inertia::render('Lodgings/Index', [
'filters' => Request::all('search', 'trashed'),
'lodgings' => Lodging::orderBy('id', 'desc')
'lodgings' => Lodging::orderBy('end_at', 'desc')
->filter(Request::only('search', 'trashed'))
->paginate()
->transform(function ($lodging) {
Expand All @@ -27,6 +27,7 @@ public function index()
'start_at' => $lodging->start_at->format('d F Y'),
'end_at' => $lodging->end_at->format('d F Y'),
'deleted_at' => $lodging->deleted_at,
'status' => $lodging->getStatus()
];
}),
]);
Expand Down
16 changes: 15 additions & 1 deletion app/Lodging.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ public function payments()
return $this->hasManyDeep(Payment::class, [Bill::class, Invoice::class]);
}

public function getStatus()
{
$now = Carbon::now();
if ($this->start_at->lessThan($now) && $this->end_at->greaterThan($now)) {
return 'Aktif';
} elseif ($this->start_at->greaterThan($now)) {
return 'Belum berjalan';
} else {
return 'Selesai';
}

}

public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? null, function ($query, $search) {
Expand All @@ -58,6 +71,7 @@ public function scopeFilter($query, array $filters)

public function scopeActive($query)
{
$query->whereDate('end_at', '>', Carbon::now());
$now = Carbon::now();
$query->whereDate('start_at', '<', $now)->whereDate('end_at', '>', $now);
}
}
2 changes: 1 addition & 1 deletion app/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function scopeFilter($query, array $filters)
});
}

public function scopeUsed($query)
public function scopeOccupied($query)
{
$query->whereHas('lodgings', function ($query) {
$query->active();
Expand Down
5 changes: 3 additions & 2 deletions database/factories/LodgingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
use Faker\Generator as Faker;

$factory->define(Lodging::class, function (Faker $faker) {
$startDate = $faker->dateTimeBetween('-1 years', '6 months');
return [
'room_id' => factory(Room::class),
'renter_id' => factory(Renter::class),
'start_at' => $faker->iso8601(),
'end_at' => $faker->iso8601()
'start_at' => $startDate,
'end_at' => $faker->dateTimeInInterval($startDate, '6 months')
];
});
4 changes: 2 additions & 2 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function run()

// $this->call(RenterSeeder::class);
// $this->call(RoomSeeder::class);
$this->call(LodgingSeeder::class);
// $this->call(LodgingSeeder::class);
// $this->call(BillSeeder::class);
$this->call(InvoiceSeeder::class);
// $this->call(InvoiceSeeder::class);
$this->call(PaymentSeeder::class);
}
}
16 changes: 14 additions & 2 deletions resources/js/Pages/Lodgings/Index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ const Lodgings = () => {
<th className="px-6 pt-5 pb-4">
Tanggal mulai
</th>
<th className="px-6 pt-5 pb-4" colSpan="2">
<th className="px-6 pt-5 pb-4">
Tanggal selesai
</th>
<th className="px-6 pt-5 pb-4" colSpan="2">
Status
</th>
</tr>
</thead>
<tbody>
{data.map(({ id, room, renter, start_at, end_at, deleted_at }) => {
{data.map(({ id, room, renter, start_at, end_at, deleted_at, status }) => {
return (
<tr
key={id}
Expand Down Expand Up @@ -86,6 +89,15 @@ const Lodgings = () => {
{end_at}
</InertiaLink>
</td>
<td className="border-t">
<InertiaLink
tabIndex="-1"
href={route('lodgings.edit', id)}
className="flex items-center px-6 py-4 focus:text-indigo"
>
{status}
</InertiaLink>
</td>
<td className="w-px border-t">
<InertiaLink
tabIndex="-1"
Expand Down

0 comments on commit 7b16cb2

Please sign in to comment.