4 # A Class for hiding the ILS's concept of the item from OpenSIP
13 use Sys::Syslog qw(syslog);
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
39 title => "Perl 5 desktop reference",
41 sip_media_type => '001',
46 title => "The deep blue alibi",
48 sip_media_type => '001',
54 reservenotes => undef,
55 constrainttype => 'a',
56 reservedate => '2008-10-09',
58 rtimestamp => '2008-10-09 11:15:06',
59 biblionumber => '406',
60 borrowernumber => '756',
66 title => "Harry Potter y el cáliz de fuego",
68 sip_media_type => '001',
76 defined $a->{priority} or return -1;
77 defined $b->{priority} or return 1;
78 return $a->{priority} <=> $b->{priority};
82 my ($class, $item_id) = @_;
83 my $type = ref($class) || $class;
85 my $itemnumber = GetItemnumberFromBarcode($item_id);
86 my $item = GetBiblioFromItemNumber($itemnumber); # actually biblio.*, biblioitems.* AND items.* (overkill)
88 syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
89 warn "new ILS::Item($item_id) : No item '$item_id'.";
92 $item->{ 'itemnumber' } = $itemnumber;
93 $item->{ 'id' } = $item->{barcode}; # to SIP, the barcode IS the id.
94 $item->{permanent_location}= $item->{homebranch};
95 $item->{'collection_code'} = $item->{ccode};
96 $item->{ 'call_number' } = $item->{itemcallnumber};
97 # $item->{'destination_loc'} = ?
99 # check if its on issue and if so get the borrower
100 my $issue = GetItemIssue($item->{'itemnumber'});
102 my $date = $issue->{ date_due };
103 my $dt = DateTime->new(
104 year => substr($date, 0, 4),
105 month => substr($date,5,2),
106 day => substr($date, 8, 2) );
107 $item->{ due_date } = $dt->epoch();
109 my $borrower = GetMember(borrowernumber=>$issue->{'borrowernumber'});
110 $item->{patron} = $borrower->{'cardnumber'};
111 my ($whatever, $arrayref) = GetReservesFromBiblionumber($item->{biblionumber});
112 $item->{hold_queue} = [ sort priority_sort @$arrayref ];
113 $item->{hold_shelf} = [( grep { defined $_->{found} and $_->{found} eq 'W' } @{$item->{hold_queue}} )];
114 $item->{pending_queue} = [( grep {(! defined $_->{found}) or $_->{found} ne 'W' } @{$item->{hold_queue}} )];
118 syslog("LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
119 $item_id, $self->{title});
130 sip_item_properties => 0,
132 permanent_location => 0,
133 current_location => 0,
140 collection_code => 0,
149 my $self = shift or return;
150 # use Data::Dumper; warn "next_hold() hold_shelf: " . Dumper($self->{hold_shelf}); warn "next_hold() pending_queue: " . $self->{pending_queue};
151 foreach (@{$self->hold_shelf}) { # If this item was taken from the hold shelf, then that reserve still governs
152 next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
155 if (scalar @{$self->{pending_queue}}) { # Otherwise, if there is at least one hold, the first (best priority) gets it
156 return $self->{pending_queue}->[0];
161 # hold_patron_id is NOT the barcode. It's the borrowernumber.
162 # That's because the reserving patron may not have a barcode, or may be from an different system entirely (ILL)!
164 my $self = shift or return;
165 my $hold = $self->next_hold() or return;
166 return $hold->{borrowernumber};
168 sub hold_patron_name {
169 my $self = shift or return;
170 # return $self->{hold_patron_name} if $self->{hold_patron_name}; TODO: consider caching
171 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
172 my $holder = GetMember(borrowernumber=>$borrowernumber);
174 syslog("LOG_ERR", "While checking hold, GetMember failed for borrowernumber '$borrowernumber'");
177 my $email = $holder->{email} || '';
178 my $phone = $holder->{phone} || '';
179 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
180 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
181 "" ; # neither populated, empty string
182 my $name = $holder->{firstname} ? $holder->{firstname} . ' ' : '';
183 $name .= $holder->{surname} . $extra;
184 # $self->{hold_patron_name} = $name; # TODO: consider caching
188 sub hold_patron_bcode {
189 my $self = shift or return;
190 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
191 my $holder = GetMember(borrowernumber => $borrowernumber);
193 if ($holder->{cardnumber}) {
194 return $holder->{cardnumber};
200 sub destination_loc {
201 my $self = shift or return;
202 my $hold = $self->next_hold();
203 return ($hold ? $hold->{branchcode} : '');
208 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
212 my $class = ref($self) or croak "$self is not an object";
213 my $name = $AUTOLOAD;
217 unless (exists $fields{$name}) {
218 croak "Cannot access '$name' field of class '$class'";
222 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
223 return $self->{$name} = shift;
225 return $self->{$name};
229 sub status_update { # FIXME: this looks unimplemented
230 my ($self, $props) = @_;
231 my $status = new ILS::Transaction;
232 $self->{sip_item_properties} = $props;
239 return $self->{title};
242 sub sip_circulation_status {
244 if ($self->{patron}) {
245 return '04'; # charged
246 } elsif (scalar @{$self->{hold_queue}}) {
247 return '08'; # waiting on hold shelf
249 return '03'; # available
250 } # FIXME: 01-13 enumerated in spec.
253 sub sip_security_marker {
254 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
257 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
262 return $self->{fee} || 0;
266 return $self->{currency} || 'USD';
270 return $self->{homebranch};
274 (defined $self->{hold_queue}) or return [];
275 return $self->{hold_queue};
279 (defined $self->{pending_queue}) or return [];
280 return $self->{pending_queue};
284 (defined $self->{hold_shelf}) or return [];
285 return $self->{hold_shelf};
288 sub hold_queue_position {
289 my ($self, $patron_id) = @_;
290 ($self->{hold_queue}) or return 0;
292 foreach (@{$self->{hold_queue}}) {
294 $_->{patron_id} or next;
295 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
296 return $i; # maybe should return $_->{priority}
304 return $self->{due_date} || 0;
308 return $self->{recall_date} || 0;
310 sub hold_pickup_date {
312 return $self->{hold_pickup_date} || 0;
315 # This is a partial check of "availability". It is not supposed to check everything here.
316 # An item is available for a patron if it is:
317 # 1) checked out to the same patron
318 # AND no pending (i.e. non-W) hold queue
321 # AND (not on hold_shelf OR is on hold_shelf for patron)
323 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
324 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
325 # patron has retrieved the item before the librarian.
327 # We don't check if the patron is at the front of the pending queue in the first case, because
328 # they should not be able to place a hold on an item they already have.
331 my ($self, $for_patron) = @_;
332 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
333 my $count2 = (defined $self->{hold_shelf} ) ? scalar @{$self->{hold_shelf} } : 0;
334 $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
335 if (defined($self->{patron_id})) {
336 ($self->{patron_id} eq $for_patron) or return 0;
337 return ($count ? 0 : 1);
338 } else { # not checked out
339 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
344 sub _barcode_to_borrowernumber ($) {
346 (defined($known)) or return undef;
347 my $member = GetMember(cardnumber=>$known) or return undef;
348 return $member->{borrowernumber};
350 sub barcode_is_borrowernumber ($$$) { # because hold_queue only has borrowernumber...
351 my $self = shift; # not really used
353 my $number = shift or return undef; # can't be zero
354 (defined($barcode)) or return undef; # might be 0 or 000 or 000000
355 my $converted = _barcode_to_borrowernumber($barcode) or return undef;
356 return ($number eq $converted); # even though both *should* be numbers, eq is safer.
358 sub fill_reserve ($$) {
360 my $hold = shift or return undef;
361 foreach (qw(biblionumber borrowernumber reservedate)) {
362 $hold->{$_} or return undef;
364 return ModReserveFill($hold);