ValidationStatus::class, 'payment_status' => PaymentStatus::class, 'delivery_status' => DeliveryStatus::class ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'basket' => 'array', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { // * ASSERTIONS static::assertCancelStatus($model); }); static::updating(function (self $model) { static::assertCancelStatus($model); }); } // * RELATIONSHIPS /** * BelongsToMany products relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function products() { return $this->belongsToMany(Product::class, 'order_lines', 'order_id', 'product_id'); } /** * BelongsTo customer relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function customer() { return $this->belongsTo(Customer::class); } /** * BelongsTo DeliveryMode relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function deliveryMode() { return $this->belongsTo(DeliveryMode::class); } /** * hasOne timeslot relation * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function timeslot() { return $this->hasOne(Timeslot::class); } // * GETTER /** * Getter can_be_cancelled * * On peut annuler la commande si le client * n'as pas payé et que la livraison n'a pas * encore été initié, et la commande n'est pas * déjà annulée. * * @return boolean */ public function getCanBeCancelledAttribute(): bool { return !$this->validation_status->equals(ValidationStatus::cancelled()) and !$this->payment_status->equals(PaymentStatus::payed()) and $this->delivery_status->equals(DeliveryStatus::pending()); } // * SETTER /** * Getter reference * * @return void * @throws \RuntimeException This is readonly. */ public function setReferenceAttribute(): void { throw new \RuntimeException('Reference is readonly'); } // * METHODS /** * Check if order can be cancelled * * @param self $model * @return void * @throws \RuntimeException If the order is canceled while it cannot be. */ public static function assertCancelStatus(self $model) { if ( !$model->can_be_cancelled and $model->validation_status->equals(ValidationStatus::cancelled()) ) { throw new \RuntimeException("La commande '{$model->id}' ne peut être annulée"); } } /** * Get updated current session order * * @return null|\App\Models\Order */ public static function getCurrentOrder(): ?self { /** @var \App\Models\Order|null $order */ if ($order = Session::get('currentOrder')) { // * Refresh order if exists in DB. $order = Order::where('id', $order->id) ->orWhere('reference', $order->ref) ->first() ?? $order; } return $order; } /** * Inject the basket items into the order. * * @param \Gloudemans\Shoppingcart\Cart $basket * @return void * @throws \Illuminate\Validation\ValidationException If a product from the basket is not accessible anymore. */ public function assignBasket(Cart $basket): void { $this->basket = $basket->content(); $this->total_discounted = \intval($basket->discount()); $this->total_ttc = \intval($basket->total()); $this->total_ht = \intval($this->total_ttc - $basket->tax()); $this->total_weight = \intval($basket->weight()); } }