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 distinct $colfield FROM borrowers WHERE 1";
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] );
233 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
234 $strcalc .= " AND issues.timestamp> '" . @
$filters[1] . "'" if (@
$filters[1]);
236 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
237 $strcalc .= " AND old_issues.timestamp> '" . @
$filters[1] . "'" if (@
$filters[1]);
239 $strcalc .= " group by borrowers.borrowernumber";
240 $strcalc .= ", $colfield" if ($column);
241 $strcalc .= " order by $colfield " if ($colfield);
245 $max = $line*@loopcol;
246 } else { $max=$line;}
247 $strcalc .= " LIMIT 0,$max";
250 my $dbcalc = $dbh->prepare($strcalc);
252 # warn "filling table";
255 while (my @data = $dbcalc->fetchrow) {
256 my ($row, $col )=@data;
257 $col = "zzEMPTY" if (!defined($col));
258 $i=1 if (($previous_col) and not($col eq $previous_col));
259 $table[$i]->{$col}=$row;
260 # warn " $i $col $row";
265 push @loopcol,{coltitle
=> "Global"} if not($column);
267 $max =(($line)?
$line:@table -1);
268 for ($i=1; $i<=$max;$i++) {
270 #@loopcol ensures the order for columns is common with column titles
271 # and the number matches the number of columns
273 foreach my $col ( @loopcol ) {
276 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}};
278 $value =$table[$i]->{"zzEMPTY"};
280 push @loopcell, {value
=> $value} ;
282 push @looprow,{ 'rowtitle' => $i ,
283 'loopcell' => \
@loopcell,
289 # the header of the table
290 $globalline{loopfilter
}=\
@loopfilter;
291 # the core of the table
292 $globalline{looprow
} = \
@looprow;
293 $globalline{loopcol
} = \
@loopcol;
294 # # the foot (totals by borrower type)
295 $globalline{loopfooter
} = \
@loopfooter;
296 $globalline{total
}= $grantotal;
297 $globalline{line
} = $line;
298 $globalline{column
} = $column;
299 push @mainloop,\
%globalline;