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
23 use C4::Auth; # checkauth, getborrowernumber.
28 use C4::Dates qw/format_date/;
31 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 },
48 # get borrower information ....
49 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
51 # get branches and itemtypes
52 my $branches = GetBranches();
53 my $itemtypes = GetItemTypes();
55 # get biblionumber.....
56 my $biblionumber = $query->param('biblionumber');
58 my $bibdata = GetBiblioData($biblionumber);
59 $template->param($bibdata);
60 $template->param( biblionumber => $biblionumber );
62 # get the rank number....
63 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
64 $template->param( reservecount => $rank );
66 foreach my $res (@$reserves) {
67 if ( $res->{'found'} eq 'W' ) {
73 $template->param( rank => $rank );
75 # pass the pickup branch along....
76 my $branch = $query->param('branch');
77 $template->param( branch => $branch );
79 # make sure it's a real branch
80 if ( !$branches->{$branch} ) {
83 $template->param( branchname => $branches->{$branch}->{'branchname'} );
85 # make branch selection options...
90 my @CGIbranchlooparray;
92 foreach my $branch ( keys %$branches ) {
95 $line{branch} = $branches->{$branch}->{'branchname'};
96 $line{value} = $branch;
97 if ($line{value} eq C4::Context->userenv->{'branch'}) {
100 push @CGIbranchlooparray, \%line;
103 @CGIbranchlooparray =
104 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
105 my $CGIbranchloop = \@CGIbranchlooparray;
106 $template->param( CGIbranch => $CGIbranchloop );
108 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
109 # get the itemtype data....
110 my @items = GetItemsInfo($biblionumber);
111 #######################################################
112 # old version, add so that old templates still work
114 foreach my $itm (@items) {
115 my $ity = $itm->{'itemtype'};
116 unless ( $types_old{$ity} ) {
117 $types_old{$ity}->{'itemtype'} = $ity;
118 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
119 $types_old{$ity}->{'description'} = $itm->{'description'};
122 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
126 foreach my $type ( values %types_old ) {
128 foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
130 $branches->{$bc}->{'branchname'} . "("
131 . $type->{'branchinfo'}->{$bc} . ")";
133 $type->{'copies'} = $copies;
136 my @types_old = values %types_old;
139 ################################
142 foreach my $itm (@items) {
143 push @temp, $itm if $itm->{'itemtype'};
146 my $itemcount = @items;
147 $template->param( itemcount => $itemcount );
155 foreach my $itm (@items) {
156 push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
157 if defined $itm->{'date_due'};
158 $itm->{ $itm->{'publictype'} } = 1;
159 warn $itm->{'notforloan'};
160 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
161 'a', ( $itm->{'biblioitemnumber'} ) );
162 $fee = sprintf "%.02f", $fee;
163 $itm->{'reservefee'} = $fee;
164 my $pty = $itm->{'publictype'};
165 $itemtypes{ $itm->{'itemtype'} } = $itm;
166 unless ( $types{$pty} ) {
167 $types{$pty}->{'count'} = 1;
168 $types{$pty}->{ $itm->{'itemtype'} } = 1;
169 push @{ $types{$pty}->{'items'} }, $itm;
172 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
173 $types{$pty}->{'count'}++;
174 $types{$pty}->{ $itm->{'itemtype'} } = 1;
175 push @{ $types{$pty}->{'items'} }, $itm;
178 $itemhash{$itm->{'itemnumber'}}=$itm;
179 if (!$itm->{'notforloan'} && !$itm->{'itemnotforloan'}){
184 $template->param( ITEMS => \@duedates );
186 my $width = keys %types;
187 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
189 foreach my $pt (@publictypes) {
190 $typecount += $pt->{'count'};
192 $template->param( onlyone => 1 ) if $typecount == 1;
195 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
197 foreach my $pty (@publictypes) {
198 my @items = @{ $pty->{'items'} };
199 push @row, $items[$rownum] if defined $items[$rownum];
202 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
203 my $fill = ( $width - $last ) * 2;
205 push @typerows, { ROW => \@row, fill => $fill };
207 $template->param( TYPE_ROWS => \@typerows );
208 $width = 2 * $width - 1;
209 $template->param( totalwidth => 2 * $width - 1, );
211 if ( $query->param('place_reserve') ) {
212 my @bibitems=$query->param('biblioitem');
213 my $notes=$query->param('notes');
214 my $checkitem=$query->param('checkitem');
217 #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.
218 if ($checkitem ne ''){
219 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
220 my $item = $checkitem;
221 $item = GetItem($item);
222 if ( $item->{'holdingbranch'} eq $branch ){
223 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
228 @bibitems=sort @bibitems;
231 $realbi[0]=$bibitems[0];
232 for (my $i=1;$i<$count;$i++) {
234 if ($realbi[$i3] ne $bibitems[$i]) {
235 $realbi[$i2]=$bibitems[$i];
239 # here we actually do the reserveration. Stage 3.
240 if ($query->param('request') eq 'any'){
241 # place a request on 1st available
242 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
244 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
246 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
250 # Here we check that the borrower can actually make reserves Stage 1.
252 my $maxoutstanding = C4::Context->preference("maxoutstanding");
253 $template->param( noreserve => 1 ) unless $maxoutstanding;
254 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
255 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
256 $template->param( message => 1 );
258 $template->param( too_much_oweing => $amount );
260 if ( $borr->{gonenoaddress} eq 1 ) {
267 if ( $borr->{lost} eq 1 ) {
274 if ( $borr->{debarred} eq 1 ) {
281 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
282 $template->param( RESERVES => \@reserves );
283 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
284 $template->param( message => 1 );
286 $template->param( too_many_reserves => scalar(@reserves));
288 foreach my $res (@reserves) {
289 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
290 $template->param( message => 1 );
292 $template->param( already_reserved => 1 );
295 unless ($noreserves) {
296 $template->param( TYPES => \@types_old );
297 $template->param( select_item_types => 1 );
302 my %itemnumbers_of_biblioitem;
303 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
304 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
306 foreach my $itemnumber (@itemnumbers) {
307 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
308 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
311 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
313 my $notforloan_label_of = get_notforloan_label_of();
314 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
317 foreach my $biblioitemnumber (@biblioitemnumbers) {
318 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
323 foreach my $biblioitemnumber (@biblioitemnumbers) {
324 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
326 $biblioitem->{description} =
327 $itemtypes->{ $biblioitem->{itemtype} }{description};
330 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
332 my $item = $itemhash{$itemnumber};
334 $item->{homebranchname} =
335 $branches->{ $item->{homebranch} }{branchname};
337 # if the holdingbranch is different than the homebranch, we show the
338 # holdingbranch of the document too
339 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
340 $item->{holdingbranchname} =
341 $branches->{ $item->{holdingbranch} }{branchname};
345 $item->{itemcallnumber} = $item->{itemcallnumber};
347 # if the item is currently on loan, we display its return date and
348 # change the background color
349 my $issues= GetItemIssue($itemnumber);
350 if ( $issues->{'date_due'} ) {
351 $item->{date_due} = format_date($issues->{'date_due'});
352 $item->{backgroundcolor} = 'onloan';
356 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
357 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
359 if ( defined $reservedate ) {
360 $item->{backgroundcolor} = 'reserved';
361 $item->{reservedate} = format_date($reservedate);
362 $item->{ReservedForBorrowernumber} = $reservedfor;
363 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
364 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
365 $item->{ExpectedAtLibrary} = $expectedAt;
369 # Management of the notforloan document
370 if ( $item->{notforloan} || $item->{itemnotforloan}) {
371 $item->{backgroundcolor} = 'other';
372 $item->{notforloanvalue} =
373 $notforloan_label_of->{ $item->{notforloan} };
376 # Management of lost or long overdue items
377 if ( $item->{itemlost} ) {
379 # FIXME localized strings should never be in Perl code
381 $item->{itemlost} == 1 ? "(lost)"
382 : $item->{itemlost} == 2 ? "(long overdue)"
384 $item->{backgroundcolor} = 'other';
387 # Check of the transfered documents
388 my ( $transfertwhen, $transfertfrom, $transfertto ) =
389 GetTransfers($itemnumber);
391 if ( $transfertwhen ne '' ) {
392 $item->{transfertwhen} = format_date($transfertwhen);
393 $item->{transfertfrom} =
394 $branches->{$transfertfrom}{branchname};
395 $item->{transfertto} = $branches->{$transfertto}{branchname};
396 $item->{nocancel} = 1;
399 # If there is no loan, return and transfer, we show a checkbox.
400 $item->{notforloan} = $item->{notforloan} || 0;
402 # FIXME: every library will define this differently
403 # An item is available only if:
405 not defined $reservedate # not reserved yet
406 and $issues->{'date_due'} eq '' # not currently on loan
407 and not $item->{itemlost} # not lost
408 and not $item->{notforloan} # not forbidden to loan
409 and $transfertwhen eq '' # not currently on transfert
412 $item->{available} = 1;
415 # FIXME: move this to a pm
416 my $dbh = C4::Context->dbh;
417 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
418 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
419 while (my $wait_hashref = $sth2->fetchrow_hashref) {
420 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
422 $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
423 push @{ $biblioitem->{itemloop} }, $item;
426 push @bibitemloop, $biblioitem;
432 bibitemloop => \@bibitemloop,
434 output_html_with_http_headers $query, $cookie, $template->output;