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

Charge::periodSession()   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 Database\Factories\ChargeFactory;
6
use Illuminate\Database\Eloquent\Casts\Attribute;
7
use Illuminate\Database\Eloquent\Factories\Factory;
8
use Illuminate\Database\Eloquent\Factories\HasFactory;
9
use Illuminate\Database\Eloquent\Builder;
10
use Siak\Tontine\Model\Traits\HasCurrency;
11
12
use function intval;
13
14
class Charge extends Base
15
{
16
    use HasFactory;
17
    use HasCurrency;
0 ignored issues
show
Bug introduced by
The trait Siak\Tontine\Model\Traits\HasCurrency requires the property $amount which is not provided by Siak\Tontine\Model\Charge.
Loading history...
18
19
    /**
20
     * @const
21
     */
22
    const TYPE_FEE = 0;
23
24
    /**
25
     * @const
26
     */
27
    const TYPE_FINE = 1;
28
29
    /**
30
     * @const
31
     */
32
    const PERIOD_NONE = 0;
33
34
    /**
35
     * @const
36
     */
37
    const PERIOD_ONCE = 1;
38
39
    /**
40
     * @const
41
     */
42
    const PERIOD_ROUND = 2;
43
44
    /**
45
     * @const
46
     */
47
    const PERIOD_SESSION = 3;
48
49
    /**
50
     * Indicates if the model should be timestamped.
51
     *
52
     * @var bool
53
     */
54
    public $timestamps = false;
55
56
    /**
57
     * The attributes that are mass assignable.
58
     *
59
     * @var array
60
     */
61
    protected $fillable = [
62
        'name',
63
        'type',
64
        'period',
65
        'amount',
66
        'active',
67
        'lendable',
68
    ];
69
70
    /**
71
     * Create a new factory instance for the model.
72
     *
73
     * @return Factory
74
     */
75
    protected static function newFactory()
76
    {
77
        return ChargeFactory::new();
78
    }
79
80
    public function tontine()
81
    {
82
        return $this->belongsTo(Tontine::class);
83
    }
84
85
    public function session_bills()
86
    {
87
        return $this->hasMany(SessionBill::class);
88
    }
89
90
    public function round_bills()
91
    {
92
        return $this->hasMany(RoundBill::class);
93
    }
94
95
    public function tontine_bills()
96
    {
97
        return $this->hasMany(TontineBill::class);
98
    }
99
100
    public function libre_bills()
101
    {
102
        return $this->hasMany(LibreBill::class);
103
    }
104
105
    public function targets()
106
    {
107
        return $this->hasMany(SettlementTarget::class);
108
    }
109
110
    public function isFee(): Attribute
111
    {
112
        return Attribute::make(
113
            get: fn() => intval($this->type) === self::TYPE_FEE,
114
        );
115
    }
116
117
    public function isFine(): Attribute
118
    {
119
        return Attribute::make(
120
            get: fn() => intval($this->type) === self::TYPE_FINE,
121
        );
122
    }
123
124
    public function periodOnce(): Attribute
125
    {
126
        return Attribute::make(
127
            get: fn() => intval($this->period) === self::PERIOD_ONCE,
128
        );
129
    }
130
131
    public function periodRound(): Attribute
132
    {
133
        return Attribute::make(
134
            get: fn() => intval($this->period) === self::PERIOD_ROUND,
135
        );
136
    }
137
138
    public function periodSession(): Attribute
139
    {
140
        return Attribute::make(
141
            get: fn() => intval($this->period) === self::PERIOD_SESSION,
142
        );
143
    }
144
145
    public function isFixed(): Attribute
146
    {
147
        return Attribute::make(
148
            get: fn() => intval($this->period) !== self::PERIOD_NONE,
149
        );
150
    }
151
152
    public function isVariable(): Attribute
153
    {
154
        return Attribute::make(
155
            get: fn() => intval($this->period) === self::PERIOD_NONE,
156
        );
157
    }
158
159
    public function hasAmount(): Attribute
160
    {
161
        return Attribute::make(
162
            get: fn() => $this->is_fixed || $this->amount > 0,
0 ignored issues
show
Bug introduced by
The property is_fixed does not seem to exist on Siak\Tontine\Model\Charge. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
163
        );
164
    }
165
166
    public function isActive(): Attribute
167
    {
168
        return Attribute::make(
169
            get: fn() => $this->active == true,
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
170
        );
171
    }
172
173
    /**
174
     * @param  Builder  $query
175
     *
176
     * @return Builder
177
     */
178
    public function scopeFee(Builder $query): Builder
179
    {
180
        return $query->where('type', self::TYPE_FEE);
181
    }
182
183
    /**
184
     * @param  Builder  $query
185
     *
186
     * @return Builder
187
     */
188
    public function scopeFine(Builder $query): Builder
189
    {
190
        return $query->where('type', self::TYPE_FINE);
191
    }
192
193
    /**
194
     * @param  Builder  $query
195
     *
196
     * @return Builder
197
     */
198
    public function scopeOnce(Builder $query): Builder
199
    {
200
        return $query->where('type', self::TYPE_FEE)->where('period', self::PERIOD_ONCE);
201
    }
202
203
    /**
204
     * @param  Builder  $query
205
     *
206
     * @return Builder
207
     */
208
    public function scopeRound(Builder $query): Builder
209
    {
210
        return $query->where('type', self::TYPE_FEE)->where('period', self::PERIOD_ROUND);
211
    }
212
213
    /**
214
     * @param  Builder  $query
215
     *
216
     * @return Builder
217
     */
218
    public function scopeSession(Builder $query): Builder
219
    {
220
        return $query->where('type', self::TYPE_FEE)->where('period', self::PERIOD_SESSION);
221
    }
222
223
    /**
224
     * @param  Builder  $query
225
     *
226
     * @return Builder
227
     */
228
    public function scopeActive(Builder $query): Builder
229
    {
230
        return $query->where('active', true);
231
    }
232
233
    /**
234
     * @param  Builder  $query
235
     *
236
     * @return Builder
237
     */
238
    public function scopeFixed(Builder $query): Builder
239
    {
240
        return $query->where('period', '!=', self::PERIOD_NONE);
241
    }
242
243
    /**
244
     * @param  Builder  $query
245
     *
246
     * @return Builder
247
     */
248
    public function scopeVariable(Builder $query): Builder
249
    {
250
        return $query->where('period', self::PERIOD_NONE);
251
    }
252
}
253