1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Model; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute; |
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 isPrincipal(): Attribute |
38
|
|
|
{ |
39
|
|
|
return Attribute::make( |
40
|
|
|
get: fn() => $this->type === self::TYPE_PRINCIPAL, |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function isInterest(): Attribute |
45
|
|
|
{ |
46
|
|
|
return Attribute::make( |
47
|
|
|
get: fn() => $this->type === self::TYPE_INTEREST, |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function typeStr(): Attribute |
52
|
|
|
{ |
53
|
|
|
return Attribute::make( |
54
|
|
|
get: fn() => $this->type === self::TYPE_PRINCIPAL ? 'principal' : 'interest', |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return Attribute |
60
|
|
|
*/ |
61
|
|
|
protected function dueAmount(): Attribute |
62
|
|
|
{ |
63
|
|
|
return Attribute::make( |
64
|
|
|
get: fn() => $this->amount - $this->partial_refunds->sum('amount'), |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return Attribute |
70
|
|
|
*/ |
71
|
|
|
protected function allRefunds(): Attribute |
72
|
|
|
{ |
73
|
|
|
return Attribute::make( |
74
|
|
|
get: fn() => !$this->refund ? $this->partial_refunds : |
75
|
|
|
$this->partial_refunds->push($this->refund), |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Builder $query |
81
|
|
|
* |
82
|
|
|
* @return Builder |
83
|
|
|
*/ |
84
|
|
|
public function scopePrincipal(Builder $query): Builder |
85
|
|
|
{ |
86
|
|
|
return $query->where('type', self::TYPE_PRINCIPAL); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Builder $query |
91
|
|
|
* |
92
|
|
|
* @return Builder |
93
|
|
|
*/ |
94
|
|
|
public function scopeInterest(Builder $query): Builder |
95
|
|
|
{ |
96
|
|
|
return $query->where('type', self::TYPE_INTEREST); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function loan() |
100
|
|
|
{ |
101
|
|
|
return $this->belongsTo(Loan::class); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function refund() |
105
|
|
|
{ |
106
|
|
|
return $this->hasOne(Refund::class); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function partial_refunds() |
110
|
|
|
{ |
111
|
|
|
return $this->hasMany(PartialRefund::class); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Will be used to get the unique partial refund for a given session |
116
|
|
|
*/ |
117
|
|
|
public function partial_refund() |
118
|
|
|
{ |
119
|
|
|
// We use latest() instead of latestOfMany() because it is simpler to |
120
|
|
|
// add clauses with extra parameters to the subquery. |
121
|
|
|
return $this->hasOne(PartialRefund::class)->latest('id'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|