215 lines
6.6 KiB
PHP
215 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Booking\Repositories;
|
|
|
|
use App\Abstracts\EntityRepository;
|
|
use Exception;
|
|
use Illuminate\Contracts\Pagination\Paginator;
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Modules\Booking\Models\RoomAvailability;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class RoomAvailabilityRepository extends EntityRepository
|
|
{
|
|
public string $table = RoomAvailability::TABLE_NAME;
|
|
|
|
protected array $fillableColumns = [
|
|
'restaurant_id',
|
|
'hotel_id',
|
|
'room_id',
|
|
'date',
|
|
'total_inventory',
|
|
'available_inventory',
|
|
'is_available',
|
|
'is_closed_to_arrival',
|
|
'is_closed_to_departure',
|
|
'base_price',
|
|
'extra_adult_price',
|
|
'extra_child_price',
|
|
'min_stay',
|
|
'max_stay',
|
|
'blocked_by_booking_id',
|
|
'block_type',
|
|
'note',
|
|
'status',
|
|
'created_at',
|
|
'updated_at',
|
|
'deleted_at',
|
|
];
|
|
|
|
protected function getQuery(): Builder
|
|
{
|
|
return parent::getQuery();
|
|
}
|
|
|
|
protected function getFilterData(array $filterData = []): array
|
|
{
|
|
$defaultArgs = [
|
|
'perPage' => 10,
|
|
'search' => '',
|
|
'orderBy' => 'id',
|
|
'order' => 'desc',
|
|
'with_deleted' => false,
|
|
];
|
|
|
|
return array_merge($defaultArgs, $filterData);
|
|
}
|
|
|
|
private function getRoomAvailabilityQuery(): Builder
|
|
{
|
|
return $this->getQuery()
|
|
->select(
|
|
'room_availabilities.id',
|
|
'room_availabilities.restaurant_id',
|
|
'room_availabilities.hotel_id',
|
|
'hotels.name as hotel_name',
|
|
'room_availabilities.room_id',
|
|
'rooms.room_number',
|
|
'rooms.room_code',
|
|
'rooms.slug as room_slug',
|
|
'rooms.max_adults',
|
|
'rooms.max_children',
|
|
'room_types.name as room_type_name',
|
|
'floors.name as floor_name',
|
|
'floors.level as floor_level',
|
|
'room_availabilities.date',
|
|
'room_availabilities.total_inventory',
|
|
'room_availabilities.available_inventory',
|
|
'room_availabilities.is_available',
|
|
'room_availabilities.is_closed_to_arrival',
|
|
'room_availabilities.is_closed_to_departure',
|
|
'room_availabilities.base_price',
|
|
'room_availabilities.extra_adult_price',
|
|
'room_availabilities.extra_child_price',
|
|
'room_availabilities.min_stay',
|
|
'room_availabilities.max_stay',
|
|
'room_availabilities.blocked_by_booking_id',
|
|
'room_availabilities.block_type',
|
|
'room_availabilities.note',
|
|
'room_availabilities.status',
|
|
'room_availabilities.created_at',
|
|
'room_availabilities.updated_at',
|
|
'room_availabilities.deleted_at'
|
|
)
|
|
->leftJoin('rooms', 'room_availabilities.room_id', '=', 'rooms.id')
|
|
->leftJoin('hotels', 'room_availabilities.hotel_id', '=', 'hotels.id')
|
|
->leftJoin('floors', 'rooms.floor_id', '=', 'floors.id')
|
|
->leftJoin('room_types', 'rooms.room_type_id', '=', 'room_types.id');
|
|
}
|
|
|
|
protected function filterSearchQuery(Builder|EloquentBuilder $query, string $searchedText): Builder
|
|
{
|
|
$searchable = "%$searchedText%";
|
|
|
|
return $query->where("{$this->table}.name", 'LIKE', $searchable)
|
|
->orWhere("{$this->table}.status", 'LIKE', $searchable);
|
|
}
|
|
|
|
public function getAll(array $filterData = []): Paginator
|
|
{
|
|
$filter = $this->getFilterData($filterData);
|
|
$query = $this->getRoomAvailabilityQuery();
|
|
|
|
if (! $filter['with_deleted']) {
|
|
$query->whereNull("{$this->table}.deleted_at");
|
|
}
|
|
|
|
if (! empty($filter['search'])) {
|
|
$query = $this->filterSearchQuery($query, $filter['search']);
|
|
}
|
|
|
|
return $query
|
|
->orderBy($filter['orderBy'], $filter['order'])
|
|
->paginate($filter['perPage']);
|
|
}
|
|
|
|
public function getCount(array $filterData = []): int
|
|
{
|
|
$filter = $this->getFilterData($filterData);
|
|
$query = $this->getQuery();
|
|
|
|
if (! $filter['with_deleted']) {
|
|
$query->whereNull("{$this->table}.deleted_at");
|
|
}
|
|
|
|
return $query->count();
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function getByColumn(string $columnName, $columnValue, array $selects = ['*']): ?object
|
|
{
|
|
$item = $this->getRoomAvailabilityQuery()
|
|
->where($columnName, $columnValue)
|
|
->first($selects);
|
|
|
|
if (empty($item)) {
|
|
throw new Exception(
|
|
$this->getExceptionMessage(static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE),
|
|
Response::HTTP_NOT_FOUND
|
|
);
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function create(array $data): object
|
|
{
|
|
$data = $this->prepareForDB($data);
|
|
$id = $this->getQuery()->insertGetId($data);
|
|
|
|
return RoomAvailability::find($id);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function update(int $id, array $data): object
|
|
{
|
|
$item = RoomAvailability::findOrFail($id);
|
|
$data = $this->prepareForDB($data, $item);
|
|
parent::update($id, $data);
|
|
|
|
return $this->getById($id);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function prepareForDB(array $data, ?object $item = null): array
|
|
{
|
|
$data = parent::prepareForDB($data, $item);
|
|
|
|
if (empty($item)) {
|
|
$data['created_at'] = now();
|
|
$data['restaurant_id'] = $this->getCurrentRestaurantId();
|
|
$data['status'] = 1;
|
|
|
|
if (! empty($data['image']) && $data['image'] instanceof \Illuminate\Http\UploadedFile) {
|
|
$data['image'] = fileUploader('Booking/', 'png', $data['image']);
|
|
}
|
|
} else {
|
|
if (! empty($data['image']) && $data['image'] instanceof \Illuminate\Http\UploadedFile) {
|
|
$data['image'] = fileUploader('Booking/', 'png', $data['image'], $item->image);
|
|
}
|
|
|
|
$data['updated_at'] = now();
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function getExceptionMessages(): array
|
|
{
|
|
return [
|
|
static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE => 'RoomAvailability does not exist.',
|
|
static::MESSAGE_ITEM_COULD_NOT_BE_DELETED => 'RoomAvailability could not be deleted.',
|
|
];
|
|
}
|
|
}
|