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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 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 my $output = $input->param("output");
50 my $basename = $input->param("basename");
51 my $mime = $input->param("MIME");
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 @mime = ( C4::Context->preference("MIME") );
117 # foreach my $mime (@mime){
121 my $CGIextChoice=CGI::scrolling_list(
128 my $CGIsepChoice = GetDelimiterChoices;
130 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
132 foreach my $thisborcat (sort keys %$labels) {
133 my %row =(value => $thisborcat,
134 description => $labels->{$thisborcat},
136 push @borcatloop, \%row;
139 CGIextChoice => $CGIextChoice,
140 CGIsepChoice => $CGIsepChoice,
141 borcatloop =>\@borcatloop,
143 output_html_with_http_headers $input, $cookie, $template->output;
148 my ($line, $column, $filters) = @_;
157 my $dbh = C4::Context->dbh;
163 for (my $i=0;$i<=2;$i++) {
165 if ( @$filters[$i] ) {
166 if (($i==1) and (@$filters[$i-1])) {
167 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
169 $cell{filter} .= @$filters[$i];
170 $cell{crit} .="Bor Cat" if ($i==0);
171 $cell{crit} .="Without issues since" if ($i==1);
172 push @loopfilter, \%cell;
178 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
180 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
181 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
182 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
185 $colfield .= $column;
186 $colorder .= $column;
189 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `old_issues` ON old_issues.borrowernumber=borrowers.borrowernumber";
191 $colfilter[0] =~ s/\*/%/g;
192 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
194 $strsth2 .=" group by $colfield";
195 $strsth2 .=" order by $colorder";
198 my $sth2 = $dbh->prepare( $strsth2 );
201 while (my ($celvalue) = $sth2->fetchrow) {
204 # warn "coltitle :".$celvalue;
205 $cell{coltitle} = $celvalue;
207 push @loopcol, \%cell;
209 # warn "fin des titres colonnes";
215 #Initialization of cell values.....
220 for (my $i=1;$i<=$line;$i++) {
221 foreach my $col ( @loopcol ) {
222 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
228 # preparing calculation
231 # Processing calculation
232 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
233 $strcalc .= " , $colfield " if ($colfield);
234 $strcalc .= " FROM borrowers ";
235 $strcalc .= "WHERE 1 ";
236 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
237 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
239 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM old_issues where old_issues.timestamp> @$filters[1] ";
240 # my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM old_issues where old_issues.timestamp> ".format_date_in_iso(@$filters[1]));
241 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
243 # $queryfilter->execute(@$filters[1]);
244 # while (my ($borrowernumber)=$queryfilter->fetchrow){
245 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
248 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM old_issues ";
249 # my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM old_issues ");
250 # $queryfilter->execute;
251 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
252 # while (my ($borrowernumber)=$queryfilter->fetchrow){
253 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
256 $strcalc .= " group by borrowers.borrowernumber";
257 $strcalc .= ", $colfield" if ($column);
258 $strcalc .= " order by $colfield " if ($colfield);
262 $max = $line*@loopcol;
263 } else { $max=$line;}
264 $strcalc .= " LIMIT 0,$max";
266 # warn "SQL :". $strcalc;
268 my $dbcalc = $dbh->prepare($strcalc);
270 # warn "filling table";
273 while (my @data = $dbcalc->fetchrow) {
274 my ($row, $col )=@data;
275 $col = "zzEMPTY" if (!defined($col));
276 $i=1 if (($previous_col) and not($col eq $previous_col));
277 $table[$i]->{$col}=$row;
278 # warn " $i $col $row";
283 push @loopcol,{coltitle => "Global"} if not($column);
285 $max =(($line)?$line:@table -1);
286 for ($i=1; $i<=$max;$i++) {
288 #@loopcol ensures the order for columns is common with column titles
289 # and the number matches the number of columns
291 foreach my $col ( @loopcol ) {
294 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
296 $value =$table[$i]->{"zzEMPTY"};
298 push @loopcell, {value => $value} ;
300 push @looprow,{ 'rowtitle' => $i ,
301 'loopcell' => \@loopcell,
307 # the header of the table
308 $globalline{loopfilter}=\@loopfilter;
309 # the core of the table
310 $globalline{looprow} = \@looprow;
311 $globalline{loopcol} = \@loopcol;
312 # # the foot (totals by borrower type)
313 $globalline{loopfooter} = \@loopfooter;
314 $globalline{total}= $grantotal;
315 $globalline{line} = $line;
316 $globalline{column} = $column;
317 push @mainloop,\%globalline;