3 # This file is part of Koha.
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
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.
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
22 use C4::Auth; # checkauth, getborrowernumber.
27 use C4::Dates qw/format_date/;
30 use C4::Branch; # GetBranches
34 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39 template_name => "opac-reserve.tmpl",
43 flagsrequired => { borrow => 1 },
49 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
53 # get borrower information ....
54 my ( $borr ) = GetMemberDetails( $borrowernumber );
56 # get branches and itemtypes
57 my $branches = GetBranches();
58 my $itemtypes = GetItemTypes();
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);
66 $template->param( biblionumber => $biblionumber );
68 my $bibdata = GetBiblioData($biblionumber);
70 $template->param(message=>1,bad_biblionumber=>$biblionumber);
71 &get_out($query, $cookie, $template->output);
73 $template->param($bibdata); # FIXME: bad form.
75 # get the rank number....
76 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
77 $template->param( reservecount => $rank );
79 foreach my $res (@$reserves) {
80 if ( $res->{'found'} eq 'W' ) {
86 $template->param( rank => $rank );
88 # pass the pickup branch along....
89 my $branch = $query->param('branch');
90 $template->param( branch => $branch );
92 # make sure it's a real branch
93 if ( !$branches->{$branch} ) {
96 $template->param( branchname => $branches->{$branch}->{'branchname'} );
98 # make branch selection options...
103 my @CGIbranchlooparray;
105 foreach my $branch ( keys %$branches ) {
108 $line{branch} = $branches->{$branch}->{'branchname'};
109 $line{value} = $branch;
110 if ($line{value} eq C4::Context->userenv->{'branch'}) {
113 push @CGIbranchlooparray, \%line;
116 @CGIbranchlooparray = sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
117 my $CGIbranchloop = \@CGIbranchlooparray;
118 $template->param( CGIbranch => $CGIbranchloop );
120 my @items = GetItemsInfo($biblionumber);
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 ;
126 $template->param( itemcount => scalar(@items) );
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)){
142 if ( $query->param('place_reserve') ) {
143 my @bibitems=$query->param('biblioitem');
144 my $notes=$query->param('notes');
145 my $checkitem=$query->param('checkitem');
148 # #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.
149 # if ($checkitem ne ''){
150 # $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
151 # my $item = $checkitem;
152 # $item = GetItem($item);
153 # if ( $item->{'holdingbranch'} eq $branch ){
154 # $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
155 my @selectedItems = split /\//, $selectedItems;
157 # Make sure there is a biblionum/itemnum/branch triplet for each item.
158 # The itemnum can be 'any', meaning next available.
159 my $selectionCount = @selectedItems;
160 if (($selectionCount == 0) || (($selectionCount % 3) != 0)) {
161 $template->param(message=>1, bad_data=>1);
162 &get_out($query, $cookie, $template->output);
165 while (@selectedItems) {
166 my $biblioNum = shift(@selectedItems);
167 my $itemNum = shift(@selectedItems);
168 my $branch = shift(@selectedItems); # i.e., branch code, not name
170 my $singleBranchMode = $template->param('singleBranchMode');
171 if ($singleBranchMode) {
172 $branch = $borr->{'branchcode'};
177 @bibitems=sort @bibitems;
180 $realbi[0]=$bibitems[0];
181 for (my $i=1;$i<$count;$i++) {
183 if ($realbi[$i3] ne $bibitems[$i]) {
184 $realbi[$i2]=$bibitems[$i];
188 # here we actually do the reserveration. Stage 3.
189 if ($query->param('request') eq 'any'){
190 # place a request on 1st available
191 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
193 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
195 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
199 # Here we check that the borrower can actually make reserves Stage 1.
201 my $maxoutstanding = C4::Context->preference("maxoutstanding");
202 $template->param( noreserve => 1 ) unless $maxoutstanding;
203 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
204 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
205 $template->param( message => 1 );
207 $template->param( too_much_oweing => $amount );
209 if ( $borr->{gonenoaddress} eq 1 ) {
216 if ( $borr->{lost} eq 1 ) {
223 if ( $borr->{debarred} eq 1 ) {
230 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
231 $template->param( RESERVES => \@reserves );
232 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
233 $template->param( message => 1 );
235 $template->param( too_many_reserves => scalar(@reserves));
237 foreach my $res (@reserves) {
238 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
239 $template->param( message => 1 );
241 $template->param( already_reserved => 1 );
244 unless ($noreserves) {
245 $template->param( select_item_types => 1 );
250 my %itemnumbers_of_biblioitem;
251 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
252 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
254 foreach my $itemnumber (@itemnumbers) {
255 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
256 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
259 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
261 my $notforloan_label_of = get_notforloan_label_of();
262 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
265 foreach my $biblioitemnumber (@biblioitemnumbers) {
266 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
271 foreach my $biblioitemnumber (@biblioitemnumbers) {
272 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
274 # Get relevant biblio data.
275 my $biblioData = $biblioDataHash{$biblioNum};
277 $template->param(message=>1, bad_biblionumber=>$biblioNum);
278 &get_out($query, $cookie, $template->output);
282 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
284 my $item = $itemhash{$itemnumber};
286 $item->{homebranchname} =
287 $branches->{ $item->{homebranch} }{branchname};
289 # if the holdingbranch is different than the homebranch, we show the
290 # holdingbranch of the document too
291 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
292 $item->{holdingbranchname} =
293 $branches->{ $item->{holdingbranch} }{branchname};
296 if (!$itemInfo->{'notforloan'} && !($itemInfo->{'itemnotforloan'} > 0)) {
297 $biblioLoopIter{forloan} = 1;
301 $biblioLoopIter{itemTypeDescription} = $itemTypes->{$biblioData->{itemtype}}{description};
303 $biblioLoopIter{itemLoop} = [];
304 my $numCopiesAvailable = 0;
305 foreach my $itemInfo (@{$biblioData->{itemInfos}}) {
306 my $itemNum = $itemInfo->{itemnumber};
307 my $itemLoopIter = {};
309 $itemLoopIter->{itemnumber} = $itemNum;
310 $itemLoopIter->{barcode} = $itemInfo->{barcode};
311 $itemLoopIter->{homeBranchName} = $branches->{$itemInfo->{homebranch}}{branchname};
312 $itemLoopIter->{callNumber} = $itemInfo->{itemcallnumber};
313 $itemLoopIter->{copynumber} = $itemInfo->{copynumber};
314 if ($itemLevelTypes) {
315 $itemLoopIter->{description} = $itemInfo->{description};
316 $itemLoopIter->{imageurl} = $itemInfo->{imageurl};
319 # If the holdingbranch is different than the homebranch, we show the
320 # holdingbranch of the document too.
321 if ( $itemInfo->{homebranch} ne $itemInfo->{holdingbranch} ) {
322 $itemLoopIter->{holdingBranchName} =
323 $branches->{ $itemInfo->{holdingbranch} }{branchname};
326 # If the item is currently on loan, we display its return date and
327 # change the background color.
328 my $issues= GetItemIssue($itemNum);
329 if ( $issues->{'date_due'} ) {
330 $item->{date_due} = format_date($issues->{'date_due'});
331 $item->{backgroundcolor} = 'onloan';
335 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
336 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
338 if ( defined $reservedate ) {
339 $itemLoopIter->{backgroundcolor} = 'reserved';
340 $itemLoopIter->{reservedate} = format_date($reservedate);
341 $itemLoopIter->{ReservedForBorrowernumber} = $reservedfor;
342 $itemLoopIter->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
343 $itemLoopIter->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
344 $itemLoopIter->{ExpectedAtLibrary} = $expectedAt;
347 # Management of the notforloan document
348 if ( $item->{notforloan} || $item->{itemnotforloan}) {
349 $item->{backgroundcolor} = 'other';
350 $item->{notforloanvalue} =
351 $notforloan_label_of->{ $item->{notforloan} };
354 # Management of lost or long overdue items
355 if ( $item->{itemlost} ) {
357 # FIXME localized strings should never be in Perl code
359 $item->{itemlost} == 1 ? "(lost)"
360 : $item->{itemlost} == 2 ? "(long overdue)"
362 $item->{backgroundcolor} = 'other';
365 # Check of the transfered documents
366 my ( $transfertwhen, $transfertfrom, $transfertto ) =
367 GetTransfers($itemnumber);
369 if ( $transfertwhen ne '' ) {
370 $item->{transfertwhen} = format_date($transfertwhen);
371 $item->{transfertfrom} =
372 $branches->{$transfertfrom}{branchname};
373 $item->{transfertto} = $branches->{$transfertto}{branchname};
374 $item->{nocancel} = 1;
377 # If there is no loan, return and transfer, we show a checkbox.
378 $item->{notforloan} = $item->{notforloan} || 0;
380 if (IsAvailableForItemLevelRequest($itemnumber)) {
381 $item->{available} = 1;
384 # FIXME: move this to a pm
385 my $dbh = C4::Context->dbh;
386 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
387 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
388 while (my $wait_hashref = $sth2->fetchrow_hashref) {
389 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
391 $itemLoopIter->{imageurl} = getitemtypeimagelocation( 'opac', $itemTypes->{ $itemInfo->{itype} }{imageurl} );
393 push @{$biblioLoopIter{itemLoop}}, $itemLoopIter;
396 if ($numCopiesAvailable > 0) {
398 $biblioLoopIter{bib_available} = 1;
399 $biblioLoopIter{holdable} = 1;
401 if ($biblioLoopIter{already_reserved}) {
402 $biblioLoopIter{holdable} = undef;
405 push @bibitemloop, $biblioitem;
408 if ( $numBibsAvailable == 0 ) {
409 $template->param( none_available => 1, message => 1 );
412 my $itemTableColspan = 5;
413 if (!$template->param('OPACItemHolds')) {
416 if ($template->param('singleBranchMode')) {
419 $template->param(itemtable_colspan => $itemTableColspan);
424 bibitemloop => \@bibitemloop,
426 output_html_with_http_headers $query, $cookie, $template->output;