Session   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 246
rs 9.84
wmc 32

30 Methods

Rating   Name   Duplication   Size   Complexity  
A loans() 0 3 1
A refunds() 0 3 1
A partial_refunds() 0 3 1
A auctions() 0 3 1
A session_bills() 0 3 1
A savings() 0 3 1
A libre_bills() 0 3 1
A receivables() 0 3 1
A casts() 0 6 1
A notLast() 0 4 1
A round() 0 3 1
A notFirst() 0 4 1
A host() 0 3 1
A payables() 0 3 1
A abbrev() 0 4 1
A pending() 0 4 1
A closed() 0 4 1
A opened() 0 4 1
A times() 0 4 1
A absences() 0 3 1
A scopeOpened() 0 3 1
A pools() 0 3 1
A scopeActive() 0 3 1
A scopeSucceedes() 0 5 2
A deposits() 0 3 1
A scopePrecedes() 0 5 2
A disabled_pools() 0 3 1
A funds() 0 3 1
A active() 0 4 1
A outflows() 0 3 1
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 active(): Attribute
117
    {
118
        return Attribute::make(
119
            get: fn() => $this->status !== self::STATUS_PENDING,
120
        );
121
    }
122
123
    public function round()
124
    {
125
        return $this->belongsTo(Round::class);
126
    }
127
128
    public function host()
129
    {
130
        return $this->belongsTo(Member::class);
131
    }
132
133
    public function payables()
134
    {
135
        return $this->hasMany(Payable::class)->orderBy('payables.id', 'asc');
136
    }
137
138
    public function receivables()
139
    {
140
        return $this->hasMany(Receivable::class);
141
    }
142
143
    public function deposits()
144
    {
145
        return $this->hasMany(Deposit::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 outflows()
184
    {
185
        return $this->hasMany(Outflow::class);
186
    }
187
188
    public function funds()
189
    {
190
        return $this->belongsToMany(Fund::class, 'v_fund_session');
191
    }
192
193
    public function pools()
194
    {
195
        return $this->belongsToMany(Pool::class, 'v_pool_session');
196
    }
197
198
    public function disabled_pools()
199
    {
200
        return $this->belongsToMany(Pool::class, 'pool_session_disabled');
201
    }
202
203
    public function absences()
204
    {
205
        return $this->belongsToMany(Member::class, 'absences');
206
    }
207
208
    /**
209
     * @param  Builder  $query
210
     *
211
     * @return Builder
212
     */
213
    public function scopeActive(Builder $query): Builder
214
    {
215
        return $query->where('sessions.status', '!=', self::STATUS_PENDING);
216
    }
217
218
    /**
219
     * @param  Builder  $query
220
     *
221
     * @return Builder
222
     */
223
    public function scopeOpened(Builder $query): Builder
224
    {
225
        return $query->where('status', '=', self::STATUS_OPENED);
226
    }
227
228
    /**
229
     * @param  Builder  $query
230
     * @param  Session  $session
231
     * @param  bool $strictly
232
     *
233
     * @return Builder
234
     */
235
    public function scopePrecedes(Builder $query, Session $session, bool $strictly = false): Builder
236
    {
237
        return $query
238
            ->where('round_id', $session->round_id)
239
            ->where('day_date', $strictly ? '<' : '<=', $session->day_date);
240
    }
241
242
    /**
243
     * @param  Builder  $query
244
     * @param  Session  $session
245
     * @param  bool $strictly
246
     *
247
     * @return Builder
248
     */
249
    public function scopeSucceedes(Builder $query, Session $session, bool $strictly = false): Builder
250
    {
251
        return $query
252
            ->where('round_id', $session->round_id)
253
            ->where('day_date', $strictly ? '>' : '>=', $session->day_date);
254
    }
255
}
256