Skip to content

Commit

Permalink
Move order & reservation cancel logic to model
Browse files Browse the repository at this point in the history
Signed-off-by: Sam <6567634+sampoyigi@users.noreply.github.com>
  • Loading branch information
sampoyigi committed Mar 16, 2023
1 parent 1099e13 commit 7c12951
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
37 changes: 31 additions & 6 deletions app/admin/models/Orders_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Igniter\Flame\Auth\Models\User;
use Igniter\Flame\Database\Casts\Serialize;
use Igniter\Flame\Database\Model;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Request;
use Main\Classes\MainController;
use System\Traits\SendsMailTemplate;
Expand Down Expand Up @@ -229,9 +228,23 @@ public function isCompleted()
if (!$this->isPaymentProcessed())
return false;

return $this->status_history()->where(
'status_id', setting('completed_order_status')
)->exists();
return $this->hasStatus(setting('completed_order_status'));
}

public function isCanceled()
{
return $this->hasStatus(setting('canceled_order_status'));
}

public function isCancelable()
{
if (!$timeout = $this->location->getOrderCancellationTimeout($this->order_type))
return false;

if (!$this->order_datetime->isFuture())
return false;

return $this->order_datetime->diffInRealMinutes() > $timeout;
}

/**
Expand Down Expand Up @@ -266,15 +279,27 @@ public function getOrderDates()
return $this->pluckDates('created_at');
}

public function markAsCanceled()
{
$canceled = false;
if ($this->addStatusHistory(setting('canceled_order_status'))) {
$canceled = true;

$this->fireSystemEvent('admin.order.canceled');
}

return $canceled;
}

public function markAsPaymentProcessed()
{
if (!$this->processed) {
Event::fire('admin.order.beforePaymentProcessed', [$this]);
$this->fireSystemEvent('admin.order.beforePaymentProcessed');

$this->processed = 1;
$this->save();

Event::fire('admin.order.paymentProcessed', [$this]);
$this->fireSystemEvent('admin.order.paymentProcessed');
}

return $this->processed;
Expand Down
32 changes: 29 additions & 3 deletions app/admin/models/Reservations_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,35 @@ public function setDurationAttribute($value)

public function isCompleted()
{
return $this->status_history()->where(
'status_id', setting('confirmed_reservation_status')
)->exists();
return $this->hasStatus(setting('confirmed_reservation_status'));
}

public function isCanceled()
{
return $this->hasStatus(setting('canceled_reservation_status'));
}

public function isCancelable()
{
if (!$timeout = $this->location->getReservationCancellationTimeout())
return false;

if (!$this->reservation_datetime->isFuture())
return false;

return $this->reservation_datetime->diffInRealMinutes() > $timeout;
}

public function markAsCanceled()
{
$canceled = false;
if ($this->addStatusHistory(setting('canceled_reservation_status'))) {
$canceled = true;

$this->fireSystemEvent('admin.reservation.canceled');
}

return $canceled;
}

public static function findReservedTables($location, $dateTime)
Expand Down

0 comments on commit 7c12951

Please sign in to comment.