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