1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use App\Enum\OrderStatus as EnumOrderStatus; |
6
|
|
|
use App\Infrastructure\Database\Eloquent\Model; |
7
|
|
|
use App\Models\OrderStatus as ModelOrderStatus; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
10
|
|
|
use Illuminate\Support\Carbon; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
|
13
|
|
|
class Order extends Model |
14
|
|
|
{ |
15
|
|
|
use HasFactory, |
|
|
|
|
16
|
|
|
Concerns\Order\Attribute, |
17
|
|
|
Concerns\Order\Event, |
18
|
|
|
Concerns\Order\Relation; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
|
|
protected $fillable = [ |
24
|
|
|
'code', |
25
|
|
|
'schedule_date', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritDoc} |
30
|
|
|
*/ |
31
|
|
|
protected $casts = [ |
32
|
|
|
'schedule_date' => 'datetime', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
protected $appends = [ |
39
|
|
|
'status', |
40
|
|
|
'item_total_bundle_quantity', |
41
|
|
|
'item_total', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
1 |
|
public function getRouteKeyName() |
48
|
|
|
{ |
49
|
1 |
|
return 'code'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function resolveChildRouteBinding($childType, $value, $field) |
56
|
|
|
{ |
57
|
|
|
$relationship = $this->{Str::plural(Str::camel($childType))}(); |
58
|
|
|
|
59
|
|
|
if ($relationship->getRelated() instanceof OrderStatus) { |
60
|
|
|
return $relationship->where('status', EnumOrderStatus::from($value))->first(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($relationship->getRelated() instanceof Item) { |
64
|
|
|
return $relationship->whereHas('denomination', function (Builder $query) use ($value) { |
65
|
|
|
return $query->where('value', $value); |
66
|
|
|
})->first(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return parent::resolveChildRouteBinding($childType, $value, $field); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Generate code for this model. |
74
|
|
|
* |
75
|
|
|
* @param string $prefix |
76
|
|
|
* @param \Illuminate\Support\Carbon|null $date |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
2 |
|
public function generateCode(string $prefix = 'BCA', Carbon $date = null): string |
80
|
|
|
{ |
81
|
2 |
|
$date ??= Carbon::today(); |
82
|
|
|
|
83
|
2 |
|
return $prefix . '-' . $date->format('Ymd') . '-' . Str::random(5); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Find the specified order based on the given code or create a new one. |
88
|
|
|
* |
89
|
|
|
* @param string|null $code |
90
|
|
|
* @param \App\Models\Customer $customer |
91
|
|
|
* @param callable|null $callable |
92
|
|
|
* @return static |
93
|
|
|
*/ |
94
|
|
|
public static function findOrCreateFromCode(?string $code, Customer $customer, callable $callable = null) |
95
|
|
|
{ |
96
|
|
|
return static::where('code', $code)->firstOr(function () use ($customer, $callable) { |
97
|
|
|
$order = new Order; |
98
|
|
|
|
99
|
|
|
$order->setCustomerRelationValue($customer)->save(); |
100
|
|
|
|
101
|
|
|
$orderStatus = new ModelOrderStatus(['status' => EnumOrderStatus::draft()]); |
102
|
|
|
|
103
|
|
|
$order->statuses()->save( |
104
|
|
|
$orderStatus->setIssuerableRelationValue($customer) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
if ($callable) { |
108
|
|
|
$callable($order); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $order; |
112
|
|
|
}); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Determine whether the "item_total" attribute value is exceeding the maximum total order value. |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function isMaximumTotalOrderExceeded(): bool |
121
|
|
|
{ |
122
|
|
|
return $this->item_total > Configuration::getMaximumTotalOrderValue(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Determine whether the order number of the day is exceeding the maximum order per day. |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public static function isMaximumOrderPerDayExceeded(): bool |
131
|
|
|
{ |
132
|
|
|
return static::whereDate('created_at', Carbon::today())->count() >= Configuration::getMaximumOrderPerDay(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|