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'});
101 $item->{due_date} = $issue->{date_due};
103 my $borrower = GetMember(borrowernumber=>$issue->{'borrowernumber'});
104 $item->{patron} = $borrower->{'cardnumber'};
105 my ($whatever, $arrayref) = GetReservesFromBiblionumber($item->{biblionumber});
106 $item->{hold_queue} = [ sort priority_sort @$arrayref ];
107 $item->{hold_shelf} = [( grep { defined $_->{found} and $_->{found} eq 'W' } @{$item->{hold_queue}} )];
108 $item->{pending_queue} = [( grep {(! defined $_->{found}) or $_->{found} ne 'W' } @{$item->{hold_queue}} )];
112 syslog("LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
113 $item_id, $self->{title});
124 sip_item_properties => 0,
126 permanent_location => 0,
127 current_location => 0,
134 collection_code => 0,
143 my $self = shift or return;
144 # use Data::Dumper; warn "next_hold() hold_shelf: " . Dumper($self->{hold_shelf}); warn "next_hold() pending_queue: " . $self->{pending_queue};
145 foreach (@{$self->hold_shelf}) { # If this item was taken from the hold shelf, then that reserve still governs
146 next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
149 if (scalar @{$self->{pending_queue}}) { # Otherwise, if there is at least one hold, the first (best priority) gets it
150 return $self->{pending_queue}->[0];
155 # hold_patron_id is NOT the barcode. It's the borrowernumber.
156 # That's because the reserving patron may not have a barcode, or may be from an different system entirely (ILL)!
158 my $self = shift or return;
159 my $hold = $self->next_hold() or return;
160 return $hold->{borrowernumber};
162 sub hold_patron_name {
163 my $self = shift or return;
164 # return $self->{hold_patron_name} if $self->{hold_patron_name}; TODO: consider caching
165 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
166 my $holder = GetMember(borrowernumber=>$borrowernumber);
168 syslog("LOG_ERR", "While checking hold, GetMember failed for borrowernumber '$borrowernumber'");
171 my $email = $holder->{email} || '';
172 my $phone = $holder->{phone} || '';
173 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
174 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
175 "" ; # neither populated, empty string
176 my $name = $holder->{firstname} ? $holder->{firstname} . ' ' : '';
177 $name .= $holder->{surname} . $extra;
178 # $self->{hold_patron_name} = $name; # TODO: consider caching
182 sub hold_patron_bcode {
183 my $self = shift or return;
184 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
185 my $holder = GetMember(borrowernumber => $borrowernumber);
187 if ($holder->{cardnumber}) {
188 return $holder->{cardnumber};
194 sub destination_loc {
195 my $self = shift or return;
196 my $hold = $self->next_hold();
197 return ($hold ? $hold->{branchcode} : '');
202 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
206 my $class = ref($self) or croak "$self is not an object";
207 my $name = $AUTOLOAD;
211 unless (exists $fields{$name}) {
212 croak "Cannot access '$name' field of class '$class'";
216 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
217 return $self->{$name} = shift;
219 return $self->{$name};
223 sub status_update { # FIXME: this looks unimplemented
224 my ($self, $props) = @_;
225 my $status = new ILS::Transaction;
226 $self->{sip_item_properties} = $props;
233 return $self->{title};
236 sub sip_circulation_status {
238 if ($self->{patron}) {
239 return '04'; # charged
240 } elsif (scalar @{$self->{hold_queue}}) {
241 return '08'; # waiting on hold shelf
243 return '03'; # available
244 } # FIXME: 01-13 enumerated in spec.
247 sub sip_security_marker {
248 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
251 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
256 return $self->{fee} || 0;
260 return $self->{currency} || 'USD';
264 return $self->{homebranch};
268 (defined $self->{hold_queue}) or return [];
269 return $self->{hold_queue};
273 (defined $self->{pending_queue}) or return [];
274 return $self->{pending_queue};
278 (defined $self->{hold_shelf}) or return [];
279 return $self->{hold_shelf};
282 sub hold_queue_position {
283 my ($self, $patron_id) = @_;
284 ($self->{hold_queue}) or return 0;
286 foreach (@{$self->{hold_queue}}) {
288 $_->{patron_id} or next;
289 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
290 return $i; # maybe should return $_->{priority}
298 return $self->{due_date} || 0;
302 return $self->{recall_date} || 0;
304 sub hold_pickup_date {
306 return $self->{hold_pickup_date} || 0;
309 # This is a partial check of "availability". It is not supposed to check everything here.
310 # An item is available for a patron if it is:
311 # 1) checked out to the same patron
312 # AND no pending (i.e. non-W) hold queue
315 # AND (not on hold_shelf OR is on hold_shelf for patron)
317 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
318 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
319 # patron has retrieved the item before the librarian.
321 # We don't check if the patron is at the front of the pending queue in the first case, because
322 # they should not be able to place a hold on an item they already have.
325 my ($self, $for_patron) = @_;
326 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
327 my $count2 = (defined $self->{hold_shelf} ) ? scalar @{$self->{hold_shelf} } : 0;
328 $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
329 if (defined($self->{patron_id})) {
330 ($self->{patron_id} eq $for_patron) or return 0;
331 return ($count ? 0 : 1);
332 } else { # not checked out
333 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
338 sub _barcode_to_borrowernumber ($) {
340 (defined($known)) or return undef;
341 my $member = GetMember(cardnumber=>$known) or return undef;
342 return $member->{borrowernumber};
344 sub barcode_is_borrowernumber ($$$) { # because hold_queue only has borrowernumber...
345 my $self = shift; # not really used
347 my $number = shift or return undef; # can't be zero
348 (defined($barcode)) or return undef; # might be 0 or 000 or 000000
349 my $converted = _barcode_to_borrowernumber($barcode) or return undef;
350 return ($number eq $converted); # even though both *should* be numbers, eq is safer.
352 sub fill_reserve ($$) {
354 my $hold = shift or return undef;
355 foreach (qw(biblionumber borrowernumber reservedate)) {
356 $hold->{$_} or return undef;
358 return ModReserveFill($hold);