|
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
|
|
|
public function guitars() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->hasMany(Guitar::class); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Pending Storage attribute. |
|
118
|
|
|
* |
|
119
|
|
|
* @return int |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getPendingStorageAttribute() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->quantity - $this->guitars()->count(); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Whether the purchase has arrived. |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function hasArrived() |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->isStatus(self::DELIVERED); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Verify the status. |
|
138
|
|
|
* |
|
139
|
|
|
* @param $status |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function isStatus($status) |
|
144
|
|
|
{ |
|
145
|
1 |
|
return $this->status == $status; |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Supplier from which guitars were bought. |
|
150
|
|
|
* |
|
151
|
|
|
* @return Eloquent\Relations\BelongsTo |
|
152
|
|
|
*/ |
|
153
|
|
|
public function supplier() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->belongsTo(Supplier::class); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Make of the guitars. |
|
160
|
|
|
* |
|
161
|
|
|
* @return Eloquent\Relations\BelongsTo |
|
162
|
|
|
*/ |
|
163
|
1 |
|
public function make() |
|
164
|
|
|
{ |
|
165
|
1 |
|
return $this->belongsTo(Make::class); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Notifications about the purchase. |
|
170
|
|
|
* |
|
171
|
|
|
* @return Eloquent\Relations\MorphMany |
|
172
|
|
|
*/ |
|
173
|
|
|
public function notifications() |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->morphMany(Notification::class, 'notifiable'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Create a new notification. |
|
180
|
|
|
* |
|
181
|
|
|
* @param Notification $notification |
|
182
|
|
|
* |
|
183
|
|
|
* @return Notification |
|
184
|
|
|
*/ |
|
185
|
|
|
public function makeNotification(Notification $notification) |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->notifications()->save($notification); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
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.