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.
26 use C4::Interface::CGI::Output;
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 ) = FindReserves( $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 # FIXME CalcReserveFee is supposed to be internal-use-only
151 my $fee = CalcReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
152 'a', ( $itm->{'biblioitemnumber'} ) );
153 $fee = sprintf "%.02f", $fee;
154 $itm->{'reservefee'} = $fee;
155 my $pty = $itm->{'publictype'};
156 $itemtypes{ $itm->{'itemtype'} } = $itm;
157 unless ( $types{$pty} ) {
158 $types{$pty}->{'count'} = 1;
159 $types{$pty}->{ $itm->{'itemtype'} } = 1;
160 push @{ $types{$pty}->{'items'} }, $itm;
163 unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
164 $types{$pty}->{'count'}++;
165 $types{$pty}->{ $itm->{'itemtype'} } = 1;
166 push @{ $types{$pty}->{'items'} }, $itm;
171 $template->param( ITEMS => \@duedates );
173 my $width = keys %types;
174 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
176 foreach my $pt (@publictypes) {
177 $typecount += $pt->{'count'};
179 $template->param( onlyone => 1 ) if $typecount == 1;
182 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
184 foreach my $pty (@publictypes) {
185 my @items = @{ $pty->{'items'} };
186 push @row, $items[$rownum] if defined $items[$rownum];
189 $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
190 my $fill = ( $width - $last ) * 2;
192 push @typerows, { ROW => \@row, fill => $fill };
194 $template->param( TYPE_ROWS => \@typerows );
195 $width = 2 * $width - 1;
196 $template->param( totalwidth => 2 * $width - 1, );
198 if ( $query->param('item_types_selected') ) {
200 # this is what happens after the itemtypes have been selected. Stage 2
201 my @itemtypes = $query->param('itemtype');
206 foreach my $itmtype (@itemtypes) {
207 $newtypes{$itmtype} = $itemtypes{$itmtype};
209 my @types = values %newtypes;
210 $template->param( TYPES => \@types );
211 foreach my $type (@itemtypes) {
213 foreach my $item (@items) {
214 if ( $item->{'itemtype'} eq $type ) {
215 push @reqbibs, $item->{'biblioitemnumber'};
219 CalcReserveFee( undef, $borrowernumber, $biblionumber, 'o',
224 elsif ( $query->param('all') ) {
225 $template->param( all => 1 );
226 # No idea why fee would be set to 1 ... it's supposed to be a monetary value, not a flag
231 if ( $proceed && $branch ) {
232 $fee = sprintf "%.02f", $fee;
234 $template->param( fee => $fee, istherefee => $fee > 0 ? 1 : 0 );
236 $template->param( item_types_selected => 1 );
237 $template->param( no_branch_selected => 1 ) unless ( $branch != '' );
240 $template->param( message => 1 );
241 $template->param( no_items_selected => 1 ) unless ($proceed);
242 $template->param( no_branch_selected => 1 ) unless ($branch);
245 elsif ( $query->param('place_reserve') ) {
247 # here we actually do the reserveration. Stage 3.
248 my $title = $bibdata->{'title'};
249 my @itemtypes = $query->param('itemtype');
250 foreach my $type (@itemtypes) {
252 foreach my $item (@items) {
253 if ( $item->{'itemtype'} eq $type ) {
254 push @reqbibs, $item->{'biblioitemnumber'};
257 CreateReserve( undef, $branch, $borrowernumber, $biblionumber, 'o',
258 \@reqbibs, $rank, '', $title );
260 if ( $query->param('all') ) {
261 CreateReserve( undef, $branch, $borrowernumber, $biblionumber, 'a',
262 undef, $rank, '', $title );
264 print $query->redirect("/cgi-bin/koha/opac-user.pl");
268 # Here we check that the borrower can actually make reserves Stage 1.
270 my $maxoutstanding = C4::Context->preference("maxoutstanding");
271 $template->param( noreserve => 1 ) unless $maxoutstanding;
272 if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
273 my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
274 $template->param( message => 1 );
276 $template->param( too_much_oweing => $amount );
278 if ( $borr->{gonenoaddress} eq 1 ) {
285 if ( $borr->{lost} eq 1 ) {
292 if ( $borr->{debarred} eq 1 ) {
299 my ( $resnum, $reserves ) = FindReserves( '', $borrowernumber );
300 $template->param( RESERVES => $reserves );
301 if ( $resnum >= $MAXIMUM_NUMBER_OF_RESERVES ) {
302 $template->param( message => 1 );
304 $template->param( too_many_reserves => $resnum );
306 foreach my $res (@$reserves) {
307 if ( $res->{'biblionumber'} == $biblionumber ) {
308 $template->param( message => 1 );
310 $template->param( already_reserved => 1 );
313 unless ($noreserves) {
314 $template->param( TYPES => \@types_old );
315 $template->param( select_item_types => 1 );
319 output_html_with_http_headers $query, $cookie, $template->output;