3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 use C4::Dates qw/format_date_in_iso/;
35 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/borrowers_out.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 $filters[1] = format_date_in_iso($filters[1]) if $filters[1];
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 our $sep = $input->param("sep") || '';
53 $sep = "\t" if ($sep eq 'tabulation');
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user({template_name => $fullreportname,
59 flagsrequired => {reports => '*'},
62 $template->param(do_it => $do_it,
63 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
67 my $results = calculate($limit, $column, \@filters);
68 if ($output eq "screen"){
69 # Printing results to screen
70 $template->param(mainloop => $results);
71 output_html_with_http_headers $input, $cookie, $template->output;
74 # Printing to a csv file
75 print $input->header(-type => 'application/vnd.sun.xml.calc',
77 -attachment=>"$basename.csv",
78 -filename=>"$basename.csv" );
79 my $cols = @$results[0]->{loopcol};
80 my $lines = @$results[0]->{looprow};
82 print "num /". @$results[0]->{column} .$sep;
84 foreach my $col ( @$cols ) {
85 print $col->{coltitle}.$sep;
89 foreach my $line ( @$lines ) {
90 my $x = $line->{loopcell};
91 print $line->{rowtitle}.$sep;
92 foreach my $cell (@$x) {
93 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
96 # print $line->{totalrow};
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
110 my $dbh = C4::Context->dbh;
116 my $CGIextChoice=CGI::scrolling_list(
119 -values => ['CSV'], # FIXME translation
123 my $CGIsepChoice = GetDelimiterChoices;
125 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
127 foreach my $thisborcat (sort keys %$labels) {
128 my %row =(value => $thisborcat,
129 description => $labels->{$thisborcat},
131 push @borcatloop, \%row;
134 CGIextChoice => $CGIextChoice,
135 CGIsepChoice => $CGIsepChoice,
136 borcatloop =>\@borcatloop,
138 output_html_with_http_headers $input, $cookie, $template->output;
143 my ($line, $column, $filters) = @_;
152 my $dbh = C4::Context->dbh;
158 for (my $i=0;$i<=2;$i++) {
160 if ( @$filters[$i] ) {
161 if (($i==1) and (@$filters[$i-1])) {
162 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
164 $cell{filter} .= @$filters[$i];
165 $cell{crit} .="Bor Cat" if ($i==0);
166 $cell{crit} .="Without issues since" if ($i==1);
167 push @loopfilter, \%cell;
173 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
175 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
176 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
177 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
180 $colfield .= $column;
181 $colorder .= $column;
184 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `old_issues` USING(borrowernumber)";
186 $colfilter[0] =~ s/\*/%/g;
187 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
189 $strsth2 .=" group by $colfield";
190 $strsth2 .=" order by $colorder";
193 my $sth2 = $dbh->prepare( $strsth2 );
195 while (my ($celvalue) = $sth2->fetchrow) {
198 # warn "coltitle :".$celvalue;
199 $cell{coltitle} = $celvalue;
201 push @loopcol, \%cell;
203 # warn "fin des titres colonnes";
209 #Initialization of cell values.....
214 for (my $i=1;$i<=$line;$i++) {
215 foreach my $col ( @loopcol ) {
216 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
222 # preparing calculation
225 # Processing calculation
226 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
227 $strcalc .= " , $colfield " if ($colfield);
228 $strcalc .= " FROM borrowers ";
229 $strcalc .= "WHERE 1 ";
230 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
231 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
232 my $strqueryfilter = "SELECT DISTINCT borrowernumber FROM old_issues WHERE borrowernumber IS NOT NULL ";
234 my $strqueryfilter .= "AND old_issues.timestamp> @$filters[1] ";
236 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
237 $strcalc .= " group by borrowers.borrowernumber";
238 $strcalc .= ", $colfield" if ($column);
239 $strcalc .= " order by $colfield " if ($colfield);
243 $max = $line*@loopcol;
244 } else { $max=$line;}
245 $strcalc .= " LIMIT 0,$max";
248 my $dbcalc = $dbh->prepare($strcalc);
250 # warn "filling table";
253 while (my @data = $dbcalc->fetchrow) {
254 my ($row, $col )=@data;
255 $col = "zzEMPTY" if (!defined($col));
256 $i=1 if (($previous_col) and not($col eq $previous_col));
257 $table[$i]->{$col}=$row;
258 # warn " $i $col $row";
263 push @loopcol,{coltitle => "Global"} if not($column);
265 $max =(($line)?$line:@table -1);
266 for ($i=1; $i<=$max;$i++) {
268 #@loopcol ensures the order for columns is common with column titles
269 # and the number matches the number of columns
271 foreach my $col ( @loopcol ) {
274 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
276 $value =$table[$i]->{"zzEMPTY"};
278 push @loopcell, {value => $value} ;
280 push @looprow,{ 'rowtitle' => $i ,
281 'loopcell' => \@loopcell,
287 # the header of the table
288 $globalline{loopfilter}=\@loopfilter;
289 # the core of the table
290 $globalline{looprow} = \@looprow;
291 $globalline{loopcol} = \@loopcol;
292 # # the foot (totals by borrower type)
293 $globalline{loopfooter} = \@loopfooter;
294 $globalline{total}= $grantotal;
295 $globalline{line} = $line;
296 $globalline{column} = $column;
297 push @mainloop,\%globalline;