fixing buggy url in cancel button
[koha.git] / reports / borrowers_out.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Output;
28 use C4::Circulation;
29 use C4::Members;
30 use Date::Manip;
31
32 =head1 NAME
33
34 plugin that shows a stats on borrowers
35
36 =head1 DESCRIPTION
37
38 =over 2
39
40 =cut
41
42 my $input = new CGI;
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/borrowers_out.tmpl";
45 my $limit = $input->param("Limit");
46 my $column = $input->param("Criteria");
47 my @filters = $input->param("Filter");
48 my $output = $input->param("output");
49 my $basename = $input->param("basename");
50 my $mime = $input->param("MIME");
51 my $del = $input->param("sep");
52 #warn "calcul : ".$calc;
53 my ($template, $borrowernumber, $cookie)
54         = get_template_and_user({template_name => $fullreportname,
55                                 query => $input,
56                                 type => "intranet",
57                                 authnotrequired => 0,
58                                 flagsrequired => {reports => 1},
59                                 debug => 1,
60                                 });
61 $template->param(do_it => $do_it,
62                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
63                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
64                 IntranetNav => C4::Context->preference("IntranetNav"),
65                 );
66 if ($do_it) {
67 # Displaying results
68         my $results = calculate($limit, $column, \@filters);
69         if ($output eq "screen"){
70 # Printing results to screen
71                 $template->param(mainloop => $results);
72                 output_html_with_http_headers $input, $cookie, $template->output;
73                 exit(1);
74         } else {
75 # Printing to a csv file
76                 print $input->header(-type => 'application/vnd.sun.xml.calc',
77                                      -encoding    => 'utf-8',
78                         -attachment=>"$basename.csv",
79                         -filename=>"$basename.csv" );
80                 my $cols = @$results[0]->{loopcol};
81                 my $lines = @$results[0]->{looprow};
82                 my $sep;
83                 $sep =C4::Context->preference("delimiter");
84 # header top-right
85                 print "num /". @$results[0]->{column} .$sep;
86 # Other header
87                 foreach my $col ( @$cols ) {
88                         print $col->{coltitle}.$sep;
89                 }
90                 print "Total\n";
91 # Table
92                 foreach my $line ( @$lines ) {
93                         my $x = $line->{loopcell};
94                         print $line->{rowtitle}.$sep;
95                         foreach my $cell (@$x) {
96                                 print $cell->{value}.$sep;
97                         }
98                         print $line->{totalrow};
99                         print "\n";
100                 }
101 # footer
102                 print "TOTAL";
103                 $cols = @$results[0]->{loopfooter};
104                 foreach my $col ( @$cols ) {
105                         print $sep.$col->{totalcol};
106                 }
107                 print $sep.@$results[0]->{total};
108                 exit(1);
109         }
110 # Displaying choices
111 } else {
112         my $dbh = C4::Context->dbh;
113         my @values;
114         my %labels;
115         my %select;
116         my $req;
117         
118         my @mime = ( C4::Context->preference("MIME") );
119 #       foreach my $mime (@mime){
120 #               warn "".$mime;
121 #       }
122         
123         my $CGIextChoice=CGI::scrolling_list(
124                                 -name     => 'MIME',
125                                 -id       => 'MIME',
126                                 -values   => \@mime,
127                                 -size     => 1,
128                                 -multiple => 0 );
129         
130         my @dels = ( C4::Context->preference("delimiter") );
131         my $CGIsepChoice=CGI::scrolling_list(
132                                 -name     => 'sep',
133                                 -id       => 'sep',
134                                 -values   => \@dels,
135                                 -size     => 1,
136                                 -multiple => 0 );
137         
138         my ($codes,$labels) = GetborCatFromCatType(undef,undef);
139         my @borcatloop;
140         foreach my $thisborcat (sort keys %$labels) {
141  #                      my $selected = 1 if $thisbranch eq $branch;
142                         my %row =(value => $thisborcat,
143  #                                                                      selected => $selected,
144                                                                         description => $labels->{$thisborcat},
145                                                         );
146                         push @borcatloop, \%row;
147         }
148         
149         
150         $template->param(
151                                         CGIextChoice => $CGIextChoice,
152                                         CGIsepChoice => $CGIsepChoice,
153                                         borcatloop =>\@borcatloop,
154                                         );
155 output_html_with_http_headers $input, $cookie, $template->output;
156 }
157
158
159
160
161 sub calculate {
162         my ($line, $column, $filters) = @_;
163         my @mainloop;
164         my @loopfooter;
165         my @loopcol;
166         my @loopline;
167         my @looprow;
168         my %globalline;
169         my $grantotal =0;
170 # extract parameters
171         my $dbh = C4::Context->dbh;
172
173 # Filters
174 # Checking filters
175 #
176         my @loopfilter;
177         for (my $i=0;$i<=2;$i++) {
178                 my %cell;
179                 if ( @$filters[$i] ) {
180                         if (($i==1) and (@$filters[$i-1])) {
181                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
182                         }
183                         $cell{filter} .= @$filters[$i];
184                         $cell{crit} .="Bor Cat" if ($i==0);
185                         $cell{crit} .="Without issues since" if ($i==1);
186                         push @loopfilter, \%cell;
187                 }
188         }
189         my $colfield;
190         my $colorder;
191         if ($column){
192                 $column = "borrowers.".$column if $column=~/categorycode/;
193                 my @colfilter ;
194                 $colfilter[0] = @$filters[0] if ($column =~ /category/ )  ;
195         #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
196         #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
197                                                                                                 
198         # loop cols.
199                 $colfield .= $column;
200                 $colorder .= $column;
201                 
202                 my $strsth2;
203                 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `issues` ON issues.borrowernumber=borrowers.borrowernumber";
204                 if ($colfilter[0]) {
205                         $colfilter[0] =~ s/\*/%/g;
206                         $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
207                 }
208                 $strsth2 .=" group by $colfield";
209                 $strsth2 .=" order by $colorder";
210                 warn "". $strsth2;
211                 
212                 my $sth2 = $dbh->prepare( $strsth2 );
213                 $sth2->execute;
214
215                 
216         
217                 while (my ($celvalue) = $sth2->fetchrow) {
218                         my %cell;
219         #               my %ft;
220         #               warn "coltitle :".$celvalue;
221                         $cell{coltitle} = $celvalue;
222         #               $ft{totalcol} = 0;
223                         push @loopcol, \%cell;
224                 }
225         #       warn "fin des titres colonnes";
226         }
227         
228         my $i=0;
229 #       my @totalcol;
230         my $hilighted=-1;
231         
232         #Initialization of cell values.....
233         my @table;
234         
235 #       warn "init table";
236         for (my $i=1;$i<=$line;$i++) {
237                 foreach my $col ( @loopcol ) {
238 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
239                         $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
240                 }
241         }
242
243
244 # preparing calculation
245         my $strcalc ;
246         
247 # Processing calculation
248         $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
249         $strcalc .= " , $colfield " if ($colfield);
250         $strcalc .= " FROM borrowers ";
251         $strcalc .= "WHERE 1 ";
252         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
253         $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
254         if (@$filters[1]){
255                 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ";
256                 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ");
257                 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
258                 
259 #               $queryfilter->execute(@$filters[1]);
260 #               while (my ($borrowernumber)=$queryfilter->fetchrow){
261 #                       $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
262 #               }
263         } else {
264                 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues ";
265                 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues ");
266                 $queryfilter->execute;
267                 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
268 #               while (my ($borrowernumber)=$queryfilter->fetchrow){
269 #                       $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
270 #               }
271         }
272         $strcalc .= " group by borrowers.borrowernumber";
273         $strcalc .= ", $colfield" if ($column);
274         $strcalc .= " order by $colfield " if ($colfield);
275         my $max;
276         if (@loopcol) {
277                 $max = $line*@loopcol;
278         } else { $max=$line;}
279         $strcalc .= " LIMIT 0,$max" if ($line);
280         warn "SQL :". $strcalc;
281         
282         my $dbcalc = $dbh->prepare($strcalc);
283         $dbcalc->execute;
284 #       warn "filling table";
285         my $previous_col;
286         my $i=1;
287         while (my  @data = $dbcalc->fetchrow) {
288                 my ($row, $col )=@data;
289                 $col = "zzEMPTY" if ($col eq undef);
290                 $i=1 if (($previous_col) and not($col eq $previous_col));
291                 $table[$i]->{$col}=$row;
292 #               warn " ".$i." ".$col. " ".$row;
293                 $i++;
294                 $previous_col=$col;
295         }
296         
297         push @loopcol,{coltitle => "Global"} if not($column);
298         
299         my $max =(($line)?$line:@table -1);
300         for ($i=1; $i<=$max;$i++) {
301                 my @loopcell;
302                 #@loopcol ensures the order for columns is common with column titles
303                 # and the number matches the number of columns
304                 my $colcount=0;
305                 foreach my $col ( @loopcol ) {
306                         my $value;
307                         if (@loopcol){
308                                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
309                         } else {
310                                 $value =$table[$i]->{"zzEMPTY"};
311                         }
312                         push @loopcell, {value => $value} ;
313                 }
314                 push @looprow,{ 'rowtitle' => $i ,
315                                                 'loopcell' => \@loopcell,
316                                                 'hilighted' => ($hilighted >0),
317                                         };
318                 $hilighted = -$hilighted;
319         }
320         
321                         
322
323         # the header of the table
324         $globalline{loopfilter}=\@loopfilter;
325         # the core of the table
326         $globalline{looprow} = \@looprow;
327         $globalline{loopcol} = \@loopcol;
328 #       # the foot (totals by borrower type)
329         $globalline{loopfooter} = \@loopfooter;
330         $globalline{total}= $grantotal;
331         $globalline{line} = $line;
332         $globalline{column} = $column;
333         push @mainloop,\%globalline;
334         return \@mainloop;
335 }
336
337 1;