Bug 15438 - Checking out an on-hold item sends holder's borrowernumber in AF (screen...
[koha.git] / C4 / SIP / ILS / Item.pm
1 #
2 # ILS::Item.pm
3
4 # A Class for hiding the ILS's concept of the item from OpenSIP
5 #
6
7 package C4::SIP::ILS::Item;
8
9 use strict;
10 use warnings;
11
12 use Sys::Syslog qw(syslog);
13 use Carp;
14 use Template;
15
16 use C4::SIP::ILS::Transaction;
17
18 use C4::Debug;
19 use C4::Context;
20 use C4::Biblio;
21 use C4::Items;
22 use C4::Circulation;
23 use C4::Members;
24 use C4::Reserves;
25 use Koha::Database;
26 use Koha::Biblios;
27 use Koha::Checkouts;
28 use Koha::DateUtils;
29 use Koha::Patrons;
30 use Koha::Items;
31
32 =encoding UTF-8
33
34 =head1 EXAMPLE
35
36  our %item_db = (
37     '1565921879' => {
38         title => "Perl 5 desktop reference",
39         id => '1565921879',
40         sip_media_type => '001',
41         magnetic_media => 0,
42         hold_queue => [],
43     },
44     '0440242746' => {
45         title => "The deep blue alibi",
46         id => '0440242746',
47         sip_media_type => '001',
48         magnetic_media => 0,
49         hold_queue => [
50             {
51             itemnumber => '823',
52             priority => '1',
53             reservenotes => undef,
54             reservedate => '2008-10-09',
55             found => undef,
56             rtimestamp => '2008-10-09 11:15:06',
57             biblionumber => '406',
58             borrowernumber => '756',
59             branchcode => 'CPL'
60             }
61         ],
62     },
63     '660' => {
64         title => "Harry Potter y el cáliz de fuego",
65         id => '660',
66         sip_media_type => '001',
67         magnetic_media => 0,
68         hold_queue => [],
69     },
70 );
71
72 =cut
73
74 sub new {
75     my ($class, $item_id) = @_;
76     my $type = ref($class) || $class;
77     my $item = Koha::Items->find( { barcode => $item_id } );
78     unless ( $item ) {
79         syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
80         warn "new ILS::Item($item_id) : No item '$item_id'.";
81         return;
82     }
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;
88
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;
92
93     # check if its on issue and if so get the borrower
94     my $issue = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
95     if ($issue) {
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;
99     }
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     bless $self, $type;
106
107     syslog( "LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
108         $item_id, $self->{title} // '' );
109
110     return $self;
111 }
112
113 # 0 means read-only
114 # 1 means read/write
115
116 my %fields = (
117     id                  => 0,
118     sip_media_type      => 0,
119     sip_item_properties => 0,
120     magnetic_media      => 0,
121     permanent_location  => 0,
122     current_location    => 0,
123     print_line          => 1,
124     screen_msg          => 1,
125     itemnumber          => 0,
126     biblionumber        => 0,
127     barcode             => 0,
128     onloan              => 0,
129     collection_code     => 0,
130     call_number         => 0,
131     enumchron           => 0,
132     location            => 0,
133     author              => 0,
134     title               => 0,
135 );
136
137 sub next_hold {
138     my $self = shift;
139     # use Data::Dumper; warn "next_hold() hold_shelf: " . Dumper($self->{hold_shelf}); warn "next_hold() pending_queue: " . $self->{pending_queue};
140     foreach (@{$self->hold_shelf}) {    # If this item was taken from the hold shelf, then that reserve still governs
141         next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
142         return $_;
143     }
144     if (scalar @{$self->{pending_queue}}) {    # Otherwise, if there is at least one hold, the first (best priority) gets it
145         return  $self->{pending_queue}->[0];
146     }
147     return;
148 }
149
150 # hold_patron_id is NOT the barcode.  It's the borrowernumber.
151 # If a return triggers capture for a hold the borrowernumber is passed
152 # and saved so that other hold info can be retrieved
153 sub hold_patron_id {
154     my $self = shift;
155     my $id   = shift;
156     if ($id) {
157         $self->{hold}->{borrowernumber} = $id;
158     }
159     if ($self->{hold} ) {
160         return $self->{hold}->{borrowernumber};
161     }
162     return;
163
164 }
165 sub hold_patron_name {
166     my ( $self, $template ) = @_;
167     my $borrowernumber = $self->hold_patron_id() or return;
168
169     if ($template) {
170         my $tt = Template->new();
171
172         my $patron = Koha::Patrons->find($borrowernumber);
173
174         my $output;
175         $tt->process( \$template, { patron => $patron }, \$output );
176         return $output;
177     }
178
179     my $holder = Koha::Patrons->find( $borrowernumber );
180     unless ($holder) {
181         syslog("LOG_ERR", "While checking hold, failed to retrieve the patron with borrowernumber '$borrowernumber'");
182         return;
183     }
184     my $email = $holder->email || '';
185     my $phone = $holder->phone || '';
186     my $extra = ($email and $phone) ? " ($email, $phone)" :  # both populated, employ comma
187                 ($email or  $phone) ? " ($email$phone)"   :  # only 1 populated, we don't care which: no comma
188                 "" ;                                         # neither populated, empty string
189     my $name = $holder->firstname ? $holder->firstname . ' ' : '';
190     $name .= $holder->surname . $extra;
191     return $name;
192 }
193
194 sub hold_patron_bcode {
195     my $self = shift;
196     my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
197     my $holder = Koha::Patrons->find( $borrowernumber );
198     if ($holder and $holder->cardnumber ) {
199         return $holder->cardnumber;
200     }
201     return;
202 }
203
204 sub destination_loc {
205     my $self = shift;
206     my $set_loc = shift;
207     if ($set_loc) {
208         $self->{dest_loc} = $set_loc;
209     }
210     if ($self->{dest_loc} ) {
211         return $self->{dest_loc};
212     }
213     return q{};
214 }
215
216 our $AUTOLOAD;
217
218 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
219
220 sub AUTOLOAD {
221     my $self = shift;
222     my $class = ref($self) or croak "$self is not an object";
223     my $name = $AUTOLOAD;
224
225     $name =~ s/.*://;
226
227     unless (exists $fields{$name}) {
228                 croak "Cannot access '$name' field of class '$class'";
229     }
230
231         if (@_) {
232         $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
233                 return $self->{$name} = shift;
234         } else {
235                 return $self->{$name};
236         }
237 }
238
239 sub status_update {     # FIXME: this looks unimplemented
240     my ($self, $props) = @_;
241     my $status = C4::SIP::ILS::Transaction->new();
242     $self->{sip_item_properties} = $props;
243     $status->{ok} = 1;
244     return $status;
245 }
246
247 sub title_id {
248     my $self = shift;
249     return $self->{title};
250 }
251
252 sub sip_circulation_status {
253     my $self = shift;
254     if ( $self->{patron} ) {
255         return '04';    # charged
256     }
257     elsif ( grep { $_->{itemnumber} == $self->{itemnumber}  } @{ $self->{hold_shelf} } ) {
258         return '08';    # waiting on hold shelf
259     }
260     else {
261         return '03';    # available
262     }    # FIXME: 01-13 enumerated in spec.
263 }
264
265 sub sip_security_marker {
266     my $self = shift;
267     return '02';        # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
268 }
269 sub sip_fee_type {
270     my $self = shift;
271     return '01';    # FIXME? 01-09 enumerated in spec.  We just use O1-other/unknown.
272 }
273
274 sub fee {
275     my $self = shift;
276     return $self->{fee} || 0;
277 }
278 sub fee_currency {
279     my $self = shift;
280     return $self->{currency} || 'USD';
281 }
282 sub owner {
283     my $self = shift;
284     return $self->{homebranch};
285 }
286 sub hold_queue {
287     my $self = shift;
288         (defined $self->{hold_queue}) or return [];
289     return $self->{hold_queue};
290 }
291 sub pending_queue {
292     my $self = shift;
293         (defined $self->{pending_queue}) or return [];
294     return $self->{pending_queue};
295 }
296 sub hold_shelf {
297     my $self = shift;
298         (defined $self->{hold_shelf}) or return [];
299     return $self->{hold_shelf};
300 }
301
302 sub hold_queue_position {
303         my ($self, $patron_id) = @_;
304         ($self->{hold_queue}) or return 0;
305         my $i = 0;
306         foreach (@{$self->{hold_queue}}) {
307                 $i++;
308                 $_->{patron_id} or next;
309                 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
310                         return $i;  # maybe should return $_->{priority}
311                 }
312         }
313     return 0;
314 }
315
316 sub due_date {
317     my $self = shift;
318     return $self->{due_date} || 0;
319 }
320 sub recall_date {
321     my $self = shift;
322     return $self->{recall_date} || 0;
323 }
324 sub hold_pickup_date {
325     my $self = shift;
326     return $self->{hold_pickup_date} || 0;
327 }
328
329 # This is a partial check of "availability".  It is not supposed to check everything here.
330 # An item is available for a patron if it is:
331 # 1) checked out to the same patron 
332 #    AND no pending (i.e. non-W) hold queue
333 # OR
334 # 2) not checked out
335 #    AND (not on hold_shelf OR is on hold_shelf for patron)
336 #
337 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
338 # have non-W holds on it, but has not been "picked" from the stacks.  That is to say, the
339 # patron has retrieved the item before the librarian.
340 #
341 # We don't check if the patron is at the front of the pending queue in the first case, because
342 # they should not be able to place a hold on an item they already have.
343
344 sub available {
345         my ($self, $for_patron) = @_;
346         my $count  = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
347         my $count2 = (defined $self->{hold_shelf}   ) ? scalar @{$self->{hold_shelf}   } : 0;
348         $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
349     if (defined($self->{patron_id})) {
350                 ($self->{patron_id} eq $for_patron) or return 0;
351                 return ($count ? 0 : 1);
352         } else {        # not checked out
353         ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
354         }
355         return 0;
356 }
357
358 sub _barcode_to_borrowernumber {
359     my $known = shift;
360     return unless defined $known;
361     my $patron = Koha::Patrons->find( { cardnumber => $known } ) or return;
362     return $patron->borrowernumber
363 }
364 sub barcode_is_borrowernumber {    # because hold_queue only has borrowernumber...
365     my $self = shift;
366     my $barcode = shift;
367     my $number  = shift or return;    # can't be zero
368     return unless defined $barcode; # might be 0 or 000 or 000000
369     my $converted = _barcode_to_borrowernumber($barcode);
370     return unless $converted;
371     return ($number == $converted);
372 }
373 sub fill_reserve {
374     my $self = shift;
375     my $hold = shift or return;
376     foreach (qw(biblionumber borrowernumber reservedate)) {
377         $hold->{$_} or return;
378     }
379     return ModReserveFill($hold);
380 }
381 1;
382 __END__
383