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