BIG COMMIT : cleaning of Reserves.pm. See detail on koha-devel
[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 ) = GetReservesFromBiblionumber( $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     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;
160     }
161     else {
162         unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
163             $types{$pty}->{'count'}++;
164             $types{$pty}->{ $itm->{'itemtype'} } = 1;
165             push @{ $types{$pty}->{'items'} }, $itm;
166         }
167     }
168 }
169
170 $template->param( ITEMS => \@duedates );
171
172 my $width = keys %types;
173 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
174 my $typecount;
175 foreach my $pt (@publictypes) {
176     $typecount += $pt->{'count'};
177 }
178 $template->param( onlyone => 1 ) if $typecount == 1;
179
180 my @typerows;
181 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
182     my @row;
183     foreach my $pty (@publictypes) {
184         my @items = @{ $pty->{'items'} };
185         push @row, $items[$rownum] if defined $items[$rownum];
186     }
187     my $last = @row;
188     $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
189     my $fill = ( $width - $last ) * 2;
190     $fill-- if $fill;
191     push @typerows, { ROW => \@row, fill => $fill };
192 }
193 $template->param( TYPE_ROWS => \@typerows );
194 $width = 2 * $width - 1;
195 $template->param( totalwidth => 2 * $width - 1, );
196
197 if ( $query->param('item_types_selected') ) {
198
199     # this is what happens after the itemtypes have been selected. Stage 2
200     my @itemtypes = $query->param('itemtype');
201     my $fee       = 0;
202     my $proceed   = 0;
203     if (@itemtypes) {
204         my %newtypes;
205         foreach my $itmtype (@itemtypes) {
206             $newtypes{$itmtype} = $itemtypes{$itmtype};
207         }
208         my @types = values %newtypes;
209         $template->param( TYPES => \@types );
210         foreach my $type (@itemtypes) {
211             my @reqbibs;
212             foreach my $item (@items) {
213                 if ( $item->{'itemtype'} eq $type ) {
214                     push @reqbibs, $item->{'biblioitemnumber'};
215                 }
216             }
217             $fee +=
218               GetReserveFee( undef, $borrowernumber, $biblionumber, 'o',
219                 \@reqbibs );
220         }
221         $proceed = 1;
222     }
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
226                 # -- JF
227                 #$fee = 1;
228                 $proceed = 1;
229     }
230     if ( $proceed && $branch ) {
231         $fee = sprintf "%.02f", $fee;
232                 if ($fee > 1) {
233         $template->param( fee => $fee, istherefee => $fee > 0 ? 1 : 0 );
234                 }
235         $template->param( item_types_selected => 1 );
236         $template->param( no_branch_selected => 1 ) unless ( $branch != '' );
237     }
238     else {
239         $template->param( message            => 1 );
240         $template->param( no_items_selected  => 1 ) unless ($proceed);
241         $template->param( no_branch_selected => 1 ) unless ($branch);
242     }
243 }
244 elsif ( $query->param('place_reserve') ) {
245
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) {
250         my @reqbibs;
251         foreach my $item (@items) {
252             if ( $item->{'itemtype'} eq $type ) {
253                 push @reqbibs, $item->{'biblioitemnumber'};
254             }
255         }
256         AddReserve( $branch, $borrowernumber, $biblionumber, 'o',
257             \@reqbibs, $rank, '', $title );
258     }
259     if ( $query->param('all') ) {
260         AddReserve( $branch, $borrowernumber, $biblionumber, 'a',
261             undef, $rank, '', $title );
262     }
263     print $query->redirect("/cgi-bin/koha/opac-user.pl");
264 }
265 else {
266
267     # Here we check that the borrower can actually make reserves Stage 1.
268     my $noreserves     = 0;
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 );
274         $noreserves = 1;
275         $template->param( too_much_oweing => $amount );
276     }
277     if ( $borr->{gonenoaddress} eq 1 ) {
278         $noreserves = 1;
279         $template->param(
280             message => 1,
281             GNA     => 1
282         );
283     }
284     if ( $borr->{lost} eq 1 ) {
285         $noreserves = 1;
286         $template->param(
287             message => 1,
288             lost    => 1
289         );
290     }
291     if ( $borr->{debarred} eq 1 ) {
292         $noreserves = 1;
293         $template->param(
294             message  => 1,
295             debarred => 1
296         );
297     }
298     my ( $resnum, $reserves ) = GetReservesFromBorrowernumber( $borrowernumber );
299     $template->param( RESERVES => $reserves );
300     if ( $resnum >= $MAXIMUM_NUMBER_OF_RESERVES ) {
301         $template->param( message => 1 );
302         $noreserves = 1;
303         $template->param( too_many_reserves => $resnum );
304     }
305     foreach my $res (@$reserves) {
306         if ( $res->{'biblionumber'} == $biblionumber ) {
307             $template->param( message => 1 );
308             $noreserves = 1;
309             $template->param( already_reserved => 1 );
310         }
311     }
312     unless ($noreserves) {
313         $template->param( TYPES             => \@types_old );
314         $template->param( select_item_types => 1 );
315     }
316 }
317
318 output_html_with_http_headers $query, $cookie, $template->output;
319
320 # Local Variables:
321 # tab-width: 8
322 # End: