|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* App\Rack. |
|
9
|
|
|
* |
|
10
|
|
|
* @property int $id |
|
11
|
|
|
* @property int $warehouse_id |
|
12
|
|
|
* @property string $name |
|
13
|
|
|
* @property int $make_id |
|
14
|
|
|
* @property int $capacity |
|
15
|
|
|
* @property \Carbon\Carbon $deleted_at |
|
16
|
|
|
* @property-read \App\Warehouse $warehouse |
|
17
|
|
|
* @property-read mixed $used |
|
18
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Guitar[] $guitars |
|
19
|
|
|
* @property-read mixed $usage |
|
20
|
|
|
* @property-read \App\Make $make |
|
21
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Notification[] $notifications |
|
22
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Rack whereId($value) |
|
23
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Rack whereWarehouseId($value) |
|
24
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Rack whereName($value) |
|
25
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Rack whereMakeId($value) |
|
26
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Rack whereCapacity($value) |
|
27
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Rack whereDeletedAt($value) |
|
28
|
|
|
* @mixin \Eloquent |
|
29
|
|
|
*/ |
|
30
|
|
|
class Rack extends Eloquent\Model |
|
31
|
|
|
{ |
|
32
|
|
|
use Eloquent\SoftDeletes; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* We do not want any timestamps. |
|
36
|
|
|
* |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public $timestamps = false; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $dates = ['deleted_at']; |
|
44
|
|
|
/** |
|
45
|
|
|
* The attributes that are not mass assignable. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $fillable = ['name', 'capacity', 'make_id', 'warehouse_id']; |
|
50
|
|
|
|
|
51
|
10 |
|
public static function boot() |
|
52
|
|
|
{ |
|
53
|
10 |
|
parent::boot(); |
|
54
|
|
|
|
|
55
|
10 |
|
static::deleting(function (Rack $rack) { |
|
56
|
2 |
|
$rack->guitars()->delete(); |
|
57
|
10 |
|
}); |
|
58
|
10 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Warehouse the rack is in. |
|
62
|
|
|
* |
|
63
|
|
|
* @return Eloquent\Relations\BelongsTo |
|
64
|
|
|
*/ |
|
65
|
|
|
public function warehouse() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->belongsTo(Warehouse::class); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Number of used rack space. |
|
72
|
|
|
* |
|
73
|
|
|
* @return int |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getUsedAttribute() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->guitars()->unsold()->count(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Guitars in the rack. |
|
82
|
|
|
* |
|
83
|
|
|
* @return Eloquent\Relations\HasMany |
|
84
|
|
|
*/ |
|
85
|
2 |
|
public function guitars() |
|
86
|
|
|
{ |
|
87
|
2 |
|
return $this->hasMany(Guitar::class); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Usage of the rack. |
|
92
|
|
|
* |
|
93
|
|
|
* @return float |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getUsageAttribute() |
|
96
|
|
|
{ |
|
97
|
|
|
return ($this->used / $this->capacity) * 100; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Is the rack full? |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function isFull() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->used >= $this->capacity; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Guitar make this rack can hold. |
|
112
|
|
|
* |
|
113
|
|
|
* @return Eloquent\Relations\BelongsTo |
|
114
|
|
|
*/ |
|
115
|
|
|
public function make() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->belongsTo(Make::class); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|