Completed
Branch feature/coupons (8ab0b9)
by Adam
13:26
created

Payment::coupon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Ramsey\Uuid;
7
8
/**
9
 * @property string $id
10
 * @property int $job_id
11
 * @property int $plan_id
12
 * @property int $status_id
13
 * @property int $days
14
 * @property int $invoice_id
15
 * @property \Carbon\Carbon $starts_at
16
 * @property \Carbon\Carbon $ends_at
17
 * @property Job $job
18
 * @property Plan $plan
19
 * @property Invoice $invoice
20
 * @property int $coupon_id
21
 * @property Coupon $coupon
22
 */
23
class Payment extends Model
24
{
25
    const NEW = 1;
26
    const PENDING = 2;
27
    const PAID = 3;
28
29
    /**
30
     * @var bool
31
     */
32
    public $incrementing = false;
33
34
    /**
35
     * @var array
36
     */
37
    protected $fillable = ['plan_id', 'status_id', 'days', 'starts_at', 'ends_at'];
38
39
    /**
40
     * @var array
41
     */
42
    protected $dates = ['created_at', 'updated_at', 'starts_at', 'ends_at'];
43
44
    /**
45
     * @var string
46
     */
47
    protected $dateFormat = 'Y-m-d H:i:se';
48
49
    /**
50
     * @var array
51
     */
52
    protected $attributes = ['status_id' => self::NEW];
53
54
    public static function boot()
55
    {
56
        parent::boot();
57
58
        static::creating(function (Payment $payment) {
59
            $payment->id = Uuid\Uuid::uuid4();
60
        });
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public static function getPaymentStatusesList()
67
    {
68
        return [Payment::NEW => 'Nowy', Payment::PENDING => 'W trakcie realizacji', Payment::PAID => 'Zapłacono'];
69
    }
70
71
    /**
72
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
73
     */
74
    public function job()
75
    {
76
        return $this->belongsTo(Job::class)->withTrashed();
77
    }
78
79
    /**
80
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
81
     */
82
    public function plan()
83
    {
84
        return $this->belongsTo(Plan::class);
85
    }
86
87
    /**
88
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
89
     */
90
    public function invoice()
91
    {
92
        return $this->belongsTo(Invoice::class);
93
    }
94
95
    /**
96
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
97
     */
98
    public function coupon()
99
    {
100
        return $this->belongsTo(Coupon::class);
101
    }
102
103
    /**
104
     * @return float
105
     */
106
    public function netPrice()
107
    {
108
        return $this->plan->price * $this->days;
109
    }
110
111
    /**
112
     * @return float
113
     */
114
    public function grossPrice()
115
    {
116
        return $this->netPrice() * $this->plan->vat_rate;
117
    }
118
119
    /**
120
     * @return float
121
     */
122
    public function vat()
123
    {
124
        return $this->grossPrice() - $this->netPrice();
125
    }
126
127
    /**
128
     * @return float
129
     */
130
    public function getNetPriceAttribute()
131
    {
132
        return $this->netPrice();
133
    }
134
135
    /**
136
     * @return float
137
     */
138
    public function getGrossPriceAttribute()
139
    {
140
        return $this->grossPrice();
141
    }
142
143
    /**
144
     * @return float
145
     */
146
    public function getVatAttribute()
147
    {
148
        return $this->vat();
149
    }
150
}
151