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 maybe_add);
20 use C4::Circulation qw( barcodedecode );
25 use Koha::Checkouts::ReturnClaims;
28 use Koha::DateUtils qw( dt_from_string );
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 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',
76 my ($class, $item_id) = @_;
77 my $type = ref($class) || $class;
78 my $item = Koha::Items->find( { barcode => barcodedecode( $item_id ) } );
80 siplog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
81 warn "new ILS::Item($item_id) : No item '$item_id'.";
84 my $self = $item->unblessed;
85 $self->{_object} = $item;
86 $self->{id} = $item->barcode; # to SIP, the barcode IS the id.
87 if (C4::Context->preference('UseLocationAsAQInSIP')) {
88 $self->{permanent_location} = $item->permanent_location;
90 $self->{permanent_location} = $item->homebranch;
92 $self->{collection_code} = $item->ccode;
93 $self->{call_number} = $item->itemcallnumber;
94 $self->{'shelving_location'} = $item->location;
95 $self->{'permanent_shelving_location'} = $item->permanent_location;
97 $self->{object} = $item;
99 my $it = $item->effective_itemtype;
100 $self->{itemtype} = $it;
101 my $itemtype = Koha::Database->new()->schema()->resultset('Itemtype')->find( $it );
102 $self->{sip_media_type} = $itemtype->sip_media_type() if $itemtype;
104 # check if its on issue and if so get the borrower
105 my $issue = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
107 $self->{due_date} = dt_from_string( $issue->date_due, 'sql' )->truncate( to => 'minute' );
108 my $patron = Koha::Patrons->find( $issue->borrowernumber );
109 $self->{borrowernumber} = $patron->borrowernumber;
111 my $biblio = Koha::Biblios->find( $self->{biblionumber} );
112 my $holds = $biblio->current_holds->unblessed;
113 $self->{hold_queue} = $holds;
114 $self->{hold_attached} = [( grep { defined $_->{found} and ( $_->{found} eq 'W' or $_->{found} eq 'P' or $_->{found} eq 'T' ) } @{$self->{hold_queue}} )];
115 $self->{pending_queue} = [( grep {(! defined $_->{found}) or ( $_->{found} ne 'W' and $_->{found} ne 'P' and $_->{found} ne 'T' ) } @{$self->{hold_queue}} )];
116 $self->{title} = $biblio->title;
117 $self->{author} = $biblio->author;
120 siplog( "LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
121 $item_id, $self->{title} // '' );
132 sip_item_properties => 0,
134 permanent_location => 0,
135 current_location => 0,
142 collection_code => 0,
143 shelving_location => 0,
144 permanent_shelving_location => 0,
155 # use Data::Dumper; warn "next_hold() hold_attached: " . Dumper($self->{hold_attached}); warn "next_hold() pending_queue: " . $self->{pending_queue};
156 foreach (@{$self->hold_attached}) { # If this item was taken from the hold shelf, then that reserve still governs
157 next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
160 if (scalar @{$self->{pending_queue}}) { # Otherwise, if there is at least one hold, the first (best priority) gets it
161 return $self->{pending_queue}->[0];
166 # hold_patron_id is NOT the barcode. It's the borrowernumber.
167 # If a return triggers capture for a hold the borrowernumber is passed
168 # and saved so that other hold info can be retrieved
173 $self->{hold}->{borrowernumber} = $id;
175 if ($self->{hold} ) {
176 return $self->{hold}->{borrowernumber};
181 sub hold_patron_name {
182 my ( $self, $template ) = @_;
183 my $borrowernumber = $self->hold_patron_id() or return q{};
186 my $tt = Template->new();
188 my $patron = Koha::Patrons->find($borrowernumber);
191 $tt->process( \$template, { patron => $patron }, \$output );
195 my $holder = Koha::Patrons->find( $borrowernumber );
197 siplog("LOG_ERR", "While checking hold, failed to retrieve the patron with borrowernumber '$borrowernumber'");
200 my $email = $holder->email || '';
201 my $phone = $holder->phone || '';
202 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
203 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
204 "" ; # neither populated, empty string
205 my $name = $holder->firstname ? $holder->firstname . ' ' : '';
206 $name .= $holder->surname . $extra;
210 sub hold_patron_bcode {
212 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return q{};
213 my $holder = Koha::Patrons->find( $borrowernumber );
214 if ($holder and $holder->cardnumber ) {
215 return $holder->cardnumber;
220 sub destination_loc {
224 $self->{dest_loc} = $set_loc;
226 if ($self->{dest_loc} ) {
227 return $self->{dest_loc};
234 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
238 my $class = ref($self) or croak "$self is not an object";
239 my $name = $AUTOLOAD;
243 unless (exists $fields{$name}) {
244 croak "Cannot access '$name' field of class '$class'";
248 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
249 return $self->{$name} = shift;
251 return $self->{$name};
255 sub status_update { # FIXME: this looks unimplemented
256 my ($self, $props) = @_;
257 my $status = C4::SIP::ILS::Transaction->new();
258 $self->{sip_item_properties} = $props;
265 return $self->{title};
268 sub sip_circulation_status {
270 if ( $self->{_object}->get_transfer ) {
271 return '10'; # in transit between libraries
273 elsif ( Koha::Checkouts::ReturnClaims->search({ itemnumber => $self->{_object}->id, resolution => undef })->count ) {
274 return '11'; # claimed returned
276 elsif ( $self->{itemlost} ) {
279 elsif ( $self->{borrowernumber} ) {
280 return '04'; # charged
282 elsif ( grep { $_->{itemnumber} == $self->{itemnumber} } @{ $self->{hold_attached} } ) {
283 return '08'; # waiting on hold shelf
285 elsif ( $self->{location} and $self->{location} eq 'CART' ) {
286 return '09'; # waiting to be re-shelved
288 elsif ( $self->{damaged} ) {
289 return '01'; # damaged
291 elsif ( $self->{notforloan} < 0 ) {
292 return '02'; # on order
295 return '03'; # available
296 } # FIXME: 01-13 enumerated in spec.
299 sub sip_security_marker {
301 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
305 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
310 return $self->{fee} || 0;
314 return $self->{currency} || 'USD';
318 return $self->{homebranch};
322 (defined $self->{hold_queue}) or return [];
323 return $self->{hold_queue};
327 (defined $self->{pending_queue}) or return [];
328 return $self->{pending_queue};
332 (defined $self->{hold_attached}) or return [];
333 return $self->{hold_attached};
336 sub hold_queue_position {
337 my ($self, $patron_id) = @_;
338 ($self->{hold_queue}) or return 0;
340 foreach (@{$self->{hold_queue}}) {
342 $_->{patron_id} or next;
343 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
344 return $i; # maybe should return $_->{priority}
352 return $self->{due_date} || 0;
356 return $self->{recall_date} || 0;
358 sub hold_pickup_date {
361 my $hold = Koha::Holds->find({ itemnumber => $self->{itemnumber}, found => 'W' });
363 return $hold->expirationdate || 0;
369 # This is a partial check of "availability". It is not supposed to check everything here.
370 # An item is available for a patron if it is:
371 # 1) checked out to the same patron
372 # AND no pending (i.e. non-W) hold queue
375 # AND (not on hold_attached OR is on hold_attached for patron)
377 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
378 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
379 # patron has retrieved the item before the librarian.
381 # We don't check if the patron is at the front of the pending queue in the first case, because
382 # they should not be able to place a hold on an item they already have.
385 my ($self, $for_patron) = @_;
386 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
387 my $count2 = (defined $self->{hold_attached} ) ? scalar @{$self->{hold_attached} } : 0;
388 if (defined($self->{borrowernumber})) {
389 ($self->{borrowernumber} eq $for_patron) or return 0;
390 return ($count ? 0 : 1);
391 } else { # not checked out
392 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_attached}[0]->{borrowernumber});
397 sub _barcode_to_borrowernumber {
399 return unless defined $known;
400 my $patron = Koha::Patrons->find( { cardnumber => $known } ) or return;
401 return $patron->borrowernumber
403 sub barcode_is_borrowernumber { # because hold_queue only has borrowernumber...
406 my $number = shift or return; # can't be zero
407 return unless defined $barcode; # might be 0 or 000 or 000000
408 my $converted = _barcode_to_borrowernumber($barcode);
409 return unless $converted;
410 return ($number == $converted);
413 =head2 build_additional_item_fields_string
415 This method builds the part of the sip message for additional item fields
416 to send in the item related message responses
420 sub build_additional_item_fields_string {
421 my ( $self, $server ) = @_;
425 if ( $server->{account}->{item_field} ) {
427 ref $server->{account}->{item_field} eq "ARRAY"
428 ? @{ $server->{account}->{item_field} }
429 : ( $server->{account}->{item_field} );
431 foreach my $f ( @fields_to_send ) {
432 my $code = $f->{code};
433 my $value = $self->{object}->$code;
434 $string .= add_field( $f->{field}, $value );
438 if ( $server->{account}->{custom_item_field} ) {
440 ref $server->{account}->{custom_item_field} eq "ARRAY"
441 ? @{ $server->{account}->{custom_item_field} }
442 : $server->{account}->{custom_item_field};
444 foreach my $custom_field ( @custom_fields ) {
445 my $field = $custom_field->{field};
446 return q{} unless defined $field;
447 my $template = $custom_field->{template};
448 my $formatted = $self->format( $template );
449 my $substring = maybe_add( $field, $formatted, $server );
450 $string .= $substring;
457 =head2 build_custom_field_string
459 This method builds the part of the sip message for custom item fields as defined in the sip config
463 sub build_custom_field_string {
464 my ( $self, $server ) = @_;
474 This method uses a template to build a string from a Koha::Item object
475 If errors are encountered in processing template we log them and return nothing
480 my ( $self, $template ) = @_;
485 my $tt = Template->new();
487 my $item = $self->{_object};
491 $tt->process( \$template, { item => $item }, \$output );
494 siplog("LOG_DEBUG", "Error processing template: $template");