|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use App\Item; |
|
6
|
|
|
use App\Loan; |
|
7
|
|
|
use App\Rules\LoanExists; |
|
8
|
|
|
use App\Rules\NeverLoanedOut; |
|
9
|
|
|
use App\Rules\ThingExists; |
|
10
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
11
|
|
|
use Scriptotek\Alma\Client as AlmaClient; |
|
12
|
|
|
|
|
13
|
|
|
class CheckinRequest extends FormRequest |
|
14
|
|
|
{ |
|
15
|
|
|
public $item; |
|
16
|
|
|
public $user; |
|
17
|
|
|
public $localUser; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Determine if the user is authorized to make this request. |
|
21
|
|
|
* |
|
22
|
|
|
* @return bool |
|
23
|
|
|
*/ |
|
24
|
|
|
public function authorize() |
|
25
|
|
|
{ |
|
26
|
|
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the validation rules that apply to the request. |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function rules() |
|
35
|
|
|
{ |
|
36
|
|
|
$library = \Auth::user(); |
|
37
|
|
|
$alma = app(AlmaClient::class); |
|
38
|
|
|
|
|
39
|
|
|
// 1. If we're given a loan id, look for that. |
|
40
|
|
|
|
|
41
|
|
|
if ($this->input('loan')) { |
|
42
|
|
|
$loan = Loan::with(['item', 'item.thing', 'user']) |
|
43
|
|
|
->find($this->input('loan')); |
|
44
|
|
|
if (is_null($loan)) { |
|
45
|
|
|
// Bail out |
|
46
|
|
|
return [ |
|
47
|
|
|
'loan' => [new LoanExists()], |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
// Success, we have located an active local loan! |
|
51
|
|
|
$this->loan = $loan; |
|
|
|
|
|
|
52
|
|
|
return []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// 2. Otherwise, start by looking up the item by barcode, locally or in Alma. |
|
56
|
|
|
|
|
57
|
|
|
$barcode = $this->input('barcode'); |
|
58
|
|
|
|
|
59
|
|
|
if (is_null($barcode)) { |
|
60
|
|
|
// Bail out, but no need for error, controller will take care of this case. |
|
61
|
|
|
return []; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$item = Item::withTrashed()->where('barcode', '=', $barcode)->first(); |
|
65
|
|
|
|
|
66
|
|
|
if (is_null($item)) { |
|
67
|
|
|
// Item doesn't exist locally, but perhaps in Alma? |
|
68
|
|
|
if (empty($library->library_code) || is_null($alma->key)) { |
|
|
|
|
|
|
69
|
|
|
// Alma integration is not configured. Bail out. |
|
70
|
|
|
return [ |
|
71
|
|
|
'barcode' => [new ThingExists()], |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$almaItem = $alma->items->fromBarcode($barcode); |
|
76
|
|
|
if (is_null($almaItem)) { |
|
77
|
|
|
// Nope, not in Alma either. Bail out. |
|
78
|
|
|
return [ |
|
79
|
|
|
'barcode' => [new ThingExists()], |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
// Success, we have located an item in Alma that does not exist locally. |
|
83
|
|
|
$this->almaItem = $almaItem; |
|
|
|
|
|
|
84
|
|
|
return []; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// 3. At this point we have a local item (deleted or not). Let's see if it's on loan, or have been. |
|
88
|
|
|
|
|
89
|
|
|
$lastLoan = $item->loans()->withTrashed()->first(); |
|
90
|
|
|
|
|
91
|
|
|
if (!is_null($lastLoan)) { |
|
92
|
|
|
$this->loan = $lastLoan; // This will be the newest one |
|
93
|
|
|
return []; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// 4. The item exists, but has never been loaned out. |
|
97
|
|
|
return [ |
|
98
|
|
|
'barcode' => [new NeverLoanedOut($item)], |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|