]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/Checkin.pm
Add cointainer() and record_list() filters to QueryParser
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / SIP / Transaction / Checkin.pm
1 package OpenILS::SIP::Transaction::Checkin;
2 use warnings; use strict;
3
4 use POSIX qw(strftime);
5 use Sys::Syslog qw(syslog);
6 use Data::Dumper;
7 use Time::HiRes q/time/;
8
9 use OpenILS::SIP;
10 use OpenILS::SIP::Transaction;
11 use OpenILS::Const qw/:const/;
12 use OpenILS::Application::AppUtils;
13 my $U = 'OpenILS::Application::AppUtils';
14
15 use base qw(OpenILS::SIP::Transaction);
16
17 my $debug = 0;
18
19 my %fields = (
20     magnetic => 0,
21     sort_bin => undef,
22     # 3M extensions: (most of the data is stored under Item)
23 #   collection_code  => undef,
24 #   call_number      => undef,
25     destination_loc  => undef,
26     alert_type       => undef,  # 00,01,02,03,04 or 99
27 #   hold_patron_id   => undef,
28 #   hold_patron_name => "",
29 #   hold             => undef,
30 );
31
32 sub new {
33     my $class = shift;;
34     my $self = $class->SUPER::new(@_);              # start with an Transaction object
35
36     foreach (keys %fields) {
37         $self->{_permitted}->{$_} = $fields{$_};    # overlaying _permitted
38     }
39
40     @{$self}{keys %fields} = values %fields;        # copying defaults into object
41
42     $self->load_override_events;
43
44     return bless $self, $class;
45 }
46
47 sub resensitize {
48     my $self = shift;
49     return 0 if !$self->{item};
50     return !$self->{item}->magnetic;
51 }
52
53 my %override_events;
54 sub load_override_events {
55     return if %override_events;
56     my $override = OpenILS::SIP->config->{implementation_config}->{checkin_override};
57     return unless $override;
58     my $events = $override->{event};
59     $events = [$events] unless ref $events eq 'ARRAY';
60     $override_events{$_} = 1 for @$events;
61 }
62
63 my %org_sn_cache;
64 sub do_checkin {
65     my $self = shift;
66     my ($sip_handler, $inst_id, $trans_date, $return_date, $current_loc, $item_props) = @_; # most unused
67
68     unless($self->{item}) {
69         $self->ok(0);
70         return undef;
71     }
72
73     $inst_id ||= '';
74
75     # physical location defaults to ws ou of the logged in sip user,
76     # which currently defaults to home_ou, since ws's aren't used.
77     my $phys_location = $sip_handler->{login_session}->ws_ou;
78
79     my $args = {barcode => $self->{item}->id};
80
81     if($return_date) {
82         # SIP date format is YYYYMMDD.  Translate to ISO8601
83         $return_date =~ s/(\d{4})(\d{2})(\d{2}).*/$1-$2-$3/;
84         syslog('LOG_INFO', "Checking in with backdate $return_date");
85         $args->{backdate} = $return_date;
86     }
87
88     if($current_loc) { # SIP client specified a physical location
89
90         my $org_id = (defined $org_sn_cache{$current_loc}) ? 
91             $org_sn_cache{$current_loc} :
92             OpenILS::SIP->editor()->search_actor_org_unit({shortname => $current_loc}, {idlist => 1})->[0];
93
94         $org_sn_cache{$current_loc} = $org_id;
95
96         # if the caller specifies a physical location, use it as the checkin circ lib
97         $args->{circ_lib} = $phys_location = $org_id if defined $org_id;
98     }
99
100     my $override = 0;
101     my ($resp, $txt, $code);
102
103     while(1) {
104
105         my $method = 'open-ils.circ.checkin';
106         $method .= '.override' if $override;
107
108         my $start_time = time();
109         $resp = $U->simplereq('open-ils.circ', $method, $self->{authtoken}, $args);
110         syslog('LOG_INFO', "OILS: Checkin API call took %0.3f seconds", (time() - $start_time));
111
112         if ($debug) {
113             my $s = Dumper($resp);
114             $s =~ s/\n//mog;
115             syslog('LOG_INFO', "OILS: Checkin response: $s");
116         }
117
118         # In oddball cases, we can receive an array of events.
119         # The first event received should be treated as the main result.
120         $resp = $$resp[0] if ref($resp) eq 'ARRAY';
121         $code = $U->event_code($resp);
122         $txt  = (defined $code) ? $resp->{textcode} : '';
123
124         last if $override;
125
126         if ( $override_events{$txt} ) {
127             $override = 1;
128         } else {
129             last;
130         }
131     }
132
133     syslog('LOG_INFO', "OILS: Checkin resulted in event: $txt, phys_location: $phys_location");
134
135     $resp->{org} &&= OpenILS::SIP::shortname_from_id($resp->{org}); # Convert id to shortname
136
137     $self->destination_loc($resp->{org}) if $resp->{org};
138
139     if ($txt eq 'ROUTE_ITEM') {
140         # Note, this alert_type will be overridden below if this is a hold transit
141         $self->alert_type('04'); # send to other branch
142
143     } elsif ($txt and $txt ne 'NO_CHANGE' and $txt ne 'SUCCESS') {
144         syslog('LOG_WARNING', "OILS: Checkin returned unexpected event $code : $txt");
145         $self->alert_type('00'); # unknown
146     }
147     
148     my $payload = $resp->{payload} || {};
149
150     my ($circ, $copy);
151
152     if(ref $payload eq 'HASH') {
153
154         # Two places to look for hold data.  These are more important and more definitive than above.
155         if ($payload->{remote_hold}) {
156             # actually only used for checkin at non-owning branch w/ hold at same branch
157             $self->item->hold($payload->{remote_hold});     
158
159         } elsif ($payload->{hold}) {
160             $self->item->hold($payload->{hold});
161         }
162
163         $circ = $resp->{payload}->{circ} || '';
164         $copy = $resp->{payload}->{copy} || '';
165     }
166
167     if ($self->item->hold) {
168         my ($pickup_lib_id, $pickup_lib_sn);
169
170         my $holder = OpenILS::SIP->editor()->retrieve_actor_user(
171             [$self->item->hold->usr, {flesh => 1, flesh_fields => {au => ['card']}}]);
172
173         my $holder_name = OpenILS::SIP::Patron::format_name($holder);
174
175         if (ref $self->item->hold->pickup_lib) {
176             $pickup_lib_id = $self->item->hold->pickup_lib->id;
177             $pickup_lib_sn = $self->item->hold->pickup_lib->shortname;
178
179         } else {
180             $pickup_lib_id = $self->item->hold->pickup_lib;
181             $pickup_lib_sn = OpenILS::SIP::shortname_from_id($pickup_lib_id);
182         }
183
184         $self->item->hold_patron_bcode( ($holder->card) ? $holder->card->barcode : '');
185         $self->item->hold_patron_name($holder_name);
186         $self->item->destination_loc($pickup_lib_sn); 
187
188         my $atype = ($pickup_lib_id == $phys_location) ? '01' : '02';
189         $self->alert_type($atype);
190     }
191
192     $self->alert(1) if defined $self->alert_type;  # alert_type could be "00", hypothetically
193
194     if ( $circ ) {
195         $self->{circ_user_id} = $circ->usr;
196         $self->ok(1);
197     } elsif ($txt eq 'NO_CHANGE' or $txt eq 'SUCCESS' or $txt eq 'ROUTE_ITEM') {
198         $self->ok(1); # NO_CHANGE means it wasn't checked out anyway, no problem
199     } else {
200         $self->alert(1);
201         $self->alert_type('00') unless $self->alert_type; # wasn't checked out, but *something* changed
202         # $self->ok(0); # maybe still ok?
203     }
204 }
205
206 1;
207 __END__
208
209 Successful Checkin event payload includes:
210     $payload->{copy}   (unfleshed)
211     $payload->{record} 
212     $payload->{circ}   
213     $payload->{transit}
214     $payload->{cancelled_hold_transit}
215     $payload->{hold}   
216     $payload->{patron} 
217
218 Some EVENT strings:
219     SUCCESS                => ,
220     ASSET_COPY_NOT_FOUND   => ,
221     NO_CHANGE              => ,
222     PERM_FAILURE           => ,
223     CIRC_CLAIMS_RETURNED   => ,
224     COPY_ALERT_MESSAGE     => ,
225     COPY_STATUS_LOST       => ,
226     COPY_STATUS_MISSING    => ,
227     COPY_BAD_STATUS        => ,
228     ITEM_DEPOSIT_PAID      => ,
229     ROUTE_ITEM             => ,
230     DATABASE_UPDATE_FAILED => ,
231     DATABASE_QUERY_FAILED  => ,
232
233 # alert_type:
234 #   00 - Unknown
235 #   01 - hold in local library
236 #   02 - hold for other branch
237 #   03 - hold for ILL (not used in EG)
238 #   04 - send to other branch (no hold)
239 #   99 - Other