1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Models\Traits\CrudTrait; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Support\Facades\App; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @mixin IdeHelperScheduledPayment |
13
|
|
|
*/ |
14
|
|
|
class ScheduledPayment extends Model |
15
|
|
|
{ |
16
|
|
|
use CrudTrait; |
|
|
|
|
17
|
|
|
|
18
|
|
|
protected $table = 'scheduled_payments'; |
19
|
|
|
|
20
|
|
|
protected $guarded = ['id']; |
21
|
|
|
|
22
|
|
|
/* |
23
|
|
|
|-------------------------------------------------------------------------- |
24
|
|
|
| FUNCTIONS |
25
|
|
|
|-------------------------------------------------------------------------- |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
public function scopeStatus(Builder $query, $status) |
29
|
|
|
{ |
30
|
|
|
return match ($status) { |
31
|
|
|
'2' => $query->where('status', 2), |
32
|
|
|
'1' => $query->where('status', 1), |
33
|
|
|
default => $query, |
34
|
|
|
}; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function markAsPaid() |
38
|
|
|
{ |
39
|
|
|
$this->status = 2; |
40
|
|
|
$this->save(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/* |
44
|
|
|
|-------------------------------------------------------------------------- |
45
|
|
|
| RELATIONS |
46
|
|
|
|-------------------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
public function enrollment() |
50
|
|
|
{ |
51
|
|
|
return $this->belongsTo(Enrollment::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function statusType() |
55
|
|
|
{ |
56
|
|
|
return $this->belongsTo(EnrollmentStatusType::class, 'status'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function invoiceDetails() |
60
|
|
|
{ |
61
|
|
|
return $this->morphMany(InvoiceDetail::class, 'product'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function invoices() |
65
|
|
|
{ |
66
|
|
|
return $this->invoiceDetails->map(fn(InvoiceDetail $invoiceDetail) => $invoiceDetail->invoice)->filter(); |
67
|
|
|
} |
68
|
|
|
/* |
69
|
|
|
|-------------------------------------------------------------------------- |
70
|
|
|
| SCOPES |
71
|
|
|
|-------------------------------------------------------------------------- |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
/* |
75
|
|
|
|-------------------------------------------------------------------------- |
76
|
|
|
| ACCESSORS |
77
|
|
|
|-------------------------------------------------------------------------- |
78
|
|
|
*/ |
79
|
|
|
|
80
|
|
|
public function getValueAttribute($value) |
81
|
|
|
{ |
82
|
|
|
return $value / 100; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getValueWithCurrencyAttribute() |
86
|
|
|
{ |
87
|
|
|
if (config('app.currency_position') === 'before') { |
88
|
|
|
return config('app.currency_symbol').' '.$this->value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->value.' '.config('app.currency_symbol'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getDateForHumansAttribute() |
95
|
|
|
{ |
96
|
|
|
if ($this->date) { |
97
|
|
|
return Carbon::parse($this->date, 'UTC')->locale(App::getLocale())->isoFormat('LL'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return Carbon::parse($this->created_at, 'UTC')->locale(App::getLocale())->isoFormat('LL'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** @deprecated */ |
104
|
|
|
public function getComputedStatusAttribute() |
105
|
|
|
{ |
106
|
|
|
// if there is a custom status, always take it |
107
|
|
|
if ($this->status) { |
108
|
|
|
return $this->status; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// otherwise, check if the scheduled payment has invoices |
112
|
|
|
return $this->invoices()->count() > 0 ? 2 : 1; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function identifiableAttribute() |
116
|
|
|
{ |
117
|
|
|
return $this->date.' ('.$this->value_with_currency.')'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getStatusTypeNameAttribute() |
121
|
|
|
{ |
122
|
|
|
return match ($this->status) { |
123
|
|
|
2 => __('Paid'), |
124
|
|
|
1 => __('Pending'), |
125
|
|
|
default => '-', |
126
|
|
|
}; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/* |
130
|
|
|
|-------------------------------------------------------------------------- |
131
|
|
|
| MUTATORS |
132
|
|
|
|-------------------------------------------------------------------------- |
133
|
|
|
*/ |
134
|
|
|
|
135
|
|
|
public function setValueAttribute($value) |
136
|
|
|
{ |
137
|
|
|
$this->attributes['value'] = $value * 100; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|