Passed
Push — master ( 45eca1...10ee28 )
by Thomas
07:06
created

Payment::getDisplayStatusAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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\Model;
8
use Illuminate\Support\Facades\App;
9
10
/**
11
 * @mixin IdeHelperPayment
12
 */
13
class Payment extends Model
14
{
15
    use 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\Payment: $fakeColumns, $identifiableAttribute, $Type
Loading history...
16
17
    protected $guarded = ['id'];
18
19
    protected $appends = ['date_for_humans', 'value_with_currency'];
20
21
    /*
22
    |--------------------------------------------------------------------------
23
    | FUNCTIONS
24
    |--------------------------------------------------------------------------
25
    */
26
27
    /*
28
    |--------------------------------------------------------------------------
29
    | RELATIONS
30
    |--------------------------------------------------------------------------
31
    */
32
33
    public function invoice()
34
    {
35
        return $this->belongsTo(Invoice::class);
36
    }
37
38
    /*
39
    |--------------------------------------------------------------------------
40
    | SCOPES
41
    |--------------------------------------------------------------------------
42
    */
43
44
    /*
45
    |--------------------------------------------------------------------------
46
    | ACCESORS
47
    |--------------------------------------------------------------------------
48
    */
49
50
    public function getValueAttribute($value)
51
    {
52
        return $value / 100;
53
    }
54
55
    public function getEnrollmentNameAttribute(): string
56
    {
57
        if ($this->invoice->enrollments()->exists()) {
58
            return $this->invoice->enrollments->first()->student_name; // TODO fix this, an invoice can theoretically contain several enrollments
59
        }
60
61
        return '';
62
    }
63
64
    public function getIbanAttribute(): string
65
    {
66
        if ($this->invoice->enrollments()->exists()) {
67
            return $this->invoices->enrollments->first()->student->iban ?? '';
0 ignored issues
show
Bug introduced by
The property invoices does not exist on App\Models\Payment. Did you mean invoice?
Loading history...
68
        }
69
70
        return '';
71
    }
72
73
    public function getBicAttribute(): string
74
    {
75
        if ($this->invoice->enrollments()->exists()) {
76
            return $this->invoices->enrollments->first()->student->bic ?? '';
0 ignored issues
show
Bug introduced by
The property invoices does not exist on App\Models\Payment. Did you mean invoice?
Loading history...
77
        }
78
79
        return '';
80
    }
81
82
    public function getDateForHumansAttribute()
83
    {
84
        if ($this->date) {
85
            return Carbon::parse($this->date, 'UTC')->locale(App::getLocale())->isoFormat('LL');
86
        }
87
88
        return Carbon::parse($this->created_at, 'UTC')->locale(App::getLocale())->isoFormat('LL');
89
    }
90
91
    public function getMonthAttribute()
92
    {
93
        return Carbon::parse($this->date)->locale(App::getLocale())->isoFormat('MMMM Y');
94
    }
95
96
    public function getValueWithCurrencyAttribute()
97
    {
98
        if (config('app.currency_position') === 'before') {
99
            return config('app.currency_symbol').' '.$this->value;
100
        }
101
102
        return $this->value.' '.config('app.currency_symbol');
103
    }
104
105
    /*
106
    |--------------------------------------------------------------------------
107
    | MUTATORS
108
    |--------------------------------------------------------------------------
109
    */
110
111
    public function setValueAttribute($value)
112
    {
113
        $this->attributes['value'] = $value * 100;
114
    }
115
}
116