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