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