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