|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use App\User; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use PDOException; |
|
9
|
|
|
use Scriptotek\Alma\Client as AlmaClient; |
|
|
|
|
|
|
10
|
|
|
use Scriptotek\Alma\Exception\RequestFailed; |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class SyncUsers extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The name and signature of the console command. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $signature = 'bibrex:sync-users'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The console command description. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $description = 'Fetch changes to Alma users and try to link unlinked users.'; |
|
27
|
|
|
|
|
28
|
|
|
/** @var AlmaClient */ |
|
29
|
|
|
protected $alma; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create a new command instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @param AlmaClient $alma |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(AlmaClient $alma) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct(); |
|
39
|
|
|
$this->alma = $alma; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Update a single user |
|
44
|
|
|
* |
|
45
|
|
|
* @param User $user |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function processUser(User $user) |
|
48
|
|
|
{ |
|
49
|
|
|
echo " - $user->name: "; |
|
50
|
|
|
|
|
51
|
|
|
if ($user->in_alma) { |
|
52
|
|
|
if ($user->updateFromAlma($this->alma)) { |
|
53
|
|
|
if ($user->isDirty()) { |
|
54
|
|
|
\Log::info(sprintf( |
|
55
|
|
|
'Oppdaterte brukeren <a href="%s">%s</a> fra Alma.', |
|
56
|
|
|
action('UsersController@getShow', $user->id), |
|
|
|
|
|
|
57
|
|
|
$user->name |
|
58
|
|
|
)); |
|
59
|
|
|
|
|
60
|
|
|
$this->info('oppdatert'); |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->line('ingen endringer'); |
|
63
|
|
|
} |
|
64
|
|
|
} else { |
|
65
|
|
|
\Log::warning(sprintf( |
|
66
|
|
|
'Brukeren <a href="%s">%s</a> ble ikke lenger funnet i Alma!', |
|
67
|
|
|
action('UsersController@getShow', $user->id), |
|
68
|
|
|
$user->name |
|
69
|
|
|
)); |
|
70
|
|
|
|
|
71
|
|
|
$this->warn('ikke i Alma lenger'); |
|
72
|
|
|
} |
|
73
|
|
|
} else { |
|
74
|
|
|
if ($user->updateFromAlma($this->alma)) { |
|
75
|
|
|
\Log::info(sprintf( |
|
76
|
|
|
'Lenket brukeren <a href="%s">%s</a> til en Alma-bruker.', |
|
77
|
|
|
action('UsersController@getShow', $user->id), |
|
78
|
|
|
$user->name |
|
79
|
|
|
)); |
|
80
|
|
|
|
|
81
|
|
|
$this->info('lenket til Alma-bruker'); |
|
82
|
|
|
$this->transferLoans($user); |
|
83
|
|
|
} else { |
|
84
|
|
|
$this->line('ikke i Alma'); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
try { |
|
88
|
|
|
$user->save(); |
|
89
|
|
|
} catch (PDOException $e) { |
|
90
|
|
|
$this->error('Konflikt!'); |
|
91
|
|
|
\Log::warning(sprintf( |
|
92
|
|
|
'Brukeren <a href="%s">%s</a> kunne ikke lagres på grunn av en konflikt - to brukere har ' . |
|
93
|
|
|
'samme strekkode eller feide-id. Sjekk i brukerlista om det er to brukere som kan slås sammen.', |
|
94
|
|
|
action('UsersController@getShow', $user->id), |
|
95
|
|
|
$user->name |
|
96
|
|
|
)); |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($user->in_alma) { |
|
101
|
|
|
// Check if user have loans of thing_id 1 and transfer them if so. |
|
102
|
|
|
// In case the user was manually synced during the day. |
|
103
|
|
|
$tempLoans = $user->loans()->whereHas('item', function ($query) { |
|
104
|
|
|
$query->where('thing_id', 1); |
|
105
|
|
|
})->count(); |
|
106
|
|
|
|
|
107
|
|
|
if ($tempLoans) { |
|
108
|
|
|
$this->transferLoans($user); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Execute the console command. |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function handle() |
|
119
|
|
|
{ |
|
120
|
|
|
foreach (User::get() as $user) { |
|
121
|
|
|
$this->processUser($user); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function transferLoans(User $localUser) |
|
126
|
|
|
{ |
|
127
|
|
|
$almaUser = $this->alma->users[$localUser->alma_primary_id]; |
|
128
|
|
|
|
|
129
|
|
|
if (is_null($almaUser)) { |
|
130
|
|
|
\Log::error("Kunne ikke overføre lån fordi Alma-brukeren ikke ble funnet. Meget uventet."); |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$n = 0; |
|
135
|
|
|
|
|
136
|
|
|
foreach ($localUser->loans as $loan) { |
|
137
|
|
|
if ($loan->item->thing_id == 1) { |
|
138
|
|
|
// Loan should be transferred from the temporary card to the user |
|
139
|
|
|
|
|
140
|
|
|
$barcode = $loan->item->barcode; |
|
141
|
|
|
$library = $loan->library; |
|
142
|
|
|
|
|
143
|
|
|
$errBecause = "Kunne ikke overføre lån av $barcode i Alma fordi"; |
|
144
|
|
|
|
|
145
|
|
|
if (is_null($library->temporary_barcode)) { |
|
146
|
|
|
\Log::error("$errBecause biblioteket ikke lenger har et midlertidig lånekort."); |
|
147
|
|
|
continue; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$tempUser = $this->alma->users[$library->temporary_barcode]; |
|
151
|
|
|
|
|
152
|
|
|
if (is_null($tempUser)) { |
|
153
|
|
|
\Log::error("$errBecause brukeren '{$library->temporary_barcode}' ikke ble funnet i Alma."); |
|
154
|
|
|
continue; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$almaItem = $this->alma->items->fromBarcode($barcode); |
|
158
|
|
|
$almaLoan = $almaItem->loan; |
|
159
|
|
|
|
|
160
|
|
|
if (is_null($almaLoan)) { |
|
161
|
|
|
\Log::warning("$errBecause dokumentet i mellomtiden har blitt returnert i Alma."); |
|
162
|
|
|
continue; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if ($almaLoan->user_id != $library->temporary_barcode) { |
|
166
|
|
|
\Log::warning("$errBecause dokumentet ikke lenger er utlånt til {$library->temporary_barcode}."); |
|
167
|
|
|
continue; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if (count($almaItem->requests)) { |
|
171
|
|
|
\Log::warning("$errBecause dokumentet har reserveringer."); |
|
172
|
|
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// Cross fingers |
|
176
|
|
|
try { |
|
177
|
|
|
$almaLoan->scanIn($library); |
|
178
|
|
|
$almaItem->checkOut($almaUser, $library); |
|
179
|
|
|
} catch (RequestFailed $e) { |
|
180
|
|
|
\Log::warning($errBecause . ' ' . $e->getMessage()); |
|
181
|
|
|
continue; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
\Log::info(sprintf( |
|
185
|
|
|
'Overførte lån av <a href="%s">%s</a> til Alma-brukeren.', |
|
186
|
|
|
action('ItemsController@show', $loan->item->id), |
|
|
|
|
|
|
187
|
|
|
$barcode |
|
188
|
|
|
)); |
|
189
|
|
|
|
|
190
|
|
|
// Checkin local loan and delete temporary item |
|
191
|
|
|
$loan->checkIn(); |
|
192
|
|
|
|
|
193
|
|
|
$n++; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
$this->info(" > Overførte $n lån"); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths