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