Merge remote branch 'kc/new/biblibre_reports' into kcmaster
[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 ILS::Item;
8
9 use strict;
10 use warnings;
11
12 use DateTime;
13 use Sys::Syslog qw(syslog);
14 use Carp;
15
16 use 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
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
27
28 BEGIN {
29         $VERSION = 2.11;
30         require Exporter;
31         @ISA = qw(Exporter);
32         @EXPORT_OK = qw();
33 }
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             constrainttype => 'a',
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'
62             }
63         ],
64     },
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 => [],
71     },
72 );
73 =cut
74
75 sub priority_sort {
76     defined $a->{priority} or return -1;
77     defined $b->{priority} or return 1;
78     return $a->{priority} <=> $b->{priority};
79 }
80
81 sub new {
82         my ($class, $item_id) = @_;
83         my $type = ref($class) || $class;
84         my $self;
85     my $itemnumber = GetItemnumberFromBarcode($item_id);
86         my $item = GetBiblioFromItemNumber($itemnumber);    # actually biblio.*, biblioitems.* AND items.*  (overkill)
87         if (! $item) {
88                 syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
89                 warn "new ILS::Item($item_id) : No item '$item_id'.";
90                 return undef;
91         }
92     $item->{  'itemnumber'   } = $itemnumber;
93     $item->{      'id'       } = $item->{barcode};     # to SIP, the barcode IS the id.
94     $item->{permanent_location}= $item->{homebranch};
95     $item->{'collection_code'} = $item->{ccode};
96     $item->{  'call_number'  } = $item->{itemcallnumber};
97     # $item->{'destination_loc'}  =  ?
98
99         # check if its on issue and if so get the borrower
100         my $issue = GetItemIssue($item->{'itemnumber'});
101     if ( $issue ) {
102         my $date = $issue->{ date_due };
103         my $dt = DateTime->new(
104             year  => substr($date, 0, 4),
105             month => substr($date,5,2),
106             day  => substr($date, 8, 2) );
107         $item->{ due_date } = $dt->epoch();
108     }
109         my $borrower = GetMember(borrowernumber=>$issue->{'borrowernumber'});
110         $item->{patron} = $borrower->{'cardnumber'};
111     my ($whatever, $arrayref) = GetReservesFromBiblionumber($item->{biblionumber});
112         $item->{hold_queue} = [ sort priority_sort @$arrayref ];
113         $item->{hold_shelf}    = [( grep {   defined $_->{found}  and $_->{found} eq 'W' } @{$item->{hold_queue}} )];
114         $item->{pending_queue} = [( grep {(! defined $_->{found}) or  $_->{found} ne 'W' } @{$item->{hold_queue}} )];
115     $item->{due_date} = $issue->{date_due};
116         $self = $item;
117         bless $self, $type;
118
119     syslog("LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
120            $item_id, $self->{title});
121
122     return $self;
123 }
124
125 # 0 means read-only
126 # 1 means read/write
127
128 my %fields = (
129     id                  => 0,
130     sip_media_type      => 0,
131     sip_item_properties => 0,
132     magnetic_media      => 0,
133     permanent_location  => 0,
134     current_location    => 0,
135     print_line          => 1,
136     screen_msg          => 1,
137     itemnumber          => 0,
138     biblionumber        => 0,
139     barcode             => 0,
140     onloan              => 0,
141     collection_code     => 0,
142     call_number         => 0,
143     enumchron           => 0,
144     location            => 0,
145     author              => 0,
146     title               => 0,
147 );
148
149 sub next_hold {
150     my $self = shift or return;
151     # use Data::Dumper; warn "next_hold() hold_shelf: " . Dumper($self->{hold_shelf}); warn "next_hold() pending_queue: " . $self->{pending_queue};
152     foreach (@{$self->hold_shelf}) {    # If this item was taken from the hold shelf, then that reserve still governs
153         next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
154         return $_;
155     }
156     if (scalar @{$self->{pending_queue}}) {    # Otherwise, if there is at least one hold, the first (best priority) gets it
157         return  $self->{pending_queue}->[0];
158     }
159     return;
160 }
161
162 # hold_patron_id is NOT the barcode.  It's the borrowernumber.
163 # That's because the reserving patron may not have a barcode, or may be from an different system entirely (ILL)!
164 sub hold_patron_id {
165     my $self = shift or return;
166     my $hold = $self->next_hold() or return;
167     return $hold->{borrowernumber};
168 }
169 sub hold_patron_name {
170     my $self = shift or return;
171     # return $self->{hold_patron_name} if $self->{hold_patron_name};    TODO: consider caching
172     my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
173     my $holder = GetMember(borrowernumber=>$borrowernumber);
174     unless ($holder) {
175         syslog("LOG_ERR", "While checking hold, GetMember failed for borrowernumber '$borrowernumber'");
176         return;
177     }
178     my $email = $holder->{email} || '';
179     my $phone = $holder->{phone} || '';
180     my $extra = ($email and $phone) ? " ($email, $phone)" :  # both populated, employ comma
181                 ($email or  $phone) ? " ($email$phone)"   :  # only 1 populated, we don't care which: no comma
182                 "" ;                                         # neither populated, empty string
183     my $name = $holder->{firstname} ? $holder->{firstname} . ' ' : '';
184     $name .= $holder->{surname} . $extra;
185     # $self->{hold_patron_name} = $name;      # TODO: consider caching
186     return $name;
187 }
188
189 sub hold_patron_bcode {
190     my $self = shift or return;
191     my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
192     my $holder = GetMember(borrowernumber => $borrowernumber);
193     if ($holder) {
194         if ($holder->{cardnumber}) {
195             return $holder->{cardnumber};
196         }
197     }
198     return;
199 }
200
201 sub destination_loc {
202     my $self = shift or return;
203     my $hold = $self->next_hold();
204     return ($hold ? $hold->{branchcode} : '');
205 }
206
207 our $AUTOLOAD;
208
209 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
210
211 sub AUTOLOAD {
212     my $self = shift;
213     my $class = ref($self) or croak "$self is not an object";
214     my $name = $AUTOLOAD;
215
216     $name =~ s/.*://;
217
218     unless (exists $fields{$name}) {
219                 croak "Cannot access '$name' field of class '$class'";
220     }
221
222         if (@_) {
223         $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
224                 return $self->{$name} = shift;
225         } else {
226                 return $self->{$name};
227         }
228 }
229
230 sub status_update {     # FIXME: this looks unimplemented
231     my ($self, $props) = @_;
232     my $status = new ILS::Transaction;
233     $self->{sip_item_properties} = $props;
234     $status->{ok} = 1;
235     return $status;
236 }
237
238 sub title_id {
239     my $self = shift;
240     return $self->{title};
241 }
242
243 sub sip_circulation_status {
244     my $self = shift;
245     if ($self->{patron}) {
246                 return '04';    # charged
247     } elsif (scalar @{$self->{hold_queue}}) {
248                 return '08';    # waiting on hold shelf
249     } else {
250                 return '03';    # available
251     }                   # FIXME: 01-13 enumerated in spec.
252 }
253
254 sub sip_security_marker {
255     return '02';        # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
256 }
257 sub sip_fee_type {
258     return '01';    # FIXME? 01-09 enumerated in spec.  We just use O1-other/unknown.
259 }
260
261 sub fee {
262     my $self = shift;
263     return $self->{fee} || 0;
264 }
265 sub fee_currency {
266     my $self = shift;
267     return $self->{currency} || 'USD';
268 }
269 sub owner {
270     my $self = shift;
271     return $self->{homebranch};
272 }
273 sub hold_queue {
274     my $self = shift;
275         (defined $self->{hold_queue}) or return [];
276     return $self->{hold_queue};
277 }
278 sub pending_queue {
279     my $self = shift;
280         (defined $self->{pending_queue}) or return [];
281     return $self->{pending_queue};
282 }
283 sub hold_shelf {
284     my $self = shift;
285         (defined $self->{hold_shelf}) or return [];
286     return $self->{hold_shelf};
287 }
288
289 sub hold_queue_position {
290         my ($self, $patron_id) = @_;
291         ($self->{hold_queue}) or return 0;
292         my $i = 0;
293         foreach (@{$self->{hold_queue}}) {
294                 $i++;
295                 $_->{patron_id} or next;
296                 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
297                         return $i;  # maybe should return $_->{priority}
298                 }
299         }
300     return 0;
301 }
302
303 sub due_date {
304     my $self = shift;
305     return $self->{due_date} || 0;
306 }
307 sub recall_date {
308     my $self = shift;
309     return $self->{recall_date} || 0;
310 }
311 sub hold_pickup_date {
312     my $self = shift;
313     return $self->{hold_pickup_date} || 0;
314 }
315
316 # This is a partial check of "availability".  It is not supposed to check everything here.
317 # An item is available for a patron if it is:
318 # 1) checked out to the same patron 
319 #    AND no pending (i.e. non-W) hold queue
320 # OR
321 # 2) not checked out
322 #    AND (not on hold_shelf OR is on hold_shelf for patron)
323 #
324 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
325 # have non-W holds on it, but has not been "picked" from the stacks.  That is to say, the
326 # patron has retrieved the item before the librarian.
327 #
328 # We don't check if the patron is at the front of the pending queue in the first case, because
329 # they should not be able to place a hold on an item they already have.
330
331 sub available {
332         my ($self, $for_patron) = @_;
333         my $count  = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
334         my $count2 = (defined $self->{hold_shelf}   ) ? scalar @{$self->{hold_shelf}   } : 0;
335         $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
336     if (defined($self->{patron_id})) {
337                 ($self->{patron_id} eq $for_patron) or return 0;
338                 return ($count ? 0 : 1);
339         } else {        # not checked out
340         ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
341         }
342         return 0;
343 }
344
345 sub _barcode_to_borrowernumber ($) {
346     my $known = shift;
347     (defined($known)) or return undef;
348     my $member = GetMember(cardnumber=>$known) or return undef;
349     return $member->{borrowernumber};
350 }
351 sub barcode_is_borrowernumber ($$$) {    # because hold_queue only has borrowernumber...
352     my $self = shift;   # not really used
353     my $barcode = shift;
354     my $number  = shift or return undef;    # can't be zero
355     (defined($barcode)) or return undef;    # might be 0 or 000 or 000000
356     my $converted = _barcode_to_borrowernumber($barcode) or return undef;
357     return ($number eq $converted); # even though both *should* be numbers, eq is safer.
358 }
359 sub fill_reserve ($$) {
360     my $self = shift;
361     my $hold = shift or return undef;
362     foreach (qw(biblionumber borrowernumber reservedate)) {
363         $hold->{$_} or return undef;
364     }
365     return ModReserveFill($hold);
366 }
367 1;
368 __END__
369