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 push @CGIbranchlooparray, \%line;
97 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
98 my $CGIbranchloop = \@CGIbranchlooparray;
99 $template->param( CGIbranch => $CGIbranchloop );
101 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
102 # get the itemtype data....
103 my @items = GetItemsInfo($biblionumber);
104 #######################################################
105 # old version, add so that old templates still work
107 foreach my $itm (@items) {
108 my $ity = $itm->{'itemtype'};
109 unless ( $types_old{$ity} ) {
110 $types_old{$ity}->{'itemtype'} = $ity;
111 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
112 $types_old{$ity}->{'description'} = $itm->{'description'};
115 $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
119 foreach my $type ( values %types_old ) {
121 foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
123 $branches->{$bc}->{'branchname'} . "("
124 . $type->{'branchinfo'}->{$bc} . ")";
126 $type->{'copies'} = $copies;
129 my @types_old = values %types_old;
132 ################################
135 foreach my $itm (@items) {
136 push @temp, $itm if $itm->{'itemtype'};
139 my $itemcount = @items;
140 $template->param( itemcount => $itemcount );
145 foreach my $itm (@items) {
146 push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
147 if defined $itm->{'date_due'};
148 $itm->{ $itm->{'publictype'} } = 1;
149 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
150 'a', ( $itm->{'biblioitemnumber'} ) );
151 $fee = sprintf "%.02f", $fee;
152 $itm->{'reservefee'} = $fee;
153 my $pty = $itm->{'publictype'};
154 $itemtypes{ $itm->{'itemtype'} } = $itm;
155 unless ( $types{$pty} ) {
156 $types{$pty}->{'count'} = 1;
157 $types{$pty}->{ $itm->{'itemtype'} } = 1;
158 push @{ $types{$pty}->{'items'} }, $itm;
161 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
162 $types{$pty}->{'count'}++;
163 $types{$pty}->{ $itm->{'itemtype'} } = 1;
164 push @{ $types{$pty}->{'items'} }, $itm;
169 $template->param( ITEMS => \@duedates );
171 my $width = keys %types;
172 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
174 foreach my $pt (@publictypes) {
175 $typecount += $pt->{'count'};
177 $template->param( onlyone => 1 ) if $typecount == 1;
180 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
182 foreach my $pty (@publictypes) {
183 my @items = @{ $pty->{'items'} };
184 push @row, $items[$rownum] if defined $items[$rownum];
187 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
188 my $fill = ( $width - $last ) * 2;
190 push @typerows, { ROW => \@row, fill => $fill };
192 $template->param( TYPE_ROWS => \@typerows );
193 $width = 2 * $width - 1;
194 $template->param( totalwidth => 2 * $width - 1, );
196 if ( $query->param('place_reserve') ) {
197 my @bibitems=$query->param('biblioitem');
198 my $notes=$query->param('notes');
199 my $checkitem=$query->param('checkitem');
202 #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.
203 if ($checkitem ne ''){
205 my $item = $checkitem;
206 $item = GetItem($item);
207 if ( $item->{'holdingbranch'} eq $branch ){
208 $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
213 @bibitems=sort @bibitems;
216 $realbi[0]=$bibitems[0];
217 for (my $i=1;$i<$count;$i++) {
219 if ($realbi[$i3] ne $bibitems[$i]) {
220 $realbi[$i2]=$bibitems[$i];
224 # here we actually do the reserveration. Stage 3.
225 if ($query->param('request') eq 'any'){
226 # place a request on 1st available
227 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
229 AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
231 print $query->redirect("/cgi-bin/koha/opac-user.pl");
235 # Here we check that the borrower can actually make reserves Stage 1.
237 my $maxoutstanding = C4::Context->preference("maxoutstanding");
238 $template->param( noreserve => 1 ) unless $maxoutstanding;
239 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
240 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
241 $template->param( message => 1 );
243 $template->param( too_much_oweing => $amount );
245 if ( $borr->{gonenoaddress} eq 1 ) {
252 if ( $borr->{lost} eq 1 ) {
259 if ( $borr->{debarred} eq 1 ) {
266 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
267 $template->param( RESERVES => \@reserves );
268 if ( scalar(@$reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
269 $template->param( message => 1 );
271 $template->param( too_many_reserves => scalar($reserves));
273 foreach my $res (@$reserves) {
274 if ( $res->{'biblionumber'} == $biblionumber ) {
275 $template->param( message => 1 );
277 $template->param( already_reserved => 1 );
280 unless ($noreserves) {
281 $template->param( TYPES => \@types_old );
282 $template->param( select_item_types => 1 );
288 my %itemnumbers_of_biblioitem;
289 my @itemnumbers = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
290 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
292 foreach my $itemnumber (@itemnumbers) {
294 $iteminfos_of->{$itemnumber}->{homebranch},
295 $iteminfos_of->{$itemnumber}->{holdingbranch} );
297 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
298 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
301 # @branchcodes = uniq @branchcodes;
303 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
305 my $branchinfos_of = get_branchinfos_of(@branchcodes);
306 my $notforloan_label_of = get_notforloan_label_of();
307 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
310 foreach my $biblioitemnumber (@biblioitemnumbers) {
311 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
314 my $itemtypeinfos_of = get_itemtypeinfos_of(@itemtypes);
318 foreach my $biblioitemnumber (@biblioitemnumbers) {
319 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
321 $biblioitem->{description} =
322 $itemtypeinfos_of->{ $biblioitem->{itemtype} }{description};
325 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
327 my $item = $iteminfos_of->{$itemnumber};
329 $item->{homebranchname} =
330 $branchinfos_of->{ $item->{homebranch} }{branchname};
332 # if the holdingbranch is different than the homebranch, we show the
333 # holdingbranch of the document too
334 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
335 $item->{holdingbranchname} =
336 $branchinfos_of->{ $item->{holdingbranch} }{branchname};
340 $item->{itemcallnumber} = $item->{itemcallnumber};
342 # if the item is currently on loan, we display its return date and
343 # change the background color
344 my $issues= GetItemIssue($itemnumber);
345 if ( $issues->{'date_due'} ) {
346 $item->{date_due} = format_date($issues->{'date_due'});
347 $item->{backgroundcolor} = 'onloan';
351 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
352 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
354 if ( defined $reservedate ) {
355 $item->{backgroundcolor} = 'reserved';
356 $item->{reservedate} = format_date($reservedate);
357 $item->{ReservedForBorrowernumber} = $reservedfor;
358 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
359 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
360 $item->{ExpectedAtLibrary} = $expectedAt;
364 # Management of the notforloan document
365 if ( $item->{notforloan} ) {
366 $item->{backgroundcolor} = 'other';
367 $item->{notforloanvalue} =
368 $notforloan_label_of->{ $item->{notforloan} };
371 # Management of lost or long overdue items
372 if ( $item->{itemlost} ) {
374 # FIXME localized strings should never be in Perl code
376 $item->{itemlost} == 1 ? "(lost)"
377 : $item->{itemlost} == 2 ? "(long overdue)"
379 $item->{backgroundcolor} = 'other';
382 # Check of the transfered documents
383 my ( $transfertwhen, $transfertfrom, $transfertto ) =
384 GetTransfers($itemnumber);
386 if ( $transfertwhen ne '' ) {
387 $item->{transfertwhen} = format_date($transfertwhen);
388 $item->{transfertfrom} =
389 $branchinfos_of->{$transfertfrom}{branchname};
390 $item->{transfertto} = $branchinfos_of->{$transfertto}{branchname};
391 $item->{nocancel} = 1;
394 # If there is no loan, return and transfer, we show a checkbox.
395 $item->{notforloan} = $item->{notforloan} || 0;
397 # An item is available only if:
399 not defined $reservedate # not reserved yet
400 and $issues->{'date_due'} eq '' # not currently on loan
401 and not $item->{itemlost} # not lost
402 and not $item->{notforloan} # not forbidden to loan
403 and $transfertwhen eq '' # not currently on transfert
406 $item->{available} = 1;
409 push @{ $biblioitem->{itemloop} }, $item;
412 push @bibitemloop, $biblioitem;
417 bibitemloop => \@bibitemloop,
419 output_html_with_http_headers $query, $cookie, $template->output;