Passed
Push — master ( ef8d4f...ddeb28 )
by Thomas
12:09
created

ScheduledPayment::statusType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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