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
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 },
47 # get borrower information ....
48 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
50 # get biblionumber.....
51 my $biblionumber = $query->param('biblionumber');
53 my $bibdata = GetBiblioData($biblionumber);
54 $template->param($bibdata);
55 $template->param( biblionumber => $biblionumber );
57 # get the rank number....
58 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
59 $template->param( reservecount => $rank );
61 foreach my $res (@$reserves) {
62 if ( $res->{'found'} eq 'W' ) {
68 $template->param( rank => $rank );
70 # pass the pickup branch along....
71 my $branch = $query->param('branch');
72 $template->param( branch => $branch );
74 my $branches = GetBranches();
76 # make sure it's a real branch
77 if ( !$branches->{$branch} ) {
80 $template->param( branchname => $branches->{$branch}->{'branchname'} );
82 # make branch selection options...
87 my @CGIbranchlooparray;
89 foreach my $branch ( keys %$branches ) {
92 $line{branch} = $branches->{$branch}->{'branchname'};
93 $line{value} = $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 );
150 foreach my $itm (@items) {
151 push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
152 if defined $itm->{'date_due'};
153 $itm->{ $itm->{'publictype'} } = 1;
154 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
155 'a', ( $itm->{'biblioitemnumber'} ) );
156 $fee = sprintf "%.02f", $fee;
157 $itm->{'reservefee'} = $fee;
158 my $pty = $itm->{'publictype'};
159 $itemtypes{ $itm->{'itemtype'} } = $itm;
160 unless ( $types{$pty} ) {
161 $types{$pty}->{'count'} = 1;
162 $types{$pty}->{ $itm->{'itemtype'} } = 1;
163 push @{ $types{$pty}->{'items'} }, $itm;
166 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
167 $types{$pty}->{'count'}++;
168 $types{$pty}->{ $itm->{'itemtype'} } = 1;
169 push @{ $types{$pty}->{'items'} }, $itm;
174 $template->param( ITEMS => \@duedates );
176 my $width = keys %types;
177 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
179 foreach my $pt (@publictypes) {
180 $typecount += $pt->{'count'};
182 $template->param( onlyone => 1 ) if $typecount == 1;
185 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
187 foreach my $pty (@publictypes) {
188 my @items = @{ $pty->{'items'} };
189 push @row, $items[$rownum] if defined $items[$rownum];
192 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
193 my $fill = ( $width - $last ) * 2;
195 push @typerows, { ROW => \@row, fill => $fill };
197 $template->param( TYPE_ROWS => \@typerows );
198 $width = 2 * $width - 1;
199 $template->param( totalwidth => 2 * $width - 1, );
201 if ( $query->param('place_reserve') ) {
202 my @bibitems=$query->param('biblioitem');
203 my $notes=$query->param('notes');
204 my $checkitem=$query->param('checkitem');
207 #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.
208 if ($checkitem ne ''){
210 my $item = $checkitem;
211 $item = GetItem($item);
212 if ( $item->{'holdingbranch'} eq $branch ){
213 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
218 @bibitems=sort @bibitems;
221 $realbi[0]=$bibitems[0];
222 for (my $i=1;$i<$count;$i++) {
224 if ($realbi[$i3] ne $bibitems[$i]) {
225 $realbi[$i2]=$bibitems[$i];
229 # here we actually do the reserveration. Stage 3.
230 if ($query->param('request') eq 'any'){
231 # place a request on 1st available
232 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
234 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
236 print $query->redirect("/cgi-bin/koha/opac-user.pl");
240 # Here we check that the borrower can actually make reserves Stage 1.
242 my $maxoutstanding = C4::Context->preference("maxoutstanding");
243 $template->param( noreserve => 1 ) unless $maxoutstanding;
244 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
245 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
246 $template->param( message => 1 );
248 $template->param( too_much_oweing => $amount );
250 if ( $borr->{gonenoaddress} eq 1 ) {
257 if ( $borr->{lost} eq 1 ) {
264 if ( $borr->{debarred} eq 1 ) {
271 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
272 $template->param( RESERVES => \@reserves );
273 if ( scalar(@$reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
274 $template->param( message => 1 );
276 $template->param( too_many_reserves => scalar($reserves));
278 foreach my $res (@$reserves) {
279 if ( $res->{'biblionumber'} == $biblionumber ) {
280 $template->param( message => 1 );
282 $template->param( already_reserved => 1 );
285 unless ($noreserves) {
286 $template->param( TYPES => \@types_old );
287 $template->param( select_item_types => 1 );
293 my %itemnumbers_of_biblioitem;
294 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
295 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
297 foreach my $itemnumber (@itemnumbers) {
299 $iteminfos_of->{$itemnumber}->{homebranch},
300 $iteminfos_of->{$itemnumber}->{holdingbranch} );
302 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
303 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
306 # @branchcodes = uniq @branchcodes;
308 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
310 my $branchinfos_of = get_branchinfos_of(@branchcodes);
311 my $notforloan_label_of = get_notforloan_label_of();
312 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
315 foreach my $biblioitemnumber (@biblioitemnumbers) {
316 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
319 my $itemtypeinfos_of = get_itemtypeinfos_of(@itemtypes);
323 foreach my $biblioitemnumber (@biblioitemnumbers) {
324 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
326 $biblioitem->{description} =
327 $itemtypeinfos_of->{ $biblioitem->{itemtype} }{description};
330 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
332 my $item = $iteminfos_of->{$itemnumber};
334 $item->{homebranchname} =
335 $branchinfos_of->{ $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 $branchinfos_of->{ $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} ) {
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 $branchinfos_of->{$transfertfrom}{branchname};
395 $item->{transfertto} = $branchinfos_of->{$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 # An item is available only if:
404 not defined $reservedate # not reserved yet
405 and $issues->{'date_due'} eq '' # not currently on loan
406 and not $item->{itemlost} # not lost
407 and not $item->{notforloan} # not forbidden to loan
408 and $transfertwhen eq '' # not currently on transfert
411 $item->{available} = 1;
414 push @{ $biblioitem->{itemloop} }, $item;
417 push @bibitemloop, $biblioitem;
422 bibitemloop => \@bibitemloop,
424 output_html_with_http_headers $query, $cookie, $template->output;