4 # A Class for hiding the ILS's concept of the item from OpenSIP
7 package C4::SIP::ILS::Item;
12 use C4::SIP::Sip qw(siplog);
16 use C4::SIP::ILS::Transaction;
17 use C4::SIP::Sip qw(add_field);
20 use C4::Circulation qw( barcodedecode );
24 use C4::Reserves qw( ModReserveFill );
26 use Koha::Checkouts::ReturnClaims;
29 use Koha::DateUtils qw( dt_from_string );
40 title => "Perl 5 desktop reference",
42 sip_media_type => '001',
47 title => "The deep blue alibi",
49 sip_media_type => '001',
55 reservenotes => undef,
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',
77 my ($class, $item_id) = @_;
78 my $type = ref($class) || $class;
79 my $item = Koha::Items->find( { barcode => barcodedecode( $item_id ) } );
81 siplog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
82 warn "new ILS::Item($item_id) : No item '$item_id'.";
85 my $self = $item->unblessed;
86 $self->{_object} = $item;
87 $self->{id} = $item->barcode; # to SIP, the barcode IS the id.
88 $self->{permanent_location} = $item->homebranch;
89 $self->{collection_code} = $item->ccode;
90 $self->{call_number} = $item->itemcallnumber;
91 $self->{'shelving_location'} = $item->location;
92 $self->{'permanent_shelving_location'} = $item->permanent_location;
94 $self->{object} = $item;
96 my $it = $item->effective_itemtype;
97 my $itemtype = Koha::Database->new()->schema()->resultset('Itemtype')->find( $it );
98 $self->{sip_media_type} = $itemtype->sip_media_type() if $itemtype;
100 # check if its on issue and if so get the borrower
101 my $issue = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
103 $self->{due_date} = dt_from_string( $issue->date_due, 'sql' )->truncate( to => 'minute' );
104 my $patron = Koha::Patrons->find( $issue->borrowernumber );
105 $self->{borrowernumber} = $patron->borrowernumber;
107 my $biblio = Koha::Biblios->find( $self->{biblionumber} );
108 my $holds = $biblio->current_holds->unblessed;
109 $self->{hold_queue} = $holds;
110 $self->{hold_attached} = [( grep { defined $_->{found} and ( $_->{found} eq 'W' or $_->{found} eq 'P' or $_->{found} eq 'T' ) } @{$self->{hold_queue}} )];
111 $self->{pending_queue} = [( grep {(! defined $_->{found}) or ( $_->{found} ne 'W' and $_->{found} ne 'P' and $_->{found} ne 'T' ) } @{$self->{hold_queue}} )];
112 $self->{title} = $biblio->title;
113 $self->{author} = $biblio->author;
116 siplog( "LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
117 $item_id, $self->{title} // '' );
128 sip_item_properties => 0,
130 permanent_location => 0,
131 current_location => 0,
138 collection_code => 0,
139 shelving_location => 0,
140 permanent_shelving_location => 0,
150 # use Data::Dumper; warn "next_hold() hold_attached: " . Dumper($self->{hold_attached}); warn "next_hold() pending_queue: " . $self->{pending_queue};
151 foreach (@{$self->hold_attached}) { # If this item was taken from the hold shelf, then that reserve still governs
152 next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
155 if (scalar @{$self->{pending_queue}}) { # Otherwise, if there is at least one hold, the first (best priority) gets it
156 return $self->{pending_queue}->[0];
161 # hold_patron_id is NOT the barcode. It's the borrowernumber.
162 # If a return triggers capture for a hold the borrowernumber is passed
163 # and saved so that other hold info can be retrieved
168 $self->{hold}->{borrowernumber} = $id;
170 if ($self->{hold} ) {
171 return $self->{hold}->{borrowernumber};
176 sub hold_patron_name {
177 my ( $self, $template ) = @_;
178 my $borrowernumber = $self->hold_patron_id() or return q{};
181 my $tt = Template->new();
183 my $patron = Koha::Patrons->find($borrowernumber);
186 $tt->process( \$template, { patron => $patron }, \$output );
190 my $holder = Koha::Patrons->find( $borrowernumber );
192 siplog("LOG_ERR", "While checking hold, failed to retrieve the patron with borrowernumber '$borrowernumber'");
195 my $email = $holder->email || '';
196 my $phone = $holder->phone || '';
197 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
198 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
199 "" ; # neither populated, empty string
200 my $name = $holder->firstname ? $holder->firstname . ' ' : '';
201 $name .= $holder->surname . $extra;
205 sub hold_patron_bcode {
207 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return q{};
208 my $holder = Koha::Patrons->find( $borrowernumber );
209 if ($holder and $holder->cardnumber ) {
210 return $holder->cardnumber;
215 sub destination_loc {
219 $self->{dest_loc} = $set_loc;
221 if ($self->{dest_loc} ) {
222 return $self->{dest_loc};
229 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
233 my $class = ref($self) or croak "$self is not an object";
234 my $name = $AUTOLOAD;
238 unless (exists $fields{$name}) {
239 croak "Cannot access '$name' field of class '$class'";
243 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
244 return $self->{$name} = shift;
246 return $self->{$name};
250 sub status_update { # FIXME: this looks unimplemented
251 my ($self, $props) = @_;
252 my $status = C4::SIP::ILS::Transaction->new();
253 $self->{sip_item_properties} = $props;
260 return $self->{title};
263 sub sip_circulation_status {
265 if ( $self->{_object}->get_transfer ) {
266 return '10'; # in transit between libraries
268 elsif ( Koha::Checkouts::ReturnClaims->search({ itemnumber => $self->{_object}->id, resolution => undef })->count ) {
269 return '11'; # claimed returned
271 elsif ( $self->{itemlost} ) {
274 elsif ( $self->{borrowernumber} ) {
275 return '04'; # charged
277 elsif ( grep { $_->{itemnumber} == $self->{itemnumber} } @{ $self->{hold_attached} } ) {
278 return '08'; # waiting on hold shelf
280 elsif ( $self->{location} eq 'CART' ) {
281 return '09'; # waiting to be re-shelved
283 elsif ( $self->{damaged} ) {
284 return '01'; # damaged
286 elsif ( $self->{notforloan} < 0 ) {
287 return '02'; # on order
290 return '03'; # available
291 } # FIXME: 01-13 enumerated in spec.
294 sub sip_security_marker {
296 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
300 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
305 return $self->{fee} || 0;
309 return $self->{currency} || 'USD';
313 return $self->{homebranch};
317 (defined $self->{hold_queue}) or return [];
318 return $self->{hold_queue};
322 (defined $self->{pending_queue}) or return [];
323 return $self->{pending_queue};
327 (defined $self->{hold_attached}) or return [];
328 return $self->{hold_attached};
331 sub hold_queue_position {
332 my ($self, $patron_id) = @_;
333 ($self->{hold_queue}) or return 0;
335 foreach (@{$self->{hold_queue}}) {
337 $_->{patron_id} or next;
338 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
339 return $i; # maybe should return $_->{priority}
347 return $self->{due_date} || 0;
351 return $self->{recall_date} || 0;
353 sub hold_pickup_date {
356 my $hold = Koha::Holds->find({ itemnumber => $self->{itemnumber}, found => 'W' });
358 return $hold->expirationdate || 0;
364 # This is a partial check of "availability". It is not supposed to check everything here.
365 # An item is available for a patron if it is:
366 # 1) checked out to the same patron
367 # AND no pending (i.e. non-W) hold queue
370 # AND (not on hold_attached OR is on hold_attached for patron)
372 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
373 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
374 # patron has retrieved the item before the librarian.
376 # We don't check if the patron is at the front of the pending queue in the first case, because
377 # they should not be able to place a hold on an item they already have.
380 my ($self, $for_patron) = @_;
381 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
382 my $count2 = (defined $self->{hold_attached} ) ? scalar @{$self->{hold_attached} } : 0;
383 if (defined($self->{borrowernumber})) {
384 ($self->{borrowernumber} eq $for_patron) or return 0;
385 return ($count ? 0 : 1);
386 } else { # not checked out
387 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_attached}[0]->{borrowernumber});
392 sub _barcode_to_borrowernumber {
394 return unless defined $known;
395 my $patron = Koha::Patrons->find( { cardnumber => $known } ) or return;
396 return $patron->borrowernumber
398 sub barcode_is_borrowernumber { # because hold_queue only has borrowernumber...
401 my $number = shift or return; # can't be zero
402 return unless defined $barcode; # might be 0 or 000 or 000000
403 my $converted = _barcode_to_borrowernumber($barcode);
404 return unless $converted;
405 return ($number == $converted);
409 my $hold = shift or return;
410 foreach (qw(biblionumber borrowernumber reservedate)) {
411 $hold->{$_} or return;
413 return ModReserveFill($hold);
416 =head2 build_additional_item_fields_string
418 This method builds the part of the sip message for additional item fields
419 to send in the item related message responses
423 sub build_additional_item_fields_string {
424 my ( $self, $server ) = @_;
428 if ( $server->{account}->{item_field} ) {
430 ref $server->{account}->{item_field} eq "ARRAY"
431 ? @{ $server->{account}->{item_field} }
432 : ( $server->{account}->{item_field} );
434 foreach my $f ( @fields_to_send ) {
435 my $code = $f->{code};
436 my $value = $self->{object}->$code;
437 $string .= add_field( $f->{field}, $value );