first commit
[sitka/iNCIPit.git] / iNCIPit.cgi
CommitLineData
4cdc4f67
JS
1#! /usr/bin/perl
2
3# This file is part of iNCIPit
4#
5# iNCIPit is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 2 of the License, or
8# (at your option) any later version.
9#
10# iNCIPit is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
13# License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with issa. If not, see <http://www.gnu.org/licenses/>.
17
18
19use warnings;
20use XML::LibXML;
21use CGI::XMLPost;
22use HTML::Entities;
23use CGI::Carp;
24use XML::XPath;
25use OpenSRF::System;
26use OpenSRF::Utils::SettingsClient;
27use Digest::MD5 qw/md5_hex/;
28use OpenILS::Utils::Fieldmapper;
29use OpenILS::Utils::CStoreEditor qw/:funcs/;
30use OpenILS::Const qw/:const/;
31use Scalar::Util qw(reftype blessed);
32use MARC::Record;
33use MARC::Field;
34use MARC::File::XML;
35use POSIX qw/strftime/;
36use DateTime;
37
38my $xmlpost = CGI::XMLPost->new();
39my $xml = $xmlpost->data();
40
41# log posted data
42open POST_DATA, ">>post_data.txt";
43print POST_DATA $xml;
44close POST_DATA;
45
46# initialize the parser
47my $parser = new XML::LibXML;
48my $doc = $parser->load_xml( string => $xml );
49
50my %session = login();
51
52# Setup our SIGALRM handler.
53$SIG{'ALRM'} = \&logout;
54
55if (defined($session{authtoken})) {
56 $doc->exists('/NCIPMessage/LookupUser') ? lookupUser() :
57 ( $doc->exists('/NCIPMessage/ItemRequested') ? item_request() :
58 ( $doc->exists('/NCIPMessage/ItemShipped') ? item_shipped() :
59 ( $doc->exists('/NCIPMessage/ItemCheckedOut') ? item_checked_out() :
60 ( $doc->exists('/NCIPMessage/CheckOutItem') ? check_out_item() :
61 ( $doc->exists('/NCIPMessage/ItemCheckedIn') ? item_checked_in() :
62 ( $doc->exists('/NCIPMessage/CheckInItem') ? check_in_item() :
63 ( $doc->exists('/NCIPMessage/ItemReceived') ? item_received() :
64 ( $doc->exists('/NCIPMessage/AcceptItem') ? accept_item() :
65 ( $doc->exists('/NCIPMessage/ItemRequestCancelled') ? item_cancelled() :
66 ( $doc->exists('/NCIPMessage/ItemRenewed') ? item_renew() :
67 fail("UNKNOWN NCIPMessage")
68 )
69 )
70 )
71 )
72 )
73 )
74 )
75 )
76 )
77 );
78
79 # Clear any SIGALRM timers.
80 alarm(0);
81 logout();
82} else {
83 # red dwarf - s1:e1
84 fail("They are all dead, Dave.");
85}
86
87sub logit {
88 my ($msg,$func) = @_;
89 open RESP_DATA, ">>resp_data.txt";
90 print RESP_DATA $msg;
91 close RESP_DATA;
92 print $msg || fail($func);
93}
94
95sub item_renew {
96 my $faidSchemeX = $doc->findvalue('/NCIPMessage/ItemRenewed/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
97 my $faidScheme = HTML::Entities::encode($faidSchemeX);
98 my $faidValue = $doc->find('/NCIPMessage/ItemRenewed/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
99 my $taidSchemeX = $doc->findvalue('/NCIPMessage/ItemRenewed/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
100 my $taidScheme = HTML::Entities::encode($taidSchemeX);
101 my $taidValue = $doc->find('/NCIPMessage/ItemRenewed/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
102
103 my $pid = $doc->findvalue('/NCIPMessage/ItemRenewed/UniqueUserId/UserIdentifierValue');
104 my $visid = $doc->findvalue('/NCIPMessage/ItemRenewed/ItemOptionalFields/ItemDescription/VisibleItemId/VisibleItemIdentifier').$faidValue;
105 #my $barcode = $doc->findvalue('/NCIPMessage/ItemRenewed/UniqueItemId/ItemIdentifierValue').$faidValue;
106 my $due_date = $doc->findvalue('/NCIPMessage/ItemRenewed/DateDue');
107
108 #my $copy = copy_from_barcode($barcode);
109 #fail($copy->{textcode}) unless (blessed $copy);
110 #my $r = update_copy($copy,0); # seemed like copy had to be available before it could be checked out, so ...
111 #my $r2 = checkout($barcode,$pid,$due_date);
112 my $r = renewal($visid,$due_date);
113
114my $hd = <<ITEMRENEWAL;
115Content-type: text/xml
116
117
118<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
119<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
120 <ItemRenewedResponse>
121 <ResponseHeader>
122 <FromAgencyId>
123 <UniqueAgencyId>
124 <Scheme>$faidScheme</Scheme>
125 <Value>$faidValue</Value>
126 </UniqueAgencyId>
127 </FromAgencyId>
128 <ToAgencyId>
129 <UniqueAgencyId>
130 <Scheme>$taidScheme</Scheme>
131 <Value>$taidValue</Value>
132 </UniqueAgencyId>
133 </ToAgencyId>
134 </ResponseHeader>
135 <UniqueItemId>
136 <ItemIdentifierValue datatype="string">$visid</ItemIdentifierValue>
137 </UniqueItemId>
138 </ItemRenewedResponse>
139</NCIPMessage>
140
141ITEMRENEWAL
142
143my $more_info = <<MOREINFO;
144
145VISID = $visid
146Desired Due Date = $date_due
147
148MOREINFO
149
150 $hd .= $more_info;
151
152 logit($hd,(caller(0))[3]);
153}
154
155sub accept_item {
156 my $faidSchemeX = $doc->findvalue('/NCIPMessage/AcceptItem/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
157 my $faidScheme = HTML::Entities::encode($faidSchemeX);
158 my $faidValue = $doc->find('/NCIPMessage/AcceptItem/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
159 my $taidSchemeX = $doc->findvalue('/NCIPMessage/AcceptItem/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
160 my $taidScheme = HTML::Entities::encode($taidSchemeX);
161 my $taidValue = $doc->find('/NCIPMessage/AcceptItem/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
162
163 my $visid = $doc->findvalue('/NCIPMessage/AcceptItem/ItemOptionalFields/ItemDescription/VisibleItemId/VisibleItemIdentifier').$faidValue;
164 my $request_id = $doc->findvalue('/NCIPMessage/AcceptItem/UniqueRequestId/RequestIdentifierValue') || "unknown" ;
165# my $barcode = $doc->findvalue('/NCIPMessage/AcceptItem/UniqueItemId/ItemIdentifierValue').$faidValue;
166 my $patron = $doc->findvalue('/NCIPMessage/AcceptItem/UserOptionalFields/VisibleUserId/VisibleUserIdentifier');
167# my $copy = copy_from_barcode($barcode);
168# my $r = place_hold ('C', $copy, $patron, OUHERE);
169 my $copy = copy_from_barcode($visid);
170 my $r2 = update_copy($copy,112); # put into INN-Reach Hold status
171
172my $hd = <<ACCEPTITEM;
173Content-type: text/xml
174
175
176<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
177<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
178 <AcceptItemResponse>
179 <ResponseHeader>
180 <FromAgencyId>
181 <UniqueAgencyId>
182 <Scheme>$faidScheme</Scheme>
183 <Value>$faidValue</Value>
184 </UniqueAgencyId>
185 </FromAgencyId>
186 <ToAgencyId>
187 <UniqueAgencyId>
188 <Scheme>$taidScheme</Scheme>
189 <Value>$taidValue</Value>
190 </UniqueAgencyId>
191 </ToAgencyId>
192 </ResponseHeader>
193 <UniqueRequestId>
194 <ItemIdentifierValue datatype="string">$request_id</ItemIdentifierValue>
195 </UniqueRequestId>
196 <UniqueItemId>
197 <ItemIdentifierValue datatype="string">$visid</ItemIdentifierValue>
198 </UniqueItemId>
199 </AcceptItemResponse>
200</NCIPMessage>
201
202ACCEPTITEM
203
204 logit($hd,(caller(0))[3]);
205}
206
207sub item_received {
208 my $faidValue = $doc->find('/NCIPMessage/ItemReceived/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
209 my $barcode = $doc->findvalue('/NCIPMessage/ItemReceived/ItemOptionalFields/ItemDescription/VisibleItemId/VisibleItemIdentifier').$faidValue;
210 #my $barcode = $doc->findvalue('/NCIPMessage/ItemReceived/UniqueItemId/ItemIdentifierValue').$faidValue;
211 my $copy = copy_from_barcode($barcode);
212 fail($copy->{textcode}) unless (blessed $copy);
213 my $r1 = checkin($barcode,OUHERE) if ($copy->status == OILS_COPY_STATUS_CHECKED_OUT); # checkin the item before delete if ItemCheckedIn step was skipped
214 my $r2 = delete_copy($copy);
215
216my $hd = <<ITEMRECEIVED;
217Content-type: text/xml
218
219
220<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
221<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
222 <ItemReceivedResponse>
223 <ResponseHeader>
224 <FromAgencyId>
225 <UniqueAgencyId>
226 <Scheme>$faidScheme</Scheme>
227 <Value>$faidValue</Value>
228 </UniqueAgencyId>
229 </FromAgencyId>
230 <ToAgencyId>
231 <UniqueAgencyId>
232 <Scheme>$taidScheme</Scheme>
233 <Value>$taidValue</Value>
234 </UniqueAgencyId>
235 </ToAgencyId>
236 </ResponseHeader>
237 <UniqueItemId>
238 <ItemIdentifierValue datatype="string">$barcode</ItemIdentifierValue>
239 </UniqueItemId>
240 </ItemReceivedResponse>
241</NCIPMessage>
242
243ITEMRECEIVED
244
245 logit($hd,(caller(0))[3]);
246}
247
248sub item_cancelled {
249 my $faidSchemeX = $doc->findvalue('/NCIPMessage/ItemRequestCancelled/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
250 my $faidScheme = HTML::Entities::encode($faidSchemeX);
251 my $faidValue = $doc->find('/NCIPMessage/ItemRequestCancelled/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
252
253 my $taidSchemeX = $doc->findvalue('/NCIPMessage/ItemRequestCancelled/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
254 my $taidScheme = HTML::Entities::encode($taidSchemeX);
255 my $taidValue = $doc->find('/NCIPMessage/ItemRequestCancelled/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
256 my $UniqueItemIdAgencyIdValue = $doc->findvalue('/NCIPMessage/ItemRequestCancelled/UniqueItemId/UniqueAgencyId/Value');
257
258 #my $barcode = $doc->findvalue('/NCIPMessage/ItemRequestCancelled/ItemOptionalFields/ItemDescription/VisibleItemId/VisibleItemIdentifier').$faidValue;
259 my $barcode = $doc->findvalue('/NCIPMessage/ItemRequestCancelled/UniqueItemId/ItemIdentifierValue').$faidValue;
260
261 if ($UniqueItemIdAgencyIdValue eq SPECIALTOAGENCY ) {
262 # my $localid = locid_from_barcode($barcode);
263 # $r = place_hold($localid, SPECIALTOAGEID );
264 # remove hold!
265 }
266 else {
267 my $copy = copy_from_barcode($barcode);
268 fail($copy->{textcode}) unless (blessed $copy);
269 my $r = delete_copy($copy);
270 }
271
272my $hd = <<ITEMREQUESTCANCELLED;
273Content-type: text/xml
274
275
276<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
277<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
278 <ItemRequestCancelledResponse>
279 <ResponseHeader>
280 <FromAgencyId>
281 <UniqueAgencyId>
282 <Scheme>$faidScheme</Scheme>
283 <Value>$faidValue</Value>
284 </UniqueAgencyId>
285 </FromAgencyId>
286 <ToAgencyId>
287 <UniqueAgencyId>
288 <Scheme>$taidScheme</Scheme>
289 <Value>$taidValue</Value>
290 </UniqueAgencyId>
291 </ToAgencyId>
292 </ResponseHeader>
293 <UniqueItemId>
294 <ItemIdentifierValue datatype="string">$barcode</ItemIdentifierValue>
295 </UniqueItemId>
296 </ItemRequestCancelledResponse>
297</NCIPMessage>
298
299ITEMREQUESTCANCELLED
300
301 logit($hd,(caller(0))[3]);
302}
303
304sub item_checked_in {
305 my $faidSchemeX = $doc->findvalue('/NCIPMessage/ItemCheckedIn/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
306 my $faidScheme = HTML::Entities::encode($faidSchemeX);
307 my $faidValue = $doc->find('/NCIPMessage/ItemCheckedIn/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
308 my $taidSchemeX = $doc->findvalue('/NCIPMessage/ItemCheckedIn/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
309 my $taidScheme = HTML::Entities::encode($taidSchemeX);
310 my $taidValue = $doc->find('/NCIPMessage/ItemCheckedIn/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
311
312 my $barcode = $doc->findvalue('/NCIPMessage/ItemCheckedIn/ItemOptionalFields/ItemDescription/VisibleItemId/VisibleItemIdentifier').$faidValue;
313 # my $barcode = $doc->findvalue('/NCIPMessage/ItemCheckedIn/UniqueItemId/ItemIdentifierValue').$faidValue;
314 my $r = checkin($barcode, PICKUPLOCATION );
315 my $copy = copy_from_barcode($barcode);
316 my $r2 = update_copy($copy,114); # "INN-Reach Transit Return" status
317
318my $hd = <<ITEMCHECKEDIN;
319Content-type: text/xml
320
321
322<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
323<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
324 <ItemCheckedInResponse>
325 <ResponseHeader>
326 <FromAgencyId>
327 <UniqueAgencyId>
328 <Scheme>$faidScheme</Scheme>
329 <Value>$faidValue</Value>
330 </UniqueAgencyId>
331 </FromAgencyId>
332 <ToAgencyId>
333 <UniqueAgencyId>
334 <Scheme>$taidScheme</Scheme>
335 <Value>$taidValue</Value>
336 </UniqueAgencyId>
337 </ToAgencyId>
338 </ResponseHeader>
339 <UniqueItemId>
340 <ItemIdentifierValue datatype="string">$barcode</ItemIdentifierValue>
341 </UniqueItemId>
342 </ItemCheckedInResponse>
343</NCIPMessage>
344
345ITEMCHECKEDIN
346
347 logit($hd,(caller(0))[3]);
348}
349
350sub item_checked_out {
351 my $faidSchemeX = $doc->findvalue('/NCIPMessage/ItemCheckedOut/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
352 my $faidScheme = HTML::Entities::encode($faidSchemeX);
353 my $faidValue = $doc->find('/NCIPMessage/ItemCheckedOut/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
354 my $taidSchemeX = $doc->findvalue('/NCIPMessage/ItemCheckedOut/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
355 my $taidScheme = HTML::Entities::encode($taidSchemeX);
356 my $taidValue = $doc->find('/NCIPMessage/ItemCheckedOut/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
357
358 my $pid = $doc->findvalue('/NCIPMessage/ItemCheckedOut/UserOptionalFields/VisibleUserId/VisibleUserIdentifier');
359 # my $barcode = $doc->findvalue('/NCIPMessage/ItemCheckedOut/UniqueItemId/ItemIdentifierValue').$faidValue;
360 my $due_date = $doc->findvalue('/NCIPMessage/ItemCheckedOut/DateDue');
361 # my $title = $doc->findvalue('/NCIPMessage/ItemCheckedOut/ItemOptionalFields/BibliographicDescription/Title');
362
363 my $visid = $doc->findvalue('/NCIPMessage/ItemCheckedOut/ItemOptionalFields/ItemDescription/VisibleItemId/VisibleItemIdentifier').$faidValue;
364
365 my $copy = copy_from_barcode($visid);
366 fail($copy->{textcode}) unless (blessed $copy);
367 my $r = update_copy($copy,0); # seemed like copy had to be available before it could be checked out, so ...
368 # my $r1 = checkin($visid, PICKUPOU ) if ($copy->status == OILS_COPY_STATUS_CHECKED_OUT); # double posted itemcheckedout messages cause error ... trying to simplify
369 my $r2 = checkout($visid,$pid,$due_date);
370
371my $hd = <<ITEMCHECKEDOUT;
372Content-type: text/xml
373
374
375<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
376<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
377 <ItemCheckedOutResponse>
378 <ResponseHeader>
379 <FromAgencyId>
380 <UniqueAgencyId>
381 <Scheme>$faidScheme</Scheme>
382 <Value>$faidValue</Value>
383 </UniqueAgencyId>
384 </FromAgencyId>
385 <ToAgencyId>
386 <UniqueAgencyId>
387 <Scheme>$taidScheme</Scheme>
388 <Value>$taidValue</Value>
389 </UniqueAgencyId>
390 </ToAgencyId>
391 </ResponseHeader>
392 <UniqueItemId>
393 <ItemIdentifierValue datatype="string">$visid</ItemIdentifierValue>
394 </UniqueItemId>
395 </ItemCheckedOutResponse>
396</NCIPMessage>
397
398ITEMCHECKEDOUT
399
400$hd .= $r;
401 logit($hd,(caller(0))[3]);
402}
403
404sub check_out_item {
405 my $faidSchemeX = $doc->findvalue('/NCIPMessage/CheckOutItem/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
406 my $faidScheme = HTML::Entities::encode($faidSchemeX);
407 my $faidValue = $doc->find('/NCIPMessage/CheckOutItem/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
408 my $taidSchemeX = $doc->findvalue('/NCIPMessage/CheckOutItem/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
409 my $taidScheme = HTML::Entities::encode($taidSchemeX);
410 my $taidValue = $doc->find('/NCIPMessage/CheckOutItem/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
411
412 my $mdate = $doc->findvalue('/NCIPMessage/CheckOutItem/MandatedAction/DateEventOccurred');
413 my $pid = $doc->find('/NCIPMessage/CheckOutItem/UserOptionalFields/UniqueAgencyId/Value');
414
415 my $barcode = $doc->findvalue('/NCIPMessage/CheckOutItem/UniqueItemId/ItemIdentifierValue');
416 my $due_date = $doc->findvalue('/NCIPMessage/CheckOutItem/DateDue');
417
418 my $copy = copy_from_barcode($barcode);
419 fail($copy->{textcode}) unless (blessed $copy);
420 # my $r = update_copy($copy,0); # seemed like copy had to be available before it could be checked out, so ...
421
422 my $r2 = checkout($barcode,$pid,$due_date);
423
424my $hd = <<CHECKOUTITEM;
425Content-type: text/xml
426
427
428<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
429<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
430 <CheckOutItemResponse>
431 <ResponseHeader>
432 <FromAgencyId>
433 <UniqueAgencyId>
434 <Scheme>$faidScheme</Scheme>
435 <Value>$faidValue</Value>
436 </UniqueAgencyId>
437 </FromAgencyId>
438 <ToAgencyId>
439 <UniqueAgencyId>
440 <Scheme>$taidScheme</Scheme>
441 <Value>$taidValue</Value>
442 </UniqueAgencyId>
443 </ToAgencyId>
444 </ResponseHeader>
445 <UniqueItemId>
446 <ItemIdentifierValue datatype="string">$barcode</ItemIdentifierValue>
447 </UniqueItemId>
448 </CheckOutItemResponse>
449</NCIPMessage>
450
451CHECKOUTITEM
452
453 logit($hd,(caller(0))[3]);
454}
455
456sub check_in_item {
457 my $faidSchemeX = $doc->findvalue('/NCIPMessage/CheckInItem/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
458 my $faidScheme = HTML::Entities::encode($faidSchemeX);
459 my $faidValue = $doc->find('/NCIPMessage/CheckInItem/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
460 my $taidSchemeX = $doc->findvalue('/NCIPMessage/CheckInItem/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
461 my $taidScheme = HTML::Entities::encode($taidSchemeX);
462 my $taidValue = $doc->find('/NCIPMessage/CheckInItem/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
463
464 my $barcode = $doc->findvalue('/NCIPMessage/CheckInItem/UniqueItemId/ItemIdentifierValue');
465 my $r = checkin($barcode, OUHERE);
466 my $copy = copy_from_barcode($barcode);
467 my $r2 = update_copy($copy,0); # Available now
468
469my $hd = <<CHECKINITEM;
470Content-type: text/xml
471
472
473<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
474<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
475 <CheckInItemResponse>
476 <ResponseHeader>
477 <FromAgencyId>
478 <UniqueAgencyId>
479 <Scheme>$faidScheme</Scheme>
480 <Value>$faidValue</Value>
481 </UniqueAgencyId>
482 </FromAgencyId>
483 <ToAgencyId>
484 <UniqueAgencyId>
485 <Scheme>$taidScheme</Scheme>
486 <Value>$taidValue</Value>
487 </UniqueAgencyId>
488 </ToAgencyId>
489 </ResponseHeader>
490 <UniqueItemId>
491 <ItemIdentifierValue datatype="string">$barcode</ItemIdentifierValue>
492 </UniqueItemId>
493 </CheckInItemResponse>
494</NCIPMessage>
495
496CHECKINITEM
497
498 logit($hd,(caller(0))[3]);
499}
500
501sub item_shipped {
502 my $faidSchemeX = $doc->findvalue('/NCIPMessage/ItemShipped/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
503 my $faidScheme = HTML::Entities::encode($faidSchemeX);
504 my $faidValue = $doc->find('/NCIPMessage/ItemShipped/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
505 my $taidSchemeX = $doc->findvalue('/NCIPMessage/ItemShipped/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
506 my $taidScheme = HTML::Entities::encode($taidSchemeX);
507 my $taidValue = $doc->find('/NCIPMessage/ItemShipped/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
508
509 my $visid = $doc->findvalue('/NCIPMessage/ItemShipped/ItemOptionalFields/ItemDescription/VisibleItemId/VisibleItemIdentifier').$faidValue;
510 my $barcode = $doc->findvalue('/NCIPMessage/ItemShipped/UniqueItemId/ItemIdentifierValue').$faidValue;
511 my $title = $doc->findvalue('/NCIPMessage/ItemShipped/ItemOptionalFields/BibliographicDescription/Title');
512 my $callnumber = $doc->findvalue('/NCIPMessage/ItemShipped/ItemOptionalFields/ItemDescription/CallNumber');
513
514 my $copy = copy_from_barcode($barcode);
515 fail($copy->{textcode}) unless (blessed $copy);
516 my $r = update_copy_shipped($copy,113,$visid); # put copy into INN-Reach Transit status & modify barcode = Visid != tempIIIiNumber
517
518my $hd = <<ITEMSHIPPED;
519Content-type: text/xml
520
521
522<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
523<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
524 <ItemShippedResponse>
525 <ResponseHeader>
526 <FromAgencyId>
527 <UniqueAgencyId>
528 <Scheme>$faidScheme</Scheme>
529 <Value>$faidValue</Value>
530 </UniqueAgencyId>
531 </FromAgencyId>
532 <ToAgencyId>
533 <UniqueAgencyId>
534 <Scheme>$taidScheme</Scheme>
535 <Value>$taidValue</Value>
536 </UniqueAgencyId>
537 </ToAgencyId>
538 </ResponseHeader>
539 <UniqueItemId>
540 <ItemIdentifierValue datatype="string">$visid</ItemIdentifierValue>
541 </UniqueItemId>
542 </ItemShippedResponse>
543</NCIPMessage>
544
545ITEMSHIPPED
546
547 logit($hd,(caller(0))[3]);
548}
549
550sub item_request {
551 my $faidSchemeX = $doc->findvalue('/NCIPMessage/ItemRequested/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
552 my $faidScheme = HTML::Entities::encode($faidSchemeX);
553 my $faidValue = $doc->find('/NCIPMessage/ItemRequested/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
554
555 my $taidSchemeX = $doc->findvalue('/NCIPMessage/ItemRequested/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
556 my $taidScheme = HTML::Entities::encode($taidSchemeX);
557 my $taidValue = $doc->find('/NCIPMessage/ItemRequested/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
558 my $UniqueItemIdAgencyIdValue = $doc->findvalue('/NCIPMessage/ItemRequested/UniqueItemId/UniqueAgencyId/Value');
559
560 my $id = $doc->findvalue('/NCIPMessage/ItemRequested/UniqueUserId/UserIdentifierValue');
561 my $barcode = $doc->findvalue('/NCIPMessage/ItemRequested/UniqueItemId/ItemIdentifierValue');
562 my $author = $doc->findvalue('/NCIPMessage/ItemRequested/ItemOptionalFields/BibliographicDescription/Author');
563 my $title = $doc->findvalue('/NCIPMessage/ItemRequested/ItemOptionalFields/BibliographicDescription/Title');
564 my $callnumber = $doc->findvalue('/NCIPMessage/ItemRequested/ItemOptionalFields/ItemDescription/CallNumber');
565
566 my $r = "default error checking response";
567
568 if ($UniqueItemIdAgencyIdValue eq SPECIALFROMAGENCY ) {
569 my $localid = locid_from_barcode($barcode);
570 $r = place_simple_hold($localid, SPECIALFROMAGENCYID );
571 }
572 else {
573 my $copy_status_id = 110; # INN-Reach loan
574 $barcode .= $faidValue;
575 $r = create_copy($title, $callnumber, $barcode, $copy_status_id);
576 }
577
578my $hd = <<ITEMREQ;
579Content-type: text/xml
580
581
582<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
583<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
584 <ItemRequestedResponse>
585 <ResponseHeader>
586 <FromAgencyId>
587 <UniqueAgencyId>
588 <Scheme>$faidScheme</Scheme>
589 <Value>$faidValue</Value>
590 </UniqueAgencyId>
591 </FromAgencyId>
592 <ToAgencyId>
593 <UniqueAgencyId>
594 <Scheme>$taidScheme</Scheme>
595 <Value>$taidValue</Value>
596 </UniqueAgencyId>
597 </ToAgencyId>
598 </ResponseHeader>
599 <UniqueUserId>
600 <UniqueAgencyId>
601 <Scheme datatype="string">$taidScheme</Scheme>
602 <Value datatype="string">$taidValue</Value>
603 </UniqueAgencyId>
604 <UserIdentifierValue datatype="string">$id</UserIdentifierValue>
605 </UniqueUserId>
606 <UniqueItemId>
607 <ItemIdentifierValue datatype="string">$barcode</ItemIdentifierValue>
608 </UniqueItemId>
609 <ItemOptionalFields>
610 <BibliographicDescription>
611 <Author datatype="string">$author</Author>
612 <Title datatype="string">$title</Title>
613 </BibliographicDescription>
614 <ItemDescription>
615 <CallNumber datatype="string">$callnumber</CallNumber>
616 </ItemDescription>
617 </ItemOptionalFields>
618 </ItemRequestedResponse>
619</NCIPMessage>
620
621ITEMREQ
622
623 logit($hd,(caller(0))[3]);
624}
625
626
627sub lookupUser {
628
629my $faidScheme = $doc->findvalue('/NCIPMessage/LookupUser/InitiationHeader/FromAgencyId/UniqueAgencyId/Scheme');
630$faidScheme = HTML::Entities::encode($faidScheme);
631my $faidValue = $doc->find('/NCIPMessage/LookupUser/InitiationHeader/FromAgencyId/UniqueAgencyId/Value');
632my $taidScheme = $doc->findvalue('/NCIPMessage/LookupUser/InitiationHeader/ToAgencyId/UniqueAgencyId/Scheme');
633$taidScheme = HTML::Entities::encode($taidScheme);
634
635my $taidValue = $doc->find('/NCIPMessage/LookupUser/InitiationHeader/ToAgencyId/UniqueAgencyId/Value');
636my $id = $doc->findvalue('/NCIPMessage/LookupUser/VisibleUserId/VisibleUserIdentifier');
637my $uidValue = user_id_from_barcode($id);
638
639if (!defined($uidValue) || (ref($uidValue) && reftype($uidValue) eq 'HASH')) {
640 do_lookup_user_error_stanza("PATRON_NOT_FOUND");
641 die;
642}
643
644my ($propername,$email,$good_until,$userprivid) = ("name here","","good until","0") ;
645
646my $patron = flesh_user($uidValue);
647
648#if (blessed($patron)) {
649 if ($patron->deleted eq 't') {
650 do_lookup_user_error_stanza("PATRON_DELETED");
651 die;
652 }
653 $propername = $patron->first_given_name . " " . $patron->family_name;
654
655 if ( defined($patron->email) ) {
656 $email = qq(
657 <UserAddressInformation>
658 <ElectronicAddress>
659 <ElectronicAddressType>
660 <Scheme datatype="string">http:/blah.com</Scheme>
661 <Value datatype="string">mailto</Value>
662 </ElectronicAddressType>
663 <ElectronicAddressData datatype="string">).HTML::Entities::encode($patron->email).qq(</ElectronicAddressData>]
664 </ElectronicAddress>
665 </UserAddressInformation>);
666 }
667
668 $good_until = $patron->expire_date || "unknown";
669 $userprivid = $patron->profile;
670 #my $homeOU = $patron->home_ou->name;
671 my $userpriv = $patron->profile->name;
672
673my $hd = <<LOOKUPUSERRESPONSE;
674Content-type: text/xml
675
676
677<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
678<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
679 <LookupUserResponse>
680 <ResponseHeader>
681 <FromAgencyId>
682 <UniqueAgencyId>
683 <Scheme>$taidScheme</Scheme>
684 <Value>$taidValue</Value>
685 </UniqueAgencyId>
686 </FromAgencyId>
687 <ToAgencyId>
688 <UniqueAgencyId>
689 <Scheme>$faidScheme</Scheme>
690 <Value>$faidValue</Value>
691 </UniqueAgencyId>
692 </ToAgencyId>
693 </ResponseHeader>
694 <UniqueUserId>
695 <UniqueAgencyId>
696 <Scheme>$taidScheme</Scheme>
697 <Value>$taidValue</Value>
698 </UniqueAgencyId>
699 <UserIdentifierValue>$id</UserIdentifierValue>
700 </UniqueUserId>
701 <UserOptionalFields>
702 <VisibleUserId>
703 <VisibleUserIdentifierType>
704 <Scheme datatype="string">http://blah.com</Scheme>
705 <Value datatype="string">Barcode</Value>
706 </VisibleUserIdentifierType>
707 <VisibleUserIdentifier datatype="string">$id</VisibleUserIdentifier>
708 </VisibleUserId>
709 <NameInformation>
710 <PersonalNameInformation>
711 <UnstructuredPersonalUserName datatype="string">$propername</UnstructuredPersonalUserName>
712 </PersonalNameInformation>
713 </NameInformation>
714 <UserPrivilege>
715 <UniqueAgencyId>
716 <Scheme datatype="string">$faidScheme</Scheme>
717 <Value datatype="string">$faidValue</Value>
718 </UniqueAgencyId>
719 <AgencyUserPrivilegeType>
720 <Scheme datatype="string">http://testing.purposes.only</Scheme>
721 <Value datatype="string">$userpriv</Value>
722 </AgencyUserPrivilegeType>
723 <ValidToDate datatype="string">$good_until</ValidToDate>
724 </UserPrivilege> $email
725 </UserOptionalFields>
726 </LookupUserResponse>
727</NCIPMessage>
728
729LOOKUPUSERRESPONSE
730
731logit($hd,(caller(0))[3]);
732}
733
734
735sub fail {
736my $error_msg = shift || "THIS IS THE DEFAULT NCIP RESP MSG";
737print "Content-type: text/xml\n\n";
738
739print <<ITEMREQ;
740<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
741<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
742 <ItemRequestedResponse>
743 <ResponseHeader>
744 <FromAgencyId>
745 <UniqueAgencyId>
746 <Scheme>http://scheme.server.here/IRCIRCD?target=get_scheme_values&amp;scheme=UniqueAgencyId</Scheme>
747 <Value></Value>
748 </UniqueAgencyId>
749 </FromAgencyId>
750 <ToAgencyId>
751 <UniqueAgencyId>
752 <Scheme>http://scheme.server.here/IRCIRCD?target=get_scheme_values&amp;scheme=UniqueAgencyId</Scheme>
753 <Value>$error_msg</Value>
754 </UniqueAgencyId>
755 </ToAgencyId>
756 </ResponseHeader>
757 </ItemRequestedResponse>
758</NCIPMessage>
759
760ITEMREQ
761}
762
763sub do_lookup_user_error_stanza {
764
765my $error = shift;
766my $hd = <<LOOKUPPROB;
767Content-type: text/xml
768
769
770<!DOCTYPE NCIPMessage PUBLIC "-//NISO//NCIP DTD Version 1.0//EN" "http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
771<NCIPMessage version="http://www.niso.org/ncip/v1_0/imp1/dtd/ncip_v1_0.dtd">
772<LookupUserResponse>
773 <ResponseHeader>
774 <FromAgencyId>
775 <UniqueAgencyId>
776 <Scheme>$taidScheme</Scheme>
777 <Value>$taidValue</Value>
778 </UniqueAgencyId>
779 </FromAgencyId>
780 <ToAgencyId>
781 <UniqueAgencyId>
782 <Scheme>$faidScheme</Scheme>
783 <Value>$faidValue</Value>
784 </UniqueAgencyId>
785 </ToAgencyId>
786 </ResponseHeader>
787 <Problem>
788 <ProcessingError>
789 <ProcessingErrorType>
790 <Scheme>http://www.niso.org/ncip/v1_0/schemes/processingerrortype/lookupuserprocessingerror.scm</Scheme>
791 <Value>$error</Value>
792 </ProcessingErrorType>
793 <ProcessingErrorElement>
794 <ElementName>AuthenticationInput</ElementName>
795 </ProcessingErrorElement></ProcessingError>
796 </ProcessingError>
797 </Problem>
798</LookupUserResponse>
799</NCIPMessage>
800
801LOOKUPPROB
802
803logit($hd,(caller(0))[3]);
804}
805
806# Login to the OpenSRF system/Evergreen.
807#
808# Returns a hash with the authtoken, authtime, and expiration (time in
809# seconds since 1/1/1970).
810sub login {
811
812my $bootstrap = '/openils/conf/opensrf_core.xml';
813my $uname = "USERNAMEHERE";
814my $password = "PASSWORDHERE";
815my $workstation = "REGISTEREDWORKSTATIONHERE";
816
817# Bootstrap the client
818OpenSRF::System->bootstrap_client(config_file => $bootstrap);
819my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
820Fieldmapper->import(IDL => $idl);
821
822# Initialize CStoreEditor:
823OpenILS::Utils::CStoreEditor->init;
824
825 my $seed = OpenSRF::AppSession
826 ->create('open-ils.auth')
827 ->request('open-ils.auth.authenticate.init', $uname)
828 ->gather(1);
829
830 return undef unless $seed;
831
832 my $response = OpenSRF::AppSession
833 ->create('open-ils.auth')
834 ->request('open-ils.auth.authenticate.complete',
835 { username => $uname,
836 password => md5_hex($seed . md5_hex($password)),
837 type => 'staff' })
838# workstation => $workstation })
839 ->gather(1);
840
841 return undef unless $response;
842
843 my %result;
844 $result{'authtoken'} = $response->{payload}->{authtoken};
845 $result{'authtime'} = $response->{payload}->{authtime};
846 $result{'expiration'} = time() + $result{'authtime'} if (defined($result{'authtime'}));
847 return %result;
848}
849
850# Check the time versus the session expiration time and login again if
851# the session has expired, consequently resetting the session
852# paramters. We want to run this before doing anything that requires
853# us to have a current session in OpenSRF.
854#
855# Arguments
856# none
857#
858# Returns
859# Nothing
860sub check_session_time {
861 if (time() > $session{'expiration'}) {
862 %session = login();
863 if (!%session) {
864 die("Failed to reinitialize the session after expiration.");
865 }
866 }
867}
868
869# Retrieve the logged in user.
870#
871sub get_session {
872 my $response = OpenSRF::AppSession->create('open-ils.auth')
873 ->request('open-ils.auth.session.retrieve', $session{authtoken})->gather(1);
874 return $response;
875}
876
877# Logout/destroy the OpenSRF session
878#
879# Argument is
880# none
881#
882# Returns
883# Does not return anything
884sub logout {
885 if (time() < $session{'expiration'}) {
886 my $response = OpenSRF::AppSession
887 ->create('open-ils.auth')
888 ->request('open-ils.auth.session.delete', $session{authtoken})
889 ->gather(1);
890 if ($response) {
891 # fail("Logout successful. Good-bye.\n");
892 # strong.silent.success
893 exit(0);
894 } else {
895 fail("Logout unsuccessful. Good-bye, anyway.");
896 }
897 }
898}
899
900sub update_copy {
901 check_session_time();
902 my ($copy,$status_id) = @_;
903 my $e = new_editor(authtoken=>$session{authtoken});
904 return $e->event->{textcode} unless ($e->checkauth);
905 $e->xact_begin;
906 $copy->status($status_id);
907 return $e->event unless $e->update_asset_copy($copy);
908 $e->commit;
909 return 'SUCCESS';
910}
911
912# my paranoia re barcode on shipped items using visid for unique value
913sub update_copy_shipped {
914 check_session_time();
915 my ($copy,$status_id,$barcode) = @_;
916 my $e = new_editor(authtoken=>$session{authtoken});
917 return $e->event->{textcode} unless ($e->checkauth);
918 $e->xact_begin;
919 $copy->status($status_id);
920 $copy->barcode($barcode);
921 return $e->event unless $e->update_asset_copy($copy);
922 $e->commit;
923 return 'SUCCESS';
924}
925
926# Delete a copy
927#
928# Argument
929# Fieldmapper asset.copy object
930#
931# Returns
932# "SUCCESS" on success
933# Event textcode if an error occurs
934sub delete_copy {
935 check_session_time();
936 my ($copy) = @_;
937
938 my $e = new_editor(authtoken=>$session{authtoken});
939 return $e->event->{textcode} unless ($e->checkauth);
940
941 # Get the calnumber
942 my $vol = $e->retrieve_asset_call_number($copy->call_number);
943 return $e->event->{textcode} unless ($vol);
944
945 # Get the biblio.record_entry
946 my $bre = $e->retrieve_biblio_record_entry($vol->record);
947 return $e->event->{textcode} unless ($bre);
948
949 # Delete everything in a transaction and rollback if anything fails.
950 $e->xact_begin;
951 my $r; # To hold results of editor calls
952 $r = $e->delete_asset_copy($copy);
953 unless ($r) {
954 my $lval = $e->event->{textcode};
955 $e->rollback;
956 return $lval;
957 }
958 my $list = $e->search_asset_copy({call_number => $vol->id, deleted => 'f'});
959 unless (@$list) {
960 $r = $e->delete_asset_call_number($vol);
961 unless ($r) {
962 my $lval = $e->event->{textcode};
963 $e->rollback;
964 return $lval;
965 }
966 $list = $e->search_asset_call_number({record => $bre->id, deleted => 'f'});
967 unless (@$list) {
968 $bre->deleted('t');
969 $r = $e->update_biblio_record_entry($bre);
970 unless ($r) {
971 my $lval = $e->event->{textcode};
972 $e->rollback;
973 return $lval;
974 }
975 }
976 }
977 $e->commit;
978 return 'SUCCESS';
979}
980
981# Get asset.copy from asset.copy.barcode.
982# Arguments
983# copy barcode
984#
985# Returns
986# asset.copy fieldmaper object
987# or hash on error
988sub copy_from_barcode {
989 check_session_time();
990 my ($barcode) = @_;
991 my $response = OpenSRF::AppSession->create('open-ils.search')
992 ->request('open-ils.search.asset.copy.find_by_barcode', $barcode)
993 ->gather(1);
994 return $response;
995}
996
997sub locid_from_barcode {
998 my ($barcode) = @_;
999 my $response = OpenSRF::AppSession->create('open-ils.search')
1000 ->request('open-ils.search.biblio.find_by_barcode', $barcode)
1001 ->gather(1);
1002 return $response->{ids}[0];
1003}
1004
1005# Convert a MARC::Record to XML for Evergreen
1006#
1007# Stolen from Dyrcona's issa framework which copied
1008# it from MVLC's Safari Load program which copied it
1009# from some code in the Open-ILS example import scripts.
1010#
1011# Argument
1012# A MARC::Record object
1013#
1014# Returns
1015# String with XML for the MARC::Record as Evergreen likes it
1016sub convert2marcxml {
1017 my $input = shift;
1018 (my $xml = $input->as_xml_record()) =~ s/\n//sog;
1019 $xml =~ s/^<\?xml.+\?\s*>//go;
1020 $xml =~ s/>\s+</></go;
1021 $xml =~ s/\p{Cc}//go;
1022 $xml = OpenILS::Application::AppUtils->entityize($xml);
1023 $xml =~ s/[\x00-\x1f]//go;
1024 return $xml;
1025}
1026
1027# Create a copy and marc record
1028#
1029# Arguments
1030# title
1031# call number
1032# copy barcode
1033#
1034# Returns
1035# bib id on succes
1036# event textcode on failure
1037sub create_copy {
1038 check_session_time();
1039 my ($title, $callnumber, $barcode, $copy_status_id) = @_;
1040
1041 my $e = new_editor(authtoken=>$session{authtoken});
1042 return $e->event->{textcode} unless ($e->checkauth);
1043
1044 my $r = $e->allowed(['CREATE_COPY', 'CREATE_MARC', 'CREATE_VOLUME']);
1045 if (ref($r) eq 'HASH') {
1046 return $r->{textcode} . ' ' . $r->{ilsperm};
1047 }
1048
1049 # Check if the barcode exists in asset.copy and bail if it does.
1050 my $list = $e->search_asset_copy({deleted => 'f', barcode => $barcode});
1051 if (@$list) {
1052# can we update it, if it exists? only if it is an INN-Reach status item
1053 $e->finish;
1054 fail('BARCODE_EXISTS');
1055 }
1056
1057 # Create MARC record
1058 my $record = MARC::Record->new();
1059 $record->encoding('UTF-8');
1060 $record->leader('00881nam a2200193 4500');
1061 my $datespec = strftime("%Y%m%d%H%M%S.0", localtime);
1062 my @fields = ();
1063 push(@fields, MARC::Field->new('005', $datespec));
1064 push(@fields, MARC::Field->new('082', '0', '4', 'a' => $callnumber));
1065 push(@fields, MARC::Field->new('245', '0', '0', 'a' => $title));
1066 $record->append_fields(@fields);
1067
1068 # Convert the record to XML
1069 my $xml = convert2marcxml($record);
1070
1071 my $bre = OpenSRF::AppSession->create('open-ils.cat')
1072 ->request('open-ils.cat.biblio.record.xml.import', $session{authtoken}, $xml, 'System Local', 1)
1073 ->gather(1);
1074 return $bre->{textcode} if (ref($bre) eq 'HASH');
1075
1076 # Create volume record
1077 my $vol = OpenSRF::AppSession->create('open-ils.cat')
1078 ->request('open-ils.cat.call_number.find_or_create', $session{authtoken}, $callnumber, $bre->id, 10)
1079 ->gather(1);
1080 return $vol->{textcode} if ($vol->{textcode});
1081
1082 # Retrieve the user
1083 my $user = get_session;
1084 # Create copy record
1085 my $copy = Fieldmapper::asset::copy->new();
1086 $copy->barcode($barcode);
1087 $copy->call_number($vol->{acn_id});
1088 $copy->circ_lib(10);
1089 $copy->circulate('t');
1090 $copy->holdable('t');
1091 $copy->opac_visible('t');
1092 $copy->deleted('f');
1093 $copy->fine_level(2);
1094 $copy->loan_duration(2);
1095 $copy->location(1);
1096 $copy->status($copy_status_id);
1097 $copy->editor('1002741');
1098 $copy->creator('1002741');
1099
1100 # Add the configured stat cat entries.
1101 #my @stat_cats;
1102 #my $nodes = $xpath->find("stat_cat_entry");
1103 #foreach my $node ($nodes->get_nodelist) {
1104 # next unless ($node->isa('XML::XPath::Node::Element'));
1105 # my $stat_cat_id = $node->getAttribute('stat_cat');
1106 # my $value = $node->string_value();
1107 # # Need to search for an existing asset.stat_cat_entry
1108 my $asce = $e->search_asset_stat_cat_entry({'stat_cat' => $stat_cat_id, 'value' => $value})->[0];
1109 # unless ($asce) {
1110 # # if not, create a new one and use its id.
1111 # $asce = Fieldmapper::asset::stat_cat_entry->new();
1112 # $asce->stat_cat($stat_cat_id);
1113 # $asce->value($value);
1114 # $asce->owner($ou->id);
1115 # $e->xact_begin;
1116 # $asce = $e->create_asset_stat_cat_entry($asce);
1117 # $e->xact_commit;
1118 # }
1119 # push(@stat_cats, $asce);
1120 #}
1121
1122 $e->xact_begin;
1123 $copy = $e->create_asset_copy($copy);
1124 #if (scalar @stat_cats) {
1125 # foreach my $asce (@stat_cats) {
1126 # my $ascecm = Fieldmapper::asset::stat_cat_entry_copy_map->new();
1127 # $ascecm->stat_cat($asce->stat_cat);
1128 # $ascecm->stat_cat_entry($asce->id);
1129 # $ascecm->owning_copy($copy->id);
1130 # $ascecm = $e->create_asset_stat_cat_entry_copy_map($ascecm);
1131 # }
1132 #}
1133 $e->commit;
1134 return $e->event->{textcode} unless ($r);
1135 return 'SUCCESS';
1136}
1137
1138# Checkout a copy to a patron
1139#
1140# Arguments
1141# copy barcode
1142# patron barcode
1143#
1144# Returns
1145# textcode of the OSRF response.
1146sub checkout
1147{
1148 check_session_time();
1149 my ($copy_barcode, $patron_barcode, $due_date) = @_;
1150
1151 # Check for copy:
1152 my $copy = copy_from_barcode($copy_barcode);
1153 unless (defined($copy) && blessed($copy)) {
1154 return 'COPY_BARCODE_NOT_FOUND';
1155 }
1156
1157 # Check for user
1158 my $uid = user_id_from_barcode($patron_barcode);
1159 return 'PATRON_BARCODE_NOT_FOUND' if (ref($uid));
1160
1161 my $response = OpenSRF::AppSession->create('open-ils.circ')
1162 ->request('open-ils.circ.checkout.full.override', $session{authtoken},
1163 { copy_barcode => $copy_barcode,
1164 patron_barcode => $patron_barcode,
1165 due_date => $due_date })
1166 ->gather(1);
1167 return $response->{textcode};
1168}
1169
1170sub renewal
1171{
1172 check_session_time();
1173 my ($copy_barcode, $due_date) = @_;
1174
1175 # Check for copy:
1176 my $copy = copy_from_barcode($copy_barcode);
1177 unless (defined($copy) && blessed($copy)) {
1178 return 'COPY_BARCODE_NOT_FOUND';
1179 }
1180
1181
1182 my $response = OpenSRF::AppSession->create('open-ils.circ')
1183 ->request('open-ils.circ.renew', $session{authtoken},
1184 { copy_barcode => $copy_barcode,
1185 due_date => $due_date })
1186 ->gather(1);
1187 return $response->{textcode};
1188}
1189
1190# Check a copy in at an org_unit
1191#
1192# Arguments
1193# copy barcode
1194# org_unit
1195#
1196# Returns
1197# "SUCCESS" on success
1198# textcode of a failed OSRF request
1199# 'COPY_NOT_CHECKED_OUT' when the copy is not checked out or not
1200# checked out to the user's work_ou
1201sub checkin
1202{
1203 check_session_time();
1204 my ($barcode, $where) = @_;
1205
1206 my $copy = copy_from_barcode($barcode);
1207 return $copy->{textcode} unless (blessed $copy);
1208
1209 return 'COPY_NOT_CHECKED_OUT' unless ($copy->status == OILS_COPY_STATUS_CHECKED_OUT);
1210
1211 my $e = new_editor(authtoken=>$session{authtoken});
1212 return $e->event->{textcode} unless ($e->checkauth);
1213
1214 my $circ = $e->search_action_circulation([ { target_copy => $copy->id, xact_finish => undef } ])->[0];
1215 #return 'COPY_NOT_CHECKED_OUT' unless ($circ->circ_lib == $where->id);
1216 return 'COPY_NOT_CHECKED_OUT' unless ($circ->circ_lib == 10);
1217
1218 my $r = OpenSRF::AppSession->create('open-ils.circ')
1219 ->request('open-ils.circ.checkin', $session{authtoken}, { barcode => $barcode, void_overdues => 1 })
1220 ->gather(1);
1221 return 'SUCCESS' if ($r->{textcode} eq 'ROUTE_ITEM');
1222 return $r->{textcode};
1223}
1224
1225# Get actor.usr.id from barcode.
1226# Arguments
1227# patron barcode
1228#
1229# Returns
1230# actor.usr.id
1231# or hash on error
1232sub user_id_from_barcode {
1233 check_session_time();
1234 my ($barcode) = @_;
1235
1236 my $response;
1237
1238 my $e = new_editor(authtoken=>$session{authtoken});
1239 return $response unless ($e->checkauth);
1240
1241 my $card = $e->search_actor_card({barcode => $barcode, active => 't'});
1242 return $e->event unless($card);
1243
1244 $response = $card->[0]->usr if (@$card);
1245
1246 $e->finish;
1247
1248 return $response;
1249}
1250
1251# Place a hold for a patron.
1252#
1253# Arguments
1254# Target object appropriate for type of hold
1255# Patron for whom the hold is place
1256#
1257# Returns
1258# "SUCCESS" on success
1259# textcode of a failed OSRF request
1260# "HOLD_TYPE_NOT_SUPPORTED" if the hold type is not supported
1261# (Currently only support 'T' and 'C')
1262
1263sub place_simple_hold {
1264 check_session_time();
1265 #my ($type, $target, $patron, $pickup_ou) = @_;
1266 my ($target, $patron) = @_;
1267 # NOTE : switch "t" to an "f" to make inactive hold active
1268 require '/usr/src/rel_2_1/Open-ILS/src/support-scripts/oils_header.pl';
1269 use vars qw/ $apputils $memcache $user $authtoken $authtime /;
1270 osrf_connect("/openils/conf/opensrf_core.xml");
1271 oils_login("USERNAMEHERE", "PASSWORDHERE");
1272 my $full_hold = '{"__c":"ahr","__p":[null,null,null,null,1,null,null,null,null,"T",null,null,"","3",null,"3",null,"'.$patron.'",1,"3","'.$target.'","'.$patron.'",null,null,null,null,null,null,"f",null]}';
1273 my $f_hold_perl = OpenSRF::Utils::JSON->JSON2perl($full_hold);
1274 my $resp = simplereq(CIRC(), 'open-ils.circ.holds.create', $authtoken, $f_hold_perl );
1275 #oils_event_die($resp);
1276 my $errors= "";
1277 if (ref($resp) eq 'ARRAY' ) {
1278 ($errors .= "error : ".$_->{textcode}) for @$resp;
1279 return $errors;
1280 }
1281 elsif (ref($resp) ne 'HASH' ) { return "Hold placed! hold_id = ". $resp ."\n" }
1282}
1283
1284# Place a hold for a patron.
1285#
1286# Arguments
1287# Type of hold
1288# Target object appropriate for type of hold
1289# Patron for whom the hold is place
1290# OU where hold is to be picked up
1291#
1292# Returns
1293# "SUCCESS" on success
1294# textcode of a failed OSRF request
1295# "HOLD_TYPE_NOT_SUPPORTED" if the hold type is not supported
1296# (Currently only support 'T' and 'C')
1297sub place_hold {
1298 check_session_time();
1299 my ($type, $target, $patron, $pickup_ou) = @_;
1300
1301 my $ou = org_unit_from_shortname($work_ou); # $work_ou is global
1302 my $ahr = Fieldmapper::action::hold_request->new;
1303 $ahr->hold_type($type);
1304 if ($type eq 'C') {
1305 # Check if we own the copy.
1306 if ($ou->id == $target->circ_lib) {
1307 # We own it, so let's place a copy hold.
1308 $ahr->target($target->id);
1309 $ahr->current_copy($target->id);
1310 } else {
1311 # We don't own it, so let's place a title hold instead.
1312 my $bib = bre_from_barcode($target->barcode);
1313 $ahr->target($bib->id);
1314 $ahr->hold_type('T');
1315 }
1316 } elsif ($type eq 'T') {
1317 $ahr->target($target);
1318 } else {
1319 return "HOLD_TYPE_NOT_SUPPORTED";
1320 }
1321 $ahr->usr($patron->id);
1322 $ahr->pickup_lib($pickup_ou->id);
1323 if (!$patron->email) {
1324 $ahr->email_notify('f');
1325 $ahr->phone_notify($patron->day_phone) if ($patron->day_phone);
1326 } else {
1327 $ahr->email_notify('t');
1328 }
1329
1330 # We must have a title hold and we want to change the hold
1331 # expiration date if we're sending the copy to the VC.
1332 set_title_hold_expiration($ahr) if ($ahr->pickup_lib == $ou->id);
1333
1334 my $params = { pickup_lib => $ahr->pickup_lib, patronid => $ahr->usr, hold_type => $ahr->hold_type };
1335
1336 if ($ahr->hold_type eq 'C') {
1337 $params->{copy_id} = $ahr->target;
1338 } else {
1339 $params->{titleid} = $ahr->target;
1340 }
1341
1342 my $r = OpenSRF::AppSession->create('open-ils.circ')
1343 ->request('open-ils.circ.title_hold.is_possible', $session{authtoken}, $params)
1344 ->gather(1);
1345
1346 if ($r->{textcode}) {
1347 return $r->{textcode};
1348 } elsif ($r->{success}) {
1349 $r = OpenSRF::AppSession->create('open-ils.circ')
1350 ->request('open-ils.circ.holds.create.override', $session{authtoken}, $ahr)
1351 ->gather(1);
1352
1353 my $returnValue = "SUCCESS";
1354 if (ref($r) eq 'HASH') {
1355 $returnValue = ($r->{textcode} eq 'PERM_FAILURE') ? $r->{ilsperm} : $r->{textcode};
1356 $returnValue =~ s/\.override$// if ($r->{textcode} eq 'PERM_FAILURE');
1357 }
1358 return $returnValue;
1359 } else {
1360 return 'HOLD_NOT_POSSIBLE';
1361 }
1362}
1363
1364# Set the expiration date on title holds
1365#
1366# Argument
1367# Fieldmapper action.hold_request object
1368#
1369# Returns
1370# Nothing
1371sub set_title_hold_expiration {
1372 my $hold = shift;
1373 if ($title_holds->{unit} && $title_holds->{duration}) {
1374 my $expiration = DateTime->now(time_zone => $tz);
1375 $expiration->add($title_holds->{unit} => $title_holds->{duration});
1376 $hold->expire_time($expiration->iso8601());
1377 }
1378}
1379
1380# Get actor.org_unit from the shortname
1381#
1382# Arguments
1383# org_unit shortname
1384#
1385# Returns
1386# Fieldmapper aou object
1387# or HASH on error
1388sub org_unit_from_shortname {
1389 check_session_time();
1390 my ($shortname) = @_;
1391 my $ou = OpenSRF::AppSession->create('open-ils.actor')
1392 ->request('open-ils.actor.org_unit.retrieve_by_shortname', $shortname)
1393 ->gather(1);
1394 return $ou;
1395}
1396
1397# Flesh user information
1398# Arguments
1399# actor.usr.id
1400#
1401# Returns
1402# fieldmapped, fleshed user or
1403# event hash on error
1404sub flesh_user {
1405 check_session_time();
1406 my ($id) = @_;
1407 my $response = OpenSRF::AppSession->create('open-ils.actor')
1408 ->request('open-ils.actor.user.fleshed.retrieve', $session{'authtoken'}, $id,
1409 [ 'card', 'cards', 'standing_penalties', 'home_ou', 'profile' ])
1410 ->gather(1);
1411 return $response;
1412}