writing absolute path.
[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::Members;
30 use C4::Branch; # GetBranches
31
32 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
33
34 my $query = new CGI;
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "opac-reserve.tmpl",
38         query           => $query,
39         type            => "opac",
40         authnotrequired => 0,
41         flagsrequired   => { borrow => 1 },
42         debug           => 1,
43     }
44 );
45
46 # get borrower information ....
47 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
48
49 # get biblionumber.....
50 my $biblionumber = $query->param('biblionumber');
51
52 my $bibdata = GetBiblioData($biblionumber);
53 $template->param($bibdata);
54 $template->param( biblionumber => $biblionumber );
55
56 # get the rank number....
57 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
58 $template->param( reservecount => $rank );
59
60 foreach my $res (@$reserves) {
61     if ( $res->{'found'} eq 'W' ) {
62         $rank--;
63     }
64 }
65
66 $rank++;
67 $template->param( rank => $rank );
68
69 # pass the pickup branch along....
70 my $branch = $query->param('branch');
71 $template->param( branch => $branch );
72
73 my $branches = GetBranches();
74
75 # make sure it's a real branch
76 if ( !$branches->{$branch} ) {
77     $branch = '';
78 }
79 $template->param( branchname => $branches->{$branch}->{'branchname'} );
80
81 # make branch selection options...
82 my @branches;
83 my @select_branch;
84 my %select_branches;
85
86 my @CGIbranchlooparray;
87
88 foreach my $branch ( keys %$branches ) {
89     if ($branch) {
90         my %line;
91         $line{branch} = $branches->{$branch}->{'branchname'};
92         $line{value}  = $branch;
93         push @CGIbranchlooparray, \%line;
94     }
95 }
96 @CGIbranchlooparray =
97   sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
98 my $CGIbranchloop = \@CGIbranchlooparray;
99 $template->param( CGIbranch => $CGIbranchloop );
100
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 = GetItemInfosOf($biblionumber);
104
105 #######################################################
106 # old version, add so that old templates still work
107 my %types_old;
108 foreach my $itm (@items) {
109     my $ity = $itm->{'itemtype'};
110     unless ( $types_old{$ity} ) {
111         $types_old{$ity}->{'itemtype'} = $ity;
112         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
113         $types_old{$ity}->{'description'} = $itm->{'description'};
114     }
115     else {
116         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
117     }
118 }
119
120 foreach my $type ( values %types_old ) {
121     my $copies = "";
122     foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
123         $copies .=
124             $branches->{$bc}->{'branchname'} . "("
125           . $type->{'branchinfo'}->{$bc} . ")";
126     }
127     $type->{'copies'} = $copies;
128 }
129
130 my @types_old = values %types_old;
131
132 # end old version
133 ################################
134
135 my @temp;
136 foreach my $itm (@items) {
137     push @temp, $itm if $itm->{'itemtype'};
138 }
139 @items = @temp;
140 my $itemcount = @items;
141 $template->param( itemcount => $itemcount );
142
143 my %types;
144 my %itemtypes;
145 my @duedates;
146 foreach my $itm (@items) {
147     push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
148       if defined $itm->{'date_due'};
149     $itm->{ $itm->{'publictype'} } = 1;
150
151     my $fee = GetReserveFee( 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               GetReserveFee( 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         AddReserve( $branch, $borrowernumber, $biblionumber, 'o',
258             \@reqbibs, $rank, '', $title );
259     }
260     if ( $query->param('all') ) {
261         AddReserve( $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 ) = GetReservesFromBorrowernumber( $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: