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,
66 my $results = calculate
($limit, $column, \
@filters);
67 if ($output eq "screen"){
68 # Printing results to screen
69 $template->param(mainloop
=> $results);
70 output_html_with_http_headers
$input, $cookie, $template->output;
73 # Printing to a csv file
74 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
76 -attachment
=>"$basename.csv",
77 -filename
=>"$basename.csv" );
78 my $cols = @
$results[0]->{loopcol
};
79 my $lines = @
$results[0]->{looprow
};
81 print "num /". @
$results[0]->{column
} .$sep;
83 foreach my $col ( @
$cols ) {
84 print $col->{coltitle
}.$sep;
88 foreach my $line ( @
$lines ) {
89 my $x = $line->{loopcell
};
90 print $line->{rowtitle
}.$sep;
91 foreach my $cell (@
$x) {
92 my $cellvalue = defined $cell->{value
} ?
$cell->{value
}.$sep : ''.$sep;
95 # print $line->{totalrow};
100 $cols = @
$results[0]->{loopfooter
};
101 foreach my $col ( @
$cols ) {
102 print $sep.$col->{totalcol
};
104 print $sep.@
$results[0]->{total
};
109 my $dbh = C4
::Context
->dbh;
115 my $CGIextChoice=CGI
::scrolling_list
(
118 -values => ['CSV'], # FIXME translation
122 my $CGIsepChoice = GetDelimiterChoices
;
124 my ($codes,$labels) = GetborCatFromCatType
(undef,undef);
126 foreach my $thisborcat (sort keys %$labels) {
127 my %row =(value
=> $thisborcat,
128 description
=> $labels->{$thisborcat},
130 push @borcatloop, \
%row;
133 CGIextChoice
=> $CGIextChoice,
134 CGIsepChoice
=> $CGIsepChoice,
135 borcatloop
=>\
@borcatloop,
137 output_html_with_http_headers
$input, $cookie, $template->output;
142 my ($line, $column, $filters) = @_;
151 my $dbh = C4
::Context
->dbh;
157 for (my $i=0;$i<=2;$i++) {
159 if ( @
$filters[$i] ) {
160 if (($i==1) and (@
$filters[$i-1])) {
161 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
163 $cell{filter
} .= @
$filters[$i];
164 $cell{crit
} .="Bor Cat" if ($i==0);
165 $cell{crit
} .="Without issues since" if ($i==1);
166 push @loopfilter, \
%cell;
172 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
174 $colfilter[0] = @
$filters[0] if ($column =~ /category/ ) ;
175 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
176 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
179 $colfield .= $column;
180 $colorder .= $column;
183 $strsth2 .= "select distinct $colfield FROM borrowers WHERE 1";
185 $colfilter[0] =~ s/\*/%/g;
186 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
188 $strsth2 .=" group by $colfield";
189 $strsth2 .=" order by $colorder";
192 my $sth2 = $dbh->prepare( $strsth2 );
194 while (my ($celvalue) = $sth2->fetchrow) {
197 # warn "coltitle :".$celvalue;
198 $cell{coltitle
} = $celvalue;
200 push @loopcol, \
%cell;
202 # warn "fin des titres colonnes";
208 #Initialization of cell values.....
213 for (my $i=1;$i<=$line;$i++) {
214 foreach my $col ( @loopcol ) {
215 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"Global"}=0;
221 # preparing calculation
224 # Processing calculation
225 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
226 $strcalc .= " , $colfield " if ($colfield);
227 $strcalc .= " FROM borrowers ";
228 $strcalc .= "WHERE 1 ";
229 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
230 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[0] ."'" if ( @
$filters[0] );
232 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
233 $strcalc .= " AND issues.timestamp> '" . @
$filters[1] . "'" if (@
$filters[1]);
235 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
236 $strcalc .= " AND old_issues.timestamp> '" . @
$filters[1] . "'" if (@
$filters[1]);
238 $strcalc .= " group by borrowers.borrowernumber";
239 $strcalc .= ", $colfield" if ($column);
240 $strcalc .= " order by $colfield " if ($colfield);
244 $max = $line*@loopcol;
245 } else { $max=$line;}
246 $strcalc .= " LIMIT 0,$max";
249 my $dbcalc = $dbh->prepare($strcalc);
251 # warn "filling table";
254 while (my @data = $dbcalc->fetchrow) {
255 my ($row, $col )=@data;
256 $col = "zzEMPTY" if (!defined($col));
257 $i=1 if (($previous_col) and not($col eq $previous_col));
258 $table[$i]->{$col}=$row;
259 # warn " $i $col $row";
264 push @loopcol,{coltitle
=> "Global"} if not($column);
266 $max =(($line)?
$line:@table -1);
267 for ($i=1; $i<=$max;$i++) {
269 #@loopcol ensures the order for columns is common with column titles
270 # and the number matches the number of columns
272 foreach my $col ( @loopcol ) {
275 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}};
277 $value =$table[$i]->{"zzEMPTY"};
279 push @loopcell, {value
=> $value} ;
281 push @looprow,{ 'rowtitle' => $i ,
282 'loopcell' => \
@loopcell,
288 # the header of the table
289 $globalline{loopfilter
}=\
@loopfilter;
290 # the core of the table
291 $globalline{looprow
} = \
@looprow;
292 $globalline{loopcol
} = \
@loopcol;
293 # # the foot (totals by borrower type)
294 $globalline{loopfooter
} = \
@loopfooter;
295 $globalline{total
}= $grantotal;
296 $globalline{line
} = $line;
297 $globalline{column
} = $column;
298 push @mainloop,\
%globalline;