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