1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use App\Events\NotificationWasCreated; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Database\Eloquent; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* App\Purchase |
12
|
|
|
* |
13
|
|
|
* @property integer $id |
14
|
|
|
* @property integer $supplier_id |
15
|
|
|
* @property integer $make_id |
16
|
|
|
* @property integer $quantity |
17
|
|
|
* @property float $price |
18
|
|
|
* @property \Carbon\Carbon $date_purchased |
19
|
|
|
* @property \Carbon\Carbon $delivery_date |
20
|
|
|
* @property \Carbon\Carbon $deleted_at |
21
|
|
|
* @property-read mixed $status |
22
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Guitar[] $guitars |
23
|
|
|
* @property-read mixed $stored |
24
|
|
|
* @property-read mixed $pending_storage |
25
|
|
|
* @property-read mixed $sold |
26
|
|
|
* @property-read \App\Supplier $supplier |
27
|
|
|
* @property-read \App\Make $make |
28
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Notification[] $notifications |
29
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase whereId($value) |
30
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase whereSupplierId($value) |
31
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase whereMakeId($value) |
32
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase whereQuantity($value) |
33
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase wherePrice($value) |
34
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase whereDatePurchased($value) |
35
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase whereDeliveryDate($value) |
36
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase whereDeletedAt($value) |
37
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase delivered() |
38
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase stored() |
39
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Purchase pendingStorage() |
40
|
|
|
* @mixin \Eloquent |
41
|
|
|
*/ |
42
|
|
|
class Purchase extends Eloquent\Model |
43
|
|
|
{ |
44
|
|
|
const UPCOMING = 'upcoming'; |
45
|
|
|
const DELIVERED = 'delivered'; |
46
|
|
|
const COMPLETED = 'completed'; |
47
|
|
|
|
48
|
|
|
use Eloquent\SoftDeletes; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* We do not want any timestamps. |
52
|
|
|
* |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
public $timestamps = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The attributes that are not mass assignable. |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $guarded = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Date mutators. |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $dates = ['date_purchased', 'delivery_date', 'deleted_at']; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Default date format. |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected $dateFormat = 'Y-m-d'; |
77
|
|
|
|
78
|
8 |
|
public static function boot() |
79
|
|
|
{ |
80
|
8 |
|
parent::boot(); |
81
|
|
|
|
82
|
8 |
|
static::deleting(function (Purchase $purchase) { |
83
|
|
|
$purchase->guitars()->delete(); |
84
|
|
|
$purchase->notifications()->delete(); |
85
|
8 |
|
}); |
86
|
8 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Delivered Scope. |
90
|
|
|
* |
91
|
|
|
* @param $query |
92
|
|
|
* |
93
|
|
|
* @return Eloquent\Builder |
94
|
|
|
*/ |
95
|
|
|
public function scopeDelivered($query) |
96
|
|
|
{ |
97
|
|
|
return $query->where('delivery_date', '<=', Carbon::today()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Stored Scope. |
102
|
|
|
* |
103
|
|
|
* @param $query |
104
|
|
|
* |
105
|
|
|
* @return Eloquent\Builder |
106
|
|
|
*/ |
107
|
|
|
public function scopeStored($query) |
108
|
|
|
{ |
109
|
|
|
return $query->has('guitars', '=', DB::raw('quantity')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Pending Storage scope. |
114
|
|
|
* |
115
|
|
|
* @param $query |
116
|
|
|
* |
117
|
|
|
* @return Eloquent\Builder |
118
|
|
|
*/ |
119
|
|
|
public function scopePendingStorage($query) |
120
|
|
|
{ |
121
|
|
|
return $query->has('guitars', '<', DB::raw('quantity')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get status attribute. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getStatusAttribute() |
130
|
|
|
{ |
131
|
|
|
if ($this->delivery_date->isFuture()) { |
132
|
|
|
return self::UPCOMING; |
133
|
|
|
} |
134
|
|
|
if ( ! $this->isPendingStorage()) { |
135
|
|
|
return self::COMPLETED; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return self::DELIVERED; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Has Guitars Pending Storage? |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isPendingStorage() |
147
|
|
|
{ |
148
|
|
|
return $this->guitars()->count() < $this->quantity; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Guitars bought. |
153
|
|
|
* |
154
|
|
|
* @return Eloquent\Relations\HasMany |
155
|
|
|
*/ |
156
|
|
|
public function guitars() |
157
|
|
|
{ |
158
|
|
|
return $this->hasMany(Guitar::class); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Storage attribute. |
163
|
|
|
* |
164
|
|
|
* @return int |
165
|
|
|
*/ |
166
|
|
|
public function getStoredAttribute() |
167
|
|
|
{ |
168
|
|
|
return $this->guitars()->count(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Pending Storage attribute. |
173
|
|
|
* |
174
|
|
|
* @return int |
175
|
|
|
*/ |
176
|
|
|
public function getPendingStorageAttribute() |
177
|
|
|
{ |
178
|
|
|
return $this->quantity - $this->stored; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Number of Guitars Sold. |
183
|
|
|
* |
184
|
|
|
* @return int |
185
|
|
|
*/ |
186
|
|
|
public function getSoldAttribute() |
187
|
|
|
{ |
188
|
|
|
return $this->guitars()->sold()->count(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Whether the purchase has arrived. |
193
|
|
|
* |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function hasArrived() |
197
|
|
|
{ |
198
|
|
|
return $this->isStatus(self::DELIVERED); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Verify the status. |
203
|
|
|
* |
204
|
|
|
* @param $status |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function isStatus($status) |
209
|
|
|
{ |
210
|
|
|
return $this->status == $status; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Supplier from which guitars were bought. |
215
|
|
|
* |
216
|
|
|
* @return Eloquent\Relations\BelongsTo |
217
|
|
|
*/ |
218
|
|
|
public function supplier() |
219
|
|
|
{ |
220
|
|
|
return $this->belongsTo(Supplier::class); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Make of the guitars. |
225
|
|
|
* |
226
|
|
|
* @return Eloquent\Relations\BelongsTo |
227
|
|
|
*/ |
228
|
|
|
public function make() |
229
|
|
|
{ |
230
|
|
|
return $this->belongsTo(Make::class); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Notifications about the purchase. |
235
|
|
|
* |
236
|
|
|
* @return Eloquent\Relations\MorphMany |
237
|
|
|
*/ |
238
|
|
|
public function notifications() |
239
|
|
|
{ |
240
|
|
|
return $this->morphMany(Notification::class, 'notifiable'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Create a new notification. |
245
|
|
|
* @return Notification |
246
|
|
|
*/ |
247
|
|
|
public function makeNotification() |
248
|
|
|
{ |
249
|
|
|
$notification = $this->notifications()->create([ |
250
|
|
|
'icon' => 'flight_land', |
251
|
|
|
'link' => '/purchases/' . $this->id, |
252
|
|
|
'message' => "Purchase of {$this->quantity} {$this->make->name} has arrived from {$this->supplier->name}, {$this->supplier->location}.", |
253
|
|
|
]); |
254
|
|
|
|
255
|
|
|
event(new NotificationWasCreated($notification)); |
|
|
|
|
256
|
|
|
|
257
|
|
|
return $notification; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.