|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
8
|
|
|
use function Stringy\create as s; |
|
9
|
|
|
|
|
10
|
|
|
class Item extends Model |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
use SoftDeletes; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The attributes that should be mutated to dates. |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $dates = ['deleted_at']; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The attributes that are mass assignable. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $fillable = ['barcode', 'library_id', 'note', 'thing_id']; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The accessors to append to the model's array form. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $appends = ['last_loan', 'available']; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The attributes that should be cast to native types. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $casts = [ |
|
42
|
|
|
'properties' => 'array', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Prepare a date for array / JSON serialization. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \DateTimeInterface $date |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function serializeDate(DateTimeInterface $date) |
|
52
|
|
|
{ |
|
53
|
|
|
return $date->format('Y-m-d H:i:s'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function thing() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->belongsTo(Thing::class, 'thing_id'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function library() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->belongsTo(Library::class, 'library_id'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the loans for this items. By default, only active loans are returned, |
|
68
|
|
|
* so the result should be zero or one loan. But all active and former loans can |
|
69
|
|
|
* be returned by adding `withTrashed()`. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function loans() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->hasMany(Loan::class) |
|
74
|
|
|
->with('user') |
|
75
|
|
|
->latest(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the last loan, active or not. |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getLastLoanAttribute() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->attributes['last_loan'] = $this->loans() |
|
86
|
|
|
->withTrashed() |
|
87
|
|
|
->orderBy('created_at', 'desc') |
|
88
|
|
|
->first(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Whether item is available or not. |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getAvailableAttribute() |
|
97
|
|
|
{ |
|
98
|
|
|
return !count($this->loans); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function allLoans() |
|
102
|
|
|
{ |
|
103
|
|
|
$library_id = auth()->user()->id; |
|
104
|
|
|
|
|
105
|
|
|
return $this->hasMany(Loan::class) |
|
106
|
|
|
->with('user') |
|
107
|
|
|
->withTrashed() |
|
108
|
|
|
->where('library_id', $library_id) |
|
109
|
|
|
->orderBy('created_at', 'desc'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function lost() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->is_lost = true; |
|
115
|
|
|
$this->save(); |
|
116
|
|
|
$this->delete(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function found() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->restore(); |
|
122
|
|
|
|
|
123
|
|
|
if ($this->is_lost) { |
|
124
|
|
|
\Log::info(sprintf( |
|
125
|
|
|
'Registrerte %s som funnet.', |
|
126
|
|
|
$this->formattedLink(false, false) |
|
127
|
|
|
), ['library' => \Auth::user()->name]); |
|
128
|
|
|
$this->is_lost = false; |
|
129
|
|
|
$this->save(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function formattedLink($ucfirst = false, $definite = true) |
|
134
|
|
|
{ |
|
135
|
|
|
$name = s($this->thing->properties->get($definite ? 'name_definite.nob' : 'name_indefinite.nob')); |
|
136
|
|
|
$name = $ucfirst ? $name->upperCaseFirst() : $name->lowerCaseFirst(); |
|
137
|
|
|
|
|
138
|
|
|
return sprintf( |
|
139
|
|
|
'<a href="%s">%s</a>', |
|
140
|
|
|
action('ItemsController@show', $this->id), |
|
141
|
|
|
$name |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|