Skip to content

Commit

Permalink
Move allocate table logic from booking manager to reservation model
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com>
  • Loading branch information
sampoyigi committed Jun 20, 2022
1 parent d91683e commit a4530bd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/admin/models/Reservations_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Reservations_model extends Model

public $relation = [
'belongsTo' => [
'customer' => 'Admin\Models\Customers_model',
'related_table' => ['Admin\Models\Tables_model', 'foreignKey' => 'table_id'],
'location' => 'Admin\Models\Locations_model',
],
Expand Down Expand Up @@ -100,6 +101,10 @@ protected function afterSave()
if (array_key_exists('tables', $this->attributes)) {
$this->addReservationTables((array)$this->attributes['tables']);
}

if ($this->location->getOption('auto_allocate_table', 1) && !$this->tables()->count()) {
$this->addReservationTables($this->getNextBookableTable()->pluck('table_id')->all());
}
}

//
Expand Down Expand Up @@ -395,6 +400,34 @@ public function addReservationTables(array $tableIds = [])
$this->tables()->sync($tableIds);
}

/**
* @return \Illuminate\Support\Collection|null
*/
public function getNextBookableTable()
{
$tables = $this->location->tables->where('table_status', 1);

$reserved = static::findReservedTables($this->location, $this->reservation_datetime);

$tables = $tables->diff($reserved)->sortBy('priority');

$result = collect();
$unseatedGuests = $this->guest_num;
foreach ($tables as $table) {
if ($table->min_capacity <= $this->guest_num && $table->max_capacity >= $this->guest_num)
return collect([$table]);

if ($table->is_joinable && $unseatedGuests >= $table->min_capacity) {
$result->push($table);
$unseatedGuests -= $table->max_capacity;
if ($unseatedGuests <= 0)
break;
}
}

return $unseatedGuests > 0 ? collect() : $result;
}

//
// Mail
//
Expand Down
2 changes: 1 addition & 1 deletion app/admin/models/config/menus_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
'modelClass' => 'Admin\Models\Menu_options_model',
'placeholder' => 'lang:admin::lang.menus.help_menu_option',
'formName' => 'lang:admin::lang.menu_options.text_option',
'popupSize' => 'modal-lg',
'popupSize' => 'modal-xl',
'addonRight' => [
'label' => '<i class="fa fa-long-arrow-down"></i> Add to Menu',
'tag' => 'button',
Expand Down

0 comments on commit a4530bd

Please sign in to comment.