Passed
Push — master ( d6e650...30012e )
by Thomas
06:40
created

ScheduledPayment::enrollment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
use App\Models\Enrollment;
9
use Illuminate\Support\Facades\App;
10
11
/**
12
 * App\Models\ScheduledPayment
13
 *
14
 * @property int $id
15
 * @property int $enrollment_id
16
 * @property int $value
17
 * @property string $date
18
 * @property int|null $status
19
 * @property \Illuminate\Support\Carbon|null $created_at
20
 * @property \Illuminate\Support\Carbon|null $updated_at
21
 * @property-read Enrollment $enrollment
22
 * @property-read mixed $computed_status
23
 * @property-read mixed $date_for_humans
24
 * @property-read mixed $value_with_currency
25
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Invoice[] $invoices
26
 * @property-read int|null $invoices_count
27
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment newModelQuery()
28
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment newQuery()
29
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment query()
30
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment whereCreatedAt($value)
31
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment whereDate($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment whereEnrollmentId($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment whereId($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment whereStatus($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment whereUpdatedAt($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|ScheduledPayment whereValue($value)
37
 * @mixin \Eloquent
38
 */
39
class ScheduledPayment extends Model
40
{
41
    use \Backpack\CRUD\app\Models\Traits\CrudTrait;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Models\Traits\CrudTrait requires some properties which are not provided by App\Models\ScheduledPayment: $fakeColumns, $identifiableAttribute, $Type
Loading history...
42
    /*
43
    |--------------------------------------------------------------------------
44
    | GLOBAL VARIABLES
45
    |--------------------------------------------------------------------------
46
    */
47
48
    protected $table = 'scheduled_payments';
49
    // protected $primaryKey = 'id';
50
    // public $timestamps = false;
51
    protected $guarded = ['id'];
52
    // protected $fillable = [];
53
    // protected $hidden = [];
54
    // protected $dates = [];
55
    protected $appends = ['computed_status'];
56
57
    /*
58
    |--------------------------------------------------------------------------
59
    | FUNCTIONS
60
    |--------------------------------------------------------------------------
61
    */
62
63
    public function scopeStatus(Builder $query, $status)
64
    {
65
        return match ($status) {
66
            "2" => $query->where('status', 2)->orWhereHas('invoices'),
67
            "1" => $query->where('status', 1)->orWhereDoesntHave('invoices'),
68
            default => $query,
69
        };
70
    }
71
72
    /*
73
    |--------------------------------------------------------------------------
74
    | RELATIONS
75
    |--------------------------------------------------------------------------
76
    */
77
78
    public function enrollment()
79
    {
80
        return $this->belongsTo(Enrollment::class);
81
    }
82
83
    public function invoices()
84
    {
85
        return $this->belongsToMany(Invoice::class, 'enrollment_invoice', 'scheduled_payment_id', 'invoice_id');
86
    }
87
88
    public function statusType()
89
    {
90
        return $this->belongsTo(EnrollmentStatusType::class, 'status');
91
    }
92
93
    /*
94
    |--------------------------------------------------------------------------
95
    | SCOPES
96
    |--------------------------------------------------------------------------
97
    */
98
99
    /*
100
    |--------------------------------------------------------------------------
101
    | ACCESSORS
102
    |--------------------------------------------------------------------------
103
    */
104
105
    public function getValueAttribute($value)
106
    {
107
        return $value / 100;
108
    }
109
110
    public function getValueWithCurrencyAttribute()
111
    {
112
        if (config('app.currency_position') === 'before')
113
        {
114
            return config('app.currency_symbol') . " ". $this->value;
115
        }
116
117
        return $this->value . " " . config('app.currency_symbol');
118
    }
119
120
    function getDateForHumansAttribute()
121
    {
122
        if ($this->date)
123
        {
124
            return Carbon::parse($this->date, 'UTC')->locale(App::getLocale())->isoFormat('LL');
125
        }
126
        return Carbon::parse($this->created_at, 'UTC')->locale(App::getLocale())->isoFormat('LL');
127
    }
128
129
    public function getComputedStatusAttribute()
130
    {
131
        // if there is a custom status, always take it
132
        if ($this->status)
133
        {
134
            return $this->status;
135
        }
136
137
        // otherwise, check if the scheduled payment has invoices
138
        return $this->invoices->count() > 0 ? 2 : 1;
139
    }
140
141
    public function identifiableAttribute()
142
    {
143
        return $this->date . " (" . $this->value_with_currency . ")";
144
    }
145
146
    public function getStatusTypeNameAttribute()
147
    {
148
        return match($this->computed_status) {
149
            2 => __('Paid'),
150
            1 => __('Pending'),
151
            default => '-',
152
        };
153
    }
154
155
    /*
156
    |--------------------------------------------------------------------------
157
    | MUTATORS
158
    |--------------------------------------------------------------------------
159
    */
160
161
    public function setValueAttribute($value)
162
    {
163
        $this->attributes['value'] = $value * 100;
164
    }
165
}
166