Passed
Push — main ( 88132a...a9f3b1 )
by Thierry
15:32 queued 13s
created

Debt::typeLabel()   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
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 Debt extends Base
9
{
10
    /**
11
     * Indicates if the model should be timestamped.
12
     *
13
     * @var bool
14
     */
15
    public $timestamps = false;
16
17
    /**
18
     * @const
19
     */
20
    const TYPE_PRINCIPAL = 'p';
21
22
    /**
23
     * @const
24
     */
25
    const TYPE_INTEREST = 'i';
26
27
    /**
28
     * The attributes that are mass assignable.
29
     *
30
     * @var array
31
     */
32
    protected $fillable = [
33
        'type',
34
        'amount',
35
    ];
36
37
    public function getIsPrincipalAttribute()
38
    {
39
        return $this->type === self::TYPE_PRINCIPAL;
40
    }
41
42
    public function getIsInterestAttribute()
43
    {
44
        return $this->type === self::TYPE_INTEREST;
45
    }
46
47
    public function getTypeStrAttribute()
48
    {
49
        return $this->type === self::TYPE_PRINCIPAL ? 'principal' : 'interest';
50
    }
51
52
    /**
53
     * @return Attribute
54
     */
55
    protected function dueAmount(): Attribute
56
    {
57
        return Attribute::make(
58
            get: fn() => $this->amount - $this->partial_refunds->sum('amount'),
59
        );
60
    }
61
62
    /**
63
     * @param  Builder  $query
64
     *
65
     * @return Builder
66
     */
67
    public function scopePrincipal(Builder $query): Builder
68
    {
69
        return $query->where('type', self::TYPE_PRINCIPAL);
70
    }
71
72
    /**
73
     * @param  Builder  $query
74
     *
75
     * @return Builder
76
     */
77
    public function scopeInterest(Builder $query): Builder
78
    {
79
        return $query->where('type', self::TYPE_INTEREST);
80
    }
81
82
    public function loan()
83
    {
84
        return $this->belongsTo(Loan::class);
85
    }
86
87
    public function refund()
88
    {
89
        return $this->hasOne(Refund::class);
90
    }
91
92
    public function partial_refunds()
93
    {
94
        return $this->hasMany(PartialRefund::class);
95
    }
96
}
97