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