Passed
Pull Request — main (#63)
by Thierry
06:38
created

Session::abbrev()   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
use function trans;
9
10
class Session extends Base
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
        'start_at',
48
        'end_at',
49
        'host_id',
50
    ];
51
52
    /**
53
     * The attributes that should be mutated to dates.
54
     *
55
     * @var array
56
     */
57
    protected $casts = [
58
        'start_at' => 'datetime',
59
        'end_at' => 'datetime',
60
    ];
61
62
    /**
63
     * The relationships that should always be loaded.
64
     *
65
     * @var array
66
     */
67
    protected $with = [
68
        'host',
69
        'disabled_pools',
70
    ];
71
72
    public function notFirst(): Attribute
73
    {
74
        return Attribute::make(
75
            get: fn() => $this->round->sessions()->where('start_at', '<', $this->start_at)->exists(),
76
        );
77
    }
78
79
    public function notLast(): Attribute
80
    {
81
        return Attribute::make(
82
            get: fn() => $this->round->sessions()->where('start_at', '>', $this->start_at)->exists(),
83
        );
84
    }
85
86
    public function abbrev(): Attribute
87
    {
88
        return Attribute::make(
89
            get: fn($value) => $value ?? $this->start_at->format('M y'),
90
        );
91
    }
92
93
    public function date(): Attribute
94
    {
95
        return Attribute::make(
96
            get: fn() => $this->start_at->translatedFormat(trans('tontine.date.format')),
97
        );
98
    }
99
100
    public function times(): Attribute
101
    {
102
        return Attribute::make(
103
            get: fn() => $this->start_at->format('H:i') . ' - ' . $this->end_at->format('H:i'),
104
        );
105
    }
106
107
    public function pending(): Attribute
108
    {
109
        return Attribute::make(
110
            get: fn() => $this->status === self::STATUS_PENDING,
111
        );
112
    }
113
114
    public function opened(): Attribute
115
    {
116
        return Attribute::make(
117
            get: fn() => $this->status === self::STATUS_OPENED,
118
        );
119
    }
120
121
    public function closed(): Attribute
122
    {
123
        return Attribute::make(
124
            get: fn() => $this->status === self::STATUS_CLOSED,
125
        );
126
    }
127
128
    public function round()
129
    {
130
        return $this->belongsTo(Round::class);
131
    }
132
133
    public function host()
134
    {
135
        return $this->belongsTo(Member::class);
136
    }
137
138
    public function payables()
139
    {
140
        return $this->hasMany(Payable::class)->orderBy('payables.id', 'asc');
141
    }
142
143
    public function receivables()
144
    {
145
        return $this->hasMany(Receivable::class);
146
    }
147
148
    public function session_bills()
149
    {
150
        return $this->hasMany(SessionBill::class);
151
    }
152
153
    public function libre_bills()
154
    {
155
        return $this->hasMany(LibreBill::class);
156
    }
157
158
    public function loans()
159
    {
160
        return $this->hasMany(Loan::class);
161
    }
162
163
    public function auctions()
164
    {
165
        return $this->hasMany(Auction::class);
166
    }
167
168
    public function refunds()
169
    {
170
        return $this->hasMany(Refund::class);
171
    }
172
173
    public function partial_refunds()
174
    {
175
        return $this->hasMany(PartialRefund::class);
176
    }
177
178
    public function savings()
179
    {
180
        return $this->hasMany(Saving::class);
181
    }
182
183
    public function closings()
184
    {
185
        return $this->hasMany(Closing::class);
186
    }
187
188
    public function disbursements()
189
    {
190
        return $this->hasMany(Disbursement::class);
191
    }
192
193
    public function disabled_pools()
194
    {
195
        return $this->belongsToMany(Pool::class, 'pool_session_disabled');
196
    }
197
198
    public function absents()
199
    {
200
        return $this->belongsToMany(Member::class, 'absences');
201
    }
202
203
    /**
204
     * @param  Builder  $query
205
     * @param  Pool $pool
206
     *
207
     * @return Builder
208
     */
209
    public function scopeEnabled(Builder $query, Pool $pool): Builder
210
    {
211
        return $query->whereDoesntHave('disabled_pools',
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereDoes...ion(...) { /* ... */ }) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
212
            fn($q) => $q->where('pools.id', $pool->id));
213
    }
214
215
    /**
216
     * @param  Builder  $query
217
     * @param  Pool $pool
218
     *
219
     * @return Builder
220
     */
221
    public function scopeDisabled(Builder $query, Pool $pool): Builder
222
    {
223
        return $query->whereHas('disabled_pools',
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas(...ion(...) { /* ... */ }) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
224
            fn($q) => $q->where('pools.id', $pool->id));
225
    }
226
227
    /**
228
     * @param  Builder  $query
229
     *
230
     * @return Builder
231
     */
232
    public function scopeActive(Builder $query): Builder
233
    {
234
        return $query->where('status', '!=', self::STATUS_PENDING);
235
    }
236
237
    /**
238
     * @param  Builder  $query
239
     *
240
     * @return Builder
241
     */
242
    public function scopeOpened(Builder $query): Builder
243
    {
244
        return $query->where('status', '=', self::STATUS_OPENED);
245
    }
246
247
    /**
248
     * @param  Builder  $query
249
     * @param  Pool $pool
250
     *
251
     * @return Builder
252
     */
253
    public function scopeOfPool(Builder $query, Pool $pool): Builder
254
    {
255
        return !$pool->pool_round ?
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! $pool->pool_rou...rt_at->format('Y-m-d')) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
256
            $query->where('round_id', $pool->round_id) :
257
            $query->whereDate('start_at', '<=', $pool->end_at->format('Y-m-d'))
258
                ->whereDate('start_at', '>=', $pool->start_at->format('Y-m-d'));
259
    }
260
}
261