Passed
Push — main ( f9c7ef...02eb29 )
by Thierry
05:46
created

Session::date()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Siak\Tontine\Model;
4
5
use Illuminate\Database\Eloquent\Casts\Attribute;
6
use Illuminate\Database\Eloquent\Builder;
7
8
class Session extends Base
9
{
10
    use Traits\DateFormatter;
11
12
    /**
13
     * @const
14
     */
15
    const STATUS_PENDING = 0;
16
17
    /**
18
     * @const
19
     */
20
    const STATUS_OPENED = 1;
21
22
    /**
23
     * @const
24
     */
25
    const STATUS_CLOSED = 2;
26
27
    /**
28
     * Indicates if the model should be timestamped.
29
     *
30
     * @var bool
31
     */
32
    public $timestamps = false;
33
34
    /**
35
     * The attributes that are mass assignable.
36
     *
37
     * @var array
38
     */
39
    protected $fillable = [
40
        'title',
41
        'abbrev',
42
        'agenda',
43
        'report',
44
        'status',
45
        'notes',
46
        'venue',
47
        'day_date',
48
        'start_time',
49
        'end_time',
50
        'host_id',
51
    ];
52
53
    /**
54
     * Get the attributes that should be cast.
55
     *
56
     * @return array<string, string>
57
     */
58
    protected function casts(): array
59
    {
60
        return [
61
            'day_date' => 'datetime:Y-m-d',
62
            'start_time' => 'datetime:H:i',
63
            'end_time' => 'datetime:H:i',
64
        ];
65
    }
66
67
    public function notFirst(): Attribute
68
    {
69
        return Attribute::make(
70
            get: fn() => $this->round->sessions()->where('day_date', '<', $this->day_date)->exists(),
71
        );
72
    }
73
74
    public function notLast(): Attribute
75
    {
76
        return Attribute::make(
77
            get: fn() => $this->round->sessions()->where('day_date', '>', $this->day_date)->exists(),
78
        );
79
    }
80
81
    public function abbrev(): Attribute
82
    {
83
        return Attribute::make(
84
            get: fn($value) => $value ?? $this->day_date->format('M y'),
85
        );
86
    }
87
88
    public function times(): Attribute
89
    {
90
        return Attribute::make(
91
            get: fn() => $this->start_time->format('H:i') . ' - ' . $this->end_time->format('H:i'),
92
        );
93
    }
94
95
    public function pending(): Attribute
96
    {
97
        return Attribute::make(
98
            get: fn() => $this->status === self::STATUS_PENDING,
99
        );
100
    }
101
102
    public function opened(): Attribute
103
    {
104
        return Attribute::make(
105
            get: fn() => $this->status === self::STATUS_OPENED,
106
        );
107
    }
108
109
    public function closed(): Attribute
110
    {
111
        return Attribute::make(
112
            get: fn() => $this->status === self::STATUS_CLOSED,
113
        );
114
    }
115
116
    public function round()
117
    {
118
        return $this->belongsTo(Round::class);
119
    }
120
121
    public function host()
122
    {
123
        return $this->belongsTo(Member::class);
124
    }
125
126
    public function payables()
127
    {
128
        return $this->hasMany(Payable::class)->orderBy('payables.id', 'asc');
129
    }
130
131
    public function receivables()
132
    {
133
        return $this->hasMany(Receivable::class);
134
    }
135
136
    public function session_bills()
137
    {
138
        return $this->hasMany(SessionBill::class);
139
    }
140
141
    public function libre_bills()
142
    {
143
        return $this->hasMany(LibreBill::class);
144
    }
145
146
    public function loans()
147
    {
148
        return $this->hasMany(Loan::class);
149
    }
150
151
    public function auctions()
152
    {
153
        return $this->hasMany(Auction::class);
154
    }
155
156
    public function refunds()
157
    {
158
        return $this->hasMany(Refund::class);
159
    }
160
161
    public function partial_refunds()
162
    {
163
        return $this->hasMany(PartialRefund::class);
164
    }
165
166
    public function savings()
167
    {
168
        return $this->hasMany(Saving::class);
169
    }
170
171
    public function outflows()
172
    {
173
        return $this->hasMany(Outflow::class);
174
    }
175
176
    public function funds()
177
    {
178
        return $this->belongsToMany(Fund::class, 'v_fund_session');
179
    }
180
181
    public function pools()
182
    {
183
        return $this->belongsToMany(Pool::class, 'v_pool_session');
184
    }
185
186
    public function disabled_pools()
187
    {
188
        return $this->belongsToMany(Pool::class, 'pool_session_disabled');
189
    }
190
191
    public function absents()
192
    {
193
        return $this->belongsToMany(Member::class, 'absences');
194
    }
195
196
    /**
197
     * @param  Builder  $query
198
     *
199
     * @return Builder
200
     */
201
    public function scopeActive(Builder $query): Builder
202
    {
203
        return $query->where('sessions.status', '!=', self::STATUS_PENDING);
204
    }
205
206
    /**
207
     * @param  Builder  $query
208
     *
209
     * @return Builder
210
     */
211
    public function scopeOpened(Builder $query): Builder
212
    {
213
        return $query->where('status', '=', self::STATUS_OPENED);
214
    }
215
}
216