ldc-legaulois/app/Models/Order.php
2024-10-08 12:22:52 +02:00

117 lines
3.5 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Shop\ContactFormCivility;
use App\Enums\Shop\OrderStatus;
use App\Enums\Shop\OrderType;
use Illuminate\Database\Eloquent\Model;
/**
* @property string $ref
* @property string $firstname
* @property string $lastname
* @property string $address
* @property string $postcode
* @property string|null $phone
* @property string $email
* @property string $city
* @property \App\Enums\Shop\ContactFormCivility $civility
* @property \App\Enums\Shop\OrderStatus $status
* @property \App\Enums\Shop\OrderType $payementType
* @property boolean $confirmMailSent
* @property \App\Models\Customer $customer_id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*/
class Order extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'firstname',
'lastname',
'address',
'postcode',
'phone',
'email',
'city',
'civility',
'status',
'payementType',
'customer_id',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'civility' => ContactFormCivility::class,
'status' => OrderStatus::class,
'payementType' => OrderType::class
];
// * METHODS
/**
* Export the model data to a Formatted Arcadia Data array.
*
* @return array<string,string|integer|float|array<string,integer>>
*/
public function exportToOrderMailData(): array
{
return [
'ref' => $this->ref,
'civility' => $this->civility->value(),
'lastname' => $this->lastname,
'firstname' => $this->firstname,
'address' => $this->address,
'postcode' => $this->postcode,
'city' => $this->city,
'email' => $this->email,
'phone' => $this->phone ?? '',
'items' => $this->sellItems()->withPivot('quantity')->get()->map(function (SellItem $item) {
// @phpstan-ignore-next-line
$item->typeCustom = $item->type->toArray()[$item->type->name()];
return $item;
})->toArray()
];
}
// * RELATIONSHIPS
/**
* Get all of the items that are assigned this order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function sellItems(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(SellItem::class)->withPivot('quantity');
}
/**
* Get the customer that are assigned this order.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Customer::class);
}
/**
* Get the delivery address that are assigned this order.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function deliveryAddress(): \Illuminate\Database\Eloquent\Relations\HasOne
{
return $this->hasOne(DeliveryAddress::class);
}
}