]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP/Transaction/Checkin.pm
b466a73abfae98049f4e3f021b9fe182dfd93016
[working/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 !$self->{item}->magnetic;
53 }
54
55 sub do_checkin {
56     my $self = shift;
57     my ($inst_id, $trans_date, $return_date, $current_loc, $item_props) = @_; # most unused
58     $inst_id ||= '';
59
60     my $resp = $U->simplereq(
61         'open-ils.circ',
62         'open-ils.circ.checkin',
63         $self->{authtoken},
64         { barcode => $self->{item}->id }
65     );
66
67     if ($debug) {
68         open (DUMP, ">/tmp/sip_do_checkin.dump");
69         print DUMP Dumper($resp);
70         close DUMP;
71     }
72
73     my $code = $U->event_code($resp);
74     my $txt  = $code ? $resp->{textcode} : '';
75
76     $resp->{org} &&= OpenILS::SIP::shortname_from_id($resp->{org}); # Convert id to shortname
77
78     $self->destination_loc($resp->{org}) if $resp->{org};
79
80     $debug and warn "Checkin textcode: $txt, org: " . ($resp->{org} || '');
81
82     if ($txt eq 'ROUTE_ITEM') {
83         # $self->destination_loc($resp->{org});   # org value already converted and added (above)
84         $self->alert_type('04');            # send to other branch
85     }
86     elsif ($txt and $txt ne 'NO_CHANGE' and $txt ne 'SUCCESS') {
87         syslog('LOG_WARNING', "OILS: Checkin returned unrecognized event $code : $txt");
88         # $self->ok(0);   # maybe still ok?
89         $self->alert_type('00');            # unknown
90     }
91     
92     my $payload = $resp->{payload} || {};
93
94     # Two places to look for hold data.  These are more important and more definitive than above.
95     if ($payload->{remote_hold}) {
96         $self->item->hold($payload->{remote_hold});     # actually only used for checkin at non-owning branch w/ hold at same branch
97     }
98     elsif ($payload->{hold}) {
99         $self->item->hold($payload->{hold});
100     }
101
102     if ($self->item->hold) {
103         my $holder = OpenILS::SIP->find_patron('usr' => $self->item->hold->usr)
104             or warn "OpenILS::SIP->find_patron cannot find hold usr => '" . $self->item->hold->usr . "'";
105         $self->item->hold_patron_bcode( $holder->id   );
106         $self->item->hold_patron_name(  $holder->name );     # Item already had the holder ID, we really just needed the name
107         $self->item->destination_loc( OpenILS::SIP::shortname_from_id($self->item->hold->pickup_lib) );   # must use pickup_lib as method
108         my $atype = ($self->item->destination_loc eq $inst_id)  ? '01' : '02';
109         $self->alert_type($atype);
110     }
111
112     $self->alert(1) if defined $self->alert_type;  # alert_type could be "00", hypothetically
113
114     my $circ = $resp->{payload}->{circ} || '';
115     my $copy = $resp->{payload}->{copy} || '';
116     if ($copy) {
117         ref($copy->call_number) and $self->item->call_number( $copy->call_number->label );
118         # ref($copy->location   ) and $self->item->collection_code($copy->location->name);
119         # This is misleading because if there is a hold we don't want to point back to the owning library OR its location.
120     }
121
122     if ( $circ ) {
123         # $self->item->{patron} = OpenILS::SIP::patron_barcode_from_id($circ->usr);     # Item.pm already does this for us!
124         $self->ok(1);
125     } elsif ($txt eq 'NO_CHANGE' or $txt eq 'SUCCESS' or $txt eq 'ROUTE_ITEM') {
126         $self->ok(1);       # NO_CHANGE means it wasn't checked out anyway, no problem
127     } else {
128         $self->alert(1);
129         $self->alert_type('00') unless $self->alert_type;   # wasn't checked out, but *something* changed
130         # $self->ok(0);     # maybe still ok?
131     }
132 }
133
134 1;
135 __END__
136
137 Successful Checkin event payload includes:
138     $payload->{copy}   (unfleshed)
139     $payload->{record} 
140     $payload->{circ}   
141     $payload->{transit}
142     $payload->{cancelled_hold_transit}
143     $payload->{hold}   
144     $payload->{patron} 
145
146 Some EVENT strings:
147     SUCCESS                => ,
148     ASSET_COPY_NOT_FOUND   => ,
149     NO_CHANGE              => ,
150     PERM_FAILURE           => ,
151     CIRC_CLAIMS_RETURNED   => ,
152     COPY_ALERT_MESSAGE     => ,
153     COPY_STATUS_LOST       => ,
154     COPY_STATUS_MISSING    => ,
155     COPY_BAD_STATUS        => ,
156     ITEM_DEPOSIT_PAID      => ,
157     ROUTE_ITEM             => ,
158     DATABASE_UPDATE_FAILED => ,
159     DATABASE_QUERY_FAILED  => ,
160
161 # alert_type:
162 #   00 - Unknown
163 #   01 - hold in local library
164 #   02 - hold for other branch
165 #   03 - hold for ILL (not used in EG)
166 #   04 - send to other branch (no hold)
167 #   99 - Other