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, $flags ) = 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 =
117 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
118 my $CGIbranchloop = \@CGIbranchlooparray;
119 $template->param( CGIbranch => $CGIbranchloop );
121 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
122 # get the itemtype data....
123 my @items = GetItemsInfo($biblionumber);
124 #######################################################
125 # old version, add so that old templates still work
127 foreach my $itm (@items) {
128 my $ity = $itm->{'itemtype'};
129 unless ( $types_old{$ity} ) {
130 $types_old{$ity}->{'itemtype'} = $ity;
131 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
132 $types_old{$ity}->{'description'} = $itm->{'description'};
135 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
139 foreach my $type ( values %types_old ) {
141 foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
143 $branches->{$bc}->{'branchname'} . "("
144 . $type->{'branchinfo'}->{$bc} . ")";
146 $type->{'copies'} = $copies;
149 my @types_old = values %types_old;
152 ################################
155 foreach my $itm (@items) {
156 push @temp, $itm if $itm->{'itemtype'};
159 my $itemcount = @items;
160 $template->param( itemcount => $itemcount );
168 foreach my $itm (@items) {
169 push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
170 if defined $itm->{'date_due'};
171 $itm->{ $itm->{'publictype'} } = 1;
172 $debug and warn $itm->{'notforloan'};
173 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
174 'a', ( $itm->{'biblioitemnumber'} ) );
175 $fee = sprintf "%.02f", $fee;
176 $itm->{'reservefee'} = $fee;
177 my $pty = $itm->{'publictype'};
178 $itemtypes{ $itm->{'itemtype'} } = $itm;
179 unless ( $types{$pty} ) {
180 $types{$pty}->{'count'} = 1;
181 $types{$pty}->{ $itm->{'itemtype'} } = 1;
182 push @{ $types{$pty}->{'items'} }, $itm;
185 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
186 $types{$pty}->{'count'}++;
187 $types{$pty}->{ $itm->{'itemtype'} } = 1;
188 push @{ $types{$pty}->{'items'} }, $itm;
191 $itemhash{$itm->{'itemnumber'}}=$itm;
192 if (!$itm->{'notforloan'} && !$itm->{'itemnotforloan'}){
197 $template->param( ITEMS => \@duedates );
199 my $width = keys %types;
200 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
202 foreach my $pt (@publictypes) {
203 $typecount += $pt->{'count'};
205 $template->param( onlyone => 1 ) if $typecount == 1;
208 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
210 foreach my $pty (@publictypes) {
211 my @items = @{ $pty->{'items'} };
212 push @row, $items[$rownum] if defined $items[$rownum];
215 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
216 my $fill = ( $width - $last ) * 2;
218 push @typerows, { ROW => \@row, fill => $fill };
220 $template->param( TYPE_ROWS => \@typerows );
221 $width = 2 * $width - 1;
222 $template->param( totalwidth => 2 * $width - 1, );
224 if ( $query->param('place_reserve') ) {
225 my @bibitems=$query->param('biblioitem');
226 my $notes=$query->param('notes');
227 my $checkitem=$query->param('checkitem');
230 #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.
231 if ($checkitem ne ''){
232 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
233 my $item = $checkitem;
234 $item = GetItem($item);
235 if ( $item->{'holdingbranch'} eq $branch ){
236 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
241 @bibitems=sort @bibitems;
244 $realbi[0]=$bibitems[0];
245 for (my $i=1;$i<$count;$i++) {
247 if ($realbi[$i3] ne $bibitems[$i]) {
248 $realbi[$i2]=$bibitems[$i];
252 # here we actually do the reserveration. Stage 3.
253 if ($query->param('request') eq 'any'){
254 # place a request on 1st available
255 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
257 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
259 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
263 # Here we check that the borrower can actually make reserves Stage 1.
265 my $maxoutstanding = C4::Context->preference("maxoutstanding");
266 $template->param( noreserve => 1 ) unless $maxoutstanding;
267 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
268 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
269 $template->param( message => 1 );
271 $template->param( too_much_oweing => $amount );
273 if ( $borr->{gonenoaddress} eq 1 ) {
280 if ( $borr->{lost} eq 1 ) {
287 if ( $borr->{debarred} eq 1 ) {
294 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
295 $template->param( RESERVES => \@reserves );
296 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
297 $template->param( message => 1 );
299 $template->param( too_many_reserves => scalar(@reserves));
301 foreach my $res (@reserves) {
302 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
303 $template->param( message => 1 );
305 $template->param( already_reserved => 1 );
308 unless ($noreserves) {
309 $template->param( TYPES => \@types_old );
310 $template->param( select_item_types => 1 );
315 my %itemnumbers_of_biblioitem;
316 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
317 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
319 foreach my $itemnumber (@itemnumbers) {
320 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
321 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
324 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
326 my $notforloan_label_of = get_notforloan_label_of();
327 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
330 foreach my $biblioitemnumber (@biblioitemnumbers) {
331 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
336 foreach my $biblioitemnumber (@biblioitemnumbers) {
337 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
339 $biblioitem->{description} =
340 $itemtypes->{ $biblioitem->{itemtype} }{description};
343 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
345 my $item = $itemhash{$itemnumber};
347 $item->{homebranchname} =
348 $branches->{ $item->{homebranch} }{branchname};
350 # if the holdingbranch is different than the homebranch, we show the
351 # holdingbranch of the document too
352 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
353 $item->{holdingbranchname} =
354 $branches->{ $item->{holdingbranch} }{branchname};
358 $item->{itemcallnumber} = $item->{itemcallnumber};
360 # if the item is currently on loan, we display its return date and
361 # change the background color
362 my $issues= GetItemIssue($itemnumber);
363 if ( $issues->{'date_due'} ) {
364 $item->{date_due} = format_date($issues->{'date_due'});
365 $item->{backgroundcolor} = 'onloan';
369 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
370 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
372 if ( defined $reservedate ) {
373 $item->{backgroundcolor} = 'reserved';
374 $item->{reservedate} = format_date($reservedate);
375 $item->{ReservedForBorrowernumber} = $reservedfor;
376 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
377 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
378 $item->{ExpectedAtLibrary} = $expectedAt;
382 # Management of the notforloan document
383 if ( $item->{notforloan} || $item->{itemnotforloan}) {
384 $item->{backgroundcolor} = 'other';
385 $item->{notforloanvalue} =
386 $notforloan_label_of->{ $item->{notforloan} };
389 # Management of lost or long overdue items
390 if ( $item->{itemlost} ) {
392 # FIXME localized strings should never be in Perl code
394 $item->{itemlost} == 1 ? "(lost)"
395 : $item->{itemlost} == 2 ? "(long overdue)"
397 $item->{backgroundcolor} = 'other';
400 # Check of the transfered documents
401 my ( $transfertwhen, $transfertfrom, $transfertto ) =
402 GetTransfers($itemnumber);
404 if ( $transfertwhen ne '' ) {
405 $item->{transfertwhen} = format_date($transfertwhen);
406 $item->{transfertfrom} =
407 $branches->{$transfertfrom}{branchname};
408 $item->{transfertto} = $branches->{$transfertto}{branchname};
409 $item->{nocancel} = 1;
412 # If there is no loan, return and transfer, we show a checkbox.
413 $item->{notforloan} = $item->{notforloan} || 0;
415 if (IsAvailableForItemLevelRequest($itemnumber)) {
416 $item->{available} = 1;
419 # FIXME: move this to a pm
420 my $dbh = C4::Context->dbh;
421 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
422 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
423 while (my $wait_hashref = $sth2->fetchrow_hashref) {
424 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
426 $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
427 push @{ $biblioitem->{itemloop} }, $item;
430 push @bibitemloop, $biblioitem;
436 bibitemloop => \@bibitemloop,
438 output_html_with_http_headers $query, $cookie, $template->output;