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.
27 use C4::Dates qw/format_date/;
30 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 },
49 output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
53 # get borrower information ....
54 my ( $borr ) = GetMemberDetails( $borrowernumber );
56 # get branches and itemtypes
57 my $branches = GetBranches();
58 my $itemtypes = GetItemTypes();
60 # get biblionumber.....
61 my $biblionumber = $query->param('biblionumber');
62 if (! $biblionumber) {
63 $template->param(message=>1,no_biblionumber=>1);
64 &get_out($query, $cookie, $template->output);
66 $template->param( biblionumber => $biblionumber );
68 my $bibdata = GetBiblioData($biblionumber);
70 $template->param(message=>1,bad_biblionumber=>$biblionumber);
71 &get_out($query, $cookie, $template->output);
73 $template->param($bibdata); # FIXME: bad form.
75 # get the rank number....
76 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
77 $template->param( reservecount => $rank );
79 foreach my $res (@$reserves) {
80 if ( $res->{'found'} eq 'W' ) {
86 $template->param( rank => $rank );
88 # pass the pickup branch along....
89 my $branch = $query->param('branch');
90 $template->param( branch => $branch );
92 # make sure it's a real branch
93 if ( !$branches->{$branch} ) {
96 $template->param( branchname => $branches->{$branch}->{'branchname'} );
98 # make branch selection options...
103 my @CGIbranchlooparray;
105 foreach my $branch ( keys %$branches ) {
108 $line{branch} = $branches->{$branch}->{'branchname'};
109 $line{value} = $branch;
110 if ($line{value} eq C4::Context->userenv->{'branch'}) {
113 push @CGIbranchlooparray, \%line;
116 @CGIbranchlooparray =
117 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
118 my $CGIbranchloop = \@CGIbranchlooparray;
119 $template->param( CGIbranch => $CGIbranchloop );
121 my @items = GetItemsInfo($biblionumber);
123 # an item was previously required to have an itemtype in order to be reserved.
124 # this behavior removed to fix bug 1891 .
125 # my @items = grep { (C4::Context->preference('item-level_itypes') ? $_->{'itype'} : $_->{'itemtype'} ) } @all_items ;
127 $template->param( itemcount => scalar(@items) );
131 foreach my $itm (@items) {
132 $debug and warn $itm->{'notforloan'};
133 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'}, 'a', ( $itm->{'biblioitemnumber'} ) );
134 $itm->{'reservefee'} = sprintf "%.02f", $fee;
135 # pass itype to itemtype for display purposes.
136 $itm->{'itemtype'} = $itm->{'itype'} if(C4::Context->preference('item-level_itypes'));
137 $itemhash{$itm->{'itemnumber'}}=$itm;
138 if (!$itm->{'notforloan'} && !($itm->{'itemnotforloan'} > 0)){
143 if ( $query->param('place_reserve') ) {
144 my @bibitems=$query->param('biblioitem');
145 my $notes=$query->param('notes');
146 my $checkitem=$query->param('checkitem');
149 #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.
150 if ($checkitem ne ''){
151 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
152 my $item = $checkitem;
153 $item = GetItem($item);
154 if ( $item->{'holdingbranch'} eq $branch ){
155 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
160 @bibitems=sort @bibitems;
163 $realbi[0]=$bibitems[0];
164 for (my $i=1;$i<$count;$i++) {
166 if ($realbi[$i3] ne $bibitems[$i]) {
167 $realbi[$i2]=$bibitems[$i];
171 # here we actually do the reserveration. Stage 3.
172 if ($query->param('request') eq 'any'){
173 # place a request on 1st available
174 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
176 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
178 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
182 # Here we check that the borrower can actually make reserves Stage 1.
184 my $maxoutstanding = C4::Context->preference("maxoutstanding");
185 $template->param( noreserve => 1 ) unless $maxoutstanding;
186 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
187 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
188 $template->param( message => 1 );
190 $template->param( too_much_oweing => $amount );
192 if ( $borr->{gonenoaddress} eq 1 ) {
199 if ( $borr->{lost} eq 1 ) {
206 if ( $borr->{debarred} eq 1 ) {
213 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
214 $template->param( RESERVES => \@reserves );
215 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
216 $template->param( message => 1 );
218 $template->param( too_many_reserves => scalar(@reserves));
220 foreach my $res (@reserves) {
221 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
222 $template->param( message => 1 );
224 $template->param( already_reserved => 1 );
227 unless ($noreserves) {
228 $template->param( select_item_types => 1 );
233 my %itemnumbers_of_biblioitem;
234 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
235 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
237 foreach my $itemnumber (@itemnumbers) {
238 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
239 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
242 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
244 my $notforloan_label_of = get_notforloan_label_of();
245 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
248 foreach my $biblioitemnumber (@biblioitemnumbers) {
249 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
254 foreach my $biblioitemnumber (@biblioitemnumbers) {
255 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
257 $biblioitem->{description} =
258 $itemtypes->{ $biblioitem->{itemtype} }{description};
261 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
263 my $item = $itemhash{$itemnumber};
265 $item->{homebranchname} =
266 $branches->{ $item->{homebranch} }{branchname};
268 # if the holdingbranch is different than the homebranch, we show the
269 # holdingbranch of the document too
270 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
271 $item->{holdingbranchname} =
272 $branches->{ $item->{holdingbranch} }{branchname};
276 $item->{itemcallnumber} = $item->{itemcallnumber};
278 # if the item is currently on loan, we display its return date and
279 # change the background color
280 my $issues= GetItemIssue($itemnumber);
281 if ( $issues->{'date_due'} ) {
282 $item->{date_due} = format_date($issues->{'date_due'});
283 $item->{backgroundcolor} = 'onloan';
287 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
288 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
290 if ( defined $reservedate ) {
291 $item->{backgroundcolor} = 'reserved';
292 $item->{reservedate} = format_date($reservedate);
293 $item->{ReservedForBorrowernumber} = $reservedfor;
294 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
295 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
296 $item->{ExpectedAtLibrary} = $expectedAt;
300 # Management of the notforloan document
301 if ( $item->{notforloan} || $item->{itemnotforloan}) {
302 $item->{backgroundcolor} = 'other';
303 $item->{notforloanvalue} =
304 $notforloan_label_of->{ $item->{notforloan} };
307 # Management of lost or long overdue items
308 if ( $item->{itemlost} ) {
310 # FIXME localized strings should never be in Perl code
312 $item->{itemlost} == 1 ? "(lost)"
313 : $item->{itemlost} == 2 ? "(long overdue)"
315 $item->{backgroundcolor} = 'other';
318 # Check of the transfered documents
319 my ( $transfertwhen, $transfertfrom, $transfertto ) =
320 GetTransfers($itemnumber);
322 if ( $transfertwhen ne '' ) {
323 $item->{transfertwhen} = format_date($transfertwhen);
324 $item->{transfertfrom} =
325 $branches->{$transfertfrom}{branchname};
326 $item->{transfertto} = $branches->{$transfertto}{branchname};
327 $item->{nocancel} = 1;
330 # If there is no loan, return and transfer, we show a checkbox.
331 $item->{notforloan} = $item->{notforloan} || 0;
333 if (IsAvailableForItemLevelRequest($itemnumber)) {
334 $item->{available} = 1;
337 # FIXME: move this to a pm
338 my $dbh = C4::Context->dbh;
339 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
340 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
341 while (my $wait_hashref = $sth2->fetchrow_hashref) {
342 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
344 $item->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $item->{itype} }{imageurl} );
345 push @{ $biblioitem->{itemloop} }, $item;
348 push @bibitemloop, $biblioitem;
354 bibitemloop => \@bibitemloop,
356 output_html_with_http_headers $query, $cookie, $template->output;