whitespace cleanup and remove editor comments
[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 C4::Debug;
32 # use Data::Dumper;
33
34 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
35
36 my $query = new CGI;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name   => "opac-reserve.tmpl",
40         query           => $query,
41         type            => "opac",
42         authnotrequired => 0,
43         flagsrequired   => { borrow => 1 },
44         debug           => 1,
45     }
46 );
47
48 sub get_out ($$$) {
49         output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
50         exit;
51 }
52
53 # get borrower information ....
54 my ( $borr ) = GetMemberDetails( $borrowernumber );
55
56 # get branches and itemtypes
57 my $branches = GetBranches();
58 my $itemtypes = GetItemTypes();
59
60 # get biblionumber.....
61 my $biblionumber = $query->param('biblionumber');
62 if (! $biblionumber) {
63         $template->param(message=>1,no_biblionumber=>1);
64         &get_out($query, $cookie, $template->output);
65 }
66 $template->param( biblionumber => $biblionumber );
67
68 my $bibdata = GetBiblioData($biblionumber);
69 if (! $bibdata) {
70         $template->param(message=>1,bad_biblionumber=>$biblionumber);
71         &get_out($query, $cookie, $template->output);
72 }
73 $template->param($bibdata);             # FIXME: bad form.
74
75 # get the rank number....
76 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
77 $template->param( reservecount => $rank );
78
79 foreach my $res (@$reserves) {
80     if ( $res->{'found'} eq 'W' ) {
81         $rank--;
82     }
83 }
84
85 $rank++;
86 $template->param( rank => $rank );
87
88 # pass the pickup branch along....
89 my $branch = $query->param('branch');
90 $template->param( branch => $branch );
91
92 # make sure it's a real branch
93 if ( !$branches->{$branch} ) {
94     $branch = '';
95 }
96 $template->param( branchname => $branches->{$branch}->{'branchname'} );
97
98 # make branch selection options...
99 my @branches;
100 my @select_branch;
101 my %select_branches;
102
103 my @CGIbranchlooparray;
104
105 foreach my $branch ( keys %$branches ) {
106     if ($branch) {
107         my %line;
108         $line{branch} = $branches->{$branch}->{'branchname'};
109         $line{value}  = $branch;
110         if ($line{value} eq C4::Context->userenv->{'branch'}) {
111             $line{selected} = 1;
112         }
113         push @CGIbranchlooparray, \%line;
114     }
115 }
116 @CGIbranchlooparray = sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
117 my $CGIbranchloop = \@CGIbranchlooparray;
118 $template->param( CGIbranch => $CGIbranchloop );
119
120 my @items = GetItemsInfo($biblionumber);
121
122 # an item was previously required to have an itemtype in order to be reserved.
123 # this behavior removed to fix bug 1891 .
124 # my @items = grep { (C4::Context->preference('item-level_itypes') ? $_->{'itype'} : $_->{'itemtype'} ) } @all_items ;
125
126 $template->param( itemcount => scalar(@items) );
127
128 my %itemhash;
129 my $forloan;
130 foreach my $itm (@items) {
131         $debug and warn $itm->{'notforloan'};
132     my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'}, 'a', ( $itm->{'biblioitemnumber'} ) );
133     $itm->{'reservefee'} = sprintf "%.02f", $fee;
134     # pass itype to itemtype for display purposes.  
135     $itm->{'itemtype'} = $itm->{'itype'} if(C4::Context->preference('item-level_itypes'));      
136         $itemhash{$itm->{'itemnumber'}}=$itm;
137     if (!$itm->{'notforloan'} && !($itm->{'itemnotforloan'} > 0)){
138                 $forloan=1;
139         }
140 }
141
142 if ( $query->param('place_reserve') ) {
143     my @bibitems=$query->param('biblioitem');
144     my $notes=$query->param('notes');
145     my $checkitem=$query->param('checkitem');
146     my $found;
147     
148 <<<<<<< HEAD:opac/opac-reserve.pl
149     #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.
150     if ($checkitem ne ''){
151         $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
152         my $item = $checkitem;
153         $item = GetItem($item);
154         if ( $item->{'holdingbranch'} eq $branch ){
155             $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
156 =======
157     my @selectedItems = split /\//, $selectedItems;
158
159     # Make sure there is a biblionum/itemnum/branch triplet for each item.
160     # The itemnum can be 'any', meaning next available.
161     my $selectionCount = @selectedItems;
162     if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
163         $template->param(message=>1, bad_data=>1);
164         &get_out($query, $cookie, $template->output);
165     }
166
167     while (@selectedItems) {
168         my $biblioNum = shift(@selectedItems);
169         my $itemNum   = shift(@selectedItems);
170         my $branch    = shift(@selectedItems); # i.e., branch code, not name
171
172         my $singleBranchMode = $template->param('singleBranchMode');
173         if ($singleBranchMode) {
174             $branch = $borr->{'branchcode'};
175 >>>>>>> 9f01cc0... whitespace cleanup and remove editor comments:opac/opac-reserve.pl
176         }
177     }
178         
179         my $count=@bibitems;
180     @bibitems=sort @bibitems;
181     my $i2=1;
182     my @realbi;
183     $realbi[0]=$bibitems[0];
184     for (my $i=1;$i<$count;$i++) {
185         my $i3=$i2-1;
186         if ($realbi[$i3] ne $bibitems[$i]) {
187             $realbi[$i2]=$bibitems[$i];
188             $i2++;
189         }
190     }
191     # here we actually do the reserveration. Stage 3.
192     if ($query->param('request') eq 'any'){
193         # place a request on 1st available
194         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
195     } else {
196         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
197     }
198     print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
199 }
200 else {
201
202     # Here we check that the borrower can actually make reserves Stage 1.
203     my $noreserves     = 0;
204     my $maxoutstanding = C4::Context->preference("maxoutstanding");
205     $template->param( noreserve => 1 ) unless $maxoutstanding;
206     if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
207         my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
208         $template->param( message => 1 );
209         $noreserves = 1;
210         $template->param( too_much_oweing => $amount );
211     }
212     if ( $borr->{gonenoaddress} eq 1 ) {
213         $noreserves = 1;
214         $template->param(
215             message => 1,
216             GNA     => 1
217         );
218     }
219     if ( $borr->{lost} eq 1 ) {
220         $noreserves = 1;
221         $template->param(
222             message => 1,
223             lost    => 1
224         );
225     }
226     if ( $borr->{debarred} eq 1 ) {
227         $noreserves = 1;
228         $template->param(
229             message  => 1,
230             debarred => 1
231         );
232     }
233     my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
234     $template->param( RESERVES => \@reserves );
235     if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
236         $template->param( message => 1 );
237         $noreserves = 1;
238         $template->param( too_many_reserves => scalar(@reserves));
239     }
240     foreach my $res (@reserves) {
241         if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
242             $template->param( message => 1 );
243             $noreserves = 1;
244             $template->param( already_reserved => 1 );
245         }
246     }
247     unless ($noreserves) {
248         $template->param( select_item_types => 1 );
249     }
250 }
251
252
253 my %itemnumbers_of_biblioitem;
254 my @itemnumbers  = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
255 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
256
257 foreach my $itemnumber (@itemnumbers) {
258     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
259     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
260 }
261
262 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
263
264 my $notforloan_label_of = get_notforloan_label_of();
265 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
266
267 my @itemtypes;
268 foreach my $biblioitemnumber (@biblioitemnumbers) {
269     push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
270 }
271
272 my @bibitemloop;
273
274 foreach my $biblioitemnumber (@biblioitemnumbers) {
275     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
276
277 <<<<<<< HEAD:opac/opac-reserve.pl
278     $biblioitem->{description} =
279       $itemtypes->{ $biblioitem->{itemtype} }{description};
280 =======
281     # Get relevant biblio data.
282     my $biblioData = $biblioDataHash{$biblioNum};
283     if (! $biblioData) {
284         $template->param(message=>1, bad_biblionumber=>$biblioNum);
285         &get_out($query, $cookie, $template->output);
286     }
287 >>>>>>> 9f01cc0... whitespace cleanup and remove editor comments:opac/opac-reserve.pl
288
289     foreach
290       my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
291     {
292                 my $item = $itemhash{$itemnumber};
293
294         $item->{homebranchname} =
295           $branches->{ $item->{homebranch} }{branchname};
296
297         # if the holdingbranch is different than the homebranch, we show the
298         # holdingbranch of the document too
299         if ( $item->{homebranch} ne $item->{holdingbranch} ) {
300             $item->{holdingbranchname} =
301               $branches->{ $item->{holdingbranch} }{branchname};
302         }
303         
304 <<<<<<< HEAD:opac/opac-reserve.pl
305 #       add information
306         $item->{itemcallnumber} = $item->{itemcallnumber};
307         
308         # if the item is currently on loan, we display its return date and
309         # change the background color
310         my $issues= GetItemIssue($itemnumber);
311 =======
312         if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
313             $biblioLoopIter{forloan} = 1;
314         }
315     }
316
317     $biblioLoopIter{itemTypeDescription} = $itemTypes->{$biblioData->{itemtype}}{description};
318
319     $biblioLoopIter{itemLoop} = [];
320     my $numCopiesAvailable = 0;
321     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
322         my $itemNum = $itemInfo->{itemnumber};
323         my $itemLoopIter = {};
324
325         $itemLoopIter->{itemnumber} = $itemNum;
326         $itemLoopIter->{barcode} = $itemInfo->{barcode};
327         $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
328         $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
329         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
330         if ($itemLevelTypes) {
331             $itemLoopIter->{description} = $itemInfo->{description};
332             $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
333         }
334
335         # If the holdingbranch is different than the homebranch, we show the
336         # holdingbranch of the document too.
337         if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
338             $itemLoopIter->{holdingBranchName} =
339               $branches->{ $itemInfo->{holdingbranch} }{branchname};
340         }
341
342         # If the item is currently on loan, we display its return date and
343         # change the background color.
344         my $issues= GetItemIssue($itemNum);
345 >>>>>>> 9f01cc0... whitespace cleanup and remove editor comments:opac/opac-reserve.pl
346         if ( $issues->{'date_due'} ) {
347             $item->{date_due} = format_date($issues->{'date_due'});
348             $item->{backgroundcolor} = 'onloan';
349         }
350
351         # checking reserve
352         my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
353         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
354
355         if ( defined $reservedate ) {
356 <<<<<<< HEAD:opac/opac-reserve.pl
357             $item->{backgroundcolor} = 'reserved';
358             $item->{reservedate}     = format_date($reservedate);
359             $item->{ReservedForBorrowernumber}     = $reservedfor;
360             $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
361             $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
362             $item->{ExpectedAtLibrary}     = $expectedAt;
363             
364 =======
365             $itemLoopIter->{backgroundcolor} = 'reserved';
366             $itemLoopIter->{reservedate}     = format_date($reservedate);
367             $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
368             $itemLoopIter->{ReservedForSurname}        = $ItemBorrowerReserveInfo->{'surname'};
369             $itemLoopIter->{ReservedForFirstname}      = $ItemBorrowerReserveInfo->{'firstname'};
370             $itemLoopIter->{ExpectedAtLibrary}         = $expectedAt;
371 >>>>>>> 9f01cc0... whitespace cleanup and remove editor comments:opac/opac-reserve.pl
372         }
373
374         # Management of the notforloan document
375         if ( $item->{notforloan} || $item->{itemnotforloan}) {
376             $item->{backgroundcolor} = 'other';
377             $item->{notforloanvalue} =
378               $notforloan_label_of->{ $item->{notforloan} };
379         }
380
381         # Management of lost or long overdue items
382         if ( $item->{itemlost} ) {
383
384             # FIXME localized strings should never be in Perl code
385             $item->{message} =
386                 $item->{itemlost} == 1 ? "(lost)"
387               : $item->{itemlost} == 2 ? "(long overdue)"
388               : "";
389             $item->{backgroundcolor} = 'other';
390         }
391
392         # Check of the transfered documents
393         my ( $transfertwhen, $transfertfrom, $transfertto ) =
394           GetTransfers($itemnumber);
395
396         if ( $transfertwhen ne '' ) {
397             $item->{transfertwhen} = format_date($transfertwhen);
398             $item->{transfertfrom} =
399               $branches->{$transfertfrom}{branchname};
400             $item->{transfertto} = $branches->{$transfertto}{branchname};
401                 $item->{nocancel} = 1;
402         }
403
404         # If there is no loan, return and transfer, we show a checkbox.
405         $item->{notforloan} = $item->{notforloan} || 0;
406
407         if (IsAvailableForItemLevelRequest($itemnumber)) {
408             $item->{available} = 1;
409         }
410
411         # FIXME: move this to a pm
412         my $dbh = C4::Context->dbh;
413         my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
414         $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
415         while (my $wait_hashref = $sth2->fetchrow_hashref) {
416             $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
417         }
418 <<<<<<< HEAD:opac/opac-reserve.pl
419         $item->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $item->{itype} }{imageurl} );
420         push @{ $biblioitem->{itemloop} }, $item;
421 =======
422         $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
423         
424         push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
425     }
426
427     if ($numCopiesAvailable > 0) {
428         $numBibsAvailable++;
429         $biblioLoopIter{bib_available} = 1;
430         $biblioLoopIter{holdable} = 1;
431     }
432     if ($biblioLoopIter{already_reserved}) {
433         $biblioLoopIter{holdable} = undef;
434 >>>>>>> 9f01cc0... whitespace cleanup and remove editor comments:opac/opac-reserve.pl
435     }
436
437     push @bibitemloop, $biblioitem;
438 }
439
440 <<<<<<< HEAD:opac/opac-reserve.pl
441 =======
442 if ( $numBibsAvailable == 0 ) {
443     $template->param( none_available => 1, message => 1 );
444 }
445
446 my $itemTableColspan = 5;
447 if (!$template->param('OPACItemHolds')) {
448     $itemTableColspan--;
449 }
450 if ($template->param('singleBranchMode')) {
451     $itemTableColspan--;
452 }
453 $template->param(itemtable_colspan => $itemTableColspan);
454
455 >>>>>>> 9f01cc0... whitespace cleanup and remove editor comments:opac/opac-reserve.pl
456 # display infos
457 $template->param(
458         forloan           => $forloan,
459     bibitemloop       => \@bibitemloop,
460 );
461 output_html_with_http_headers $query, $cookie, $template->output;
462
463 <<<<<<< HEAD:opac/opac-reserve.pl
464 # Local Variables:
465 # tab-width: 8
466 # End:
467 =======
468 >>>>>>> 9f01cc0... whitespace cleanup and remove editor comments:opac/opac-reserve.pl