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 warn "===>".$line{value}." eq ".C4::Context->userenv->{'branch'};
94 if ($line{value} eq C4::Context->userenv->{'branch'}) {
97 push @CGIbranchlooparray, \%line;
100 @CGIbranchlooparray =
101 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
102 my $CGIbranchloop = \@CGIbranchlooparray;
103 $template->param( CGIbranch => $CGIbranchloop );
105 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
106 # get the itemtype data....
107 my @items = GetItemsInfo($biblionumber);
108 #######################################################
109 # old version, add so that old templates still work
111 foreach my $itm (@items) {
112 my $ity = $itm->{'itemtype'};
113 unless ( $types_old{$ity} ) {
114 $types_old{$ity}->{'itemtype'} = $ity;
115 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
116 $types_old{$ity}->{'description'} = $itm->{'description'};
119 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
123 foreach my $type ( values %types_old ) {
125 foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
127 $branches->{$bc}->{'branchname'} . "("
128 . $type->{'branchinfo'}->{$bc} . ")";
130 $type->{'copies'} = $copies;
133 my @types_old = values %types_old;
136 ################################
139 foreach my $itm (@items) {
140 push @temp, $itm if $itm->{'itemtype'};
143 my $itemcount = @items;
144 $template->param( itemcount => $itemcount );
149 foreach my $itm (@items) {
150 push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
151 if defined $itm->{'date_due'};
152 $itm->{ $itm->{'publictype'} } = 1;
153 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
154 'a', ( $itm->{'biblioitemnumber'} ) );
155 $fee = sprintf "%.02f", $fee;
156 $itm->{'reservefee'} = $fee;
157 my $pty = $itm->{'publictype'};
158 $itemtypes{ $itm->{'itemtype'} } = $itm;
159 unless ( $types{$pty} ) {
160 $types{$pty}->{'count'} = 1;
161 $types{$pty}->{ $itm->{'itemtype'} } = 1;
162 push @{ $types{$pty}->{'items'} }, $itm;
165 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
166 $types{$pty}->{'count'}++;
167 $types{$pty}->{ $itm->{'itemtype'} } = 1;
168 push @{ $types{$pty}->{'items'} }, $itm;
173 $template->param( ITEMS => \@duedates );
175 my $width = keys %types;
176 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
178 foreach my $pt (@publictypes) {
179 $typecount += $pt->{'count'};
181 $template->param( onlyone => 1 ) if $typecount == 1;
184 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
186 foreach my $pty (@publictypes) {
187 my @items = @{ $pty->{'items'} };
188 push @row, $items[$rownum] if defined $items[$rownum];
191 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
192 my $fill = ( $width - $last ) * 2;
194 push @typerows, { ROW => \@row, fill => $fill };
196 $template->param( TYPE_ROWS => \@typerows );
197 $width = 2 * $width - 1;
198 $template->param( totalwidth => 2 * $width - 1, );
200 if ( $query->param('place_reserve') ) {
201 my @bibitems=$query->param('biblioitem');
202 my $notes=$query->param('notes');
203 my $checkitem=$query->param('checkitem');
206 #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.
207 if ($checkitem ne ''){
209 my $item = $checkitem;
210 $item = GetItem($item);
211 if ( $item->{'holdingbranch'} eq $branch ){
212 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
217 @bibitems=sort @bibitems;
220 $realbi[0]=$bibitems[0];
221 for (my $i=1;$i<$count;$i++) {
223 if ($realbi[$i3] ne $bibitems[$i]) {
224 $realbi[$i2]=$bibitems[$i];
228 # here we actually do the reserveration. Stage 3.
229 if ($query->param('request') eq 'any'){
230 # place a request on 1st available
231 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
233 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
235 print $query->redirect("/cgi-bin/koha/opac-user.pl");
239 # Here we check that the borrower can actually make reserves Stage 1.
241 my $maxoutstanding = C4::Context->preference("maxoutstanding");
242 $template->param( noreserve => 1 ) unless $maxoutstanding;
243 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
244 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
245 $template->param( message => 1 );
247 $template->param( too_much_oweing => $amount );
249 if ( $borr->{gonenoaddress} eq 1 ) {
256 if ( $borr->{lost} eq 1 ) {
263 if ( $borr->{debarred} eq 1 ) {
270 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
271 $template->param( RESERVES => \@reserves );
272 if ( scalar(@$reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
273 $template->param( message => 1 );
275 $template->param( too_many_reserves => scalar($reserves));
277 foreach my $res (@$reserves) {
278 if ( $res->{'biblionumber'} == $biblionumber ) {
279 $template->param( message => 1 );
281 $template->param( already_reserved => 1 );
284 unless ($noreserves) {
285 $template->param( TYPES => \@types_old );
286 $template->param( select_item_types => 1 );
292 my %itemnumbers_of_biblioitem;
293 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
294 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
296 foreach my $itemnumber (@itemnumbers) {
298 $iteminfos_of->{$itemnumber}->{homebranch},
299 $iteminfos_of->{$itemnumber}->{holdingbranch} );
301 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
302 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
305 # @branchcodes = uniq @branchcodes;
307 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
309 my $branchinfos_of = get_branchinfos_of(@branchcodes);
310 my $notforloan_label_of = get_notforloan_label_of();
311 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
314 foreach my $biblioitemnumber (@biblioitemnumbers) {
315 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
318 my $itemtypeinfos_of = get_itemtypeinfos_of(@itemtypes);
322 foreach my $biblioitemnumber (@biblioitemnumbers) {
323 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
325 $biblioitem->{description} =
326 $itemtypeinfos_of->{ $biblioitem->{itemtype} }{description};
329 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
331 my $item = $iteminfos_of->{$itemnumber};
333 $item->{homebranchname} =
334 $branchinfos_of->{ $item->{homebranch} }{branchname};
336 # if the holdingbranch is different than the homebranch, we show the
337 # holdingbranch of the document too
338 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
339 $item->{holdingbranchname} =
340 $branchinfos_of->{ $item->{holdingbranch} }{branchname};
344 $item->{itemcallnumber} = $item->{itemcallnumber};
346 # if the item is currently on loan, we display its return date and
347 # change the background color
348 my $issues= GetItemIssue($itemnumber);
349 if ( $issues->{'date_due'} ) {
350 $item->{date_due} = format_date($issues->{'date_due'});
351 $item->{backgroundcolor} = 'onloan';
355 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
356 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
358 if ( defined $reservedate ) {
359 $item->{backgroundcolor} = 'reserved';
360 $item->{reservedate} = format_date($reservedate);
361 $item->{ReservedForBorrowernumber} = $reservedfor;
362 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
363 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
364 $item->{ExpectedAtLibrary} = $expectedAt;
368 # Management of the notforloan document
369 if ( $item->{notforloan} ) {
370 $item->{backgroundcolor} = 'other';
371 $item->{notforloanvalue} =
372 $notforloan_label_of->{ $item->{notforloan} };
375 # Management of lost or long overdue items
376 if ( $item->{itemlost} ) {
378 # FIXME localized strings should never be in Perl code
380 $item->{itemlost} == 1 ? "(lost)"
381 : $item->{itemlost} == 2 ? "(long overdue)"
383 $item->{backgroundcolor} = 'other';
386 # Check of the transfered documents
387 my ( $transfertwhen, $transfertfrom, $transfertto ) =
388 GetTransfers($itemnumber);
390 if ( $transfertwhen ne '' ) {
391 $item->{transfertwhen} = format_date($transfertwhen);
392 $item->{transfertfrom} =
393 $branchinfos_of->{$transfertfrom}{branchname};
394 $item->{transfertto} = $branchinfos_of->{$transfertto}{branchname};
395 $item->{nocancel} = 1;
398 # If there is no loan, return and transfer, we show a checkbox.
399 $item->{notforloan} = $item->{notforloan} || 0;
401 # An item is available only if:
403 not defined $reservedate # not reserved yet
404 and $issues->{'date_due'} eq '' # not currently on loan
405 and not $item->{itemlost} # not lost
406 and not $item->{notforloan} # not forbidden to loan
407 and $transfertwhen eq '' # not currently on transfert
410 $item->{available} = 1;
413 push @{ $biblioitem->{itemloop} }, $item;
416 push @bibitemloop, $biblioitem;
421 bibitemloop => \@bibitemloop,
423 output_html_with_http_headers $query, $cookie, $template->output;