201 lines
6 KiB
PHP
201 lines
6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Order\DeliveryStatus;
|
|
use App\Enums\Order\PaymentStatus;
|
|
use App\Enums\Order\ValidationStatus;
|
|
use Gloudemans\Shoppingcart\Cart;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
/**
|
|
* Order
|
|
*
|
|
* @property-read string $reference The order unique reference string.
|
|
* @property array $basket The order serialized basket.
|
|
* @property string $email The customer email used for this order.
|
|
* @property boolean $mail_sent Has the confirmation mail been sent to the customer.
|
|
* @property array $shipping The shipping address info.
|
|
* @property array $billing The billing address info.
|
|
* @property integer $total_discounted Total discounted on this order in cents.
|
|
* @property integer $total_ttc Total payed fro mthe customer in cents.
|
|
* @property integer $total_ht Total without taxes in cents.
|
|
* @property integer $total_weight Total weight in gramms.
|
|
* @property \App\Enums\Order\ValidationStatus $validation_status The order validation status.
|
|
* @property \App\Enums\Order\PaymentStatus $payment_status The order payment status.
|
|
* @property \App\Enums\Order\DeliveryStatus $delivery_status The order delivery status.
|
|
*
|
|
* @property-read boolean $can_be_cancelled Can the order be cancelled ?
|
|
*
|
|
* @package App\Models
|
|
*/
|
|
class Order extends Model
|
|
{
|
|
use CastsEnums;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* Map attribute names to enum classes.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $enumCasts = [
|
|
'validation_status' => 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());
|
|
}
|
|
}
|