Passed
Push — develop ( d32912...6dcea6 )
by Septianata
05:48
created

Order::generateCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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\Factories\HasFactory;
9
use Illuminate\Support\Carbon;
10
use Illuminate\Support\Str;
11
12
class Order extends Model
13
{
14
    use HasFactory,
0 ignored issues
show
Bug introduced by
The trait App\Models\Concerns\Order\Event requires the property $map which is not provided by App\Models\Order.
Loading history...
introduced by
The trait App\Models\Concerns\Order\Attribute requires some properties which are not provided by App\Models\Order: $bundle_quantity, $total, $sum
Loading history...
15
        Concerns\Order\Attribute,
16
        Concerns\Order\Event,
17
        Concerns\Order\Relation;
18
19
    /**
20
     * {@inheritDoc}
21
     */
22
    protected $fillable = [
23
        'code',
24
        'schedule_date',
25
    ];
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    protected $casts = [
31
        'schedule_date' => 'datetime',
32
    ];
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    protected $appends = [
38
        'status',
39
        'item_total_bundle_quantity',
40
        'item_total',
41
    ];
42
43
    /**
44
     * Generate code for this model.
45
     *
46
     * @param  string  $prefix
47
     * @param  \Illuminate\Support\Carbon|null  $date
48
     * @return string
49
     */
50 3
    public function generateCode(string $prefix = 'BCA', Carbon $date = null): string
51
    {
52 3
        $date ??= Carbon::today();
53
54 3
        return $prefix . '-' . $date->format('Ymd') . '-' . Str::random(5);
55
    }
56
57
    /**
58
     * Find the specified order based on the given code or create a new one.
59
     *
60
     * @param  string|null  $code
61
     * @param  \App\Models\Customer  $customer
62
     * @param  callable|null  $callable
63
     * @return static
64
     */
65
    public static function findOrCreateFromCode(?string $code, Customer $customer, callable $callable = null)
66
    {
67
        return static::where('code', $code)->firstOr(function () use ($customer, $callable) {
68
            $order = new Order;
69
70
            $order->setCustomerRelationValue($customer)->save();
71
72
            $orderStatus = new ModelOrderStatus(['status' => EnumOrderStatus::draft()]);
73
74
            $order->statuses()->save(
75
                $orderStatus->setIssuerableRelationValue($customer)
76
            );
77
78
            if ($callable) {
79
                $callable($order);
80
            }
81
82
            return $order;
83
        });
84
    }
85
86
    /**
87
     * Determine whether the "item_total" attribute value is exceeding the maximum total order value.
88
     *
89
     * @return bool
90
     */
91
    public function isMaximumTotalOrderExceeded(): bool
92
    {
93
        return $this->item_total > Configuration::getMaximumTotalOrderValue();
94
    }
95
96
    /**
97
     * Determine whether the order number of the day is exceeding the maximum order per day.
98
     *
99
     * @return bool
100
     */
101
    public static function isMaximumOrderPerDayExceeded(): bool
102
    {
103
        return static::whereDate('created_at', Carbon::today())->count() >= Configuration::getMaximumOrderPerDay();
104
    }
105
}
106