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
33 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38 template_name => "opac-reserve.tmpl",
42 flagsrequired => { borrow => 1 },
48 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
52 # get borrower information ....
53 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
55 # get branches and itemtypes
56 my $branches = GetBranches();
57 my $itemtypes = GetItemTypes();
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);
65 $template->param( biblionumber => $biblionumber );
67 my $bibdata = GetBiblioData($biblionumber);
69 $template->param(message=>1,bad_biblionumber=>$biblionumber);
70 &get_out($query, $cookie, $template->output);
72 $template->param($bibdata); # FIXME: bad form.
74 # get the rank number....
75 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
76 $template->param( reservecount => $rank );
78 foreach my $res (@$reserves) {
79 if ( $res->{'found'} eq 'W' ) {
85 $template->param( rank => $rank );
87 # pass the pickup branch along....
88 my $branch = $query->param('branch');
89 $template->param( branch => $branch );
91 # make sure it's a real branch
92 if ( !$branches->{$branch} ) {
95 $template->param( branchname => $branches->{$branch}->{'branchname'} );
97 # make branch selection options...
102 my @CGIbranchlooparray;
104 foreach my $branch ( keys %$branches ) {
107 $line{branch} = $branches->{$branch}->{'branchname'};
108 $line{value} = $branch;
109 if ($line{value} eq C4::Context->userenv->{'branch'}) {
112 push @CGIbranchlooparray, \%line;
115 @CGIbranchlooparray =
116 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
117 my $CGIbranchloop = \@CGIbranchlooparray;
118 $template->param( CGIbranch => $CGIbranchloop );
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
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'};
134 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
138 foreach my $type ( values %types_old ) {
140 foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
142 $branches->{$bc}->{'branchname'} . "("
143 . $type->{'branchinfo'}->{$bc} . ")";
145 $type->{'copies'} = $copies;
148 my @types_old = values %types_old;
151 ################################
154 foreach my $itm (@items) {
155 push @temp, $itm if $itm->{'itemtype'};
158 my $itemcount = @items;
159 $template->param( itemcount => $itemcount );
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;
183 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
184 $types{$pty}->{'count'}++;
185 $types{$pty}->{ $itm->{'itemtype'} } = 1;
186 push @{ $types{$pty}->{'items'} }, $itm;
189 $itemhash{$itm->{'itemnumber'}}=$itm;
190 if (!$itm->{'notforloan'} && !$itm->{'itemnotforloan'}){
195 $template->param( ITEMS => \@duedates );
197 my $width = keys %types;
198 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
200 foreach my $pt (@publictypes) {
201 $typecount += $pt->{'count'};
203 $template->param( onlyone => 1 ) if $typecount == 1;
206 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
208 foreach my $pty (@publictypes) {
209 my @items = @{ $pty->{'items'} };
210 push @row, $items[$rownum] if defined $items[$rownum];
213 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
214 my $fill = ( $width - $last ) * 2;
216 push @typerows, { ROW => \@row, fill => $fill };
218 $template->param( TYPE_ROWS => \@typerows );
219 $width = 2 * $width - 1;
220 $template->param( totalwidth => 2 * $width - 1, );
222 if ( $query->param('place_reserve') ) {
223 my @bibitems=$query->param('biblioitem');
224 my $notes=$query->param('notes');
225 my $checkitem=$query->param('checkitem');
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');
239 @bibitems=sort @bibitems;
242 $realbi[0]=$bibitems[0];
243 for (my $i=1;$i<$count;$i++) {
245 if ($realbi[$i3] ne $bibitems[$i]) {
246 $realbi[$i2]=$bibitems[$i];
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);
255 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
257 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
261 # Here we check that the borrower can actually make reserves Stage 1.
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 );
269 $template->param( too_much_oweing => $amount );
271 if ( $borr->{gonenoaddress} eq 1 ) {
278 if ( $borr->{lost} eq 1 ) {
285 if ( $borr->{debarred} eq 1 ) {
292 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
293 $template->param( RESERVES => \@reserves );
294 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
295 $template->param( message => 1 );
297 $template->param( too_many_reserves => scalar(@reserves));
299 foreach my $res (@reserves) {
300 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
301 $template->param( message => 1 );
303 $template->param( already_reserved => 1 );
306 unless ($noreserves) {
307 $template->param( TYPES => \@types_old );
308 $template->param( select_item_types => 1 );
313 my %itemnumbers_of_biblioitem;
314 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
315 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
317 foreach my $itemnumber (@itemnumbers) {
318 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
319 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
322 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
324 my $notforloan_label_of = get_notforloan_label_of();
325 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
328 foreach my $biblioitemnumber (@biblioitemnumbers) {
329 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
334 foreach my $biblioitemnumber (@biblioitemnumbers) {
335 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
337 $biblioitem->{description} =
338 $itemtypes->{ $biblioitem->{itemtype} }{description};
341 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
343 my $item = $itemhash{$itemnumber};
345 $item->{homebranchname} =
346 $branches->{ $item->{homebranch} }{branchname};
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};
356 $item->{itemcallnumber} = $item->{itemcallnumber};
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';
367 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
368 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
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;
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} };
387 # Management of lost or long overdue items
388 if ( $item->{itemlost} ) {
390 # FIXME localized strings should never be in Perl code
392 $item->{itemlost} == 1 ? "(lost)"
393 : $item->{itemlost} == 2 ? "(long overdue)"
395 $item->{backgroundcolor} = 'other';
398 # Check of the transfered documents
399 my ( $transfertwhen, $transfertfrom, $transfertto ) =
400 GetTransfers($itemnumber);
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;
410 # If there is no loan, return and transfer, we show a checkbox.
411 $item->{notforloan} = $item->{notforloan} || 0;
413 if (IsAvailableForItemLevelRequest($itemnumber)) {
414 $item->{available} = 1;
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});
424 $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
425 push @{ $biblioitem->{itemloop} }, $item;
428 push @bibitemloop, $biblioitem;
434 bibitemloop => \@bibitemloop,
436 output_html_with_http_headers $query, $cookie, $template->output;