Passed
Push — master ( 2db616...058151 )
by Thomas
07:55 queued 12s
created

ScheduledPayment   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 19
c 2
b 0
f 1
dl 0
loc 102
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A enrollment() 0 3 1
A getDateForHumansAttribute() 0 7 2
A invoices() 0 3 1
A getValueAttribute() 0 3 1
A getValueWithCurrencyAttribute() 0 8 2
A getComputedStatusAttribute() 0 10 3
A identifiableAttribute() 0 3 1
A setValueAttribute() 0 3 1
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
    /*
79
    |--------------------------------------------------------------------------
80
    | SCOPES
81
    |--------------------------------------------------------------------------
82
    */
83
84
    /*
85
    |--------------------------------------------------------------------------
86
    | ACCESSORS
87
    |--------------------------------------------------------------------------
88
    */
89
90
    public function getValueAttribute($value)
91
    {
92
        return $value / 100;
93
    }
94
95
    public function getValueWithCurrencyAttribute()
96
    {
97
        if (config('app.currency_position') === 'before')
98
        {
99
            return config('app.currency_symbol') . " ". $this->value;
100
        }
101
102
        return $this->value . " " . config('app.currency_symbol');
103
    }
104
105
    function getDateForHumansAttribute()
106
    {
107
        if ($this->date)
108
        {
109
            return Carbon::parse($this->date, 'UTC')->locale(App::getLocale())->isoFormat('LL');
110
        }
111
        return Carbon::parse($this->created_at, 'UTC')->locale(App::getLocale())->isoFormat('LL');
112
    }
113
114
    public function getComputedStatusAttribute()
115
    {
116
        // if there is a custom status, always take it
117
        if ($this->status)
118
        {
119
            return $this->status;
120
        }
121
122
        // otherwise, check if the scheduled payment has invoices
123
        return $this->invoices->count() > 0 ? 2 : 1;
124
    }
125
126
    public function identifiableAttribute()
127
    {
128
        return $this->date . " (" . $this->value_with_currency . ")";
129
    }
130
131
    /*
132
    |--------------------------------------------------------------------------
133
    | MUTATORS
134
    |--------------------------------------------------------------------------
135
    */
136
137
    public function setValueAttribute($value)
138
    {
139
        $this->attributes['value'] = $value * 100;
140
    }
141
}
142