Passed
Push — master ( 952446...f0a3f9 )
by Thomas
07:48 queued 11s
created

Payment::paymentmethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
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 2
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
    public function paymentmethod() {
39
        return $this->belongsTo(Paymentmethod::class, 'payment_method', 'code')->withDefault();
40
    }
41
42
    /*
43
    |--------------------------------------------------------------------------
44
    | SCOPES
45
    |--------------------------------------------------------------------------
46
    */
47
48
    /*
49
    |--------------------------------------------------------------------------
50
    | ACCESORS
51
    |--------------------------------------------------------------------------
52
    */
53
54
    public function getValueAttribute($value)
55
    {
56
        return $value / 100;
57
    }
58
59
    public function getEnrollmentNameAttribute(): string
60
    {
61
        if ($this->invoice->enrollments()->exists()) {
62
            return $this->invoice->enrollments->first()->student_name; // TODO fix this, an invoice can theoretically contain several enrollments
63
        }
64
65
        return '';
66
    }
67
68
    public function getIbanAttribute(): string
69
    {
70
        if ($this->invoice->enrollments()->exists()) {
71
            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...
72
        }
73
74
        return '';
75
    }
76
77
    public function getBicAttribute(): string
78
    {
79
        if ($this->invoice->enrollments()->exists()) {
80
            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...
81
        }
82
83
        return '';
84
    }
85
86
    public function getDateForHumansAttribute()
87
    {
88
        if ($this->date) {
89
            return Carbon::parse($this->date, 'UTC')->locale(App::getLocale())->isoFormat('LL');
90
        }
91
92
        return Carbon::parse($this->created_at, 'UTC')->locale(App::getLocale())->isoFormat('LL');
93
    }
94
95
    public function getMonthAttribute()
96
    {
97
        return Carbon::parse($this->date)->locale(App::getLocale())->isoFormat('MMMM Y');
98
    }
99
100
    public function getValueWithCurrencyAttribute()
101
    {
102
        if (config('app.currency_position') === 'before') {
103
            return config('app.currency_symbol').' '.$this->value;
104
        }
105
106
        return $this->value.' '.config('app.currency_symbol');
107
    }
108
109
    /*
110
    |--------------------------------------------------------------------------
111
    | MUTATORS
112
    |--------------------------------------------------------------------------
113
    */
114
115
    public function setValueAttribute($value)
116
    {
117
        $this->attributes['value'] = $value * 100;
118
    }
119
}
120