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');
150 #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.
151 if ($checkitem ne ''){
152 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
153 my $item = $checkitem;
154 $item = GetItem($item);
155 if ( $item->{'holdingbranch'} eq $branch ){
156 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
161 @bibitems=sort @bibitems;
164 $realbi[0]=$bibitems[0];
165 for (my $i=1;$i<$count;$i++) {
167 if ($realbi[$i3] ne $bibitems[$i]) {
168 $realbi[$i2]=$bibitems[$i];
173 if ($checkitem ne ''){
174 $canreserve = 1 if CanItemBeReserved($borrowernumber,$checkitem);
175 $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
176 my $item = GetItem($checkitem);
177 if ( $item->{'holdingbranch'} eq $branch ){
178 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
182 $canreserve = 1 if CanBookBeReserved($borrowernumber,$biblionumber);
183 # Inserts a null into the 'itemnumber' field of 'reserves' table.
187 # here we actually do the reserveration. Stage 3.
188 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,
189 $bibdata->{'title'},$checkitem, $found) if ($canreserve);
190 print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
194 # Here we check that the borrower can actually make reserves Stage 1.
196 my $maxoutstanding = C4::Context->preference("maxoutstanding");
197 $template->param( noreserve => 1 ) unless $maxoutstanding;
198 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
199 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
200 $template->param( message => 1 );
202 $template->param( too_much_oweing => $amount );
204 if ( $borr->{gonenoaddress} eq 1 ) {
211 if ( $borr->{lost} eq 1 ) {
218 if ( $borr->{debarred} eq 1 ) {
225 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
226 $template->param( RESERVES => \@reserves );
227 if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
228 $template->param( message => 1 );
230 $template->param( too_many_reserves => scalar(@reserves));
232 foreach my $res (@reserves) {
233 if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
234 $template->param( message => 1 );
236 $template->param( already_reserved => 1 );
239 unless ($noreserves) {
240 $template->param( select_item_types => 1 );
245 my %itemnumbers_of_biblioitem;
246 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
247 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
249 foreach my $itemnumber (@itemnumbers) {
250 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
251 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
254 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
256 my $notforloan_label_of = get_notforloan_label_of();
257 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
260 foreach my $biblioitemnumber (@biblioitemnumbers) {
261 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
265 my $num_available = 0;
267 foreach my $biblioitemnumber (@biblioitemnumbers) {
268 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
270 $biblioitem->{description} =
271 $itemtypes->{ $biblioitem->{itemtype} }{description};
274 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
276 my $item = $itemhash{$itemnumber};
278 $item->{homebranchname} =
279 $branches->{ $item->{homebranch} }{branchname};
281 # if the holdingbranch is different than the homebranch, we show the
282 # holdingbranch of the document too
283 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
284 $item->{holdingbranchname} =
285 $branches->{ $item->{holdingbranch} }{branchname};
289 $item->{itemcallnumber} = $item->{itemcallnumber};
291 # if the item is currently on loan, we display its return date and
292 # change the background color
293 my $issues= GetItemIssue($itemnumber);
294 if ( $issues->{'date_due'} ) {
295 $item->{date_due} = format_date($issues->{'date_due'});
296 $item->{backgroundcolor} = 'onloan';
300 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
301 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
303 if ( defined $reservedate ) {
304 $item->{backgroundcolor} = 'reserved';
305 $item->{reservedate} = format_date($reservedate);
306 $item->{ReservedForBorrowernumber} = $reservedfor;
307 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
308 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
309 $item->{ExpectedAtLibrary} = $expectedAt;
313 # Management of the notforloan document
314 if ( $item->{notforloan} || $item->{itemnotforloan}) {
315 $item->{backgroundcolor} = 'other';
316 $item->{notforloanvalue} =
317 $notforloan_label_of->{ $item->{notforloan} };
320 # Management of lost or long overdue items
321 if ( $item->{itemlost} ) {
323 # FIXME localized strings should never be in Perl code
325 $item->{itemlost} == 1 ? "(lost)"
326 : $item->{itemlost} == 2 ? "(long overdue)"
328 $item->{backgroundcolor} = 'other';
331 # Check of the transfered documents
332 my ( $transfertwhen, $transfertfrom, $transfertto ) =
333 GetTransfers($itemnumber);
335 if ( $transfertwhen ne '' ) {
336 $item->{transfertwhen} = format_date($transfertwhen);
337 $item->{transfertfrom} =
338 $branches->{$transfertfrom}{branchname};
339 $item->{transfertto} = $branches->{$transfertto}{branchname};
340 $item->{nocancel} = 1;
343 # If there is no loan, return and transfer, we show a checkbox.
344 $item->{notforloan} = $item->{notforloan} || 0;
346 my $branch = C4::Circulation::_GetCircControlBranch($item, $borr);
348 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itemtype'} );
350 my $policy_holdallowed = 1;
352 if ( $branchitemrule->{'holdallowed'} == 0 ||
353 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $item->{'homebranch'} ) ) {
354 $policy_holdallowed = 0;
357 if (IsAvailableForItemLevelRequest($itemnumber) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemnumber)) {
358 $item->{available} = 1;
362 # FIXME: move this to a pm
363 my $dbh = C4::Context->dbh;
364 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
365 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
366 while (my $wait_hashref = $sth2->fetchrow_hashref) {
367 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
370 $item->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $item->{itype} }{imageurl} );
372 push @{ $biblioitem->{itemloop} }, $item;
375 if(not CanBookBeReserved($borrowernumber,$biblionumber)){
376 $biblioitem->{available} = undef;
377 $biblioitem->{notholdable} = 1;
380 push @bibitemloop, $biblioitem;
383 if ( $num_available == 0 ) {
384 $template->param( none_available => 1, message => 1 );
390 bibitemloop => \@bibitemloop,
392 output_html_with_http_headers $query, $cookie, $template->output;