2020-11-17 13:41:31 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-11-17 13:41:31 +01:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Casts\DeliveryAddressCast;
|
|
|
|
|
use App\Casts\MoneyCast;
|
|
|
|
|
use App\Casts\OrderEmployeeCast;
|
|
|
|
|
use App\Casts\OrderReferenceCast;
|
|
|
|
|
use App\Traits\Models\HasUuidV7AsPrimaryKey;
|
|
|
|
|
use Carbon\CarbonInterface;
|
|
|
|
|
use Domain\Enums\OrderStatus;
|
|
|
|
|
use Domain\Models\ValueObjects\Entities\OrderId;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2020-11-17 13:41:31 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
2020-11-17 13:41:31 +01:00
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/**
|
|
|
|
|
* @property string $id
|
|
|
|
|
* @property string|null $shop_id
|
|
|
|
|
* @property \Domain\Models\ValueObjects\OrderReference $reference
|
|
|
|
|
* @property \Domain\Models\ValueObjects\OrderEmployee $employee
|
|
|
|
|
* @property \Domain\Models\ValueObjects\DeliveryAddress $delivery_address
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $pickup_day
|
|
|
|
|
* @property \Money\Money $total
|
|
|
|
|
* @property OrderStatus $status
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
|
* @property string|null $employee_matricule
|
|
|
|
|
* @property-read \Carbon\CarbonInterface|null $created_at
|
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\OrderLine> $lines
|
|
|
|
|
* @property-read \App\Models\Shop|null $shop
|
|
|
|
|
*
|
|
|
|
|
* @method static Builder<static>|Order whereCreatedAtBetween(\Carbon\CarbonInterface $start, \Carbon\CarbonInterface $end)
|
|
|
|
|
*
|
|
|
|
|
* @mixin \Eloquent
|
|
|
|
|
*/
|
|
|
|
|
#[Fillable([])]
|
2021-03-17 09:28:02 +01:00
|
|
|
class Order extends Model
|
2020-11-17 13:41:31 +01:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
use HasUuidV7AsPrimaryKey;
|
|
|
|
|
|
|
|
|
|
public const CREATED_AT = null;
|
2020-11-17 13:41:31 +01:00
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
public function getId(): OrderId
|
2022-09-08 11:29:39 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
return new OrderId(Uuid::fromString($this->id));
|
2021-03-27 20:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\OrderLine,$this> */
|
|
|
|
|
public function lines(): HasMany
|
2021-03-27 20:55:16 +01:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(OrderLine::class);
|
2020-11-17 13:41:31 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Shop,$this> */
|
|
|
|
|
public function shop(): BelongsTo
|
2022-09-08 11:29:39 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
return $this->belongsTo(Shop::class);
|
2020-11-17 13:41:31 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @param Builder<static> $query */
|
|
|
|
|
protected function scopeWhereCreatedAtBetween(Builder $query, CarbonInterface $start, CarbonInterface $end): void
|
2022-09-08 11:29:39 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
$minUuid = (string) Uuid::uuid7($start->copy()->startOfDay());
|
|
|
|
|
$maxUuid = (string) Uuid::uuid7($end->copy()->endOfDay());
|
2021-03-29 15:10:07 +02:00
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
$query->whereBetween('id', [$minUuid, $maxUuid]);
|
2020-11-17 13:41:31 +01:00
|
|
|
}
|
2021-04-08 15:49:20 +02:00
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
#[\Override]
|
|
|
|
|
protected function casts(): array
|
2021-04-08 15:49:20 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
return [
|
|
|
|
|
'pickup_day' => 'date',
|
|
|
|
|
'employee' => OrderEmployeeCast::class,
|
|
|
|
|
'total' => MoneyCast::class,
|
|
|
|
|
'status' => OrderStatus::class,
|
|
|
|
|
'delivery_address' => DeliveryAddressCast::class,
|
|
|
|
|
'reference' => OrderReferenceCast::class,
|
|
|
|
|
];
|
2021-04-08 15:49:20 +02:00
|
|
|
}
|
2020-11-17 13:41:31 +01:00
|
|
|
}
|