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