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.
29 use C4::Branch; # GetBranches
31 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36 template_name => "opac-reserve.tmpl",
40 flagsrequired => { borrow => 1 },
45 # get borrower information ....
46 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
48 # get biblionumber.....
49 my $biblionumber = $query->param('biblionumber');
51 my $bibdata = GetBiblioData($biblionumber);
52 $template->param($bibdata);
53 $template->param( biblionumber => $biblionumber );
55 # get the rank number....
56 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
57 $template->param( reservecount => $rank );
59 foreach my $res (@$reserves) {
60 if ( $res->{'found'} eq 'W' ) {
66 $template->param( rank => $rank );
68 # pass the pickup branch along....
69 my $branch = $query->param('branch');
70 $template->param( branch => $branch );
72 my $branches = GetBranches();
74 # make sure it's a real branch
75 if ( !$branches->{$branch} ) {
78 $template->param( branchname => $branches->{$branch}->{'branchname'} );
80 # make branch selection options...
85 my @CGIbranchlooparray;
87 foreach my $branch ( keys %$branches ) {
90 $line{branch} = $branches->{$branch}->{'branchname'};
91 $line{value} = $branch;
92 push @CGIbranchlooparray, \%line;
96 sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
97 my $CGIbranchloop = \@CGIbranchlooparray;
98 $template->param( CGIbranch => $CGIbranchloop );
100 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
101 # get the itemtype data....
102 my @items = GetItemInfosOf($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;
150 my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
151 'a', ( $itm->{'biblioitemnumber'} ) );
152 $fee = sprintf "%.02f", $fee;
153 $itm->{'reservefee'} = $fee;
154 my $pty = $itm->{'publictype'};
155 $itemtypes{ $itm->{'itemtype'} } = $itm;
156 unless ( $types{$pty} ) {
157 $types{$pty}->{'count'} = 1;
158 $types{$pty}->{ $itm->{'itemtype'} } = 1;
159 push @{ $types{$pty}->{'items'} }, $itm;
162 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
163 $types{$pty}->{'count'}++;
164 $types{$pty}->{ $itm->{'itemtype'} } = 1;
165 push @{ $types{$pty}->{'items'} }, $itm;
170 $template->param( ITEMS => \@duedates );
172 my $width = keys %types;
173 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
175 foreach my $pt (@publictypes) {
176 $typecount += $pt->{'count'};
178 $template->param( onlyone => 1 ) if $typecount == 1;
181 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
183 foreach my $pty (@publictypes) {
184 my @items = @{ $pty->{'items'} };
185 push @row, $items[$rownum] if defined $items[$rownum];
188 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
189 my $fill = ( $width - $last ) * 2;
191 push @typerows, { ROW => \@row, fill => $fill };
193 $template->param( TYPE_ROWS => \@typerows );
194 $width = 2 * $width - 1;
195 $template->param( totalwidth => 2 * $width - 1, );
197 if ( $query->param('item_types_selected') ) {
199 # this is what happens after the itemtypes have been selected. Stage 2
200 my @itemtypes = $query->param('itemtype');
205 foreach my $itmtype (@itemtypes) {
206 $newtypes{$itmtype} = $itemtypes{$itmtype};
208 my @types = values %newtypes;
209 $template->param( TYPES => \@types );
210 foreach my $type (@itemtypes) {
212 foreach my $item (@items) {
213 if ( $item->{'itemtype'} eq $type ) {
214 push @reqbibs, $item->{'biblioitemnumber'};
218 GetReserveFee( undef, $borrowernumber, $biblionumber, 'o',
223 elsif ( $query->param('all') ) {
224 $template->param( all => 1 );
225 # No idea why fee would be set to 1 ... it's supposed to be a monetary value, not a flag
230 if ( $proceed && $branch ) {
231 $fee = sprintf "%.02f", $fee;
233 $template->param( fee => $fee, istherefee => $fee > 0 ? 1 : 0 );
235 $template->param( item_types_selected => 1 );
236 $template->param( no_branch_selected => 1 ) unless ( $branch != '' );
239 $template->param( message => 1 );
240 $template->param( no_items_selected => 1 ) unless ($proceed);
241 $template->param( no_branch_selected => 1 ) unless ($branch);
244 elsif ( $query->param('place_reserve') ) {
246 # here we actually do the reserveration. Stage 3.
247 my $title = $bibdata->{'title'};
248 my @itemtypes = $query->param('itemtype');
249 foreach my $type (@itemtypes) {
251 foreach my $item (@items) {
252 if ( $item->{'itemtype'} eq $type ) {
253 push @reqbibs, $item->{'biblioitemnumber'};
256 AddReserve( $branch, $borrowernumber, $biblionumber, 'o',
257 \@reqbibs, $rank, '', $title );
259 if ( $query->param('all') ) {
260 AddReserve( $branch, $borrowernumber, $biblionumber, 'a',
261 undef, $rank, '', $title );
263 print $query->redirect("/cgi-bin/koha/opac-user.pl");
267 # Here we check that the borrower can actually make reserves Stage 1.
269 my $maxoutstanding = C4::Context->preference("maxoutstanding");
270 $template->param( noreserve => 1 ) unless $maxoutstanding;
271 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
272 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
273 $template->param( message => 1 );
275 $template->param( too_much_oweing => $amount );
277 if ( $borr->{gonenoaddress} eq 1 ) {
284 if ( $borr->{lost} eq 1 ) {
291 if ( $borr->{debarred} eq 1 ) {
298 my ( $resnum, $reserves ) = GetReservesFromBorrowernumber( $borrowernumber );
299 $template->param( RESERVES => $reserves );
300 if ( $resnum >= $MAXIMUM_NUMBER_OF_RESERVES ) {
301 $template->param( message => 1 );
303 $template->param( too_many_reserves => $resnum );
305 foreach my $res (@$reserves) {
306 if ( $res->{'biblionumber'} == $biblionumber ) {
307 $template->param( message => 1 );
309 $template->param( already_reserved => 1 );
312 unless ($noreserves) {
313 $template->param( TYPES => \@types_old );
314 $template->param( select_item_types => 1 );
318 output_html_with_http_headers $query, $cookie, $template->output;