1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
|
9
|
|
|
class Purchase extends Eloquent\Model |
10
|
|
|
{ |
11
|
|
|
const UPCOMING = 'upcoming'; |
12
|
|
|
const DELIVERED = 'delivered'; |
13
|
|
|
const COMPLETED = 'completed'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* We do not want any timestamps. |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
public $timestamps = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The attributes that are not mass assignable. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $guarded = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Date mutators. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $dates = ['date_purchased', 'delivery_date']; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Default date format. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $dateFormat = 'Y-m-d'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Delivered Scope. |
45
|
|
|
* |
46
|
|
|
* @param $query |
47
|
|
|
* |
48
|
|
|
* @return Eloquent\Builder |
49
|
|
|
*/ |
50
|
|
|
public function scopeDelivered($query) |
51
|
|
|
{ |
52
|
|
|
return $query->where('delivery_date', '<=', Carbon::today()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Stored Scope. |
57
|
|
|
* |
58
|
|
|
* @param $query |
59
|
|
|
* |
60
|
|
|
* @return Eloquent\Builder |
61
|
|
|
*/ |
62
|
|
|
public function scopeStored($query) |
63
|
|
|
{ |
64
|
|
|
return $query->has('guitars', '=', DB::raw('quantity')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Pending Storage scope. |
69
|
|
|
* |
70
|
|
|
* @param $query |
71
|
|
|
* |
72
|
|
|
* @return Eloquent\Builder |
73
|
|
|
*/ |
74
|
|
|
public function scopePendingStorage($query) |
75
|
|
|
{ |
76
|
|
|
return $query->has('guitars', '<', DB::raw('quantity')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get status attribute. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
2 |
|
public function getStatusAttribute() |
85
|
|
|
{ |
86
|
2 |
|
if ($this->delivery_date->isFuture()) { |
|
|
|
|
87
|
2 |
|
return self::UPCOMING; |
88
|
|
|
} |
89
|
|
|
if ( ! $this->isPendingStorage()) { |
90
|
|
|
return self::COMPLETED; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return self::DELIVERED; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Has Guitars Pending Storage? |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function isPendingStorage() |
102
|
|
|
{ |
103
|
|
|
return $this->guitars()->count() < $this->quantity; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Guitars bought. |
108
|
|
|
* |
109
|
|
|
* @return Eloquent\Relations\HasMany |
110
|
|
|
*/ |
111
|
1 |
|
public function guitars() |
112
|
|
|
{ |
113
|
1 |
|
return $this->hasMany(Guitar::class); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Storage attribute. |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
1 |
|
public function getStoredAttribute() |
122
|
|
|
{ |
123
|
1 |
|
return $this->guitars()->count(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Pending Storage attribute. |
128
|
|
|
* |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
public function getPendingStorageAttribute() |
132
|
|
|
{ |
133
|
|
|
return $this->quantity - $this->stored; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Number of Guitars Sold. |
138
|
|
|
* |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
1 |
|
public function getSoldAttribute() |
142
|
|
|
{ |
143
|
1 |
|
return $this->guitars()->unsold()->count(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Whether the purchase has arrived. |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
1 |
|
public function hasArrived() |
152
|
|
|
{ |
153
|
1 |
|
return $this->isStatus(self::DELIVERED); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Verify the status. |
158
|
|
|
* |
159
|
|
|
* @param $status |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
1 |
|
public function isStatus($status) |
164
|
|
|
{ |
165
|
1 |
|
return $this->status == $status; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Supplier from which guitars were bought. |
170
|
|
|
* |
171
|
|
|
* @return Eloquent\Relations\BelongsTo |
172
|
|
|
*/ |
173
|
|
|
public function supplier() |
174
|
|
|
{ |
175
|
|
|
return $this->belongsTo(Supplier::class); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Make of the guitars. |
180
|
|
|
* |
181
|
|
|
* @return Eloquent\Relations\BelongsTo |
182
|
|
|
*/ |
183
|
1 |
|
public function make() |
184
|
|
|
{ |
185
|
1 |
|
return $this->belongsTo(Make::class); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Notifications about the purchase. |
190
|
|
|
* |
191
|
|
|
* @return Eloquent\Relations\MorphMany |
192
|
|
|
*/ |
193
|
|
|
public function notifications() |
194
|
|
|
{ |
195
|
|
|
return $this->morphMany(Notification::class, 'notifiable'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Create a new notification. |
200
|
|
|
* |
201
|
|
|
* @param Notification $notification |
202
|
|
|
* |
203
|
|
|
* @return Notification |
204
|
|
|
*/ |
205
|
|
|
public function makeNotification(Notification $notification) |
206
|
|
|
{ |
207
|
|
|
return $this->notifications()->save($notification); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
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@property
annotation 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.