This repository was archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissa.pl
More file actions
1140 lines (1027 loc) · 35.3 KB
/
issa.pl
File metadata and controls
1140 lines (1027 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# Copyright © 2011 Jason J.A. Stephenson <jason@sigio.com>
# Portions Copyright © 2012 Merrimack Valley Library Consortium
# <jstephenson@mvlc.org>
#
# This file is part of issa.
#
# issa is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# issa is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with issa. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use XML::XPath;
use OpenSRF::System;
use OpenSRF::Utils::SettingsClient;
use Digest::MD5 qw/md5_hex/;
use OpenILS::Utils::Fieldmapper;
use OpenILS::Utils::CStoreEditor qw/:funcs/;
use OpenILS::Const qw/:const/;
use Scalar::Util qw(reftype blessed);
use MARC::Record;
use MARC::Field;
use MARC::File::XML;
use POSIX qw/strftime/;
use DateTime;
# Hash to configure the prompts used by this program. Each named entry
# points to an array with the following members:
# 0 - The prompt string to display.
# 1 - A regex pattern to validate the prompt.
# 2 - A message to display when the input fails validation.
my %prompts = (
'main' => [ 'Enter Choice [1-7,Q]>', '^[1-7qQ]$', 'Please enter a single digit from 1 to 6 or a single letter Q.' ],
'patron' => [ 'Enter patron barcode>', '.+', '' ],
'copy' => [ 'Enter copy barcode>', '.+', '' ],
'hold' => [ 'Enter Choice [1-2]>', '^[12qQ]$', 'Please enter a single digit from 1 to 2 or a single letter Q.' ],
'bib' => [ 'Enter bibliographic id>', '^(?:\d+|[qQ])$', 'Please enter a string of digits or a single letter Q.' ],
'title' => [ 'Enter title>', '.+', '' ],
'callnumber' => [ 'Enter call number>', '.+', '' ],
'pickup' => [ 'Enter pickup library>', '.+', '' ],
);
my $config_file = '/openils/conf/issa.xml';
$config_file = $ARGV[1] if (defined($ARGV[0]) && $ARGV[0] eq "-config");
# Load our configuration
my $xpath = XML::XPath->new(filename => $config_file);
my $bootstrap = $xpath->findvalue("/issa/bootstrap_config")->value();
my $username = $xpath->findvalue("/issa/credentials/username")->value();
my $password = $xpath->findvalue("/issa/credentials/password")->value();
my $work_ou = $xpath->findvalue("/issa/credentials/work_ou")->value();
my $workstation = $xpath->findvalue("/issa/credentials/workstation")->value();
my $bre_source = $xpath->findvalue("/issa/config_bib_source")->value();
my $timeout = $xpath->findvalue("/issa/timeout")->value();
# Parameters for title holds:
my $title_holds = {};
$title_holds->{unit} = $xpath->findvalue('/issa/holds/title/duration/@unit')->value();
$title_holds->{duration} = $xpath->findvalue('/issa/holds/title/duration')->value();
# Use force holds for copy holds:
my $use_force = 0;
# Block types to block patrons:
my $block_types = [];
my $bnodes = $xpath->find('/issa/patrons/block_on/@block');
if ($bnodes) {
if ($bnodes->isa('XML::XPath::NodeSet')) {
foreach ($bnodes->get_nodelist) {
push(@{$block_types}, $_->getData) if ($_->isa('XML::XPath::Node::Attribute'));
}
} elsif ($bnodes->isa('XML::XPath::Node::Attribute')) {
push(@{$block_types}, $bnodes->getData);
}
}
# Patron grp ids (profiles) to block.
my $barred_grps = [];
undef($bnodes) if ($bnodes);
$bnodes = $xpath->find('/issa/patrons/block_profile/@grp');
if ($bnodes) {
if ($bnodes->isa('XML::XPath::NodeSet')) {
foreach ($bnodes->get_nodelist) {
push(@{$barred_grps}, $_->getData) if ($_->isa('XML::XPath::Node::Attribute'));
}
} elsif ($bnodes->isa('XML::XPath::Node::Attribute')) {
push(@{$barred_grps}, $bnodes->getData);
}
}
# Patron grp names (profiles) to block.
my $barred_profiles = [];
undef($bnodes) if ($bnodes);
$bnodes = $xpath->find('/issa/patrons/block_profile[not(@grp)]');
if ($bnodes) {
if ($bnodes->isa('XML::XPath::NodeSet')) {
foreach ($bnodes->get_nodelist) {
push(@{$barred_profiles}, $_->string_value()) if ($_->isa('XML::XPath::Node::Element'));
}
} elsif ($bnodes->isa('XML::XPath::Node::Element')) {
push(@{$barred_profiles}, $bnodes->string_value());
}
}
# Setup our SIGALRM handler.
$SIG{'ALRM'} = \&logout;
# Get our local timezone for setting hold expiration dates.
my $tz = DateTime::TimeZone->new(name => 'local');
# Make sure that the work_ou shortname is prepended to the workstation
# name.
$workstation = $work_ou . '-' . $workstation if ($workstation !~ /^${work_ou}-/);
customize_prompts();
# Bootstrap the client
OpenSRF::System->bootstrap_client(config_file => $bootstrap);
my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
Fieldmapper->import(IDL => $idl);
# Initialize CStoreEditor:
OpenILS::Utils::CStoreEditor->init;
my %session = login($username, $password, $workstation);
if (defined($session{authtoken})) {
my $input = "";
my $ou = org_unit_from_shortname($work_ou);
if (!blessed($ou)) {
die("Misconfiguration encountered.\nPlease inform support that $work_ou org_unit does not exist.");
}
$use_force = check_force_holds($session{authtoken});
while (lc($input) ne 'q') {
print_main_menu();
$input = prompt('main');
if ($input eq '1') {
my $p = prompt('patron');
next if ($p =~ /^[qQ]$/);
print("\n");
my $pid = user_id_from_barcode($p);
if (!defined($pid) || (ref($pid) && reftype($pid) eq 'HASH')) {
print("PATRON_NOT_FOUND\n\n");
next;
}
my $patron = flesh_user($pid);
if (blessed($patron)) {
if ($patron->deleted eq 't') {
print("PATRON_DELETED\n\n");
next;
}
printf("Name: %s %s%s\n", $patron->first_given_name, defined($patron->second_given_name) ? $patron->second_given_name . " " : "", $patron->family_name);
printf("Email: %s\n", defined($patron->email) ? $patron->email : "N/A");
printf("Home Library: %s (%s)\n", $patron->home_ou->name, $patron->home_ou->shortname);
print("Active Cards:");
printf(" %s", $patron->card->barcode) if ($patron->card && $patron->card->active eq 't');
my $card;
foreach $card (@{$patron->cards}) {
next if ($card->id eq $patron->card->id);
printf(" %s", $card->barcode) if ($card->active eq 't');
}
print("\n");
printf("Expiration Date: %s\n", defined($patron->expire_date) ? $patron->expire_date : "N/A");
print("Status:");
my $patron_ok = 1;
my @penalties = @{$patron->standing_penalties};
if ($patron->barred eq 't') {
print(" Barred\n");
$patron_ok = 0;
} elsif ($patron->active eq 'f') {
print(" Inactive\n");
$patron_ok = 0;
} elsif ($#penalties > -1) {
my $penalty;
foreach $penalty (@penalties) {
if (defined($penalty->standing_penalty->block_list)) {
my @block_list = split(/\|/, $penalty->standing_penalty->block_list);
foreach my $block (@block_list) {
foreach my $block_on (@$block_types) {
if ($block eq $block_on) {
printf(" %s", $penalty->standing_penalty->name);
$patron_ok = 0;
}
last unless ($patron_ok);
}
last unless ($patron_ok);
}
}
}
print("\n") unless ($patron_ok);
} elsif (profile_barred($patron->profile)) {
print(" Barred\n");
$patron_ok = 0;
} elsif ($patron->juvenile eq 't') {
print(" Juvenile\n");
# We lie, just to keep Active from also printing.
$patron_ok = 0;
}
if ($patron_ok) {
print(" Active\n");
}
print("\n");
} else {
print("PATRON_NOT_FOUND\n\n");
next;
}
} elsif ($input eq '2') {
my $c = prompt('copy');
next if ($c =~ /^[qQ]$/);
print("\n");
my $r = bre_from_barcode($c);
if (!defined($r)) {
print("COPY_BARCODE_NOT_FOUND\n\n");
next;
} elsif (blessed($r)) {
printf("%d\n\n", $r->id);
} elsif (ref($r) && reftype($r) eq 'HASH') {
printf("%s\n\n", $r->{textcode});
}
} elsif ($input eq '3') {
print_hold_identifier_menu();
my $which = prompt('hold');
next if ($which =~ /^[qQ]$/);
my $bib = 0;
my $copy = 0;
if ($which eq '1') {
$bib = prompt('bib');
next if ($bib =~ /^[qQ]$/);
} else {
my $c = prompt('copy');
next if ($c =~ /^[qQ]$/);
$copy = copy_from_barcode($c);
if (!blessed($copy) && ref($copy) && reftype($copy) eq 'HASH') {
printf("\n%s\n\n", $copy->{textcode});
next;
}
}
my $pcode = prompt('patron');
next if ($pcode =~ /^[qQ]$/);
my $pid = user_id_from_barcode($pcode);
if (!defined($pid) || (ref($pid) && reftype($pid) eq 'HASH')) {
print("\nPATRON_NOT_FOUND\n\n");
next;
}
my $patron = flesh_user($pid);
my $pickup = prompt('pickup');
next if ($pickup =~ /^[qQ]$/);
$pickup = org_unit_from_shortname($pickup);
if (ref($pickup) eq 'HASH') {
printf("\n%s\n\n", $pickup->{textcode});
next;
} elsif (!can_have_users($pickup)) {
print("\nNOT_A_VALID_PICKUP_POINT\n\n");
next;
}
my $r;
if ($bib) {
$r = place_hold('T', $bib, $patron, $pickup);
} elsif ($copy) {
$r = place_hold('C', $copy, $patron, $pickup);
}
print("\n$r\n\n");
} elsif ($input eq '4') {
my $p = prompt('patron');
next if ($p =~ /^[qQ]$/);
my $c = prompt('copy');
next if ($c =~ /^[qQ]$/);
my $r = checkout($c, $p);
if ($r eq 'COPY_IN_TRANSIT') {
my $copy = copy_from_barcode($c);
my $transit = fetch_transit_by_copy($copy);
if ($transit->dest == $ou->id) {
my $rez = receive_copy_transit($copy);
$r = checkout($c, $p) if ($rez->{textcode} eq 'SUCCESS');
}
elsif ($transit->source == $ou->id) {
$r = abort_copy_transit($copy);
$r = checkout($c, $p);
}
}
print("\n$r\n\n");
} elsif ($input eq '5') {
my $c = prompt('copy');
next if ($c =~ /^[Qq]$/);
my $r = checkin($c, $ou);
print("\n$r\n\n");
} elsif ($input eq '6') {
my $t = prompt('title');
my $cn = prompt('callnumber');
next if ($cn =~ /^[Qq]$/);
my $c = prompt('copy');
next if ($c =~ /^[Qq]$/);
my $r = create_copy($t, $cn, $c, $ou);
print("\n$r\n\n");
} elsif ($input eq '7') {
my $c = prompt('copy');
next if ($c =~ /^[Qq]$/);
my $copy = copy_from_barcode($c);
if (ref($copy) eq 'HASH') {
printf("\n%s\n\n", $copy->{textcode});
next;
}
print("ASSET_COPY_NOT_FOUND\n\n") unless ($copy && $copy->circ_lib == $ou->id);
my $r = delete_copy($copy);
print("\n$r\n\n");
}
}
# Clear any SIGALRM timers.
alarm(0);
logout();
} else {
die("Hes's dead, Jim.");
}
# Functions to print the menus/command prompts:
# Print a prompt, wait for and validate input.
sub prompt {
my $name = shift;
my @info = @{ $prompts{$name} };
my $input;
# Reset our SIGALRM timer:
alarm($timeout);
while (1) {
printf("%s ", $info[0]);
$input = <STDIN>;
chomp($input);
if ($input =~ /$info[1]/) {
last;
} else {
printf("%s\n", $info[2]);
}
}
return $input;
}
# The main menu
sub print_main_menu {
my $text =<<EOMAINMENU;
Main Menu
1. Retrieve Patron
2. Retrieve Bibliographic ID
3. Place Hold
4. Checkout Copy
5. Checkin Copy
6. Create Temporary Copy
7. Delete Temporary Copy
Q. Quit
EOMAINMENU
; # stupid perl-mode
print("$text");
}
# Place Hold Identifier Menu
sub print_hold_identifier_menu {
my $text =<<EOIDENTIFIERMENU;
Place Hold
Choose Target Identifier
1. Bibliographic Record ID
2. Copy Barcode
EOIDENTIFIERMENU
; # stupid perl-mode
print("$text");
}
# Some useful functions needed by the program.
# Login to the OpenSRF system/Evergreen.
#
# Arguments are:
# username
# password
# workstation
#
# Returns a hash with the authtoken, authtime, and expiration (time in
# seconds since 1/1/1970).
sub login {
my ($uname, $password, $workstation) = @_;
my $seed = OpenSRF::AppSession
->create('open-ils.auth')
->request('open-ils.auth.authenticate.init', $uname)
->gather(1);
return undef unless $seed;
my $response = OpenSRF::AppSession
->create('open-ils.auth')
->request('open-ils.auth.authenticate.complete',
{ username => $uname,
password => md5_hex($seed . md5_hex($password)),
type => 'staff',
workstation => $workstation })
->gather(1);
return undef unless $response;
my %result;
$result{'authtoken'} = $response->{payload}->{authtoken};
$result{'authtime'} = $response->{payload}->{authtime};
$result{'expiration'} = time() + $result{'authtime'} if (defined($result{'authtime'}));
return %result;
}
# Logout/destroy the OpenSRF session
#
# Argument is
# none
#
# Returns
# Does not return anything
sub logout {
if (time() < $session{'expiration'}) {
my $response = OpenSRF::AppSession
->create('open-ils.auth')
->request('open-ils.auth.session.delete', $session{authtoken})
->gather(1);
if ($response) {
print("Logout successful. Good-bye.\n");
exit(0);
} else {
die("Logout unsuccessful. Good-bye, anyway.");
}
}
}
# Retrieve the logged in user.
#
# Argument
#
# Returns
# User logged via issa.
sub get_session {
my $response = OpenSRF::AppSession->create('open-ils.auth')
->request('open-ils.auth.session.retrieve', $session{authtoken})->gather(1);
return $response;
}
# Get actor.org_unit from the shortname
#
# Arguments
# org_unit shortname
#
# Returns
# Fieldmapper aou object
# or HASH on error
sub org_unit_from_shortname {
check_session_time();
my ($shortname) = @_;
my $ou = OpenSRF::AppSession->create('open-ils.actor')
->request('open-ils.actor.org_unit.retrieve_by_shortname', $shortname)
->gather(1);
return $ou;
}
# Checkout a copy to a patron
#
# Arguments
# copy barcode
# patron barcode
#
# Returns
# textcode of the OSRF response.
sub checkout {
check_session_time();
my ($copy_barcode, $patron_barcode) = @_;
# Check for copy:
my $copy = copy_from_barcode($copy_barcode);
unless (defined($copy) && blessed($copy)) {
return 'COPY_BARCODE_NOT_FOUND';
}
# Check for user
my $uid = user_id_from_barcode($patron_barcode);
return 'PATRON_BARCODE_NOT_FOUND' if (ref($uid));
my $response = OpenSRF::AppSession->create('open-ils.circ')
->request('open-ils.circ.checkout.full.override', $session{authtoken},
{ copy_barcode => $copy_barcode,
patron_barcode => $patron_barcode })
->gather(1);
return $response->{textcode};
}
# Check a copy in at an org_unit
#
# Arguments
# copy barcode
# org_unit
#
# Returns
# "SUCCESS" on success
# textcode of a failed OSRF request
# 'COPY_NOT_CHECKED_OUT' when the copy is not checked out or not
# checked out to the user's work_ou
sub checkin {
check_session_time();
my ($barcode, $where) = @_;
my $copy = copy_from_barcode($barcode);
return $copy->{textcode} unless (blessed $copy);
return 'COPY_NOT_CHECKED_OUT' unless ($copy->status == OILS_COPY_STATUS_CHECKED_OUT);
my $e = new_editor(authtoken=>$session{authtoken});
return $e->event->{textcode} unless ($e->checkauth);
my $circ = $e->search_action_circulation([ { target_copy => $copy->id, xact_finish => undef } ])->[0];
return 'COPY_NOT_CHECKED_OUT' unless (defined($circ) && $circ->circ_lib == $where->id);
my $r = OpenSRF::AppSession->create('open-ils.circ')
->request('open-ils.circ.checkin', $session{authtoken}, { barcode => $barcode, void_overdues => 1 })
->gather(1);
return 'SUCCESS' if ($r->{textcode} eq 'ROUTE_ITEM');
return $r->{textcode};
}
# Abort a copy transit
#
# Arguments
# copy
#
# Returns the OSRF response, which could be a scalar or a hash
sub abort_copy_transit {
check_session_time();
my ($copy) = @_;
my $response = OpenSRF::AppSession->create('open-ils.circ')
->request('open-ils.circ.transit.abort', $session{authtoken},
{ copyid => $copy->id })
->gather(1);
return $response;
}
# Receive a copy transit
#
# Arguments
# copy
#
# Returns the OSRF response, which could be a scalar or a hash
sub receive_copy_transit {
check_session_time();
my ($copy) = @_;
my $response = OpenSRF::AppSession->create('open-ils.circ')
->request('open-ils.circ.copy_transit.receive', $session{authtoken},
{ copyid => $copy->id })
->gather(1);
return $response;
}
# Fetch a transit object by copy
#
# Arguments
# copy
#
# Returns
# transit object
# or event hash on error
sub fetch_transit_by_copy {
check_session_time();
my ($copy) = @_;
my $response = OpenSRF::AppSession->create('open-ils.circ')
->request('open-ils.circ.open_copy_transit.retrieve', $session{authtoken}, $copy->id)
->gather(1);
return $response;
}
# Get biblio.record_entry from asset.copy.barcode.
# Arguments
# copy barcode
#
# Returns
# biblio.record_entry fieldmapper object or
# hash on error
sub bre_from_barcode {
check_session_time();
my ($barcode) = @_;
my $response = OpenSRF::AppSession->create('open-ils.storage')
->request('open-ils.storage.biblio.record_entry.retrieve_by_barcode', $barcode)
->gather(1);
return $response;
}
# Get asset.copy from asset.copy.barcode.
# Arguments
# copy barcode
#
# Returns
# asset.copy fieldmaper object
# or hash on error
sub copy_from_barcode {
check_session_time();
my ($barcode) = @_;
my $response = OpenSRF::AppSession->create('open-ils.search')
->request('open-ils.search.asset.copy.find_by_barcode', $barcode)
->gather(1);
return $response;
}
# Get actor.usr.id from barcode.
# Arguments
# patron barcode
#
# Returns
# actor.usr.id
# or hash on error
sub user_id_from_barcode {
check_session_time();
my ($barcode) = @_;
my $response;
my $e = new_editor(authtoken=>$session{authtoken});
return $response unless ($e->checkauth);
my $card = $e->search_actor_card({barcode => $barcode, active => 't'});
return $e->event unless($card);
$response = $card->[0]->usr if (@$card);
$e->finish;
return $response;
}
# Flesh user information
# Arguments
# actor.usr.id
#
# Returns
# fieldmapped, fleshed user or
# event hash on error
sub flesh_user {
check_session_time();
my ($id) = @_;
my $response = OpenSRF::AppSession->create('open-ils.actor')
->request('open-ils.actor.user.fleshed.retrieve', $session{'authtoken'}, $id,
[ 'card', 'cards', 'standing_penalties', 'home_ou', 'profile' ])
->gather(1);
return $response;
}
# Check if a patron's profile is configured as barred.
# Arguments
# Fleshed actor.usr.profile
#
# Returns
# 1 if the profile is barred, 0 otherwise
sub profile_barred {
my ($profile) = @_;
my $barred = 0;
# Check if we got a profile.
return 1 unless($profile); # Assume patron without a profile is bad.
foreach (@{$barred_grps}) {
if ($_ == $profile->id) {
$barred = 1;
last;
}
}
unless ($barred) {
foreach (@{$barred_profiles}) {
if ($_ eq $profile->name) {
$barred = 1;
last;
}
}
}
return $barred;
}
# Place a hold for a patron.
#
# Arguments
# Type of hold
# Target object appropriate for type of hold
# Patron for whom the hold is place
# OU where hold is to be picked up
#
# Returns
# "SUCCESS" on success
# textcode of a failed OSRF request
# "HOLD_TYPE_NOT_SUPPORTED" if the hold type is not supported
# (Currently only support 'T' and 'C')
sub place_hold {
check_session_time();
my ($type, $target, $patron, $pickup_ou) = @_;
my $ou = org_unit_from_shortname($work_ou); # $work_ou is global
my $ahr = Fieldmapper::action::hold_request->new;
$ahr->hold_type($type);
if ($type eq 'C') {
# Check if we own the copy.
if ($ou->id == $target->circ_lib) {
# We own it, so let's place a copy hold.
$ahr->target($target->id);
$ahr->current_copy($target->id);
# If we have permission to place force holds, then use it.
$ahr->hold_type('F') if ($use_force);
} else {
# We don't own it, so let's place a title hold instead.
my $bib = bre_from_barcode($target->barcode);
$ahr->target($bib->id);
$ahr->hold_type('T');
$type = 'T';
}
} elsif ($type eq 'T') {
$ahr->target($target);
} else {
return "HOLD_TYPE_NOT_SUPPORTED";
}
$ahr->usr($patron->id);
$ahr->pickup_lib($pickup_ou->id);
if (!$patron->email) {
$ahr->email_notify('f');
$ahr->phone_notify($patron->day_phone) if ($patron->day_phone);
} else {
$ahr->email_notify('t');
}
# We must have a title hold and we want to change the hold
# expiration date if we're sending the copy to the VC.
set_title_hold_expiration($ahr) if ($ahr->pickup_lib == $ou->id);
my $params = { pickup_lib => $ahr->pickup_lib, patronid => $ahr->usr, hold_type => $ahr->hold_type };
if ($type eq 'C') {
$params->{copy_id} = $ahr->target;
} else {
$params->{titleid} = $ahr->target;
}
my $r = OpenSRF::AppSession->create('open-ils.circ')
->request('open-ils.circ.title_hold.is_possible', $session{authtoken}, $params)
->gather(1);
if ($r->{textcode}) {
return $r->{textcode};
} elsif ($r->{success}) {
$r = OpenSRF::AppSession->create('open-ils.circ')
->request('open-ils.circ.holds.create.override', $session{authtoken}, $ahr)
->gather(1);
my $returnValue = "SUCCESS";
if (ref($r) eq 'HASH') {
$returnValue = ($r->{textcode} eq 'PERM_FAILURE') ? $r->{ilsperm} : $r->{textcode};
$returnValue =~ s/\.override$// if ($r->{textcode} eq 'PERM_FAILURE');
}
return $returnValue;
} else {
return 'HOLD_NOT_POSSIBLE';
}
}
# Set the expiration date on title holds
#
# Argument
# Fieldmapper action.hold_request object
#
# Returns
# Nothing
sub set_title_hold_expiration {
my $hold = shift;
if ($title_holds->{unit} && $title_holds->{duration}) {
my $expiration = DateTime->now(time_zone => $tz);
$expiration->add($title_holds->{unit} => $title_holds->{duration});
$hold->expire_time($expiration->iso8601());
}
}
# Delete a copy created by URSA
#
# Argument
# Fieldmapper asset.copy object
#
# Returns
# "SUCCESS" on success
# Event textcode if an error occurs
sub delete_copy {
check_session_time();
my ($copy) = @_;
my $e = new_editor(authtoken=>$session{authtoken});
return $e->event->{textcode} unless ($e->checkauth);
# Get the calnumber
my $vol = $e->retrieve_asset_call_number($copy->call_number);
return $e->event->{textcode} unless ($vol);
# Get the biblio.record_entry
my $bre = $e->retrieve_biblio_record_entry($vol->record);
return $e->event->{textcode} unless ($bre);
# Delete everything in a transaction and rollback if anything fails.
$e->xact_begin;
my $r; # To hold results of editor calls
# If the copy is checked out, let's try to check it in.
if ($copy->status == OILS_COPY_STATUS_CHECKED_OUT) {
$r = OpenSRF::AppSession->create('open-ils.circ')
->request('open-ils.circ.checkin', $session{authtoken},
{ barcode => $copy->barcode, void_overdues => 1,
noop => 1})->gather(1);
# We don't care if it succeeds or not.
}
# If the copy is on hold and they are unfulfilled, then cancel them.
$r = $e->search_action_hold_request(
{
target => $copy->id, hold_type => ['C','F'],
cancel_time => undef, fulfillment_time => undef
});
if ($r && scalar @$r) {
foreach my $h (@$r) {
$h->cancel_time('now()');
$h->cancel_cause(1);
$h->cancel_note('Virtual catalog deleted.');
$e->update_action_hold_request($h);
}
}
# If the copy is in transit, kill the transit.
$r = $e->search_action_transit_copy(
{
target_copy => $copy->id, dest_recv_time => undef
}
);
if ($r && scalar @$r) {
foreach my $atc (@$r) {
$e->delete_action_transit_copy($atc);
}
}
$r = $e->delete_asset_copy($copy);
unless ($r) {
my $lval = $e->event->{textcode};
$e->rollback;
return $lval;
}
my $list = $e->search_asset_copy({call_number => $vol->id, deleted => 'f'});
unless (@$list) {
$r = $e->delete_asset_call_number($vol);
unless ($r) {
my $lval = $e->event->{textcode};
$e->rollback;
return $lval;
}
$list = $e->search_asset_call_number({record => $bre->id, deleted => 'f'});
unless (@$list) {
$bre->deleted('t');
$r = $e->update_biblio_record_entry($bre);
unless ($r) {
my $lval = $e->event->{textcode};
$e->rollback;
return $lval;
}
}
}
$e->commit;
return 'SUCCESS';
}
# Create a copy and marc record
#
# Arguments
# title
# call number
# copy barcode
#
# Returns
# bib id on succes
# event textcode on failure
sub create_copy {
check_session_time();
my ($title, $callnumber, $barcode, $ou) = @_;
my $e = new_editor(authtoken=>$session{authtoken});
return $e->event->{textcode} unless ($e->checkauth);
my $r = $e->allowed(['CREATE_COPY', 'CREATE_MARC', 'CREATE_VOLUME']);
if (ref($r) eq 'HASH') {
return $r->{textcode} . ' ' . $r->{ilsperm};
}
# Check if the barcode exists in asset.copy and bail if it does.
my $list = $e->search_asset_copy({deleted => 'f', barcode => $barcode});
if (@$list) {
$e->finish;
return 'BARCODE_EXISTS';
}
# Create MARC record
my $record = MARC::Record->new();
$record->encoding('UTF-8');
$record->leader('00881nam a2200193 4500');
my $datespec = strftime("%Y%m%d%H%M%S.0", localtime);
my @fields = ();
push(@fields, MARC::Field->new('005', $datespec));
push(@fields, MARC::Field->new('082', '0', '4', 'a' => $callnumber));
push(@fields, MARC::Field->new('245', '0', '0', 'a' => $title));
$record->append_fields(@fields);
# Convert the record to XML
my $xml = convert2marcxml($record);
my $bre = OpenSRF::AppSession->create('open-ils.cat')
->request('open-ils.cat.biblio.record.xml.import', $session{authtoken}, $xml, $bre_source, 1)
->gather(1);
return $bre->{textcode} if (ref($bre) eq 'HASH');
# Create volume record
my $vol = OpenSRF::AppSession->create('open-ils.cat')
->request('open-ils.cat.call_number.find_or_create', $session{authtoken}, $callnumber, $bre->id, $ou->id)
->gather(1);
return $vol->{textcode} if ($vol->{textcode});
# Retrieve the user
my $user = get_session;
# Create copy record
my $copy = Fieldmapper::asset::copy->new();
$copy->barcode($barcode);
$copy->call_number($vol->{acn_id});
$copy->circ_lib($ou->id);
$copy->circulate('t');
$copy->holdable('t');
$copy->opac_visible('f');
$copy->deleted('f');
$copy->fine_level(2);
$copy->loan_duration(2);
$copy->location(1);
$copy->status(0);
$copy->editor($user->id);
$copy->creator($user->id);
# If we have permissions to use force holds, then we'll create the
# copies as not holdable. This way, staff at member libraries
# can't place holds on VC copies that will never fill.
$copy->holdable('f') if ($use_force);
$e->xact_begin;
$copy = $e->create_asset_copy($copy);
# Add the configured stat cat entries.
my $nodes = $xpath->find("/issa/copy/stat_cat_entry");
if ($nodes && $nodes->get_nodelist) {
my @stat_cats = ();
foreach my $node ($nodes->get_nodelist) {
next unless ($node->isa('XML::XPath::Node::Element'));
my $stat_cat_id = $node->getAttribute('stat_cat');
my $value = $node->string_value();
# Need to search for an existing asset.stat_cat_entry
my $search_result;
my $asce;
$search_result = $e->search_asset_stat_cat_entry({'stat_cat' => $stat_cat_id, 'value' => $value});
if ($search_result) {
$asce = $search_result->[0];
} else {
# check if the stat_cat exists.
$search_result = $e->search_asset_stat_cat({'id' => $stat_cat_id});
next unless($search_result);
}
unless ($asce) {
# if not, create a new one and use its id.
$asce = Fieldmapper::asset::stat_cat_entry->new();
$asce->stat_cat($stat_cat_id);
$asce->value($value);
$asce->owner($ou->id);
$asce = $e->create_asset_stat_cat_entry($asce);
}
push(@stat_cats, $asce);