|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of ibrand/discount. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) iBrand <https://www.ibrand.cc> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace iBrand\Component\Discount\Models; |
|
13
|
|
|
|
|
14
|
|
|
use iBrand\Component\Discount\Contracts\DiscountContract; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
16
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
17
|
|
|
|
|
18
|
|
|
class Discount extends Model implements DiscountContract |
|
19
|
|
|
{ |
|
20
|
|
|
use SoftDeletes; |
|
21
|
|
|
|
|
22
|
|
|
protected $guarded = ['id', 'orderAmountLimit']; |
|
23
|
|
|
|
|
24
|
|
|
protected $appends = ['use_start_time', 'use_end_time', 'action_type']; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Address constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $attributes |
|
30
|
|
|
*/ |
|
31
|
14 |
|
public function __construct(array $attributes = []) |
|
32
|
|
|
{ |
|
33
|
14 |
|
$this->setTable(config('ibrand.app.database.prefix', 'ibrand_').'discount'); |
|
34
|
|
|
|
|
35
|
14 |
|
parent::__construct($attributes); |
|
36
|
14 |
|
} |
|
37
|
|
|
|
|
38
|
14 |
|
public function rules() |
|
39
|
|
|
{ |
|
40
|
14 |
|
return $this->hasMany(Rule::class); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
14 |
|
public function actions() |
|
44
|
|
|
{ |
|
45
|
14 |
|
return $this->hasMany(Action::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function hasRules() |
|
52
|
|
|
{ |
|
53
|
4 |
|
return 0 !== $this->rules()->count(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function isCouponBased() |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->coupon_based; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
public function getActions() |
|
62
|
|
|
{ |
|
63
|
3 |
|
return $this->actions; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
public function getRules() |
|
67
|
|
|
{ |
|
68
|
4 |
|
return $this->rules; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function setCouponUsed() |
|
72
|
|
|
{ |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function getUsestartAtAttribute($value) |
|
76
|
|
|
{ |
|
77
|
2 |
|
if (empty($value)) { |
|
78
|
2 |
|
return $this->starts_at; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
return $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
public function getUseendAtAttribute($value) |
|
85
|
|
|
{ |
|
86
|
2 |
|
if (empty($value)) { |
|
87
|
1 |
|
return $this->ends_at; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $value; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
public function getStartsAt() |
|
94
|
|
|
{ |
|
95
|
4 |
|
return $this->starts_at; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
public function getEndsAt() |
|
99
|
|
|
{ |
|
100
|
4 |
|
return $this->ends_at; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
4 |
|
public function getUsed() |
|
107
|
|
|
{ |
|
108
|
4 |
|
return $this->used; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
4 |
|
public function getUsageLimit() |
|
115
|
|
|
{ |
|
116
|
4 |
|
return $this->usage_limit; |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
public function getUseStartTimeAttribute() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
if (!$this->attributes['usestart_at']) { |
|
122
|
|
|
$time = $this->starts_at; |
|
|
|
|
|
|
123
|
|
|
} else { |
|
124
|
|
|
$time = $this->attributes['usestart_at']; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return date('Y-m-d', strtotime($time)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
View Code Duplication |
public function getUseEndTimeAttribute() |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
if (!$this->attributes['useend_at']) { |
|
133
|
|
|
$time = $this->ends_at; |
|
|
|
|
|
|
134
|
|
|
} else { |
|
135
|
|
|
$time = $this->attributes['useend_at']; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return date('Y-m-d', strtotime($time)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* 为方便前台展示优惠券信息. |
|
143
|
|
|
* |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getActionTypeAttribute() |
|
147
|
|
|
{ |
|
148
|
|
|
$action = $this->actions()->first(); |
|
149
|
|
|
|
|
150
|
|
|
$type = []; |
|
151
|
|
|
|
|
152
|
|
View Code Duplication |
if(str_contains($action->type,'fixed')){ |
|
|
|
|
|
|
153
|
|
|
$type['type'] = 'cash'; |
|
154
|
|
|
$type['value'] = json_decode($action->configuration, true)['amount'] / 100; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
View Code Duplication |
if(str_contains($action->type,'percentage')){ |
|
|
|
|
|
|
158
|
|
|
$type['type'] = 'percentage'; |
|
159
|
|
|
$type['value'] = json_decode($action->configuration, true)['percentage'] / 10; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $type; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.