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