bug 1710 and 1739: availability for item-level holds
[koha.git] / opac / opac-reserve.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 use CGI;
20 use C4::Biblio;
21 use C4::Items;
22 use C4::Auth;    # checkauth, getborrowernumber.
23 use C4::Koha;
24 use C4::Circulation;
25 use C4::Reserves;
26 use C4::Output;
27 use C4::Dates qw/format_date/;
28 use C4::Context;
29 use C4::Members;
30 use C4::Branch; # GetBranches
31 # use Data::Dumper;
32
33 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
34
35 my $query = new CGI;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-reserve.tmpl",
39         query           => $query,
40         type            => "opac",
41         authnotrequired => 0,
42         flagsrequired   => { borrow => 1 },
43         debug           => 1,
44     }
45 );
46
47 sub get_out ($$$) {
48         output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
49         exit;
50 }
51
52 # get borrower information ....
53 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
54
55 # get branches and itemtypes
56 my $branches = GetBranches();
57 my $itemtypes = GetItemTypes();
58
59 # get biblionumber.....
60 my $biblionumber = $query->param('biblionumber');
61 if (! $biblionumber) {
62         $template->param(message=>1,no_biblionumber=>1);
63         &get_out($query, $cookie, $template->output);
64 }
65 $template->param( biblionumber => $biblionumber );
66
67 my $bibdata = GetBiblioData($biblionumber);
68 if (! $bibdata) {
69         $template->param(message=>1,bad_biblionumber=>$biblionumber);
70         &get_out($query, $cookie, $template->output);
71 }
72 $template->param($bibdata);             # FIXME: bad form.
73
74 # get the rank number....
75 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
76 $template->param( reservecount => $rank );
77
78 foreach my $res (@$reserves) {
79     if ( $res->{'found'} eq 'W' ) {
80         $rank--;
81     }
82 }
83
84 $rank++;
85 $template->param( rank => $rank );
86
87 # pass the pickup branch along....
88 my $branch = $query->param('branch');
89 $template->param( branch => $branch );
90
91 # make sure it's a real branch
92 if ( !$branches->{$branch} ) {
93     $branch = '';
94 }
95 $template->param( branchname => $branches->{$branch}->{'branchname'} );
96
97 # make branch selection options...
98 my @branches;
99 my @select_branch;
100 my %select_branches;
101
102 my @CGIbranchlooparray;
103
104 foreach my $branch ( keys %$branches ) {
105     if ($branch) {
106         my %line;
107         $line{branch} = $branches->{$branch}->{'branchname'};
108         $line{value}  = $branch;
109         if ($line{value} eq C4::Context->userenv->{'branch'}) {
110             $line{selected} = 1;
111         }
112         push @CGIbranchlooparray, \%line;
113     }
114 }
115 @CGIbranchlooparray =
116   sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
117 my $CGIbranchloop = \@CGIbranchlooparray;
118 $template->param( CGIbranch => $CGIbranchloop );
119
120 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
121 # get the itemtype data....
122 my @items = GetItemsInfo($biblionumber);
123 #######################################################
124 # old version, add so that old templates still work
125 my %types_old;
126 foreach my $itm (@items) {
127     my $ity = $itm->{'itemtype'};
128     unless ( $types_old{$ity} ) {
129         $types_old{$ity}->{'itemtype'} = $ity;
130         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
131         $types_old{$ity}->{'description'} = $itm->{'description'};
132     }
133     else {
134         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
135     }
136 }
137
138 foreach my $type ( values %types_old ) {
139     my $copies = "";
140     foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
141         $copies .=
142             $branches->{$bc}->{'branchname'} . "("
143           . $type->{'branchinfo'}->{$bc} . ")";
144     }
145     $type->{'copies'} = $copies;
146 }
147
148 my @types_old = values %types_old;
149
150 # end old version
151 ################################
152
153 my @temp;
154 foreach my $itm (@items) {
155     push @temp, $itm if $itm->{'itemtype'};
156 }
157 @items = @temp;
158 my $itemcount = @items;
159 $template->param( itemcount => $itemcount );
160
161 my %types;
162 my %itemtypes;
163 my @duedates;
164 #die @items;
165 my %itemhash;
166 my $forloan;
167 foreach my $itm (@items) {
168     push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
169       if defined $itm->{'date_due'};
170     $itm->{ $itm->{'publictype'} } = 1;
171     my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
172         'a', ( $itm->{'biblioitemnumber'} ) );
173     $fee = sprintf "%.02f", $fee;
174     $itm->{'reservefee'} = $fee;
175     my $pty = $itm->{'publictype'};
176     $itemtypes{ $itm->{'itemtype'} } = $itm;
177     unless ( $types{$pty} ) {
178         $types{$pty}->{'count'} = 1;
179         $types{$pty}->{ $itm->{'itemtype'} } = 1;
180         push @{ $types{$pty}->{'items'} }, $itm;
181     }
182     else {
183         unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
184             $types{$pty}->{'count'}++;
185             $types{$pty}->{ $itm->{'itemtype'} } = 1;
186             push @{ $types{$pty}->{'items'} }, $itm;
187         }
188     }
189         $itemhash{$itm->{'itemnumber'}}=$itm;
190         if (!$itm->{'notforloan'} && !$itm->{'itemnotforloan'}){
191                 $forloan=1;
192         }
193 }
194
195 $template->param( ITEMS => \@duedates );
196
197 my $width = keys %types;
198 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
199 my $typecount;
200 foreach my $pt (@publictypes) {
201     $typecount += $pt->{'count'};
202 }
203 $template->param( onlyone => 1 ) if $typecount == 1;
204
205 my @typerows;
206 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
207     my @row;
208     foreach my $pty (@publictypes) {
209         my @items = @{ $pty->{'items'} };
210         push @row, $items[$rownum] if defined $items[$rownum];
211     }
212     my $last = @row;
213     $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
214     my $fill = ( $width - $last ) * 2;
215     $fill-- if $fill;
216     push @typerows, { ROW => \@row, fill => $fill };
217 }
218 $template->param( TYPE_ROWS => \@typerows );
219 $width = 2 * $width - 1;
220 $template->param( totalwidth => 2 * $width - 1, );
221
222 if ( $query->param('place_reserve') ) {
223     my @bibitems=$query->param('biblioitem');
224     my $notes=$query->param('notes');
225     my $checkitem=$query->param('checkitem');
226     my $found;
227     
228     #if we have an item selectionned, and the pickup branch is the same as the holdingbranch of the document, we force the value $rank and $found.
229     if ($checkitem ne ''){
230         $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
231         my $item = $checkitem;
232         $item = GetItem($item);
233         if ( $item->{'holdingbranch'} eq $branch ){
234             $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
235         }
236     }
237         
238         my $count=@bibitems;
239     @bibitems=sort @bibitems;
240     my $i2=1;
241     my @realbi;
242     $realbi[0]=$bibitems[0];
243     for (my $i=1;$i<$count;$i++) {
244         my $i3=$i2-1;
245         if ($realbi[$i3] ne $bibitems[$i]) {
246             $realbi[$i2]=$bibitems[$i];
247             $i2++;
248         }
249     }
250     # here we actually do the reserveration. Stage 3.
251     if ($query->param('request') eq 'any'){
252         # place a request on 1st available
253         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
254     } else {
255         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
256     }
257     print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
258 }
259 else {
260
261     # Here we check that the borrower can actually make reserves Stage 1.
262     my $noreserves     = 0;
263     my $maxoutstanding = C4::Context->preference("maxoutstanding");
264     $template->param( noreserve => 1 ) unless $maxoutstanding;
265     if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
266         my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
267         $template->param( message => 1 );
268         $noreserves = 1;
269         $template->param( too_much_oweing => $amount );
270     }
271     if ( $borr->{gonenoaddress} eq 1 ) {
272         $noreserves = 1;
273         $template->param(
274             message => 1,
275             GNA     => 1
276         );
277     }
278     if ( $borr->{lost} eq 1 ) {
279         $noreserves = 1;
280         $template->param(
281             message => 1,
282             lost    => 1
283         );
284     }
285     if ( $borr->{debarred} eq 1 ) {
286         $noreserves = 1;
287         $template->param(
288             message  => 1,
289             debarred => 1
290         );
291     }
292     my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
293     $template->param( RESERVES => \@reserves );
294     if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
295         $template->param( message => 1 );
296         $noreserves = 1;
297         $template->param( too_many_reserves => scalar(@reserves));
298     }
299     foreach my $res (@reserves) {
300         if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
301             $template->param( message => 1 );
302             $noreserves = 1;
303             $template->param( already_reserved => 1 );
304         }
305     }
306     unless ($noreserves) {
307         $template->param( TYPES             => \@types_old );
308         $template->param( select_item_types => 1 );
309     }
310 }
311
312
313 my %itemnumbers_of_biblioitem;
314 my @itemnumbers  = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
315 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
316
317 foreach my $itemnumber (@itemnumbers) {
318     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
319     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
320 }
321
322 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
323
324 my $notforloan_label_of = get_notforloan_label_of();
325 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
326
327 my @itemtypes;
328 foreach my $biblioitemnumber (@biblioitemnumbers) {
329     push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
330 }
331
332 my @bibitemloop;
333
334 foreach my $biblioitemnumber (@biblioitemnumbers) {
335     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
336
337     $biblioitem->{description} =
338       $itemtypes->{ $biblioitem->{itemtype} }{description};
339
340     foreach
341       my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
342     {
343                 my $item = $itemhash{$itemnumber};
344
345         $item->{homebranchname} =
346           $branches->{ $item->{homebranch} }{branchname};
347
348         # if the holdingbranch is different than the homebranch, we show the
349         # holdingbranch of the document too
350         if ( $item->{homebranch} ne $item->{holdingbranch} ) {
351             $item->{holdingbranchname} =
352               $branches->{ $item->{holdingbranch} }{branchname};
353         }
354         
355 #       add information
356         $item->{itemcallnumber} = $item->{itemcallnumber};
357         
358         # if the item is currently on loan, we display its return date and
359         # change the background color
360         my $issues= GetItemIssue($itemnumber);
361         if ( $issues->{'date_due'} ) {
362             $item->{date_due} = format_date($issues->{'date_due'});
363             $item->{backgroundcolor} = 'onloan';
364         }
365
366         # checking reserve
367         my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
368         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
369
370         if ( defined $reservedate ) {
371             $item->{backgroundcolor} = 'reserved';
372             $item->{reservedate}     = format_date($reservedate);
373             $item->{ReservedForBorrowernumber}     = $reservedfor;
374             $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
375             $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
376             $item->{ExpectedAtLibrary}     = $expectedAt;
377             
378         }
379
380         # Management of the notforloan document
381         if ( $item->{notforloan} || $item->{itemnotforloan}) {
382             $item->{backgroundcolor} = 'other';
383             $item->{notforloanvalue} =
384               $notforloan_label_of->{ $item->{notforloan} };
385         }
386
387         # Management of lost or long overdue items
388         if ( $item->{itemlost} ) {
389
390             # FIXME localized strings should never be in Perl code
391             $item->{message} =
392                 $item->{itemlost} == 1 ? "(lost)"
393               : $item->{itemlost} == 2 ? "(long overdue)"
394               : "";
395             $item->{backgroundcolor} = 'other';
396         }
397
398         # Check of the transfered documents
399         my ( $transfertwhen, $transfertfrom, $transfertto ) =
400           GetTransfers($itemnumber);
401
402         if ( $transfertwhen ne '' ) {
403             $item->{transfertwhen} = format_date($transfertwhen);
404             $item->{transfertfrom} =
405               $branches->{$transfertfrom}{branchname};
406             $item->{transfertto} = $branches->{$transfertto}{branchname};
407                 $item->{nocancel} = 1;
408         }
409
410         # If there is no loan, return and transfer, we show a checkbox.
411         $item->{notforloan} = $item->{notforloan} || 0;
412
413         if (IsAvailableForItemLevelRequest($itemnumber)) {
414             $item->{available} = 1;
415         }
416
417         # FIXME: move this to a pm
418         my $dbh = C4::Context->dbh;
419         my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
420         $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
421         while (my $wait_hashref = $sth2->fetchrow_hashref) {
422             $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
423         }
424         $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
425         push @{ $biblioitem->{itemloop} }, $item;
426     }
427
428     push @bibitemloop, $biblioitem;
429 }
430
431 # display infos
432 $template->param(
433         forloan           => $forloan,
434     bibitemloop       => \@bibitemloop,
435 );
436 output_html_with_http_headers $query, $cookie, $template->output;
437
438 # Local Variables:
439 # tab-width: 8
440 # End: