UI tweaks to OPAC holds screen following addition of multiple holds functionality...
[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 warnings;
20 use CGI;
21 use C4::Biblio;
22 use C4::Items;
23 use C4::Auth;    # checkauth, getborrowernumber.
24 use C4::Koha;
25 use C4::Circulation;
26 use C4::Reserves;
27 use C4::Output;
28 use C4::Dates qw/format_date/;
29 use C4::Context;
30 use C4::Members;
31 use C4::Branch; # GetBranches
32 use C4::Debug;
33 # use Data::Dumper;
34
35 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
36
37 my $query = new CGI;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39     {
40         template_name   => "opac-reserve.tmpl",
41         query           => $query,
42         type            => "opac",
43         authnotrequired => 0,
44         flagsrequired   => { borrow => 1 },
45         debug           => 1,
46     }
47 );
48
49 sub get_out ($$$) {
50         output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
51         exit;
52 }
53
54 # get borrower information ....
55 my ( $borr ) = GetMemberDetails( $borrowernumber );
56
57 # get branches and itemtypes
58 my $branches = GetBranches();
59 my $itemTypes = GetItemTypes();
60
61 # There are two ways of calling this script, with a single biblio num
62 # or multiple biblio nums.
63 my $biblionumbers = $query->param('biblionumbers');
64 my $reserveMode = $query->param('reserve_mode');
65 if ($reserveMode && ($reserveMode eq 'single')) {
66     my $bib = $query->param('single_bib');
67     $biblionumbers = "$bib/";
68 }
69 if (! $biblionumbers) {
70     $biblionumbers = $query->param('biblionumber');
71 }
72
73 if ((! $biblionumbers) && (! $query->param('place_reserve'))) {
74     $template->param(message=>1, no_biblionumber=>1);
75     &get_out($query, $cookie, $template->output);
76 }
77
78 # Pass the numbers to the page so they can be fed back
79 # when the hold is confirmed. TODO: Not necessary?
80 $template->param( biblionumbers => $biblionumbers );
81
82 # Each biblio number is suffixed with '/', e.g. "1/2/3/"
83 my @biblionumbers = split /\//, $biblionumbers;
84 if (($#biblionumbers < 0) && (! $query->param('place_reserve'))) {
85     # TODO: New message?
86     $template->param(message=>1, no_biblionumber=>1);
87     &get_out($query, $cookie, $template->output);
88 }
89
90 # pass the pickup branch along....
91 my $branch = $query->param('branch');
92 $template->param( branch => $branch );
93
94 # make sure it's a real branch
95 if ( !$branch || !$branches->{$branch} ) {
96     $branch = '';
97 }
98 $template->param( branchname => $branches->{$branch}->{'branchname'} );
99
100 # make branch selection options...
101 my @branches;
102 my @select_branch;
103 my %select_branches;
104
105 my @CGIbranchlooparray;
106
107 foreach my $branch ( keys %$branches ) {
108     if ($branch) {
109         my %line;
110         $line{branch} = $branches->{$branch}->{'branchname'};
111         $line{value}  = $branch;
112         if ($line{value} eq C4::Context->userenv->{'branch'}) {
113             $line{selected} = 1;
114         }
115         push @CGIbranchlooparray, \%line;
116     }
117 }
118 @CGIbranchlooparray =
119    sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
120 my $CGIbranchloop = \@CGIbranchlooparray;
121 $template->param( CGIbranch => $CGIbranchloop );
122
123
124 #Debug
125 #output_html_with_http_headers($query,$cookie,"<html><head></head><body> @biblionumbers </body></html>\n");
126 #exit;
127 #my %bibdata;
128 #my $rank;
129 #my $biblionumber;
130 #my $bibdata;
131 #my %itemhash;
132 #my $forloan;
133
134 #
135 #
136 # Build hashes of the requested biblio(item)s and items.
137 #
138 #
139
140 # Hash of biblionumber to biblio/biblioitems record.
141 my %biblioDataHash;
142
143 # Hash of itemnumber to item info.
144 my %itemInfoHash;
145
146 foreach my $biblioNumber (@biblionumbers) {
147
148     my $biblioData = GetBiblioData($biblioNumber);
149     $biblioDataHash{$biblioNumber} = $biblioData;
150
151     my @itemInfos = GetItemsInfo($biblioNumber);
152     $biblioData->{itemInfos} = \@itemInfos;
153     foreach my $itemInfo (@itemInfos) {
154         $itemInfoHash{$itemInfo->{itemnumber}} = $itemInfo;
155     }
156
157     # Compute the priority rank.
158     my ( $rank, $reserves ) = GetReservesFromBiblionumber($biblioNumber);
159     $biblioData->{reservecount} = $rank;
160     foreach my $res (@$reserves) {
161         my $found = $res->{'found'};
162         if ( $found && ($found eq 'W') ) {
163             $rank--;
164         }
165     }
166     $rank++;
167     $biblioData->{rank} = $rank;
168 }
169
170 #
171 #
172 # If this is the second time through this script, it
173 # means we are carrying out the hold request, possibly
174 # with a specific item for each biblionumber.
175 #
176 #
177 if ( $query->param('place_reserve') ) {
178
179     my $notes = $query->param('notes');
180
181     # List is composed of alternating biblio/item/branch
182     my $selectedItems = $query->param('selecteditems');
183
184     if ($query->param('reserve_mode') eq 'single') {
185         # This indicates non-JavaScript mode, so there was
186         # only a single biblio number selected.
187         my $bib = $query->param('single_bib');
188         my $item = $query->param("checkitem_$bib");
189         if ($item eq 'any') {
190             $item = '';
191         }
192         my $branch = $query->param('branch');
193         $selectedItems = "$bib/$item/$branch/";
194     }
195     
196     my @selectedItems = split /\//, $selectedItems;
197
198     # Make sure there is a biblionum/itemnum/branch triplet for each item.
199     # The itemnum can be 'any', meaning next available.
200     my $selectionCount = @selectedItems;
201     if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
202         $template->param(message=>1, bad_data=>1);
203         &get_out($query, $cookie, $template->output);
204     }
205
206     while (@selectedItems) {
207         my $biblioNum = shift(@selectedItems);
208         my $itemNum = shift(@selectedItems);
209         my $branch = shift(@selectedItems); # i.e., branch code, not name
210
211         my $singleBranchMode = $template->param('singleBranchMode');
212         if ($singleBranchMode) {
213             $branch = $borr->{'branchcode'};
214         }
215
216         my $biblioData = $biblioDataHash{$biblioNum};
217         my $found;
218         
219         # If a specific item was selected and the pickup branch is the same as the
220         # holdingbranch, force the value $rank and $found.
221         my $rank = $biblioData->{rank};
222         if ($itemNum ne ''){
223             $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
224             my $item = GetItem($itemNum);
225             if ( $item->{'holdingbranch'} eq $branch ){
226                 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
227             }
228         }
229         else {
230             # Inserts a null into the 'itemnumber' field of 'reserves' table.
231             $itemNum = undef;
232         }
233         
234         # Here we actually do the reserveration. Stage 3.
235         AddReserve($branch, $borrowernumber, $biblioNum, 'a', [$biblioNum], $rank, $notes,
236                    $biblioData->{'title'}, $itemNum, $found);
237     }
238
239     print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
240     exit;
241 }
242
243 #
244 #
245 # Here we check that the borrower can actually make reserves Stage 1.
246 #
247 #
248 my $noreserves     = 0;
249 my $maxoutstanding = C4::Context->preference("maxoutstanding");
250 $template->param( noreserve => 1 ) unless $maxoutstanding;
251 if ( $borr->{'amountoutstanding'} && ($borr->{'amountoutstanding'} > $maxoutstanding) ) {
252     my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
253     $template->param( message => 1 );
254     $noreserves = 1;
255     $template->param( too_much_oweing => $amount );
256 }
257 if ( $borr->{gonenoaddress} && ($borr->{gonenoaddress} eq 1) ) {
258     $noreserves = 1;
259     $template->param(
260                      message => 1,
261                      GNA     => 1
262                     );
263 }
264 if ( $borr->{lost} && ($borr->{lost} eq 1) ) {
265     $noreserves = 1;
266     $template->param(
267                      message => 1,
268                      lost    => 1
269                     );
270 }
271 if ( $borr->{debarred} && ($borr->{debarred} eq 1) ) {
272     $noreserves = 1;
273     $template->param(
274                      message  => 1,
275                      debarred => 1
276                     );
277 }
278
279 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
280 $template->param( RESERVES => \@reserves );
281 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
282     $template->param( message => 1 );
283     $noreserves = 1;
284     $template->param( too_many_reserves => scalar(@reserves));
285 }
286 foreach my $res (@reserves) {
287     foreach my $biblionumber (@biblionumbers) {
288         if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
289 #            $template->param( message => 1 );
290 #            $noreserves = 1;
291 #            $template->param( already_reserved => 1 );
292             $biblioDataHash{$biblionumber}->{already_reserved} = 1;
293         }
294     }
295 }
296
297 unless ($noreserves) {
298     $template->param( select_item_types => 1 );
299 }
300
301
302 #
303 #
304 # Build the template parameters that will show the info
305 # and items for each biblionumber.
306 #
307 #
308 my $notforloan_label_of = get_notforloan_label_of();
309
310 my $biblioLoop = [];
311 my $numBibsAvailable = 0;
312 my $itemLevelTypes = C4::Context->preference('item-level_itypes');
313 $template->param('item-level_itypes' => $itemLevelTypes);
314
315 foreach my $biblioNum (@biblionumbers) {
316
317     my $record = GetMarcBiblio($biblioNum);
318     my $subtitle = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
319     # Init the bib item with the choices for branch pickup
320     my %biblioLoopIter = ( branchChoicesLoop => $CGIbranchloop );
321
322     # Get relevant biblio data.
323     my $biblioData = $biblioDataHash{$biblioNum};
324     if (! $biblioData) {
325         $template->param(message=>1, bad_biblionumber=>$biblioNum);
326         &get_out($query, $cookie, $template->output);
327     }
328
329     $biblioLoopIter{biblionumber} = $biblioData->{biblionumber};
330     $biblioLoopIter{title} = $biblioData->{title};
331     $biblioLoopIter{subtitle} = $subtitle;
332     $biblioLoopIter{author} = $biblioData->{author};
333     $biblioLoopIter{rank} = $biblioData->{rank};
334     $biblioLoopIter{reservecount} = $biblioData->{reservecount};
335     $biblioLoopIter{already_reserved} = $biblioData->{already_reserved};
336
337     if (!$itemLevelTypes && $biblioData->{itemtype}) {
338         $biblioLoopIter{description} = $itemTypes->{$biblioData->{itemtype}}{description};
339         $biblioLoopIter{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$biblioData->{itemtype}}{imageurl};
340     }
341
342     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
343         $debug and warn $itemInfo->{'notforloan'};
344
345         # Get reserve fee.
346         my $fee = GetReserveFee(undef, $borrowernumber, $itemInfo->{'biblionumber'}, 'a',
347                                 ( $itemInfo->{'biblioitemnumber'} ) );
348         $itemInfo->{'reservefee'} = sprintf "%.02f", ($fee ? $fee : 0.0);
349         
350         if ($itemLevelTypes && $itemInfo->{itype}) {
351             $itemInfo->{description} = $itemTypes->{$itemInfo->{itype}}{description};
352             $itemInfo->{imageurl} = getitemtypeimagesrc() . "/". $itemTypes->{$itemInfo->{itype}}{imageurl};
353         }
354         
355         if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
356             $biblioLoopIter{forloan} = 1;
357         }
358     }
359
360     $biblioLoopIter{itemTypeDescription} =
361       $itemTypes->{$biblioData->{itemtype}}{description};
362     
363
364     $biblioLoopIter{itemLoop} = [];
365     my $numCopiesAvailable = 0;
366     foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
367         my $itemNum = $itemInfo->{itemnumber};
368         my $itemLoopIter = {};
369
370         $itemLoopIter->{itemnumber} = $itemNum;
371         $itemLoopIter->{barcode} = $itemInfo->{barcode};
372         $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
373         $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
374         $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
375         if ($itemLevelTypes) {
376             $itemLoopIter->{description} = $itemInfo->{description};
377             $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
378         }
379
380         # If the holdingbranch is different than the homebranch, we show the
381         # holdingbranch of the document too.
382         if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
383             $itemLoopIter->{holdingBranchName} =
384               $branches->{ $itemInfo->{holdingbranch} }{branchname};
385         }
386
387         # If the item is currently on loan, we display its return date and
388         # change the background color.
389         my $issues= GetItemIssue($itemNum);
390         if ( $issues->{'date_due'} ) {
391             $itemLoopIter->{dateDue} = format_date($issues->{'date_due'});
392             $itemLoopIter->{backgroundcolor} = 'onloan';
393         }
394
395         # checking reserve
396         my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemNum);
397         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
398
399         if ( defined $reservedate ) {
400             $itemLoopIter->{backgroundcolor} = 'reserved';
401             $itemLoopIter->{reservedate}     = format_date($reservedate);
402             $itemLoopIter->{ReservedForBorrowernumber}     = $reservedfor;
403             $itemLoopIter->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
404             $itemLoopIter->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
405             $itemLoopIter->{ExpectedAtLibrary}     = $expectedAt;
406         }
407
408         $itemLoopIter->{notforloan} = $itemInfo->{notforloan};
409         $itemLoopIter->{itemnotforloan} = $itemInfo->{itemnotforloan};
410
411         # Management of the notforloan document
412         if ( $itemLoopIter->{notforloan} || $itemLoopIter->{itemnotforloan}) {
413             $itemLoopIter->{backgroundcolor} = 'other';
414             $itemLoopIter->{notforloanvalue} =
415               $notforloan_label_of->{ $itemLoopIter->{notforloan} };
416         }
417
418         # Management of lost or long overdue items
419         if ( $itemInfo->{itemlost} ) {
420
421             # FIXME localized strings should never be in Perl code
422             $itemLoopIter->{message} =
423                 $itemInfo->{itemlost} == 1 ? "(lost)"
424               : $itemInfo->{itemlost} == 2 ? "(long overdue)"
425               : "";
426             $itemInfo->{backgroundcolor} = 'other';
427         }
428
429         # Check of the transfered documents
430         my ( $transfertwhen, $transfertfrom, $transfertto ) =
431           GetTransfers($itemNum);
432         if ( $transfertwhen && ($transfertwhen ne '') ) {
433             $itemLoopIter->{transfertwhen} = format_date($transfertwhen);
434             $itemLoopIter->{transfertfrom} =
435               $branches->{$transfertfrom}{branchname};
436             $itemLoopIter->{transfertto} = $branches->{$transfertto}{branchname};
437             $itemLoopIter->{nocancel} = 1;
438         }
439
440         # If there is no loan, return and transfer, we show a checkbox.
441         $itemLoopIter->{notforloan} = $itemLoopIter->{notforloan} || 0;
442
443         my $branchitemrule = GetBranchItemRule( $borr->{'branchcode'}, $itemInfo->{'itype'} );
444         my $policy_holdallowed = 1;
445
446         if ( $branchitemrule->{'holdallowed'} == 0 ||
447                 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $itemInfo->{'homebranch'} ) ) {
448             $policy_holdallowed = 0;
449         }
450
451         if (IsAvailableForItemLevelRequest($itemNum) and $policy_holdallowed) {
452             $itemLoopIter->{available} = 1;
453             $numCopiesAvailable++;
454         }
455
456         # FIXME: move this to a pm
457         my $dbh = C4::Context->dbh;
458         my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
459         $sth2->execute($itemLoopIter->{ReservedForBorrowernumber}, $itemNum);
460         while (my $wait_hashref = $sth2->fetchrow_hashref) {
461             $itemLoopIter->{waitingdate} = format_date($wait_hashref->{waitingdate});
462         }
463         $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
464
465         
466         push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
467     }
468
469     if ($numCopiesAvailable > 0) {
470         $numBibsAvailable++;
471         $biblioLoopIter{bib_available} = 1;
472         $biblioLoopIter{holdable} = 1;
473     }
474     if ($biblioLoopIter{already_reserved}) {
475         $biblioLoopIter{holdable} = undef;
476     }
477
478     push @$biblioLoop, \%biblioLoopIter;
479 }
480
481
482 if ( $numBibsAvailable == 0 ) {
483     $template->param( none_available => 1, message => 1 );
484 }
485
486
487 my $itemTableColspan = 5;
488 if (!$template->param('OPACItemHolds')) {
489     $itemTableColspan--;
490 }
491 if ($template->param('singleBranchMode')) {
492     $itemTableColspan--;
493 }
494 $template->param(itemtable_colspan => $itemTableColspan);
495
496
497
498 # display infos
499 $template->param(bibitemloop => $biblioLoop);
500 output_html_with_http_headers $query, $cookie, $template->output;
501
502
503
504
505
506 # Local Variables:
507 # tab-width: 8
508 # End: