Charge   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
c 0
b 0
f 0
dl 0
loc 203
rs 10
wmc 25

24 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeFine() 0 3 1
A isFine() 0 4 1
A libre_bills() 0 3 1
A session_bills() 0 3 1
A isVariable() 0 4 1
A scopeFixed() 0 3 1
A round_bills() 0 3 1
A scopeLendable() 0 4 1
A targets() 0 3 1
A periodRound() 0 4 1
A isFixed() 0 4 1
A hasAmount() 0 4 2
A onetime_bills() 0 3 1
A round() 0 3 1
A scopeSession() 0 3 1
A periodOnce() 0 4 1
A scopeRound() 0 3 1
A scopeOnce() 0 3 1
A scopeFee() 0 3 1
A scopeVariable() 0 3 1
A def() 0 3 1
A booted() 0 6 1
A isFee() 0 4 1
A periodSession() 0 4 1
1
<?php
2
3
namespace Siak\Tontine\Model;
4
5
use Illuminate\Database\Eloquent\Casts\Attribute;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Support\Facades\DB;
8
9
use function intval;
10
11
class Charge extends Base
12
{
13
    /**
14
     * Indicates if the model should be timestamped.
15
     *
16
     * @var bool
17
     */
18
    public $timestamps = false;
19
20
    /**
21
     * The attributes that are mass assignable.
22
     *
23
     * @var array
24
     */
25
    protected $fillable = [
26
        'def_id',
27
        'round_id',
28
    ];
29
30
    /**
31
     * The "booted" method of the model.
32
     *
33
     * @return void
34
     */
35
    protected static function booted()
36
    {
37
        // Also select fields from the charge_defs table.
38
        static::addGlobalScope('def', fn(Builder $query) => $query
39
            ->addSelect(['charges.*', 'd.name', 'd.type', 'd.period', 'd.amount', 'd.lendable'])
40
            ->join(DB::raw('charge_defs as d'), 'd.id', '=', 'charges.def_id'));
41
    }
42
43
    public function isFee(): Attribute
44
    {
45
        return Attribute::make(
46
            get: fn() => intval($this->type) === ChargeDef::TYPE_FEE,
47
        );
48
    }
49
50
    public function isFine(): Attribute
51
    {
52
        return Attribute::make(
53
            get: fn() => intval($this->type) === ChargeDef::TYPE_FINE,
54
        );
55
    }
56
57
    public function periodOnce(): Attribute
58
    {
59
        return Attribute::make(
60
            get: fn() => intval($this->period) === ChargeDef::PERIOD_ONCE,
61
        );
62
    }
63
64
    public function periodRound(): Attribute
65
    {
66
        return Attribute::make(
67
            get: fn() => intval($this->period) === ChargeDef::PERIOD_ROUND,
68
        );
69
    }
70
71
    public function periodSession(): Attribute
72
    {
73
        return Attribute::make(
74
            get: fn() => intval($this->period) === ChargeDef::PERIOD_SESSION,
75
        );
76
    }
77
78
    public function isFixed(): Attribute
79
    {
80
        return Attribute::make(
81
            get: fn() => intval($this->period) !== ChargeDef::PERIOD_NONE,
82
        );
83
    }
84
85
    public function isVariable(): Attribute
86
    {
87
        return Attribute::make(
88
            get: fn() => intval($this->period) === ChargeDef::PERIOD_NONE,
89
        );
90
    }
91
92
    public function hasAmount(): Attribute
93
    {
94
        return Attribute::make(
95
            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...
96
        );
97
    }
98
99
    public function def()
100
    {
101
        return $this->belongsTo(ChargeDef::class, 'def_id');
102
    }
103
104
    public function round()
105
    {
106
        return $this->belongsTo(Round::class);
107
    }
108
109
    public function session_bills()
110
    {
111
        return $this->hasMany(SessionBill::class);
112
    }
113
114
    public function round_bills()
115
    {
116
        return $this->hasMany(RoundBill::class);
117
    }
118
119
    public function onetime_bills()
120
    {
121
        return $this->hasMany(OnetimeBill::class);
122
    }
123
124
    public function libre_bills()
125
    {
126
        return $this->hasMany(LibreBill::class);
127
    }
128
129
    public function targets()
130
    {
131
        return $this->hasMany(SettlementTarget::class);
132
    }
133
134
    /**
135
     * @param  Builder  $query
136
     *
137
     * @return Builder
138
     */
139
    public function scopeFixed(Builder $query): Builder
140
    {
141
        return $query->whereHas('def', fn(Builder $qd) => $qd->fixed());
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...
142
    }
143
144
    /**
145
     * @param  Builder  $query
146
     *
147
     * @return Builder
148
     */
149
    public function scopeVariable(Builder $query): Builder
150
    {
151
        return $query->whereHas('def', fn(Builder $qd) => $qd->variable());
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...
152
    }
153
154
    /**
155
     * @param  Builder  $query
156
     * @param  bool  $lendable
157
     *
158
     * @return Builder
159
     */
160
    public function scopeLendable(Builder $query, bool $lendable): Builder
161
    {
162
        return $query->whereHas('def', fn(Builder $qd) =>
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...
163
            $qd->where('lendable', $lendable));
164
    }
165
166
    /**
167
     * @param  Builder  $query
168
     *
169
     * @return Builder
170
     */
171
    public function scopeFee(Builder $query): Builder
172
    {
173
        return $query->whereHas('def', fn(Builder $qd) => $qd->fee());
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...
174
    }
175
176
    /**
177
     * @param  Builder  $query
178
     *
179
     * @return Builder
180
     */
181
    public function scopeFine(Builder $query): Builder
182
    {
183
        return $query->whereHas('def', fn(Builder $qd) => $qd->fine());
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...
184
    }
185
186
    /**
187
     * @param  Builder  $query
188
     *
189
     * @return Builder
190
     */
191
    public function scopeOnce(Builder $query): Builder
192
    {
193
        return $query->whereHas('def', fn(Builder $qd) => $qd->once());
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...
194
    }
195
196
    /**
197
     * @param  Builder  $query
198
     *
199
     * @return Builder
200
     */
201
    public function scopeRound(Builder $query): Builder
202
    {
203
        return $query->whereHas('def', fn(Builder $qd) => $qd->round());
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...
204
    }
205
206
    /**
207
     * @param  Builder  $query
208
     *
209
     * @return Builder
210
     */
211
    public function scopeSession(Builder $query): Builder
212
    {
213
        return $query->whereHas('def', fn(Builder $qd) => $qd->session());
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...
214
    }
215
}
216