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.
30 use C4::Branch; # GetBranches
32 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37 template_name => "opac-reserve.tmpl",
41 flagsrequired => { borrow => 1 },
46 # get borrower information ....
47 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
49 # get biblionumber.....
50 my $biblionumber = $query->param('biblionumber');
52 my $bibdata = GetBiblioData($biblionumber);
53 $template->param($bibdata);
54 $template->param( biblionumber => $biblionumber );
56 # get the rank number....
57 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
58 $template->param( reservecount => $rank );
60 foreach my $res (@$reserves) {
61 if ( $res->{'found'} eq 'W' ) {
67 $template->param( rank => $rank );
69 # pass the pickup branch along....
70 my $branch = $query->param('branch');
71 $template->param( branch => $branch );
73 my $branches = GetBranches();
75 # make sure it's a real branch
76 if ( !$branches->{$branch} ) {
79 $template->param( branchname => $branches->{$branch}->{'branchname'} );
81 # make branch selection options...
86 my @CGIbranchlooparray;
88 foreach my $branch ( keys %$branches ) {
91 $line{branch} = $branches->{$branch}->{'branchname'};
92 $line{value} = $branch;
93 if ($line{value} eq C4::Context->userenv->{'branch'}) {
96 push @CGIbranchlooparray, \%line;
100 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
101 my $CGIbranchloop = \@CGIbranchlooparray;
102 $template->param( CGIbranch => $CGIbranchloop );
104 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
105 # get the itemtype data....
106 my @items = GetItemsInfo($biblionumber);
107 #######################################################
108 # old version, add so that old templates still work
110 foreach my $itm (@items) {
111 my $ity = $itm->{'itemtype'};
112 unless ( $types_old{$ity} ) {
113 $types_old{$ity}->{'itemtype'} = $ity;
114 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
115 $types_old{$ity}->{'description'} = $itm->{'description'};
118 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
122 foreach my $type ( values %types_old ) {
124 foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
126 $branches->{$bc}->{'branchname'} . "("
127 . $type->{'branchinfo'}->{$bc} . ")";
129 $type->{'copies'} = $copies;
132 my @types_old = values %types_old;
135 ################################
138 foreach my $itm (@items) {
139 push @temp, $itm if $itm->{'itemtype'};
142 my $itemcount = @items;
143 $template->param( itemcount => $itemcount );
148 foreach my $itm (@items) {
149 push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
150 if defined $itm->{'date_due'};
151 $itm->{ $itm->{'publictype'} } = 1;
152 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
153 'a', ( $itm->{'biblioitemnumber'} ) );
154 $fee = sprintf "%.02f", $fee;
155 $itm->{'reservefee'} = $fee;
156 my $pty = $itm->{'publictype'};
157 $itemtypes{ $itm->{'itemtype'} } = $itm;
158 unless ( $types{$pty} ) {
159 $types{$pty}->{'count'} = 1;
160 $types{$pty}->{ $itm->{'itemtype'} } = 1;
161 push @{ $types{$pty}->{'items'} }, $itm;
164 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
165 $types{$pty}->{'count'}++;
166 $types{$pty}->{ $itm->{'itemtype'} } = 1;
167 push @{ $types{$pty}->{'items'} }, $itm;
172 $template->param( ITEMS => \@duedates );
174 my $width = keys %types;
175 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
177 foreach my $pt (@publictypes) {
178 $typecount += $pt->{'count'};
180 $template->param( onlyone => 1 ) if $typecount == 1;
183 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
185 foreach my $pty (@publictypes) {
186 my @items = @{ $pty->{'items'} };
187 push @row, $items[$rownum] if defined $items[$rownum];
190 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
191 my $fill = ( $width - $last ) * 2;
193 push @typerows, { ROW => \@row, fill => $fill };
195 $template->param( TYPE_ROWS => \@typerows );
196 $width = 2 * $width - 1;
197 $template->param( totalwidth => 2 * $width - 1, );
199 if ( $query->param('place_reserve') ) {
200 my @bibitems=$query->param('biblioitem');
201 my $notes=$query->param('notes');
202 my $checkitem=$query->param('checkitem');
205 #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.
206 if ($checkitem ne ''){
208 my $item = $checkitem;
209 $item = GetItem($item);
210 if ( $item->{'holdingbranch'} eq $branch ){
211 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
216 @bibitems=sort @bibitems;
219 $realbi[0]=$bibitems[0];
220 for (my $i=1;$i<$count;$i++) {
222 if ($realbi[$i3] ne $bibitems[$i]) {
223 $realbi[$i2]=$bibitems[$i];
227 # here we actually do the reserveration. Stage 3.
228 if ($query->param('request') eq 'any'){
229 # place a request on 1st available
230 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
232 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
234 print $query->redirect("/cgi-bin/koha/opac-user.pl");
238 # Here we check that the borrower can actually make reserves Stage 1.
240 my $maxoutstanding = C4::Context->preference("maxoutstanding");
241 $template->param( noreserve => 1 ) unless $maxoutstanding;
242 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
243 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
244 $template->param( message => 1 );
246 $template->param( too_much_oweing => $amount );
248 if ( $borr->{gonenoaddress} eq 1 ) {
255 if ( $borr->{lost} eq 1 ) {
262 if ( $borr->{debarred} eq 1 ) {
269 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
270 $template->param( RESERVES => \@reserves );
271 if ( scalar(@$reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
272 $template->param( message => 1 );
274 $template->param( too_many_reserves => scalar($reserves));
276 foreach my $res (@$reserves) {
277 if ( $res->{'biblionumber'} == $biblionumber ) {
278 $template->param( message => 1 );
280 $template->param( already_reserved => 1 );
283 unless ($noreserves) {
284 $template->param( TYPES => \@types_old );
285 $template->param( select_item_types => 1 );
291 my %itemnumbers_of_biblioitem;
292 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
293 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
295 foreach my $itemnumber (@itemnumbers) {
297 $iteminfos_of->{$itemnumber}->{homebranch},
298 $iteminfos_of->{$itemnumber}->{holdingbranch} );
300 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
301 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
304 # @branchcodes = uniq @branchcodes;
306 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
308 my $branchinfos_of = get_branchinfos_of(@branchcodes);
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};
317 my $itemtypeinfos_of = get_itemtypeinfos_of(@itemtypes);
321 foreach my $biblioitemnumber (@biblioitemnumbers) {
322 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
324 $biblioitem->{description} =
325 $itemtypeinfos_of->{ $biblioitem->{itemtype} }{description};
328 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
330 my $item = $iteminfos_of->{$itemnumber};
332 $item->{homebranchname} =
333 $branchinfos_of->{ $item->{homebranch} }{branchname};
335 # if the holdingbranch is different than the homebranch, we show the
336 # holdingbranch of the document too
337 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
338 $item->{holdingbranchname} =
339 $branchinfos_of->{ $item->{holdingbranch} }{branchname};
343 $item->{itemcallnumber} = $item->{itemcallnumber};
345 # if the item is currently on loan, we display its return date and
346 # change the background color
347 my $issues= GetItemIssue($itemnumber);
348 if ( $issues->{'date_due'} ) {
349 $item->{date_due} = format_date($issues->{'date_due'});
350 $item->{backgroundcolor} = 'onloan';
354 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
355 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
357 if ( defined $reservedate ) {
358 $item->{backgroundcolor} = 'reserved';
359 $item->{reservedate} = format_date($reservedate);
360 $item->{ReservedForBorrowernumber} = $reservedfor;
361 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
362 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
363 $item->{ExpectedAtLibrary} = $expectedAt;
367 # Management of the notforloan document
368 if ( $item->{notforloan} ) {
369 $item->{backgroundcolor} = 'other';
370 $item->{notforloanvalue} =
371 $notforloan_label_of->{ $item->{notforloan} };
374 # Management of lost or long overdue items
375 if ( $item->{itemlost} ) {
377 # FIXME localized strings should never be in Perl code
379 $item->{itemlost} == 1 ? "(lost)"
380 : $item->{itemlost} == 2 ? "(long overdue)"
382 $item->{backgroundcolor} = 'other';
385 # Check of the transfered documents
386 my ( $transfertwhen, $transfertfrom, $transfertto ) =
387 GetTransfers($itemnumber);
389 if ( $transfertwhen ne '' ) {
390 $item->{transfertwhen} = format_date($transfertwhen);
391 $item->{transfertfrom} =
392 $branchinfos_of->{$transfertfrom}{branchname};
393 $item->{transfertto} = $branchinfos_of->{$transfertto}{branchname};
394 $item->{nocancel} = 1;
397 # If there is no loan, return and transfer, we show a checkbox.
398 $item->{notforloan} = $item->{notforloan} || 0;
400 # An item is available only if:
402 not defined $reservedate # not reserved yet
403 and $issues->{'date_due'} eq '' # not currently on loan
404 and not $item->{itemlost} # not lost
405 and not $item->{notforloan} # not forbidden to loan
406 and $transfertwhen eq '' # not currently on transfert
409 $item->{available} = 1;
412 push @{ $biblioitem->{itemloop} }, $item;
415 push @bibitemloop, $biblioitem;
420 bibitemloop => \@bibitemloop,
422 output_html_with_http_headers $query, $cookie, $template->output;