Bug 17680: C4::Circulation - Remove GetItemIssue, simple calls
[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::Patrons;
29
30 =encoding UTF-8
31
32 =head1 EXAMPLE
33
34  our %item_db = (
35     '1565921879' => {
36         title => "Perl 5 desktop reference",
37         id => '1565921879',
38         sip_media_type => '001',
39         magnetic_media => 0,
40         hold_queue => [],
41     },
42     '0440242746' => {
43         title => "The deep blue alibi",
44         id => '0440242746',
45         sip_media_type => '001',
46         magnetic_media => 0,
47         hold_queue => [
48             {
49             itemnumber => '823',
50             priority => '1',
51             reservenotes => undef,
52             reservedate => '2008-10-09',
53             found => undef,
54             rtimestamp => '2008-10-09 11:15:06',
55             biblionumber => '406',
56             borrowernumber => '756',
57             branchcode => 'CPL'
58             }
59         ],
60     },
61     '660' => {
62         title => "Harry Potter y el cáliz de fuego",
63         id => '660',
64         sip_media_type => '001',
65         magnetic_media => 0,
66         hold_queue => [],
67     },
68 );
69 =cut
70
71 sub new {
72         my ($class, $item_id) = @_;
73         my $type = ref($class) || $class;
74         my $self;
75     my $itemnumber = GetItemnumberFromBarcode($item_id);
76         my $item = GetBiblioFromItemNumber($itemnumber);    # actually biblio.*, biblioitems.* AND items.*  (overkill)
77         if (! $item) {
78                 syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
79                 warn "new ILS::Item($item_id) : No item '$item_id'.";
80         return;
81         }
82     $item->{  'itemnumber'   } = $itemnumber;
83     $item->{      'id'       } = $item->{barcode};     # to SIP, the barcode IS the id.
84     $item->{permanent_location}= $item->{homebranch};
85     $item->{'collection_code'} = $item->{ccode};
86     $item->{  'call_number'  } = $item->{itemcallnumber};
87
88     my $it = C4::Context->preference('item-level_itypes') ? $item->{itype} : $item->{itemtype};
89     my $itemtype = Koha::Database->new()->schema()->resultset('Itemtype')->find( $it );
90     $item->{sip_media_type} = $itemtype->sip_media_type() if $itemtype;
91
92         # check if its on issue and if so get the borrower
93     my $issue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
94     if ($issue) {
95         $item->{due_date} = dt_from_string( $issue->date_due, 'sql' )->truncate( to => 'minute' );
96     }
97     my $borrower = $issue ? GetMember( borrowernumber => $issue->borrowernumber ) : {};
98         $item->{patron} = $borrower->{'cardnumber'};
99     my $biblio = Koha::Biblios->find( $item->{biblionumber } );
100     my $holds = $biblio->current_holds->unblessed;
101     $item->{hold_queue} = $holds;
102         $item->{hold_shelf}    = [( grep {   defined $_->{found}  and $_->{found} eq 'W' } @{$item->{hold_queue}} )];
103         $item->{pending_queue} = [( grep {(! defined $_->{found}) or  $_->{found} ne 'W' } @{$item->{hold_queue}} )];
104         $self = $item;
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 = GetMember(borrowernumber=>$borrowernumber);
180     unless ($holder) {
181         syslog("LOG_ERR", "While checking hold, GetMember failed for 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 = GetMember(borrowernumber => $borrowernumber);
198     if ($holder) {
199         if ($holder->{cardnumber}) {
200             return $holder->{cardnumber};
201         }
202     }
203     return;
204 }
205
206 sub destination_loc {
207     my $self = shift;
208     my $set_loc = shift;
209     if ($set_loc) {
210         $self->{dest_loc} = $set_loc;
211     }
212     if ($self->{dest_loc} ) {
213         return $self->{dest_loc};
214     }
215     return q{};
216 }
217
218 our $AUTOLOAD;
219
220 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
221
222 sub AUTOLOAD {
223     my $self = shift;
224     my $class = ref($self) or croak "$self is not an object";
225     my $name = $AUTOLOAD;
226
227     $name =~ s/.*://;
228
229     unless (exists $fields{$name}) {
230                 croak "Cannot access '$name' field of class '$class'";
231     }
232
233         if (@_) {
234         $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
235                 return $self->{$name} = shift;
236         } else {
237                 return $self->{$name};
238         }
239 }
240
241 sub status_update {     # FIXME: this looks unimplemented
242     my ($self, $props) = @_;
243     my $status = C4::SIP::ILS::Transaction->new();
244     $self->{sip_item_properties} = $props;
245     $status->{ok} = 1;
246     return $status;
247 }
248
249 sub title_id {
250     my $self = shift;
251     return $self->{title};
252 }
253
254 sub sip_circulation_status {
255     my $self = shift;
256     if ( $self->{patron} ) {
257         return '04';    # charged
258     }
259     elsif ( grep { $_->{itemnumber} == $self->{itemnumber}  } @{ $self->{hold_shelf} } ) {
260         return '08';    # waiting on hold shelf
261     }
262     else {
263         return '03';    # available
264     }    # FIXME: 01-13 enumerated in spec.
265 }
266
267 sub sip_security_marker {
268     my $self = shift;
269     return '02';        # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
270 }
271 sub sip_fee_type {
272     my $self = shift;
273     return '01';    # FIXME? 01-09 enumerated in spec.  We just use O1-other/unknown.
274 }
275
276 sub fee {
277     my $self = shift;
278     return $self->{fee} || 0;
279 }
280 sub fee_currency {
281     my $self = shift;
282     return $self->{currency} || 'USD';
283 }
284 sub owner {
285     my $self = shift;
286     return $self->{homebranch};
287 }
288 sub hold_queue {
289     my $self = shift;
290         (defined $self->{hold_queue}) or return [];
291     return $self->{hold_queue};
292 }
293 sub pending_queue {
294     my $self = shift;
295         (defined $self->{pending_queue}) or return [];
296     return $self->{pending_queue};
297 }
298 sub hold_shelf {
299     my $self = shift;
300         (defined $self->{hold_shelf}) or return [];
301     return $self->{hold_shelf};
302 }
303
304 sub hold_queue_position {
305         my ($self, $patron_id) = @_;
306         ($self->{hold_queue}) or return 0;
307         my $i = 0;
308         foreach (@{$self->{hold_queue}}) {
309                 $i++;
310                 $_->{patron_id} or next;
311                 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
312                         return $i;  # maybe should return $_->{priority}
313                 }
314         }
315     return 0;
316 }
317
318 sub due_date {
319     my $self = shift;
320     return $self->{due_date} || 0;
321 }
322 sub recall_date {
323     my $self = shift;
324     return $self->{recall_date} || 0;
325 }
326 sub hold_pickup_date {
327     my $self = shift;
328     return $self->{hold_pickup_date} || 0;
329 }
330
331 # This is a partial check of "availability".  It is not supposed to check everything here.
332 # An item is available for a patron if it is:
333 # 1) checked out to the same patron 
334 #    AND no pending (i.e. non-W) hold queue
335 # OR
336 # 2) not checked out
337 #    AND (not on hold_shelf OR is on hold_shelf for patron)
338 #
339 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
340 # have non-W holds on it, but has not been "picked" from the stacks.  That is to say, the
341 # patron has retrieved the item before the librarian.
342 #
343 # We don't check if the patron is at the front of the pending queue in the first case, because
344 # they should not be able to place a hold on an item they already have.
345
346 sub available {
347         my ($self, $for_patron) = @_;
348         my $count  = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
349         my $count2 = (defined $self->{hold_shelf}   ) ? scalar @{$self->{hold_shelf}   } : 0;
350         $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
351     if (defined($self->{patron_id})) {
352                 ($self->{patron_id} eq $for_patron) or return 0;
353                 return ($count ? 0 : 1);
354         } else {        # not checked out
355         ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
356         }
357         return 0;
358 }
359
360 sub _barcode_to_borrowernumber {
361     my $known = shift;
362     return unless defined $known;
363     my $member = GetMember(cardnumber=>$known) or return;
364     return $member->{borrowernumber};
365 }
366 sub barcode_is_borrowernumber {    # because hold_queue only has borrowernumber...
367     my $self = shift;
368     my $barcode = shift;
369     my $number  = shift or return;    # can't be zero
370     return unless defined $barcode; # might be 0 or 000 or 000000
371     my $converted = _barcode_to_borrowernumber($barcode);
372     return unless $converted;
373     return ($number == $converted);
374 }
375 sub fill_reserve {
376     my $self = shift;
377     my $hold = shift or return;
378     foreach (qw(biblionumber borrowernumber reservedate)) {
379         $hold->{$_} or return;
380     }
381     return ModReserveFill($hold);
382 }
383 1;
384 __END__
385