]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/Checkout.pm
Merge branch 'master' of git://git.evergreen-ils.org/Evergreen into ttopac
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / SIP / Transaction / Checkout.pm
1 #
2 # An object to handle checkout status
3 #
4
5 package OpenILS::SIP::Transaction::Checkout;
6
7 use warnings;
8 use strict;
9
10 use POSIX qw(strftime);
11
12 use OpenILS::SIP;
13 use OpenILS::SIP::Transaction;
14 use OpenILS::SIP::Msg qw/:const/;
15 use Sys::Syslog qw(syslog);
16
17 use OpenILS::Application::AppUtils;
18 my $U = 'OpenILS::Application::AppUtils';
19
20
21 our @ISA = qw(OpenILS::SIP::Transaction);
22
23 # Most fields are handled by the Transaction superclass
24 my %fields = (
25               security_inhibit => 0,
26               due              => undef,
27               renew_ok         => 0,
28              );
29
30 sub new {
31     my $class = shift;
32
33     my $self = $class->SUPER::new(@_);
34
35     my $element;
36
37     foreach $element (keys %fields) {
38         $self->{_permitted}->{$element} = $fields{$element};
39     }
40
41     @{$self}{keys %fields} = values %fields;
42
43     $self->load_override_events;
44
45     return bless $self, $class;
46 }
47
48 # Lifted from Checkin.pm to load the list of events that we'll try to
49 # override if they occur during checkout or renewal.
50 my %override_events;
51 sub load_override_events {
52     return if %override_events;
53     my $override = OpenILS::SIP->config->{implementation_config}->{checkout_override};
54     return unless $override;
55     my $events = $override->{event};
56     $events = [$events] unless ref $events eq 'ARRAY';
57     $override_events{$_} = 1 for @$events;
58 }
59
60 # if this item is already checked out to the requested patron,
61 # renew the item and set $self->renew_ok to true.
62 # XXX if it's a renewal and the renewal is not permitted, set
63 # $self->screen_msg("Item on Hold for Another User"); (or somesuch)
64 # XXX Set $self->ok(0) on any errors
65 sub do_checkout {
66     my $self = shift;
67     my $is_renew = shift || 0;
68
69     $self->ok(0);
70
71     my $args = {
72                 barcode => $self->{item}->id,
73                 patron_barcode => $self->{patron}->id
74                };
75
76     my ($resp, $method);
77
78     my $override = 0;
79
80     while (1) {
81         if ($is_renew) {
82             $method = 'open-ils.circ.renew';
83             $method .= '.override' if ($override);
84             $resp = $U->simplereq('open-ils.circ', $method, $self->{authtoken}, $args);
85         } else {
86             $method = 'open-ils.circ.checkout.permit';
87             $method .= '.override' if ($override);
88             $resp = $U->simplereq('open-ils.circ', $method, $self->{authtoken}, $args);
89
90             $resp = [$resp] unless ref $resp eq 'ARRAY';
91
92             syslog('LOG_DEBUG', "OILS: $method returned event: " . OpenSRF::Utils::JSON->perl2JSON($resp));
93
94             if (@$resp == 1 && !$U->event_code($$resp[0])) {
95                 my $key = $$resp[0]->{payload};
96                 syslog('LOG_INFO', "OILS: circ permit key => $key");
97                 # --------------------------------------------------------------------
98                 # Now do the actual checkout
99                 # --------------------------------------------------------------------
100                 my $cko_args = $args;
101                 $cko_args->{permit_key} = $key;
102                 $method = 'open-ils.circ.checkout';
103                 $resp = $U->simplereq('open-ils.circ', $method, $self->{authtoken}, $cko_args);
104             } else {
105                 # We got one or more non-success events
106                 $self->screen_msg('');
107                 for my $r (@$resp) {
108                     if ( my $code = $U->event_code($r) ) {
109                         my $txt = $r->{textcode};
110                         syslog('LOG_INFO', "OILS: $method failed with event $code : $txt");
111
112                         if ($override_events{$txt} && $method !~ /override$/) {
113                             # Found an event we've been configured to override.
114                             $override = 1;
115                         } elsif ( $txt eq 'OPEN_CIRCULATION_EXISTS' ) {
116                             $self->screen_msg(OILS_SIP_MSG_CIRC_EXISTS);
117                             return 0;
118                         } else {
119                             $self->screen_msg(OILS_SIP_MSG_CIRC_PERMIT_FAILED);
120                             return 0;
121                         }
122                     }
123                 }
124                 # This looks potentially dangerous, but we shouldn't
125                 # end up here if the loop iterated with $override = 1;
126                 next if ($override && $method !~ /override$/);
127             }
128         }
129         syslog('LOG_INFO', "OILS: $method returned event: " . OpenSRF::Utils::JSON->perl2JSON($resp));
130         # XXX Check for events
131         if ( $resp ) {
132
133             if ( my $code = $U->event_code($resp) ) {
134                 my $txt = $resp->{textcode};
135                 if ($override_events{$txt} && $method !~ /override$/) {
136                     $override = 1;
137                 } else {
138                     syslog('LOG_INFO', "OILS: $method failed with event $code : $txt");
139                     $self->screen_msg('Checkout failed.  Please contact a librarian');
140                     last;
141                 }
142             } else {
143                 syslog('LOG_INFO', "OILS: $method succeeded");
144
145                 my $circ = $resp->{payload}->{circ};
146                 $self->{'due'} = OpenILS::SIP->format_date($circ->due_date, 'due');
147                 $self->ok(1);
148                 last;
149             }
150
151         }
152         last if ($method =~ /override$/);
153     }
154
155
156     return $self->ok;
157 }
158
159
160
161 1;