Completed
Push — master ( 1e5dce...700420 )
by Scott
03:02
created

Discount::beforeSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Bedard\Shop\Models;
2
3
use Flash;
4
use Lang;
5
use Model;
6
use October\Rain\Database\ModelException;
7
8
/**
9
 * Discount Model.
10
 */
11
class Discount extends Model
12
{
13
    use \October\Rain\Database\Traits\Purgeable,
14
        \October\Rain\Database\Traits\Validation;
15
16
    /**
17
     * @var string The database table used by the model.
18
     */
19
    public $table = 'bedard_shop_discounts';
20
21
    /**
22
     * @var array Default attributes
23
     */
24
    public $attributes = [
25
        'is_percentage' => true,
26
    ];
27
28
    /**
29
     * @var array Attribute casting
30
     */
31
    protected $casts = [
32
        //
33
    ];
34
35
    /**
36
     * @var array Date casting
37
     */
38
    protected $dates = [
39
        'end_at',
40
        'start_at',
41
    ];
42
43
    /**
44
     * @var array Guarded fields
45
     */
46
    protected $guarded = ['*'];
47
48
    /**
49
     * @var array Fillable fields
50
     */
51
    protected $fillable = [
52
        'amount',
53
        'amount_exact',
54
        'amount_percentage',
55
        'end_at',
56
        'is_percentage',
57
        'name',
58
        'start_at',
59
    ];
60
61
    /**
62
     * @var array Purgeable vields
63
     */
64
    protected $purgeable = [
65
        'amount_exact',
66
        'amount_percentage',
67
    ];
68
69
    /**
70
     * @var array Relations
71
     */
72
    public $hasMany = [];
73
    public $morphMany = [];
74
75
    /**
76
     * @var  array Validation rules
77
     */
78
    public $rules = [
79
        'end_at' => 'date',
80
        'name' => 'required',
81
        'start_at' => 'date',
82
        'amount_exact' => 'numeric|min:0',
83
        'amount_percentage' => 'integer|min:0',
84
    ];
85
86
    /**
87
     * After validate.
88
     *
89
     * @return void
90
     */
91
    public function afterValidate()
92
    {
93
        $this->validateDates();
94
    }
95
96
    /**
97
     * Before save.
98
     *
99
     * @return void
100
     */
101
    public function beforeSave()
102
    {
103
        $this->setAmount();
104
    }
105
106
    /**
107
     * Filter form fields.
108
     *
109
     * @param  object   $fields
110
     * @return void
111
     */
112
    public function filterFields($fields)
113
    {
114
        $fields->amount_exact->hidden = $this->is_percentage;
115
        $fields->amount_percentage->hidden = ! $this->is_percentage;
116
    }
117
118
    public function setAmount()
119
    {
120
        $exact = $this->getOriginalPurgeValue('amount_exact');
121
        $percentage = $this->getOriginalPurgeValue('amount_percentage');
122
123
        $this->amount = $this->is_percentage
124
            ? $percentage
125
            : $exact;
126
    }
127
128
    /**
129
     * Ensure the start and end dates are valid.
130
     *
131
     * @return void
132
     */
133
    public function validateDates()
134
    {
135
        // Start date must be after the end date
136
        if ($this->start_at !== null &&
137
            $this->end_at !== null &&
138
            $this->start_at >= $this->end_at) {
139
            Flash::error(Lang::get('bedard.shop::lang.discounts.form.start_at_invalid'));
140
            throw new ModelException($this);
141
        }
142
    }
143
}
144