File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Reservations/Kms/ReservationService.php
<?php
namespace App\Komma\Reservations\Kms;
use App\Helpers\KommaHelpers;
use App\Komma\Availability\Types\Availability;
use App\Komma\Availability\Types\TimeSlot;
use App\Komma\CalendarBlockOuts\Models\CalendarBlockOut;
use App\Komma\CalendarNotes\Models\CalendarNote;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\ModelService;
use App\Komma\Kms\Core\Sections\SidebarListItem;
use App\Komma\Locations\Models\Location;
use App\Komma\Pages\Models\Page;
use App\Komma\Reservations\Models\Reservation;
use App\Komma\Reservations\Models\ReservationItem;
use App\Komma\Users\Models\KmsUserRole;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class ReservationService extends ModelService
{
protected $orderBy = 'date';
public function __construct()
{
parent::__construct();
$this->setModelClassName(Reservation::class);
}
public static function getStartRange()
{
return Carbon::now()->startOfWeek()->subWeeks(2);
}
/**
* Gets the values of an Eloquent model and passes them to a collection of attributes
*
* @param Model $model
* @param Collection $attributes
* @return mixed
*/
public function load(Model $model, Collection $attributes = null): Collection
{
if ($attributes == null) {
return new Collection();
}
$this->checkContainsAttributes($attributes);
/** @var Page $model */
$attributes = $attributes->map(function (Attribute $attribute) use (&$model) {
$valueReference = $attribute->getsValueFromReference();
if ($valueReference == 'date') {
$dateForPicker = Carbon::createFromFormat('Y-m-d', $model->date);
$attribute->setValue($dateForPicker);
}
return $attribute;
});
return parent::load($model, $attributes);
}
/**
* Extends the save model, for saving the price excluding vat
*
* @param Model $model
* @param Collection|null $attributes
* @return Model
*/
public function save(Model $model, Collection $attributes = null): Model
{
$originalLocationId = $model->location_id;
$originalDate = $model->date;
$model = parent::save($model, $attributes);
$attributes = $attributes->map(function (Attribute $attribute) use (&$model) {
$valueReference = $attribute->getsValueFromReference();
if ($valueReference == 'date') {
$dateAttribute = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $attribute->getValue());
$model->date = $dateAttribute->format('Y-m-d');
}
return $attribute;
});
$reservationItems = request('reservation-items');
if ($model->location_id != $originalLocationId) {
$model->items()->delete();
} else {
if ($model->date != $originalDate) {
foreach ($model->items as $reservationItem) {
$reservationItem->date = $model->date;
$reservationItem->save();
}
}
if (isset($reservationItems)) {
foreach ($reservationItems as $reservationItemId => $reservationItemInput) {
$reservationItem = $model->items()->find($reservationItemId);
$reservationItem->quantity = $reservationItemInput['quantity'];
$reservationItem->remarks = $reservationItemInput['remarks'];
$availability = new Availability($reservationItem->product, $reservationItem->location);
$availability->amountOfPersons = $reservationItem->quantity;
$reservationItem->price_total = $availability->getTotal(true, false);
if (isset($reservationItemInput['time'])) {
$timeSlot = TimeSlot::create($model->getDateAsDateTime(), $reservationItemInput['time'], $reservationItem->product->duration);
$reservationItem->start_time = $timeSlot->start->format('H:i:s');
$reservationItem->end_time = $timeSlot->end->format('H:i:s');
}
$reservationItem->save();
}
}
}
$model->save();
return $model;
}
/**
* Extend the destroy for Model so the related items are deleted
*
* @param Model $model
* @return Model
*/
public function destroyForModel(Model $model): Model
{
// Delete the reservation items belonging to this reservation
$model->items()->delete();
return parent::destroyForModel($model);
}
public function getModelsForSideBar(): array
{
$modelQuery = Reservation::where('date', '>=', Carbon::today())
->orderBy('date', 'asc');
if (auth()->user()->role == KmsUserRole::Editor && isset(auth()->user()->location_id)) {
$modelQuery = $modelQuery->where('location_id', auth()->user()->location_id);
}
return $this->convertModelsForSidebar($modelQuery->get());
}
public function getArchiveModelsForSideBar(): array
{
$modelQuery = Reservation::where('date', '<', Carbon::today())
->orderBy('date', 'desc');
if (auth()->user()->role == KmsUserRole::Editor && isset(auth()->user()->location_id)) {
$modelQuery = $modelQuery->where('location_id', auth()->user()->location_id);
}
return $this->convertModelsForSidebar($modelQuery->take(100)->get());
}
public function searchModelsForSidebar(string $query): array
{
$modelQuery = Reservation::where('reservation_number', 'LIKE', '%'.$query.'%')
->orWhere('company_name', 'LIKE', '%'.$query.'%')
->orWhere('first_name', 'LIKE', '%'.$query.'%')
->orWhere('last_name', 'LIKE', '%'.$query.'%')
->orderBy('date', 'desc');
if (auth()->user()->role == KmsUserRole::Editor && isset(auth()->user()->location_id)) {
$modelQuery = $modelQuery->where('location_id', auth()->user()->location_id);
}
return [
$this->convertModelsForSidebar($modelQuery->take(100)->get()),
$modelQuery->count(),
];
}
/**
* @param Collection $models
* @return array
*/
private function convertModelsForSidebar(Collection $models): array
{
$sidebarList = [];
foreach ($models as $model) {
$sidebarListItem = new SidebarListItem();
$sidebarListItem->setThumbnail($model);
$sidebarListItem->alsoSearchInAttributesOfModel($model);
$model->generateThumbnail();
//Set the values for the sidebar
$sidebarListItem->setId($model->id);
$sidebarListItem->setStatus($model->active);
// $title = KommaHelpers::str_limit_full_word($model->getSidebarName(), 75);
$sidebarListItem->setName($model->getSidebarName());
$sidebarListItem->setThumbnail($model->getThumbnail());
$sidebarList[] = $sidebarListItem;
}
return $sidebarList;
}
public function getReservationItemsForGivenDayAndLocation($date, $location)
{
return ReservationItem::where('date', '=', $date)
->where('location_id', '=', $location->id)
->with('product', 'product.translation', 'reservation')
->has('reservation')
->orderBy('start_time')
->get();
}
public function getReservationItemsFrom(Location $location)
{
return ReservationItem::where('location_id', $location->id)
->where('date', '>=', self::getStartRange())
->with('product', 'product.translation', 'reservation')
->has('product')
->has('reservation')
->get();
}
public function getAllBlockOuts()
{
return CalendarBlockOut::query()
// ->where('start_date', '>=', self::getStartRange())
->where('end_date', '>=', self::getStartRange())
->with('product', 'product.translation')
->get();
}
public function getBlockOutsFrom(Location $location)
{
return CalendarBlockOut::where('location_id', $location->id)
// ->where('start_date', '>=', self::getStartRange())
->where('end_date', '>=', self::getStartRange())
->with('product', 'product.translation', 'location')
->get();
}
public function getNotesFrom(Location $location)
{
return CalendarNote::where('location_id', $location->id)
// ->where('start_date', '>=', self::getStartRange())
->where('end_date', '>=', self::getStartRange())
->with('location')
->get();
}
}