a6c735c4ef60d389df8cd45d6950158a8ddb4d78
[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::Patrons;
28
29 =encoding UTF-8
30
31 =head1 EXAMPLE
32
33  our %item_db = (
34     '1565921879' => {
35         title => "Perl 5 desktop reference",
36         id => '1565921879',
37         sip_media_type => '001',
38         magnetic_media => 0,
39         hold_queue => [],
40     },
41     '0440242746' => {
42         title => "The deep blue alibi",
43         id => '0440242746',
44         sip_media_type => '001',
45         magnetic_media => 0,
46         hold_queue => [
47             {
48             itemnumber => '823',
49             priority => '1',
50             reservenotes => undef,
51             reservedate => '2008-10-09',
52             found => undef,
53             rtimestamp => '2008-10-09 11:15:06',
54             biblionumber => '406',
55             borrowernumber => '756',
56             branchcode => 'CPL'
57             }
58         ],
59     },
60     '660' => {
61         title => "Harry Potter y el cáliz de fuego",
62         id => '660',
63         sip_media_type => '001',
64         magnetic_media => 0,
65         hold_queue => [],
66     },
67 );
68 =cut
69
70 sub new {
71         my ($class, $item_id) = @_;
72         my $type = ref($class) || $class;
73         my $self;
74     my $itemnumber = GetItemnumberFromBarcode($item_id);
75         my $item = GetBiblioFromItemNumber($itemnumber);    # actually biblio.*, biblioitems.* AND items.*  (overkill)
76         if (! $item) {
77                 syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
78                 warn "new ILS::Item($item_id) : No item '$item_id'.";
79         return;
80         }
81     $item->{  'itemnumber'   } = $itemnumber;
82     $item->{      'id'       } = $item->{barcode};     # to SIP, the barcode IS the id.
83     $item->{permanent_location}= $item->{homebranch};
84     $item->{'collection_code'} = $item->{ccode};
85     $item->{  'call_number'  } = $item->{itemcallnumber};
86
87     my $it = C4::Context->preference('item-level_itypes') ? $item->{itype} : $item->{itemtype};
88     my $itemtype = Koha::Database->new()->schema()->resultset('Itemtype')->find( $it );
89     $item->{sip_media_type} = $itemtype->sip_media_type() if $itemtype;
90
91         # check if its on issue and if so get the borrower
92         my $issue = GetItemIssue($item->{'itemnumber'});
93     if ($issue) {
94         $item->{due_date} = $issue->{date_due};
95     }
96         my $borrower = GetMember(borrowernumber=>$issue->{'borrowernumber'});
97         $item->{patron} = $borrower->{'cardnumber'};
98     my $biblio = Koha::Biblios->find( $item->{biblionumber } );
99     my $holds = $biblio->current_holds->unblessed;
100     $item->{hold_queue} = $holds;
101         $item->{hold_shelf}    = [( grep {   defined $_->{found}  and $_->{found} eq 'W' } @{$item->{hold_queue}} )];
102         $item->{pending_queue} = [( grep {(! defined $_->{found}) or  $_->{found} ne 'W' } @{$item->{hold_queue}} )];
103         $self = $item;
104         bless $self, $type;
105
106     syslog("LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
107            $item_id, $self->{title});
108
109     return $self;
110 }
111
112 # 0 means read-only
113 # 1 means read/write
114
115 my %fields = (
116     id                  => 0,
117     sip_media_type      => 0,
118     sip_item_properties => 0,
119     magnetic_media      => 0,
120     permanent_location  => 0,
121     current_location    => 0,
122     print_line          => 1,
123     screen_msg          => 1,
124     itemnumber          => 0,
125     biblionumber        => 0,
126     barcode             => 0,
127     onloan              => 0,
128     collection_code     => 0,
129     call_number         => 0,
130     enumchron           => 0,
131     location            => 0,
132     author              => 0,
133     title               => 0,
134 );
135
136 sub next_hold {
137     my $self = shift;
138     # use Data::Dumper; warn "next_hold() hold_shelf: " . Dumper($self->{hold_shelf}); warn "next_hold() pending_queue: " . $self->{pending_queue};
139     foreach (@{$self->hold_shelf}) {    # If this item was taken from the hold shelf, then that reserve still governs
140         next unless ($_->{itemnumber} and $_->{itemnumber} == $self->{itemnumber});
141         return $_;
142     }
143     if (scalar @{$self->{pending_queue}}) {    # Otherwise, if there is at least one hold, the first (best priority) gets it
144         return  $self->{pending_queue}->[0];
145     }
146     return;
147 }
148
149 # hold_patron_id is NOT the barcode.  It's the borrowernumber.
150 # If a return triggers capture for a hold the borrowernumber is passed
151 # and saved so that other hold info can be retrieved
152 sub hold_patron_id {
153     my $self = shift;
154     my $id   = shift;
155     if ($id) {
156         $self->{hold}->{borrowernumber} = $id;
157     }
158     if ($self->{hold} ) {
159         return $self->{hold}->{borrowernumber};
160     }
161     return;
162
163 }
164 sub hold_patron_name {
165     my ( $self, $template ) = @_;
166     my $borrowernumber = $self->hold_patron_id() or return;
167
168     if ($template) {
169         my $tt = Template->new();
170
171         my $patron = Koha::Patrons->find($borrowernumber);
172
173         my $output;
174         $tt->process( \$template, { patron => $patron }, \$output );
175         return $output;
176     }
177
178     my $holder = GetMember(borrowernumber=>$borrowernumber);
179     unless ($holder) {
180         syslog("LOG_ERR", "While checking hold, GetMember failed for borrowernumber '$borrowernumber'");
181         return;
182     }
183     my $email = $holder->{email} || '';
184     my $phone = $holder->{phone} || '';
185     my $extra = ($email and $phone) ? " ($email, $phone)" :  # both populated, employ comma
186                 ($email or  $phone) ? " ($email$phone)"   :  # only 1 populated, we don't care which: no comma
187                 "" ;                                         # neither populated, empty string
188     my $name = $holder->{firstname} ? $holder->{firstname} . ' ' : '';
189     $name .= $holder->{surname} . $extra;
190     return $name;
191 }
192
193 sub hold_patron_bcode {
194     my $self = shift;
195     my $borrowernumber = (@_ ? shift: $self->hold_patron_id()) or return;
196     my $holder = GetMember(borrowernumber => $borrowernumber);
197     if ($holder) {
198         if ($holder->{cardnumber}) {
199             return $holder->{cardnumber};
200         }
201     }
202     return;
203 }
204
205 sub destination_loc {
206     my $self = shift;
207     my $set_loc = shift;
208     if ($set_loc) {
209         $self->{dest_loc} = $set_loc;
210     }
211     if ($self->{dest_loc} ) {
212         return $self->{dest_loc};
213     }
214     return q{};
215 }
216
217 our $AUTOLOAD;
218
219 sub DESTROY { } # keeps AUTOLOAD from catching inherent DESTROY calls
220
221 sub AUTOLOAD {
222     my $self = shift;
223     my $class = ref($self) or croak "$self is not an object";
224     my $name = $AUTOLOAD;
225
226     $name =~ s/.*://;
227
228     unless (exists $fields{$name}) {
229                 croak "Cannot access '$name' field of class '$class'";
230     }
231
232         if (@_) {
233         $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
234                 return $self->{$name} = shift;
235         } else {
236                 return $self->{$name};
237         }
238 }
239
240 sub status_update {     # FIXME: this looks unimplemented
241     my ($self, $props) = @_;
242     my $status = C4::SIP::ILS::Transaction->new();
243     $self->{sip_item_properties} = $props;
244     $status->{ok} = 1;
245     return $status;
246 }
247
248 sub title_id {
249     my $self = shift;
250     return $self->{title};
251 }
252
253 sub sip_circulation_status {
254     my $self = shift;
255     if ( $self->{patron} ) {
256         return '04';    # charged
257     }
258     elsif ( grep { $_->{itemnumber} == $self->{itemnumber}  } @{ $self->{hold_shelf} } ) {
259         return '08';    # waiting on hold shelf
260     }
261     else {
262         return '03';    # available
263     }    # FIXME: 01-13 enumerated in spec.
264 }
265
266 sub sip_security_marker {
267     my $self = shift;
268     return '02';        # FIXME? 00-other; 01-None; 02-Tattle-Tape Security Strip (3M); 03-Whisper Tape (3M)
269 }
270 sub sip_fee_type {
271     my $self = shift;
272     return '01';    # FIXME? 01-09 enumerated in spec.  We just use O1-other/unknown.
273 }
274
275 sub fee {
276     my $self = shift;
277     return $self->{fee} || 0;
278 }
279 sub fee_currency {
280     my $self = shift;
281     return $self->{currency} || 'USD';
282 }
283 sub owner {
284     my $self = shift;
285     return $self->{homebranch};
286 }
287 sub hold_queue {
288     my $self = shift;
289         (defined $self->{hold_queue}) or return [];
290     return $self->{hold_queue};
291 }
292 sub pending_queue {
293     my $self = shift;
294         (defined $self->{pending_queue}) or return [];
295     return $self->{pending_queue};
296 }
297 sub hold_shelf {
298     my $self = shift;
299         (defined $self->{hold_shelf}) or return [];
300     return $self->{hold_shelf};
301 }
302
303 sub hold_queue_position {
304         my ($self, $patron_id) = @_;
305         ($self->{hold_queue}) or return 0;
306         my $i = 0;
307         foreach (@{$self->{hold_queue}}) {
308                 $i++;
309                 $_->{patron_id} or next;
310                 if ($self->barcode_is_borrowernumber($patron_id, $_->{borrowernumber})) {
311                         return $i;  # maybe should return $_->{priority}
312                 }
313         }
314     return 0;
315 }
316
317 sub due_date {
318     my $self = shift;
319     return $self->{due_date} || 0;
320 }
321 sub recall_date {
322     my $self = shift;
323     return $self->{recall_date} || 0;
324 }
325 sub hold_pickup_date {
326     my $self = shift;
327     return $self->{hold_pickup_date} || 0;
328 }
329
330 # This is a partial check of "availability".  It is not supposed to check everything here.
331 # An item is available for a patron if it is:
332 # 1) checked out to the same patron 
333 #    AND no pending (i.e. non-W) hold queue
334 # OR
335 # 2) not checked out
336 #    AND (not on hold_shelf OR is on hold_shelf for patron)
337 #
338 # What this means is we are consciously allowing the patron to checkout (but not renew) an item that DOES
339 # have non-W holds on it, but has not been "picked" from the stacks.  That is to say, the
340 # patron has retrieved the item before the librarian.
341 #
342 # We don't check if the patron is at the front of the pending queue in the first case, because
343 # they should not be able to place a hold on an item they already have.
344
345 sub available {
346         my ($self, $for_patron) = @_;
347         my $count  = (defined $self->{pending_queue}) ? scalar @{$self->{pending_queue}} : 0;
348         my $count2 = (defined $self->{hold_shelf}   ) ? scalar @{$self->{hold_shelf}   } : 0;
349         $debug and print STDERR "availability check: pending_queue size $count, hold_shelf size $count2\n";
350     if (defined($self->{patron_id})) {
351                 ($self->{patron_id} eq $for_patron) or return 0;
352                 return ($count ? 0 : 1);
353         } else {        # not checked out
354         ($count2) and return $self->barcode_is_borrowernumber($for_patron, $self->{hold_shelf}[0]->{borrowernumber});
355         }
356         return 0;
357 }
358
359 sub _barcode_to_borrowernumber {
360     my $known = shift;
361     return unless defined $known;
362     my $member = GetMember(cardnumber=>$known) or return;
363     return $member->{borrowernumber};
364 }
365 sub barcode_is_borrowernumber {    # because hold_queue only has borrowernumber...
366     my $self = shift;
367     my $barcode = shift;
368     my $number  = shift or return;    # can't be zero
369     return unless defined $barcode; # might be 0 or 000 or 000000
370     my $converted = _barcode_to_borrowernumber($barcode);
371     return unless $converted;
372     return ($number == $converted);
373 }
374 sub fill_reserve {
375     my $self = shift;
376     my $hold = shift or return;
377     foreach (qw(biblionumber borrowernumber reservedate)) {
378         $hold->{$_} or return;
379     }
380     return ModReserveFill($hold);
381 }
382 1;
383 __END__
384