2 # ILS.pm: Koha ILS interface module
9 use Sys
::Syslog
qw(syslog);
12 use C4
::SIP
::ILS
::Item
;
13 use C4
::SIP
::ILS
::Patron
;
14 use C4
::SIP
::ILS
::Transaction
;
15 use C4
::SIP
::ILS
::Transaction
::Checkout
;
16 use C4
::SIP
::ILS
::Transaction
::Checkin
;
17 use C4
::SIP
::ILS
::Transaction
::FeePayment
;
18 use C4
::SIP
::ILS
::Transaction
::Hold
;
19 use C4
::SIP
::ILS
::Transaction
::Renew
;
20 use C4
::SIP
::ILS
::Transaction
::RenewAll
;
25 'magnetic media' => 1,
26 'security inhibit' => 0,
27 'offline operation' => 0,
28 "patron status request" => 1,
34 "patron information" => 1,
35 "end patron session" => 1,
37 "item information" => 1,
38 "item status update" => 0,
46 my ($class, $institution) = @_;
47 my $type = ref($class) || $class;
49 $debug and warn "new ILS: INSTITUTION: " . Dumper
($institution);
50 syslog
("LOG_DEBUG", "new ILS '%s'", $institution->{id
});
51 $self->{institution
} = $institution;
52 return bless $self, $type;
57 $debug and warn "ILS: finding patron";
58 return C4
::SIP
::ILS
::Patron
->new(@_);
63 $debug and warn "ILS: finding item";
64 return C4
::SIP
::ILS
::Item
->new(@_);
69 return $self->{institution
}->{id
}; # consider making this return the whole institution
74 return $self->{institution
}->{id
};
79 return (exists($supports{$op}) && $supports{$op});
83 my ($self, $id, $whence) = @_;
84 if ($id ne $self->{institution
}->{id
}) {
85 syslog
("LOG_WARNING", "%s: received institution '%s', expected '%s'", $whence, $id, $self->{institution
}->{id
});
86 # Just an FYI check, we don't expect the user to change location from that in SIPconfig.xml
92 # If it's defined, and matches a true sort of string, or is
93 # a non-zero number, then it's true.
94 defined($bool) or return; # false
95 ($bool =~ /true|y|yes/i) and return 1; # true
96 return ($bool =~ /^\d+$/ and $bool != 0); # true for non-zero numbers, false otherwise
101 return (exists($self->{institution
}->{policy
}->{checkout
})
102 && to_bool
($self->{institution
}->{policy
}->{checkout
}));
106 return (exists($self->{institution
}->{policy
}->{checkin
})
107 && to_bool
($self->{institution
}->{policy
}->{checkin
}));
109 sub status_update_ok
{
111 return (exists($self->{institution
}->{policy
}->{status_update
})
112 && to_bool
($self->{institution
}->{policy
}->{status_update
}));
116 return (exists($self->{institution
}->{policy
}->{offline
})
117 && to_bool
($self->{institution
}->{policy
}->{offline
}));
121 # Checkout(patron_id, item_id, sc_renew):
122 # patron_id & item_id are the identifiers send by the terminal
123 # sc_renew is the renewal policy configured on the terminal
124 # returns a status opject that can be queried for the various bits
125 # of information that the protocol (SIP or NCIP) needs to generate
129 my ($self, $patron_id, $item_id, $sc_renew, $fee_ack) = @_;
130 my ($patron, $item, $circ);
132 $circ = C4
::SIP
::ILS
::Transaction
::Checkout
->new();
134 $circ->patron($patron = C4
::SIP
::ILS
::Patron
->new( $patron_id));
135 $circ->item($item = C4
::SIP
::ILS
::Item
->new( $item_id));
137 $circ->fee_ack($fee_ack);
141 $circ->screen_msg("Invalid Patron");
142 } elsif (!$patron->charge_ok) {
143 $circ->screen_msg("Patron Blocked");
145 $circ->screen_msg("Invalid Item");
146 # holds checked inside do_checkout
147 # } elsif ($item->hold_queue && @{$item->hold_queue} && ! $item->barcode_is_borrowernumber($patron_id, $item->hold_queue->[0]->{borrowernumber})) {
148 # $circ->screen_msg("Item on Hold for Another User");
149 } elsif ($item->{patron
} && ($item->{patron
} ne $patron_id)) {
150 # I can't deal with this right now
151 $circ->screen_msg("Item checked out to another patron");
153 $circ->do_checkout();
155 $debug and warn "circ is ok";
156 # If the item is already associated with this patron, then
158 $circ->renew_ok($item->{patron
} && ($item->{patron
} eq $patron_id));
160 $item->{patron
} = $patron_id;
161 $item->{due_date
} = $circ->{due
};
162 push(@
{$patron->{items
}}, $item_id);
163 $circ->desensitize(!$item->magnetic_media);
165 syslog
("LOG_DEBUG", "ILS::Checkout: patron %s has checked out %s",
166 $patron_id, join(', ', @
{$patron->{items
}}));
169 syslog
("LOG_ERR", "ILS::Checkout Issue failed");
178 my ( $self, $item_id, $trans_date, $return_date, $current_loc, $item_props, $cancel, $checked_in_ok ) = @_;
179 my ( $patron, $item, $circ );
181 $circ = C4
::SIP
::ILS
::Transaction
::Checkin
->new();
184 $circ->item( $item = C4
::SIP
::ILS
::Item
->new($item_id) );
187 $circ->do_checkin( $current_loc, $return_date );
191 $circ->alert_type(99);
192 $circ->screen_msg('Invalid Item');
195 # It's ok to check it in if it exists, and if it was checked out
196 # or it was not checked out but the checked_in_ok flag was set
197 $circ->ok( ( $checked_in_ok && $item ) || ( $item && $item->{patron
} ) );
199 if ( !defined( $item->{patron
} ) ) {
200 $circ->screen_msg("Item not checked out") unless $checked_in_ok;
204 $circ->patron( $patron = C4
::SIP
::ILS
::Patron
->new( $item->{patron
} ) );
205 delete $item->{patron
};
206 delete $item->{due_date
};
207 $patron->{items
} = [ grep { $_ ne $item_id } @
{ $patron->{items
} } ];
216 # If the ILS caches patron information, this lets it free
218 sub end_patron_session
{
219 my ($self, $patron_id) = @_;
221 # success?, screen_msg, print_line
222 return (1, 'Thank you !', '');
226 my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
227 $pay_type, $fee_id, $trans_id, $currency) = @_;
230 $trans = C4
::SIP
::ILS
::Transaction
::FeePayment
->new();
233 $trans->transaction_id($trans_id);
235 $trans->patron($patron = C4
::SIP
::ILS
::Patron
->new($patron_id));
237 $trans->screen_msg('Invalid patron barcode.');
240 $trans->pay($patron->{borrowernumber
},$fee_amt, $pay_type);
247 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
248 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
251 my $trans = C4
::SIP
::ILS
::Transaction
::Hold
->new();
253 $patron = C4
::SIP
::ILS
::Patron
->new( $patron_id);
255 || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
256 $trans->screen_msg("Invalid Patron.");
260 unless ($item = C4
::SIP
::ILS
::Item
->new($item_id || $title_id)) {
261 $trans->screen_msg("No such item.");
265 if ( $patron->holds_blocked_by_excessive_fees() ) {
266 $trans->screen_msg("Excessive fees blocking placement of hold.");
269 if ($item->fee and $fee_ack ne 'Y') {
270 $trans->screen_msg = "Fee required to place hold.";
275 item_id
=> $item->id,
276 patron_id
=> $patron->id,
277 expiration_date
=> $expiry_date,
278 pickup_location
=> $pickup_location,
279 hold_type
=> $hold_type,
283 $trans->patron($patron);
285 $trans->pickup_location($pickup_location);
288 push(@
{$item->hold_queue}, $hold);
289 push(@
{$patron->{hold_items
}}, $hold);
295 my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
296 my ($patron, $item, $hold);
298 my $trans = C4
::SIP
::ILS
::Transaction
::Hold
->new();
300 $patron = C4
::SIP
::ILS
::Patron
->new( $patron_id );
302 $trans->screen_msg("Invalid patron barcode.");
304 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
305 $trans->screen_msg('Invalid patron password.');
309 unless ($item = C4
::SIP
::ILS
::Item
->new($item_id || $title_id)) {
310 $trans->screen_msg("No such item.");
314 $trans->patron($patron);
317 unless ($trans->ok) {
318 $trans->screen_msg("Error with transaction drop_hold: " . $trans->screen_msg);
321 # Remove the hold from the patron's record first
322 $trans->ok($patron->drop_hold($item_id)); # different than the transaction drop!
324 unless ($trans->ok) {
325 # We didn't find it on the patron record
326 $trans->screen_msg("No such hold on patron record.");
330 # Now, remove it from the item record. If it was on the patron
331 # record but not on the item record, we'll treat that as success.
332 foreach my $i (0 .. scalar @
{$item->hold_queue}) {
333 $hold = $item->hold_queue->[$i];
334 if ($item->barcode_is_borrowernumber($patron->id, $hold->{borrowernumber
})) {
335 # found it: delete it.
336 splice @
{$item->hold_queue}, $i, 1;
337 last; # ?? should we keep going, in case there are multiples
341 $trans->screen_msg("Hold Cancelled.");
347 # The patron and item id's can't be altered, but the
348 # date, location, and type can.
350 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
351 $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
356 $trans = C4
::SIP
::ILS
::Transaction
::Hold
->new();
359 $patron = C4
::SIP
::ILS
::Patron
->new( $patron_id );
361 $trans->screen_msg("Invalid patron barcode: '$patron_id'.");
365 foreach my $i (0 .. scalar @
{$patron->{hold_items
}}) {
366 $hold = $patron->{hold_items
}[$i];
368 if ($hold->{item_id
} eq $item_id) {
369 # Found it. So fix it.
370 $hold->{expiration_date
} = $expiry_date if $expiry_date;
371 $hold->{pickup_location
} = $pickup_location if $pickup_location;
372 $hold->{hold_type
} = $hold_type if $hold_type;
373 $trans->change_hold();
375 $trans->screen_msg("Hold updated.");
376 $trans->patron($patron);
377 $trans->item(C4
::SIP
::ILS
::Item
->new( $hold->{item_id
}));
382 # The same hold structure is linked into both the patron's
383 # list of hold items and into the queue of outstanding holds
384 # for the item, so we don't need to search the hold queue for
385 # the item, since it's already been updated by the patron code.
388 $trans->screen_msg("No such outstanding hold.");
395 my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
396 $no_block, $nb_due_date, $third_party,
397 $item_props, $fee_ack) = @_;
401 $trans = C4
::SIP
::ILS
::Transaction
::Renew
->new();
402 $trans->patron($patron = C4
::SIP
::ILS
::Patron
->new( $patron_id ));
405 $trans->screen_msg("Invalid patron barcode.");
407 } elsif (!$patron->renew_ok) {
408 $trans->screen_msg("Renewals not allowed.");
412 # Previously: renewing a title, rather than an item (sort of)
413 # This is gross, but in a real ILS it would be better
415 # if (defined($title_id)) {
416 # foreach my $i (@{$patron->{items}}) {
417 # $item = new ILS::Item $i;
418 # last if ($title_id eq $item->title_id);
423 my $count = scalar @
{$patron->{items
}};
424 foreach my $i (@
{$patron->{items
}}) {
425 unless (defined $i->{barcode
}) { # FIXME: using data instead of objects may violate the abstraction layer
426 syslog
("LOG_ERR", "No barcode for item %s of %s: $item_id", $j+1, $count);
429 syslog
("LOG_DEBUG", "checking item %s of %s: $item_id vs. %s", ++$j, $count, $i->{barcode
});
430 if ($i->{barcode
} eq $item_id) {
431 # We have it checked out
432 $item = C4
::SIP
::ILS
::Item
->new( $item_id );
440 if (!defined($item)) {
441 $trans->screen_msg("Item not checked out to " . $patron->name); # not checked out to $patron_id
445 if ($trans->renewal_ok()) {
446 $item->{due_date
} = $trans->{due
};
447 $trans->desensitize(0);
455 my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
456 my ($patron, $item_id);
459 $trans = C4
::SIP
::ILS
::Transaction
::RenewAll
->new();
461 $trans->patron($patron = C4
::SIP
::ILS
::Patron
->new( $patron_id ));
462 if (defined $patron) {
463 syslog
("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s", $patron->name, $patron->renew_ok);
465 syslog
("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'", $patron_id);
468 if (!defined($patron)) {
469 $trans->screen_msg("Invalid patron barcode.");
471 } elsif (!$patron->renew_ok) {
472 $trans->screen_msg("Renewals not allowed.");
474 } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
475 $trans->screen_msg("Invalid patron password.");
479 $trans->do_renew_all;