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);
16 use C4::SIP::ILS::Transaction;
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 reservedate => '2008-10-09',
56 rtimestamp => '2008-10-09 11:15:06',
57 biblionumber => '406',
58 borrowernumber => '756',
64 title => "Harry Potter y el cáliz de fuego",
66 sip_media_type => '001',
75 my ($class, $item_id) = @_;
76 my $type = ref($class) || $class;
77 my $item = Koha::Items->find( { barcode => $item_id } );
79 syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
80 warn "new ILS::Item($item_id) : No item '$item_id'.";
83 my $self = $item->unblessed;
84 $self->{ 'id' } = $item->barcode; # to SIP, the barcode IS the id.
85 $self->{permanent_location}= $item->homebranch;
86 $self->{'collection_code'} = $item->ccode;
87 $self->{ 'call_number' } = $item->itemcallnumber;
89 my $it = $item->effective_itemtype;
90 my $itemtype = Koha::Database->new()->schema()->resultset('Itemtype')->find( $it );
91 $self->{sip_media_type} = $itemtype->sip_media_type() if $itemtype;
93 # check if its on issue and if so get the borrower
94 my $issue = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
96 $self->{due_date} = dt_from_string( $issue->date_due, 'sql' )->truncate( to => 'minute' );
97 my $patron = Koha::Patrons->find( $issue->borrowernumber );
98 $self->{patron} = $patron->cardnumber;
100 my $biblio = Koha::Biblios->find( $self->{biblionumber} );
101 my $holds = $biblio->current_holds->unblessed;
102 $self->{hold_queue} = $holds;
103 $self->{hold_shelf} = [( grep { defined $_->{found} and $_->{found} eq 'W' } @{$self->{hold_queue}} )];
104 $self->{pending_queue} = [( grep {(! defined $_->{found}) or $_->{found} ne 'W' } @{$self->{hold_queue}} )];
105 $self->{title} = $biblio->title;
106 $self->{author} = $biblio->author;
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 {
168 my ( $self, $template ) = @_;
169 my $borrowernumber = $self->hold_patron_id() or return;
172 my $tt = Template->new();
174 my $patron = Koha::Patrons->find($borrowernumber);
177 $tt->process( \$template, { patron => $patron }, \$output );
181 my $holder = Koha::Patrons->find( $borrowernumber );
183 syslog("LOG_ERR", "While checking hold, failed to retrieve the patron with borrowernumber '$borrowernumber'");
186 my $email = $holder->email || '';
187 my $phone = $holder->phone || '';
188 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
189 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
190 "" ; # neither populated, empty string
191 my $name = $holder->firstname ? $holder->firstname . ' ' : '';
192 $name .= $holder->surname . $extra;
196 sub hold_patron_bcode {
198 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
199 my $holder = Koha::Patrons->find( $borrowernumber );
200 if ($holder and $holder->cardnumber ) {
201 return $holder->cardnumber;
206 sub destination_loc {
210 $self->{dest_loc} = $set_loc;
212 if ($self->{dest_loc} ) {
213 return $self->{dest_loc};
220 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
224 my $class = ref($self) or croak "$self is not an object";
225 my $name = $AUTOLOAD;
229 unless (exists $fields{$name}) {
230 croak "Cannot access '$name' field of class '$class'";
234 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
235 return $self->{$name} = shift;
237 return $self->{$name};
241 sub status_update { # FIXME: this looks unimplemented
242 my ($self, $props) = @_;
243 my $status = C4::SIP::ILS::Transaction->new();
244 $self->{sip_item_properties} = $props;
251 return $self->{title};
254 sub sip_circulation_status {
256 if ( $self->{patron} ) {
257 return '04'; # charged
259 elsif ( grep { $_->{itemnumber} == $self->{itemnumber} } @{ $self->{hold_shelf} } ) {
260 return '08'; # waiting on hold shelf
263 return '03'; # available
264 } # FIXME: 01-13 enumerated in spec.
267 sub sip_security_marker {
269 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
273 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
278 return $self->{fee} || 0;
282 return $self->{currency} || 'USD';
286 return $self->{homebranch};
290 (defined $self->{hold_queue}) or return [];
291 return $self->{hold_queue};
295 (defined $self->{pending_queue}) or return [];
296 return $self->{pending_queue};
300 (defined $self->{hold_shelf}) or return [];
301 return $self->{hold_shelf};
304 sub hold_queue_position {
305 my ($self, $patron_id) = @_;
306 ($self->{hold_queue}) or return 0;
308 foreach (@{$self->{hold_queue}}) {
310 $_->{patron_id} or next;
311 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
312 return $i; # maybe should return $_->{priority}
320 return $self->{due_date} || 0;
324 return $self->{recall_date} || 0;
326 sub hold_pickup_date {
328 return $self->{hold_pickup_date} || 0;
331 # This is a partial check of "availability". It is not supposed to check everything here.
332 # An item is available for a patron if it is:
333 # 1) checked out to the same patron
334 # AND no pending (i.e. non-W) hold queue
337 # AND (not on hold_shelf OR is on hold_shelf for patron)
339 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
340 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
341 # patron has retrieved the item before the librarian.
343 # We don't check if the patron is at the front of the pending queue in the first case, because
344 # they should not be able to place a hold on an item they already have.
347 my ($self, $for_patron) = @_;
348 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
349 my $count2 = (defined $self->{hold_shelf} ) ? scalar @{$self->{hold_shelf} } : 0;
350 $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
351 if (defined($self->{patron_id})) {
352 ($self->{patron_id} eq $for_patron) or return 0;
353 return ($count ? 0 : 1);
354 } else { # not checked out
355 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
360 sub _barcode_to_borrowernumber {
362 return unless defined $known;
363 my $patron = Koha::Patrons->find( { cardnumber => $known } ) or return;
364 return $patron->borrowernumber
366 sub barcode_is_borrowernumber { # because hold_queue only has borrowernumber...
369 my $number = shift or return; # can't be zero
370 return unless defined $barcode; # might be 0 or 000 or 000000
371 my $converted = _barcode_to_borrowernumber($barcode);
372 return unless $converted;
373 return ($number == $converted);
377 my $hold = shift or return;
378 foreach (qw(biblionumber borrowernumber reservedate)) {
379 $hold->{$_} or return;
381 return ModReserveFill($hold);