4 # A Class for hiding the ILS's concept of the item from OpenSIP
12 use Sys::Syslog qw(syslog);
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
38 title => "Perl 5 desktop reference",
40 sip_media_type => '001',
45 title => "The deep blue alibi",
47 sip_media_type => '001',
53 reservenotes => undef,
54 constrainttype => 'a',
55 reservedate => '2008-10-09',
57 rtimestamp => '2008-10-09 11:15:06',
58 biblionumber => '406',
59 borrowernumber => '756',
65 title => "Harry Potter y el cáliz de fuego",
67 sip_media_type => '001',
75 defined $a->{priority} or return -1;
76 defined $b->{priority} or return 1;
77 return $a->{priority} <=> $b->{priority};
81 my ($class, $item_id) = @_;
82 my $type = ref($class) || $class;
84 my $itemnumber = GetItemnumberFromBarcode($item_id);
85 my $item = GetBiblioFromItemNumber($itemnumber); # actually biblio.*, biblioitems.* AND items.* (overkill)
87 syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
88 warn "new ILS::Item($item_id) : No item '$item_id'.";
91 $item->{ 'itemnumber' } = $itemnumber;
92 $item->{ 'id' } = $item->{barcode}; # to SIP, the barcode IS the id.
93 $item->{permanent_location}= $item->{homebranch};
94 $item->{'collection_code'} = $item->{ccode};
95 $item->{ 'call_number' } = $item->{itemcallnumber};
96 # $item->{'destination_loc'} = ?
98 # check if its on issue and if so get the borrower
99 my $issue = GetItemIssue($item->{'itemnumber'});
100 my $borrower = GetMember(borrowernumber=>$issue->{'borrowernumber'});
101 $item->{patron} = $borrower->{'cardnumber'};
102 my ($whatever, $arrayref) = GetReservesFromBiblionumber($item->{biblionumber});
103 $item->{hold_queue} = [ sort priority_sort @$arrayref ];
104 $item->{hold_shelf} = [( grep { defined $_->{found} and $_->{found} eq 'W' } @{$item->{hold_queue}} )];
105 $item->{pending_queue} = [( grep {(! defined $_->{found}) or $_->{found} ne 'W' } @{$item->{hold_queue}} )];
109 syslog("LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
110 $item_id, $self->{title});
121 sip_item_properties => 0,
123 permanent_location => 0,
124 current_location => 0,
131 collection_code => 0,
140 my $self = shift or return;
141 # use Data::Dumper; warn "next_hold() hold_shelf: " . Dumper($self->{hold_shelf}); warn "next_hold() pending_queue: " . $self->{pending_queue};
142 foreach (@{$self->hold_shelf}) { # If this item was taken from the hold shelf, then that reserve still governs
143 next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
146 if (scalar @{$self->{pending_queue}}) { # Otherwise, if there is at least one hold, the first (best priority) gets it
147 return $self->{pending_queue}->[0];
152 # hold_patron_id is NOT the barcode. It's the borrowernumber.
153 # That's because the reserving patron may not have a barcode, or may be from an different system entirely (ILL)!
155 my $self = shift or return;
156 my $hold = $self->next_hold() or return;
157 return $hold->{borrowernumber};
159 sub hold_patron_name {
160 my $self = shift or return;
161 # return $self->{hold_patron_name} if $self->{hold_patron_name}; TODO: consider caching
162 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
163 my $holder = GetMember(borrowernumber=>$borrowernumber);
165 syslog("LOG_ERR", "While checking hold, GetMember failed for borrowernumber '$borrowernumber'");
168 my $email = $holder->{email} || '';
169 my $phone = $holder->{phone} || '';
170 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
171 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
172 "" ; # neither populated, empty string
173 my $name = $holder->{firstname} ? $holder->{firstname} . ' ' : '';
174 $name .= $holder->{surname} . $extra;
175 # $self->{hold_patron_name} = $name; # TODO: consider caching
179 sub hold_patron_bcode {
180 my $self = shift or return;
181 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
182 my $holder = GetMember($borrowernumber, 'borrowernumber');
184 if ($holder->{cardnumber}) {
185 return $holder->{cardnumber};
191 sub destination_loc {
192 my $self = shift or return;
193 my $hold = $self->next_hold();
194 return ($hold ? $hold->{branchcode} : '');
199 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
203 my $class = ref($self) or croak "$self is not an object";
204 my $name = $AUTOLOAD;
208 unless (exists $fields{$name}) {
209 croak "Cannot access '$name' field of class '$class'";
213 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
214 return $self->{$name} = shift;
216 return $self->{$name};
220 sub status_update { # FIXME: this looks unimplemented
221 my ($self, $props) = @_;
222 my $status = new ILS::Transaction;
223 $self->{sip_item_properties} = $props;
230 return $self->{title};
233 sub sip_circulation_status {
235 if ($self->{patron}) {
236 return '04'; # charged
237 } elsif (scalar @{$self->{hold_queue}}) {
238 return '08'; # waiting on hold shelf
240 return '03'; # available
241 } # FIXME: 01-13 enumerated in spec.
244 sub sip_security_marker {
245 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
248 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
253 return $self->{fee} || 0;
257 return $self->{currency} || 'USD';
261 return $self->{homebranch};
265 (defined $self->{hold_queue}) or return [];
266 return $self->{hold_queue};
270 (defined $self->{pending_queue}) or return [];
271 return $self->{pending_queue};
275 (defined $self->{hold_shelf}) or return [];
276 return $self->{hold_shelf};
279 sub hold_queue_position {
280 my ($self, $patron_id) = @_;
281 ($self->{hold_queue}) or return 0;
283 foreach (@{$self->{hold_queue}}) {
285 $_->{patron_id} or next;
286 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
287 return $i; # maybe should return $_->{priority}
295 return $self->{due_date} || 0;
299 return $self->{recall_date} || 0;
301 sub hold_pickup_date {
303 return $self->{hold_pickup_date} || 0;
306 # This is a partial check of "availability". It is not supposed to check everything here.
307 # An item is available for a patron if it is:
308 # 1) checked out to the same patron
309 # AND no pending (i.e. non-W) hold queue
312 # AND (not on hold_shelf OR is on hold_shelf for patron)
314 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
315 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
316 # patron has retrieved the item before the librarian.
318 # We don't check if the patron is at the front of the pending queue in the first case, because
319 # they should not be able to place a hold on an item they already have.
322 my ($self, $for_patron) = @_;
323 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
324 my $count2 = (defined $self->{hold_shelf} ) ? scalar @{$self->{hold_shelf} } : 0;
325 $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
326 if (defined($self->{patron_id})) {
327 ($self->{patron_id} eq $for_patron) or return 0;
328 return ($count ? 0 : 1);
329 } else { # not checked out
330 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
335 sub _barcode_to_borrowernumber ($) {
337 (defined($known)) or return undef;
338 my $member = GetMember(cardnumber=>$known) or return undef;
339 return $member->{borrowernumber};
341 sub barcode_is_borrowernumber ($$$) { # because hold_queue only has borrowernumber...
342 my $self = shift; # not really used
344 my $number = shift or return undef; # can't be zero
345 (defined($barcode)) or return undef; # might be 0 or 000 or 000000
346 my $converted = _barcode_to_borrowernumber($barcode) or return undef;
347 return ($number eq $converted); # even though both *should* be numbers, eq is safer.
349 sub fill_reserve ($$) {
351 my $hold = shift or return undef;
352 foreach (qw(biblionumber borrowernumber reservedate)) {
353 $hold->{$_} or return undef;
355 return ModReserveFill($hold);