4 # A Class for hiding the ILS's concept of the item from OpenSIP
7 package C4::SIP::ILS::Item;
12 use Sys::Syslog qw(syslog);
15 use C4::SIP::ILS::Transaction;
33 title => "Perl 5 desktop reference",
35 sip_media_type => '001',
40 title => "The deep blue alibi",
42 sip_media_type => '001',
48 reservenotes => undef,
49 reservedate => '2008-10-09',
51 rtimestamp => '2008-10-09 11:15:06',
52 biblionumber => '406',
53 borrowernumber => '756',
59 title => "Harry Potter y el cáliz de fuego",
61 sip_media_type => '001',
69 defined $a->{priority} or return -1;
70 defined $b->{priority} or return 1;
71 return $a->{priority} <=> $b->{priority};
75 my ($class, $item_id) = @_;
76 my $type = ref($class) || $class;
78 my $itemnumber = GetItemnumberFromBarcode($item_id);
79 my $item = GetBiblioFromItemNumber($itemnumber); # actually biblio.*, biblioitems.* AND items.* (overkill)
81 syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
82 warn "new ILS::Item($item_id) : No item '$item_id'.";
85 $item->{ 'itemnumber' } = $itemnumber;
86 $item->{ 'id' } = $item->{barcode}; # to SIP, the barcode IS the id.
87 $item->{permanent_location}= $item->{homebranch};
88 $item->{'collection_code'} = $item->{ccode};
89 $item->{ 'call_number' } = $item->{itemcallnumber};
91 my $it = C4::Context->preference('item-level_itypes') ? $item->{itype} : $item->{itemtype};
92 my $itemtype = Koha::Database->new()->schema()->resultset('Itemtype')->find( $it );
93 $item->{sip_media_type} = $itemtype->sip_media_type() if $itemtype;
95 # check if its on issue and if so get the borrower
96 my $issue = GetItemIssue($item->{'itemnumber'});
98 $item->{due_date} = $issue->{date_due};
100 my $borrower = GetMember(borrowernumber=>$issue->{'borrowernumber'});
101 $item->{patron} = $borrower->{'cardnumber'};
102 my $reserves = GetReservesFromBiblionumber({ biblionumber => $item->{biblionumber} });
103 $item->{hold_queue} = [ sort priority_sort @$reserves ];
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,
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 # If a return triggers capture for a hold the borrowernumber is passed
154 # and saved so that other hold info can be retrieved
159 $self->{hold}->{borrowernumber} = $id;
161 if ($self->{hold} ) {
162 return $self->{hold}->{borrowernumber};
167 sub hold_patron_name {
169 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
170 my $holder = GetMember(borrowernumber=>$borrowernumber);
172 syslog("LOG_ERR", "While checking hold, GetMember failed for borrowernumber '$borrowernumber'");
175 my $email = $holder->{email} || '';
176 my $phone = $holder->{phone} || '';
177 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
178 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
179 "" ; # neither populated, empty string
180 my $name = $holder->{firstname} ? $holder->{firstname} . ' ' : '';
181 $name .= $holder->{surname} . $extra;
185 sub hold_patron_bcode {
187 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
188 my $holder = GetMember(borrowernumber => $borrowernumber);
190 if ($holder->{cardnumber}) {
191 return $holder->{cardnumber};
197 sub destination_loc {
201 $self->{dest_loc} = $set_loc;
203 if ($self->{dest_loc} ) {
204 return $self->{dest_loc};
211 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
215 my $class = ref($self) or croak "$self is not an object";
216 my $name = $AUTOLOAD;
220 unless (exists $fields{$name}) {
221 croak "Cannot access '$name' field of class '$class'";
225 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
226 return $self->{$name} = shift;
228 return $self->{$name};
232 sub status_update { # FIXME: this looks unimplemented
233 my ($self, $props) = @_;
234 my $status = C4::SIP::ILS::Transaction->new();
235 $self->{sip_item_properties} = $props;
242 return $self->{title};
245 sub sip_circulation_status {
247 if ($self->{patron}) {
248 return '04'; # charged
249 } elsif (scalar @{$self->{hold_queue}}) {
250 return '08'; # waiting on hold shelf
252 return '03'; # available
253 } # FIXME: 01-13 enumerated in spec.
256 sub sip_security_marker {
258 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
262 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
267 return $self->{fee} || 0;
271 return $self->{currency} || 'USD';
275 return $self->{homebranch};
279 (defined $self->{hold_queue}) or return [];
280 return $self->{hold_queue};
284 (defined $self->{pending_queue}) or return [];
285 return $self->{pending_queue};
289 (defined $self->{hold_shelf}) or return [];
290 return $self->{hold_shelf};
293 sub hold_queue_position {
294 my ($self, $patron_id) = @_;
295 ($self->{hold_queue}) or return 0;
297 foreach (@{$self->{hold_queue}}) {
299 $_->{patron_id} or next;
300 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
301 return $i; # maybe should return $_->{priority}
309 return $self->{due_date} || 0;
313 return $self->{recall_date} || 0;
315 sub hold_pickup_date {
317 return $self->{hold_pickup_date} || 0;
320 # This is a partial check of "availability". It is not supposed to check everything here.
321 # An item is available for a patron if it is:
322 # 1) checked out to the same patron
323 # AND no pending (i.e. non-W) hold queue
326 # AND (not on hold_shelf OR is on hold_shelf for patron)
328 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
329 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
330 # patron has retrieved the item before the librarian.
332 # We don't check if the patron is at the front of the pending queue in the first case, because
333 # they should not be able to place a hold on an item they already have.
336 my ($self, $for_patron) = @_;
337 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
338 my $count2 = (defined $self->{hold_shelf} ) ? scalar @{$self->{hold_shelf} } : 0;
339 $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
340 if (defined($self->{patron_id})) {
341 ($self->{patron_id} eq $for_patron) or return 0;
342 return ($count ? 0 : 1);
343 } else { # not checked out
344 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
349 sub _barcode_to_borrowernumber {
351 return unless defined $known;
352 my $member = GetMember(cardnumber=>$known) or return;
353 return $member->{borrowernumber};
355 sub barcode_is_borrowernumber { # because hold_queue only has borrowernumber...
358 my $number = shift or return; # can't be zero
359 return unless defined $barcode; # might be 0 or 000 or 000000
360 my $converted = _barcode_to_borrowernumber($barcode);
361 return unless $converted;
362 return ($number == $converted);
366 my $hold = shift or return;
367 foreach (qw(biblionumber borrowernumber reservedate)) {
368 $hold->{$_} or return;
370 return ModReserveFill($hold);