Bug 25344: Add support for circulation status 10 ( item in transit )
[koha.git] / C4 / SIP / ILS / Item.pm
blobd5e61362bd9c690166f16f76950119ae459a23ef
2 # ILS::Item.pm
3 #
4 # A Class for hiding the ILS's concept of the item from OpenSIP
7 package C4::SIP::ILS::Item;
9 use strict;
10 use warnings;
12 use C4::SIP::Sip qw(siplog);
13 use Carp;
14 use Template;
16 use C4::SIP::ILS::Transaction;
17 use C4::SIP::Sip qw(add_field);
19 use C4::Debug;
20 use C4::Context;
21 use C4::Biblio;
22 use C4::Items;
23 use C4::Circulation;
24 use C4::Members;
25 use C4::Reserves;
26 use Koha::Database;
27 use Koha::Biblios;
28 use Koha::Checkouts;
29 use Koha::DateUtils;
30 use Koha::Patrons;
31 use Koha::Items;
32 use Koha::Holds;
34 =encoding UTF-8
36 =head1 EXAMPLE
38 our %item_db = (
39 '1565921879' => {
40 title => "Perl 5 desktop reference",
41 id => '1565921879',
42 sip_media_type => '001',
43 magnetic_media => 0,
44 hold_queue => [],
46 '0440242746' => {
47 title => "The deep blue alibi",
48 id => '0440242746',
49 sip_media_type => '001',
50 magnetic_media => 0,
51 hold_queue => [
53 itemnumber => '823',
54 priority => '1',
55 reservenotes => undef,
56 reservedate => '2008-10-09',
57 found => undef,
58 rtimestamp => '2008-10-09 11:15:06',
59 biblionumber => '406',
60 borrowernumber => '756',
61 branchcode => 'CPL'
65 '660' => {
66 title => "Harry Potter y el cáliz de fuego",
67 id => '660',
68 sip_media_type => '001',
69 magnetic_media => 0,
70 hold_queue => [],
74 =cut
76 sub new {
77 my ($class, $item_id) = @_;
78 my $type = ref($class) || $class;
79 my $item = Koha::Items->find( { barcode => barcodedecode( $item_id ) } );
80 unless ( $item ) {
81 siplog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
82 warn "new ILS::Item($item_id) : No item '$item_id'.";
83 return;
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;
92 $self->{object} = $item;
94 my $it = $item->effective_itemtype;
95 my $itemtype = Koha::Database->new()->schema()->resultset('Itemtype')->find( $it );
96 $self->{sip_media_type} = $itemtype->sip_media_type() if $itemtype;
98 # check if its on issue and if so get the borrower
99 my $issue = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
100 if ($issue) {
101 $self->{due_date} = dt_from_string( $issue->date_due, 'sql' )->truncate( to => 'minute' );
102 my $patron = Koha::Patrons->find( $issue->borrowernumber );
103 $self->{borrowernumber} = $patron->borrowernumber;
105 my $biblio = Koha::Biblios->find( $self->{biblionumber} );
106 my $holds = $biblio->current_holds->unblessed;
107 $self->{hold_queue} = $holds;
108 $self->{hold_shelf} = [( grep { defined $_->{found} and $_->{found} eq 'W' } @{$self->{hold_queue}} )];
109 $self->{pending_queue} = [( grep {(! defined $_->{found}) or $_->{found} ne 'W' } @{$self->{hold_queue}} )];
110 $self->{title} = $biblio->title;
111 $self->{author} = $biblio->author;
112 bless $self, $type;
114 siplog( "LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
115 $item_id, $self->{title} // '' );
117 return $self;
120 # 0 means read-only
121 # 1 means read/write
123 my %fields = (
124 id => 0,
125 sip_media_type => 0,
126 sip_item_properties => 0,
127 magnetic_media => 0,
128 permanent_location => 0,
129 current_location => 0,
130 print_line => 1,
131 screen_msg => 1,
132 itemnumber => 0,
133 biblionumber => 0,
134 barcode => 0,
135 onloan => 0,
136 collection_code => 0,
137 call_number => 0,
138 enumchron => 0,
139 location => 0,
140 author => 0,
141 title => 0,
144 sub next_hold {
145 my $self = shift;
146 # use Data::Dumper; warn "next_hold() hold_shelf: " . Dumper($self->{hold_shelf}); warn "next_hold() pending_queue: " . $self->{pending_queue};
147 foreach (@{$self->hold_shelf}) { # If this item was taken from the hold shelf, then that reserve still governs
148 next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
149 return $_;
151 if (scalar @{$self->{pending_queue}}) { # Otherwise, if there is at least one hold, the first (best priority) gets it
152 return $self->{pending_queue}->[0];
154 return;
157 # hold_patron_id is NOT the barcode. It's the borrowernumber.
158 # If a return triggers capture for a hold the borrowernumber is passed
159 # and saved so that other hold info can be retrieved
160 sub hold_patron_id {
161 my $self = shift;
162 my $id = shift;
163 if ($id) {
164 $self->{hold}->{borrowernumber} = $id;
166 if ($self->{hold} ) {
167 return $self->{hold}->{borrowernumber};
169 return;
172 sub hold_patron_name {
173 my ( $self, $template ) = @_;
174 my $borrowernumber = $self->hold_patron_id() or return q{};
176 if ($template) {
177 my $tt = Template->new();
179 my $patron = Koha::Patrons->find($borrowernumber);
181 my $output;
182 $tt->process( \$template, { patron => $patron }, \$output );
183 return $output;
186 my $holder = Koha::Patrons->find( $borrowernumber );
187 unless ($holder) {
188 siplog("LOG_ERR", "While checking hold, failed to retrieve the patron with borrowernumber '$borrowernumber'");
189 return q{};
191 my $email = $holder->email || '';
192 my $phone = $holder->phone || '';
193 my $extra = ($email and $phone) ? " ($email, $phone)" : # both populated, employ comma
194 ($email or $phone) ? " ($email$phone)" : # only 1 populated, we don't care which: no comma
195 "" ; # neither populated, empty string
196 my $name = $holder->firstname ? $holder->firstname . ' ' : '';
197 $name .= $holder->surname . $extra;
198 return $name;
201 sub hold_patron_bcode {
202 my $self = shift;
203 my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return q{};
204 my $holder = Koha::Patrons->find( $borrowernumber );
205 if ($holder and $holder->cardnumber ) {
206 return $holder->cardnumber;
208 return q();
211 sub destination_loc {
212 my $self = shift;
213 my $set_loc = shift;
214 if ($set_loc) {
215 $self->{dest_loc} = $set_loc;
217 if ($self->{dest_loc} ) {
218 return $self->{dest_loc};
220 return q{};
223 our $AUTOLOAD;
225 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
227 sub AUTOLOAD {
228 my $self = shift;
229 my $class = ref($self) or croak "$self is not an object";
230 my $name = $AUTOLOAD;
232 $name =~ s/.*://;
234 unless (exists $fields{$name}) {
235 croak "Cannot access '$name' field of class '$class'";
238 if (@_) {
239 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
240 return $self->{$name} = shift;
241 } else {
242 return $self->{$name};
246 sub status_update { # FIXME: this looks unimplemented
247 my ($self, $props) = @_;
248 my $status = C4::SIP::ILS::Transaction->new();
249 $self->{sip_item_properties} = $props;
250 $status->{ok} = 1;
251 return $status;
254 sub title_id {
255 my $self = shift;
256 return $self->{title};
259 sub sip_circulation_status {
260 my $self = shift;
261 if ( $self->{_object}->get_transfer ) {
262 return '10'; # in transit between libraries
264 elsif ( $self->{borrowernumber} ) {
265 return '04'; # charged
267 elsif ( grep { $_->{itemnumber} == $self->{itemnumber} } @{ $self->{hold_shelf} } ) {
268 return '08'; # waiting on hold shelf
270 else {
271 return '03'; # available
272 } # FIXME: 01-13 enumerated in spec.
275 sub sip_security_marker {
276 my $self = shift;
277 return '02'; # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
279 sub sip_fee_type {
280 my $self = shift;
281 return '01'; # FIXME? 01-09 enumerated in spec. We just use O1-other/unknown.
284 sub fee {
285 my $self = shift;
286 return $self->{fee} || 0;
288 sub fee_currency {
289 my $self = shift;
290 return $self->{currency} || 'USD';
292 sub owner {
293 my $self = shift;
294 return $self->{homebranch};
296 sub hold_queue {
297 my $self = shift;
298 (defined $self->{hold_queue}) or return [];
299 return $self->{hold_queue};
301 sub pending_queue {
302 my $self = shift;
303 (defined $self->{pending_queue}) or return [];
304 return $self->{pending_queue};
306 sub hold_shelf {
307 my $self = shift;
308 (defined $self->{hold_shelf}) or return [];
309 return $self->{hold_shelf};
312 sub hold_queue_position {
313 my ($self, $patron_id) = @_;
314 ($self->{hold_queue}) or return 0;
315 my $i = 0;
316 foreach (@{$self->{hold_queue}}) {
317 $i++;
318 $_->{patron_id} or next;
319 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
320 return $i; # maybe should return $_->{priority}
323 return 0;
326 sub due_date {
327 my $self = shift;
328 return $self->{due_date} || 0;
330 sub recall_date {
331 my $self = shift;
332 return $self->{recall_date} || 0;
334 sub hold_pickup_date {
335 my $self = shift;
337 my $hold = Koha::Holds->find({ itemnumber => $self->{itemnumber}, found => 'W' });
338 if ( $hold ) {
339 return $hold->expirationdate || 0;
342 return 0;
345 # This is a partial check of "availability". It is not supposed to check everything here.
346 # An item is available for a patron if it is:
347 # 1) checked out to the same patron
348 # AND no pending (i.e. non-W) hold queue
349 # OR
350 # 2) not checked out
351 # AND (not on hold_shelf OR is on hold_shelf for patron)
353 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
354 # have non-W holds on it, but has not been "picked" from the stacks. That is to say, the
355 # patron has retrieved the item before the librarian.
357 # We don't check if the patron is at the front of the pending queue in the first case, because
358 # they should not be able to place a hold on an item they already have.
360 sub available {
361 my ($self, $for_patron) = @_;
362 my $count = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
363 my $count2 = (defined $self->{hold_shelf} ) ? scalar @{$self->{hold_shelf} } : 0;
364 $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
365 if (defined($self->{borrowernumber})) {
366 ($self->{borrowernumber} eq $for_patron) or return 0;
367 return ($count ? 0 : 1);
368 } else { # not checked out
369 ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
371 return 0;
374 sub _barcode_to_borrowernumber {
375 my $known = shift;
376 return unless defined $known;
377 my $patron = Koha::Patrons->find( { cardnumber => $known } ) or return;
378 return $patron->borrowernumber
380 sub barcode_is_borrowernumber { # because hold_queue only has borrowernumber...
381 my $self = shift;
382 my $barcode = shift;
383 my $number = shift or return; # can't be zero
384 return unless defined $barcode; # might be 0 or 000 or 000000
385 my $converted = _barcode_to_borrowernumber($barcode);
386 return unless $converted;
387 return ($number == $converted);
389 sub fill_reserve {
390 my $self = shift;
391 my $hold = shift or return;
392 foreach (qw(biblionumber borrowernumber reservedate)) {
393 $hold->{$_} or return;
395 return ModReserveFill($hold);
398 =head2 build_additional_item_fields_string
400 This method builds the part of the sip message for additional item fields
401 to send in the item related message responses
403 =cut
405 sub build_additional_item_fields_string {
406 my ( $self, $server ) = @_;
408 my $string = q{};
410 if ( $server->{account}->{item_field} ) {
411 my @fields_to_send =
412 ref $server->{account}->{item_field} eq "ARRAY"
413 ? @{ $server->{account}->{item_field} }
414 : ( $server->{account}->{item_field} );
416 foreach my $f ( @fields_to_send ) {
417 my $code = $f->{code};
418 my $value = $self->{object}->$code;
419 $string .= add_field( $f->{field}, $value );
423 return $string;
427 __END__