1 package OpenILS::SIP::Transaction::Checkin;
2 use warnings; use strict;
4 use POSIX qw(strftime);
5 use Sys::Syslog qw(syslog);
7 use Time::HiRes q/time/;
10 use OpenILS::SIP::Transaction;
11 use OpenILS::Const qw/:const/;
12 use OpenILS::Application::AppUtils;
13 my $U = 'OpenILS::Application::AppUtils';
15 use base qw(OpenILS::SIP::Transaction);
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 => "",
34 my $self = $class->SUPER::new(@_); # start with an Transaction object
36 foreach (keys %fields) {
37 $self->{_permitted}->{$_} = $fields{$_}; # overlaying _permitted
40 @{$self}{keys %fields} = values %fields; # copying defaults into object
42 $self->load_override_events;
44 return bless $self, $class;
49 return 0 if !$self->{item};
50 return !$self->{item}->magnetic;
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;
66 my ($sip_handler, $inst_id, $trans_date, $return_date, $current_loc, $item_props) = @_; # most unused
68 unless($self->{item}) {
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;
79 my $args = {barcode => $self->{item}->id};
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;
88 if($current_loc) { # SIP client specified a physical location
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];
94 $org_sn_cache{$current_loc} = $org_id;
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;
101 my ($resp, $txt, $code);
105 my $method = 'open-ils.circ.checkin';
106 $method .= '.override' if $override;
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));
113 my $s = Dumper($resp);
115 syslog('LOG_INFO', "OILS: Checkin response: $s");
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} : '';
126 if ( $override_events{$txt} ) {
133 syslog('LOG_INFO', "OILS: Checkin resulted in event: $txt, phys_location: $phys_location");
135 $resp->{org} &&= OpenILS::SIP::shortname_from_id($resp->{org}); # Convert id to shortname
137 $self->destination_loc($resp->{org}) if $resp->{org};
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
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
148 my $payload = $resp->{payload} || {};
152 if(ref $payload eq 'HASH') {
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});
159 } elsif ($payload->{hold}) {
160 $self->item->hold($payload->{hold});
163 $circ = $resp->{payload}->{circ} || '';
164 $copy = $resp->{payload}->{copy} || '';
167 if ($self->item->hold) {
168 my ($pickup_lib_id, $pickup_lib_sn);
170 my $holder = OpenILS::SIP->editor()->retrieve_actor_user(
171 [$self->item->hold->usr, {flesh => 1, flesh_fields => {au => ['card']}}]);
173 my $holder_name = OpenILS::SIP::Patron::format_name($holder);
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;
180 $pickup_lib_id = $self->item->hold->pickup_lib;
181 $pickup_lib_sn = OpenILS::SIP::shortname_from_id($pickup_lib_id);
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);
188 my $atype = ($pickup_lib_id == $phys_location) ? '01' : '02';
189 $self->alert_type($atype);
192 $self->alert(1) if defined $self->alert_type; # alert_type could be "00", hypothetically
195 $self->{circ_user_id} = $circ->usr;
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
201 $self->alert_type('00') unless $self->alert_type; # wasn't checked out, but *something* changed
202 # $self->ok(0); # maybe still ok?
209 Successful Checkin event payload includes:
210 $payload->{copy} (unfleshed)
214 $payload->{cancelled_hold_transit}
220 ASSET_COPY_NOT_FOUND => ,
223 CIRC_CLAIMS_RETURNED => ,
224 COPY_ALERT_MESSAGE => ,
225 COPY_STATUS_LOST => ,
226 COPY_STATUS_MISSING => ,
228 ITEM_DEPOSIT_PAID => ,
230 DATABASE_UPDATE_FAILED => ,
231 DATABASE_QUERY_FAILED => ,
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)