functions that were in C4::Interface::CGI::Output are now in C4::Output.
[koha.git] / opac / opac-reserve.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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
17
18 use strict;
19 require Exporter;
20 use CGI;
21 use C4::Biblio;
22 use C4::Auth;    # checkauth, getborrowernumber.
23 use C4::Koha;
24 use C4::Circulation;
25 use C4::Reserves;
26 use C4::Output;
27 use C4::Date;
28 use C4::Context;
29 use C4::Branch; # GetBranches
30
31 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
32
33 my $query = new CGI;
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35     {
36         template_name   => "opac-reserve.tmpl",
37         query           => $query,
38         type            => "opac",
39         authnotrequired => 0,
40         flagsrequired   => { borrow => 1 },
41         debug           => 1,
42     }
43 );
44
45 # get borrower information ....
46 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
47
48 # get biblionumber.....
49 my $biblionumber = $query->param('biblionumber');
50
51 my $bibdata = GetBiblioData($biblionumber);
52 $template->param($bibdata);
53 $template->param( biblionumber => $biblionumber );
54
55 # get the rank number....
56 my ( $rank, $reserves ) = FindReserves( $biblionumber, '' );
57 $template->param( reservecount => $rank );
58
59 foreach my $res (@$reserves) {
60     if ( $res->{'found'} eq 'W' ) {
61         $rank--;
62     }
63 }
64
65 $rank++;
66 $template->param( rank => $rank );
67
68 # pass the pickup branch along....
69 my $branch = $query->param('branch');
70 $template->param( branch => $branch );
71
72 my $branches = GetBranches();
73
74 # make sure it's a real branch
75 if ( !$branches->{$branch} ) {
76     $branch = '';
77 }
78 $template->param( branchname => $branches->{$branch}->{'branchname'} );
79
80 # make branch selection options...
81 my @branches;
82 my @select_branch;
83 my %select_branches;
84
85 my @CGIbranchlooparray;
86
87 foreach my $branch ( keys %$branches ) {
88     if ($branch) {
89         my %line;
90         $line{branch} = $branches->{$branch}->{'branchname'};
91         $line{value}  = $branch;
92         push @CGIbranchlooparray, \%line;
93     }
94 }
95 @CGIbranchlooparray =
96   sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
97 my $CGIbranchloop = \@CGIbranchlooparray;
98 $template->param( CGIbranch => $CGIbranchloop );
99
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);
103
104 #######################################################
105 # old version, add so that old templates still work
106 my %types_old;
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'};
113     }
114     else {
115         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
116     }
117 }
118
119 foreach my $type ( values %types_old ) {
120     my $copies = "";
121     foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
122         $copies .=
123             $branches->{$bc}->{'branchname'} . "("
124           . $type->{'branchinfo'}->{$bc} . ")";
125     }
126     $type->{'copies'} = $copies;
127 }
128
129 my @types_old = values %types_old;
130
131 # end old version
132 ################################
133
134 my @temp;
135 foreach my $itm (@items) {
136     push @temp, $itm if $itm->{'itemtype'};
137 }
138 @items = @temp;
139 my $itemcount = @items;
140 $template->param( itemcount => $itemcount );
141
142 my %types;
143 my %itemtypes;
144 my @duedates;
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
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;
161     }
162     else {
163         unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
164             $types{$pty}->{'count'}++;
165             $types{$pty}->{ $itm->{'itemtype'} } = 1;
166             push @{ $types{$pty}->{'items'} }, $itm;
167         }
168     }
169 }
170
171 $template->param( ITEMS => \@duedates );
172
173 my $width = keys %types;
174 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
175 my $typecount;
176 foreach my $pt (@publictypes) {
177     $typecount += $pt->{'count'};
178 }
179 $template->param( onlyone => 1 ) if $typecount == 1;
180
181 my @typerows;
182 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
183     my @row;
184     foreach my $pty (@publictypes) {
185         my @items = @{ $pty->{'items'} };
186         push @row, $items[$rownum] if defined $items[$rownum];
187     }
188     my $last = @row;
189     $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
190     my $fill = ( $width - $last ) * 2;
191     $fill-- if $fill;
192     push @typerows, { ROW => \@row, fill => $fill };
193 }
194 $template->param( TYPE_ROWS => \@typerows );
195 $width = 2 * $width - 1;
196 $template->param( totalwidth => 2 * $width - 1, );
197
198 if ( $query->param('item_types_selected') ) {
199
200     # this is what happens after the itemtypes have been selected. Stage 2
201     my @itemtypes = $query->param('itemtype');
202     my $fee       = 0;
203     my $proceed   = 0;
204     if (@itemtypes) {
205         my %newtypes;
206         foreach my $itmtype (@itemtypes) {
207             $newtypes{$itmtype} = $itemtypes{$itmtype};
208         }
209         my @types = values %newtypes;
210         $template->param( TYPES => \@types );
211         foreach my $type (@itemtypes) {
212             my @reqbibs;
213             foreach my $item (@items) {
214                 if ( $item->{'itemtype'} eq $type ) {
215                     push @reqbibs, $item->{'biblioitemnumber'};
216                 }
217             }
218             $fee +=
219               CalcReserveFee( undef, $borrowernumber, $biblionumber, 'o',
220                 \@reqbibs );
221         }
222         $proceed = 1;
223     }
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
227                 # -- JF
228                 #$fee = 1;
229                 $proceed = 1;
230     }
231     if ( $proceed && $branch ) {
232         $fee = sprintf "%.02f", $fee;
233                 if ($fee > 1) {
234         $template->param( fee => $fee, istherefee => $fee > 0 ? 1 : 0 );
235                 }
236         $template->param( item_types_selected => 1 );
237         $template->param( no_branch_selected => 1 ) unless ( $branch != '' );
238     }
239     else {
240         $template->param( message            => 1 );
241         $template->param( no_items_selected  => 1 ) unless ($proceed);
242         $template->param( no_branch_selected => 1 ) unless ($branch);
243     }
244 }
245 elsif ( $query->param('place_reserve') ) {
246
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) {
251         my @reqbibs;
252         foreach my $item (@items) {
253             if ( $item->{'itemtype'} eq $type ) {
254                 push @reqbibs, $item->{'biblioitemnumber'};
255             }
256         }
257         CreateReserve( undef, $branch, $borrowernumber, $biblionumber, 'o',
258             \@reqbibs, $rank, '', $title );
259     }
260     if ( $query->param('all') ) {
261         CreateReserve( undef, $branch, $borrowernumber, $biblionumber, 'a',
262             undef, $rank, '', $title );
263     }
264     print $query->redirect("/cgi-bin/koha/opac-user.pl");
265 }
266 else {
267
268     # Here we check that the borrower can actually make reserves Stage 1.
269     my $noreserves     = 0;
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 );
275         $noreserves = 1;
276         $template->param( too_much_oweing => $amount );
277     }
278     if ( $borr->{gonenoaddress} eq 1 ) {
279         $noreserves = 1;
280         $template->param(
281             message => 1,
282             GNA     => 1
283         );
284     }
285     if ( $borr->{lost} eq 1 ) {
286         $noreserves = 1;
287         $template->param(
288             message => 1,
289             lost    => 1
290         );
291     }
292     if ( $borr->{debarred} eq 1 ) {
293         $noreserves = 1;
294         $template->param(
295             message  => 1,
296             debarred => 1
297         );
298     }
299     my ( $resnum, $reserves ) = FindReserves( '', $borrowernumber );
300     $template->param( RESERVES => $reserves );
301     if ( $resnum >= $MAXIMUM_NUMBER_OF_RESERVES ) {
302         $template->param( message => 1 );
303         $noreserves = 1;
304         $template->param( too_many_reserves => $resnum );
305     }
306     foreach my $res (@$reserves) {
307         if ( $res->{'biblionumber'} == $biblionumber ) {
308             $template->param( message => 1 );
309             $noreserves = 1;
310             $template->param( already_reserved => 1 );
311         }
312     }
313     unless ($noreserves) {
314         $template->param( TYPES             => \@types_old );
315         $template->param( select_item_types => 1 );
316     }
317 }
318
319 output_html_with_http_headers $query, $cookie, $template->output;
320
321 # Local Variables:
322 # tab-width: 8
323 # End: