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 );
154 foreach my $itm (@items) {
155 push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
156 if defined $itm->{'date_due'};
157 $itm->{ $itm->{'publictype'} } = 1;
158 warn $itm->{'notforloan'};
159 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
160 'a', ( $itm->{'biblioitemnumber'} ) );
161 $fee = sprintf "%.02f", $fee;
162 $itm->{'reservefee'} = $fee;
163 my $pty = $itm->{'publictype'};
164 $itemtypes{ $itm->{'itemtype'} } = $itm;
165 unless ( $types{$pty} ) {
166 $types{$pty}->{'count'} = 1;
167 $types{$pty}->{ $itm->{'itemtype'} } = 1;
168 push @{ $types{$pty}->{'items'} }, $itm;
171 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
172 $types{$pty}->{'count'}++;
173 $types{$pty}->{ $itm->{'itemtype'} } = 1;
174 push @{ $types{$pty}->{'items'} }, $itm;
177 $itemhash{$itm->{'itemnumber'}}=$itm;
180 $template->param( ITEMS => \@duedates );
182 my $width = keys %types;
183 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
185 foreach my $pt (@publictypes) {
186 $typecount += $pt->{'count'};
188 $template->param( onlyone => 1 ) if $typecount == 1;
191 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
193 foreach my $pty (@publictypes) {
194 my @items = @{ $pty->{'items'} };
195 push @row, $items[$rownum] if defined $items[$rownum];
198 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
199 my $fill = ( $width - $last ) * 2;
201 push @typerows, { ROW => \@row, fill => $fill };
203 $template->param( TYPE_ROWS => \@typerows );
204 $width = 2 * $width - 1;
205 $template->param( totalwidth => 2 * $width - 1, );
207 if ( $query->param('place_reserve') ) {
208 my @bibitems=$query->param('biblioitem');
209 my $notes=$query->param('notes');
210 my $checkitem=$query->param('checkitem');
213 #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.
214 if ($checkitem ne ''){
215 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
216 my $item = $checkitem;
217 $item = GetItem($item);
218 if ( $item->{'holdingbranch'} eq $branch ){
219 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
224 @bibitems=sort @bibitems;
227 $realbi[0]=$bibitems[0];
228 for (my $i=1;$i<$count;$i++) {
230 if ($realbi[$i3] ne $bibitems[$i]) {
231 $realbi[$i2]=$bibitems[$i];
235 # here we actually do the reserveration. Stage 3.
236 if ($query->param('request') eq 'any'){
237 # place a request on 1st available
238 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
240 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
242 print $query->redirect("/cgi-bin/koha/opac-user.pl");
246 # Here we check that the borrower can actually make reserves Stage 1.
248 my $maxoutstanding = C4::Context->preference("maxoutstanding");
249 $template->param( noreserve => 1 ) unless $maxoutstanding;
250 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
251 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
252 $template->param( message => 1 );
254 $template->param( too_much_oweing => $amount );
256 if ( $borr->{gonenoaddress} eq 1 ) {
263 if ( $borr->{lost} eq 1 ) {
270 if ( $borr->{debarred} eq 1 ) {
277 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
278 $template->param( RESERVES => \@reserves );
279 if ( scalar(@$reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
280 $template->param( message => 1 );
282 $template->param( too_many_reserves => scalar($reserves));
284 foreach my $res (@$reserves) {
285 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
286 $template->param( message => 1 );
288 $template->param( already_reserved => 1 );
291 unless ($noreserves) {
292 $template->param( TYPES => \@types_old );
293 $template->param( select_item_types => 1 );
298 my %itemnumbers_of_biblioitem;
299 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
300 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
302 foreach my $itemnumber (@itemnumbers) {
303 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
304 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
307 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
309 my $notforloan_label_of = get_notforloan_label_of();
310 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
313 foreach my $biblioitemnumber (@biblioitemnumbers) {
314 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
319 foreach my $biblioitemnumber (@biblioitemnumbers) {
320 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
322 $biblioitem->{description} =
323 $itemtypes->{ $biblioitem->{itemtype} }{description};
326 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
328 my $item = $itemhash{$itemnumber};
330 $item->{homebranchname} =
331 $branches->{ $item->{homebranch} }{branchname};
333 # if the holdingbranch is different than the homebranch, we show the
334 # holdingbranch of the document too
335 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
336 $item->{holdingbranchname} =
337 $branches->{ $item->{holdingbranch} }{branchname};
341 $item->{itemcallnumber} = $item->{itemcallnumber};
343 # if the item is currently on loan, we display its return date and
344 # change the background color
345 my $issues= GetItemIssue($itemnumber);
346 if ( $issues->{'date_due'} ) {
347 $item->{date_due} = format_date($issues->{'date_due'});
348 $item->{backgroundcolor} = 'onloan';
352 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
353 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
355 if ( defined $reservedate ) {
356 $item->{backgroundcolor} = 'reserved';
357 $item->{reservedate} = format_date($reservedate);
358 $item->{ReservedForBorrowernumber} = $reservedfor;
359 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
360 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
361 $item->{ExpectedAtLibrary} = $expectedAt;
365 # Management of the notforloan document
366 if ( $item->{notforloan} || $item->{itemnotforloan}) {
367 $item->{backgroundcolor} = 'other';
368 $item->{notforloanvalue} =
369 $notforloan_label_of->{ $item->{notforloan} };
372 # Management of lost or long overdue items
373 if ( $item->{itemlost} ) {
375 # FIXME localized strings should never be in Perl code
377 $item->{itemlost} == 1 ? "(lost)"
378 : $item->{itemlost} == 2 ? "(long overdue)"
380 $item->{backgroundcolor} = 'other';
383 # Check of the transfered documents
384 my ( $transfertwhen, $transfertfrom, $transfertto ) =
385 GetTransfers($itemnumber);
387 if ( $transfertwhen ne '' ) {
388 $item->{transfertwhen} = format_date($transfertwhen);
389 $item->{transfertfrom} =
390 $branches->{$transfertfrom}{branchname};
391 $item->{transfertto} = $branches->{$transfertto}{branchname};
392 $item->{nocancel} = 1;
395 # If there is no loan, return and transfer, we show a checkbox.
396 $item->{notforloan} = $item->{notforloan} || 0;
398 # FIXME: every library will define this differently
399 # An item is available only if:
401 not defined $reservedate # not reserved yet
402 and $issues->{'date_due'} eq '' # not currently on loan
403 and not $item->{itemlost} # not lost
404 and not $item->{notforloan} # not forbidden to loan
405 and $transfertwhen eq '' # not currently on transfert
408 $item->{available} = 1;
411 # FIXME: move this to a pm
412 my $dbh = C4::Context->dbh;
413 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W' AND cancellationdate IS NULL");
414 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
415 while (my $wait_hashref = $sth2->fetchrow_hashref) {
416 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
418 $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
419 push @{ $biblioitem->{itemloop} }, $item;
422 push @bibitemloop, $biblioitem;
427 bibitemloop => \@bibitemloop,
429 output_html_with_http_headers $query, $cookie, $template->output;