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}} )];
115 $item->{due_date} = $issue->{date_due};
119 syslog("LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
120 $item_id, $self->{title});
131 sip_item_properties => 0,
133 permanent_location => 0,
134 current_location => 0,
141 collection_code => 0,
150 my $self = shift or return;
151 # use Data::Dumper; warn "next_hold() hold_shelf: " . Dumper($self->{hold_shelf}); warn "next_hold() pending_queue: " . $self->{pending_queue};
152 foreach (@{$self->hold_shelf}) { # If this item was taken from the hold shelf, then that reserve still governs
153 next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
156 if (scalar @{$self->{pending_queue}}) { # Otherwise, if there is at least one hold, the first (best priority) gets it
157 return $self->{pending_queue}->[0];
162 # hold_patron_id is NOT the barcode. It's the borrowernumber.
163 # That's because the reserving patron may not have a barcode, or may be from an different system entirely (ILL)!
165 my $self = shift or return;
166 my $hold = $self->next_hold() or return;
167 return $hold->{borrowernumber};
169 sub hold_patron_name {
170 my $self = shift or return;
171 # return $self->{hold_patron_name} if $self->{hold_patron_name}; TODO: consider caching
172 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
173 my $holder = GetMember(borrowernumber=>$borrowernumber);
175 syslog("LOG_ERR", "While checking hold, GetMember failed for borrowernumber '$borrowernumber'");
178 my $email = $holder->{email} || '';
179 my $phone = $holder->{phone} || '';
180 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
181 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
182 "" ; # neither populated, empty string
183 my $name = $holder->{firstname} ? $holder->{firstname} . ' ' : '';
184 $name .= $holder->{surname} . $extra;
185 # $self->{hold_patron_name} = $name; # TODO: consider caching
189 sub hold_patron_bcode {
190 my $self = shift or return;
191 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
192 my $holder = GetMember(borrowernumber => $borrowernumber);
194 if ($holder->{cardnumber}) {
195 return $holder->{cardnumber};
201 sub destination_loc {
202 my $self = shift or return;
203 my $hold = $self->next_hold();
204 return ($hold ? $hold->{branchcode} : '');
209 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
213 my $class = ref($self) or croak "$self is not an object";
214 my $name = $AUTOLOAD;
218 unless (exists $fields{$name}) {
219 croak "Cannot access '$name' field of class '$class'";
223 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
224 return $self->{$name} = shift;
226 return $self->{$name};
230 sub status_update { # FIXME: this looks unimplemented
231 my ($self, $props) = @_;
232 my $status = new ILS::Transaction;
233 $self->{sip_item_properties} = $props;
240 return $self->{title};
243 sub sip_circulation_status {
245 if ($self->{patron}) {
246 return '04'; # charged
247 } elsif (scalar @{$self->{hold_queue}}) {
248 return '08'; # waiting on hold shelf
250 return '03'; # available
251 } # FIXME: 01-13 enumerated in spec.
254 sub sip_security_marker {
255 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
258 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
263 return $self->{fee} || 0;
267 return $self->{currency} || 'USD';
271 return $self->{homebranch};
275 (defined $self->{hold_queue}) or return [];
276 return $self->{hold_queue};
280 (defined $self->{pending_queue}) or return [];
281 return $self->{pending_queue};
285 (defined $self->{hold_shelf}) or return [];
286 return $self->{hold_shelf};
289 sub hold_queue_position {
290 my ($self, $patron_id) = @_;
291 ($self->{hold_queue}) or return 0;
293 foreach (@{$self->{hold_queue}}) {
295 $_->{patron_id} or next;
296 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
297 return $i; # maybe should return $_->{priority}
305 return $self->{due_date} || 0;
309 return $self->{recall_date} || 0;
311 sub hold_pickup_date {
313 return $self->{hold_pickup_date} || 0;
316 # This is a partial check of "availability". It is not supposed to check everything here.
317 # An item is available for a patron if it is:
318 # 1) checked out to the same patron
319 # AND no pending (i.e. non-W) hold queue
322 # AND (not on hold_shelf OR is on hold_shelf for patron)
324 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
325 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
326 # patron has retrieved the item before the librarian.
328 # We don't check if the patron is at the front of the pending queue in the first case, because
329 # they should not be able to place a hold on an item they already have.
332 my ($self, $for_patron) = @_;
333 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
334 my $count2 = (defined $self->{hold_shelf} ) ? scalar @{$self->{hold_shelf} } : 0;
335 $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
336 if (defined($self->{patron_id})) {
337 ($self->{patron_id} eq $for_patron) or return 0;
338 return ($count ? 0 : 1);
339 } else { # not checked out
340 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
345 sub _barcode_to_borrowernumber ($) {
347 (defined($known)) or return undef;
348 my $member = GetMember(cardnumber=>$known) or return undef;
349 return $member->{borrowernumber};
351 sub barcode_is_borrowernumber ($$$) { # because hold_queue only has borrowernumber...
352 my $self = shift; # not really used
354 my $number = shift or return undef; # can't be zero
355 (defined($barcode)) or return undef; # might be 0 or 000 or 000000
356 my $converted = _barcode_to_borrowernumber($barcode) or return undef;
357 return ($number eq $converted); # even though both *should* be numbers, eq is safer.
359 sub fill_reserve ($$) {
361 my $hold = shift or return undef;
362 foreach (qw(biblionumber borrowernumber reservedate)) {
363 $hold->{$_} or return undef;
365 return ModReserveFill($hold);