Passed
Push — main ( bd1e14...7c6bac )
by Thierry
06:03
created

Loan::fixedInterest()   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\Collection;
7
8
/**
9
 * @property string $interest_type
10
 * @property float $interest_rate
11
 * @property-read int $principal
12
 * @property-read int $interest
13
 * @property-read bool $fixed_interest
14
 * @property-read bool $simple_interest
15
 * @property-read bool $compound_interest
16
 * @property-read int $refunds_count
17
 * @property-read Session $session
18
 * @property-read Member $member
19
 * @property-read Fund $fund
20
 * @property-read Debt $principal_debt
21
 * @property-read Debt $interest_debt
22
 * @property-read Collection $debts
23
 * @property-read Collection $refunds
24
 */
25
class Loan extends Base
26
{
27
    /**
28
     * @const
29
     */
30
    const INTEREST_FIXED = 'f';
31
32
    /**
33
     * @const
34
     */
35
    const INTEREST_UNIQUE = 'u';
36
37
    /**
38
     * @const
39
     */
40
    const INTEREST_SIMPLE = 's';
41
42
    /**
43
     * @const
44
     */
45
    const INTEREST_COMPOUND = 'c';
46
47
    /**
48
     * Indicates if the model should be timestamped.
49
     *
50
     * @var bool
51
     */
52
    public $timestamps = false;
53
54
    /**
55
     * The attributes that are mass assignable.
56
     *
57
     * @var array
58
     */
59
    protected $fillable = [
60
        'interest_type',
61
        'interest_rate',
62
        'member_id',
63
        'session_id',
64
    ];
65
66
    /**
67
     * @return Attribute
68
     */
69
    protected function principal(): Attribute
70
    {
71
        return Attribute::make(
72
            get: fn() => $this->principal_debt?->amount ?? 0,
73
        );
74
    }
75
76
    /**
77
     * @return Attribute
78
     */
79
    protected function interest(): Attribute
80
    {
81
        return Attribute::make(
82
            get: fn() => $this->interest_debt?->amount ?? 0,
83
        );
84
    }
85
86
    /**
87
     * @return Attribute
88
     */
89
    protected function fixedInterest(): Attribute
90
    {
91
        return Attribute::make(
92
            get: fn() => $this->interest_type === self::INTEREST_FIXED,
93
        );
94
    }
95
96
    /**
97
     * @return Attribute
98
     */
99
    protected function uniqueInterest(): Attribute
100
    {
101
        return Attribute::make(
102
            get: fn() => $this->interest_type === self::INTEREST_UNIQUE,
103
        );
104
    }
105
106
    /**
107
     * @return Attribute
108
     */
109
    protected function simpleInterest(): Attribute
110
    {
111
        return Attribute::make(
112
            get: fn() => $this->interest_type === self::INTEREST_SIMPLE,
113
        );
114
    }
115
116
    /**
117
     * @return Attribute
118
     */
119
    protected function compoundInterest(): Attribute
120
    {
121
        return Attribute::make(
122
            get: fn() => $this->interest_type === self::INTEREST_COMPOUND,
123
        );
124
    }
125
126
    /**
127
     * @return Attribute
128
     */
129
    protected function recurrentInterest(): Attribute
130
    {
131
        // Interests that grows after each session.
132
        return Attribute::make(
133
            get: fn() => $this->interest_type === self::INTEREST_SIMPLE ||
134
                $this->interest_type === self::INTEREST_COMPOUND,
135
        );
136
    }
137
138
    public function session()
139
    {
140
        return $this->belongsTo(Session::class);
141
    }
142
143
    public function member()
144
    {
145
        return $this->belongsTo(Member::class);
146
    }
147
148
    public function fund()
149
    {
150
        return $this->belongsTo(Fund::class)->withoutGlobalScope('user');
151
    }
152
153
    public function debts()
154
    {
155
        return $this->hasMany(Debt::class);
156
    }
157
158
    public function refunds()
159
    {
160
        return $this->hasManyThrough(Refund::class, Debt::class)
161
            ->select('refunds.*');
162
    }
163
164
    public function principal_debt()
165
    {
166
        // Read from the database
167
        return $this->hasOne(Debt::class)->where('type', Debt::TYPE_PRINCIPAL);
168
    }
169
170
    public function interest_debt()
171
    {
172
        // Read from the database
173
        return $this->hasOne(Debt::class)->where('type', Debt::TYPE_INTEREST);
174
    }
175
176
    /**
177
     * @return Attribute
178
     */
179
    protected function pDebt(): Attribute
180
    {
181
        // Read from the "debts" collection
182
        return Attribute::make(
183
            get: fn() => $this->debts->first(fn($debt) => $debt->type === Debt::TYPE_PRINCIPAL),
184
        );
185
    }
186
187
    /**
188
     * @return Attribute
189
     */
190
    protected function iDebt(): Attribute
191
    {
192
        // Read from the "debts" collection
193
        return Attribute::make(
194
            get: fn() => $this->debts->first(fn($debt) => $debt->type === Debt::TYPE_INTEREST),
195
        );
196
    }
197
198
    /**
199
     * @return Attribute
200
     */
201
    protected function allRefunds(): Attribute
202
    {
203
        return Attribute::make(
204
            get: function() {
205
                $debt = $this->p_debt;
0 ignored issues
show
Bug introduced by
The property p_debt does not seem to exist on Siak\Tontine\Model\Loan. 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...
206
                $refunds = $debt->partial_refunds;
207
                if($debt->refund != null)
208
                {
209
                    $refunds->push($debt->refund);
210
                }
211
                $debt = $this->i_debt;
0 ignored issues
show
Bug introduced by
The property i_debt does not seem to exist on Siak\Tontine\Model\Loan. 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...
212
                if($debt != null)
213
                {
214
                    $refunds = $refunds->concat($debt->partial_refunds);
215
                    if($debt->refund != null)
216
                    {
217
                        $refunds->push($debt->refund);
218
                    }
219
                }
220
                return $refunds;
221
            },
222
        );
223
    }
224
}
225