4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
33 plugin that shows a stats on borrowers
42 my $do_it=$input->param('do_it');
43 my $fullreportname = "reports/borrowers_out.tmpl";
44 my $limit = $input->param("Limit");
45 my $column = $input->param("Criteria");
46 my @filters = $input->param("Filter");
47 my $output = $input->param("output");
48 my $basename = $input->param("basename");
49 my $mime = $input->param("MIME");
50 my $del = $input->param("sep");
51 #warn "calcul : ".$calc;
52 my ($template, $borrowernumber, $cookie)
53 = get_template_and_user({template_name => $fullreportname,
57 flagsrequired => {reports => 1},
60 $template->param(do_it => $do_it,
61 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
65 my $results = calculate($limit, $column, \@filters);
66 if ($output eq "screen"){
67 # Printing results to screen
68 $template->param(mainloop => $results);
69 output_html_with_http_headers $input, $cookie, $template->output;
72 # Printing to a csv file
73 print $input->header(-type => 'application/vnd.sun.xml.calc',
75 -attachment=>"$basename.csv",
76 -filename=>"$basename.csv" );
77 my $cols = @$results[0]->{loopcol};
78 my $lines = @$results[0]->{looprow};
80 $sep =C4::Context->preference("delimiter");
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 print $cell->{value}.$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 @mime = ( C4::Context->preference("MIME") );
116 # foreach my $mime (@mime){
120 my $CGIextChoice=CGI::scrolling_list(
127 my @dels = ( C4::Context->preference("delimiter") );
128 my $CGIsepChoice=CGI::scrolling_list(
135 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
137 foreach my $thisborcat (sort keys %$labels) {
138 # my $selected = 1 if $thisbranch eq $branch;
139 my %row =(value => $thisborcat,
140 # selected => $selected,
141 description => $labels->{$thisborcat},
143 push @borcatloop, \%row;
146 CGIextChoice => $CGIextChoice,
147 CGIsepChoice => $CGIsepChoice,
148 borcatloop =>\@borcatloop,
150 output_html_with_http_headers $input, $cookie, $template->output;
157 my ($line, $column, $filters) = @_;
166 my $dbh = C4::Context->dbh;
172 for (my $i=0;$i<=2;$i++) {
174 if ( @$filters[$i] ) {
175 if (($i==1) and (@$filters[$i-1])) {
176 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
178 $cell{filter} .= @$filters[$i];
179 $cell{crit} .="Bor Cat" if ($i==0);
180 $cell{crit} .="Without issues since" if ($i==1);
181 push @loopfilter, \%cell;
187 $column = "borrowers.".$column if $column=~/categorycode/;
189 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
190 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
191 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
194 $colfield .= $column;
195 $colorder .= $column;
198 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `issues` ON issues.borrowernumber=borrowers.borrowernumber";
200 $colfilter[0] =~ s/\*/%/g;
201 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
203 $strsth2 .=" group by $colfield";
204 $strsth2 .=" order by $colorder";
207 my $sth2 = $dbh->prepare( $strsth2 );
212 while (my ($celvalue) = $sth2->fetchrow) {
215 # warn "coltitle :".$celvalue;
216 $cell{coltitle} = $celvalue;
218 push @loopcol, \%cell;
220 # warn "fin des titres colonnes";
227 #Initialization of cell values.....
231 for (my $i=1;$i<=$line;$i++) {
232 foreach my $col ( @loopcol ) {
233 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
234 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
239 # preparing calculation
242 # Processing calculation
243 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
244 $strcalc .= " , $colfield " if ($colfield);
245 $strcalc .= " FROM borrowers ";
246 $strcalc .= "WHERE 1 ";
247 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
248 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
250 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ";
251 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> ".format_date_in_iso(@$filters[1]));
252 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
254 # $queryfilter->execute(@$filters[1]);
255 # while (my ($borrowernumber)=$queryfilter->fetchrow){
256 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
259 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues ";
260 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues ");
261 $queryfilter->execute;
262 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
263 # while (my ($borrowernumber)=$queryfilter->fetchrow){
264 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
267 $strcalc .= " group by borrowers.borrowernumber";
268 $strcalc .= ", $colfield" if ($column);
269 $strcalc .= " order by $colfield " if ($colfield);
272 $max = $line*@loopcol;
273 } else { $max=$line;}
274 $strcalc .= " LIMIT 0,$max" if ($line);
275 warn "SQL :". $strcalc;
277 my $dbcalc = $dbh->prepare($strcalc);
279 # warn "filling table";
282 while (my @data = $dbcalc->fetchrow) {
283 my ($row, $col )=@data;
284 $col = "zzEMPTY" if ($col eq undef);
285 $i=1 if (($previous_col) and not($col eq $previous_col));
286 $table[$i]->{$col}=$row;
287 # warn " ".$i." ".$col. " ".$row;
292 push @loopcol,{coltitle => "Global"} if not($column);
294 my $max =(($line)?$line:@table -1);
295 for ($i=1; $i<=$max;$i++) {
297 #@loopcol ensures the order for columns is common with column titles
298 # and the number matches the number of columns
300 foreach my $col ( @loopcol ) {
303 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
305 $value =$table[$i]->{"zzEMPTY"};
307 push @loopcell, {value => $value} ;
309 push @looprow,{ 'rowtitle' => $i ,
310 'loopcell' => \@loopcell,
311 'hilighted' => ($hilighted >0),
313 $hilighted = -$hilighted;
318 # the header of the table
319 $globalline{loopfilter}=\@loopfilter;
320 # the core of the table
321 $globalline{looprow} = \@looprow;
322 $globalline{loopcol} = \@loopcol;
323 # # the foot (totals by borrower type)
324 $globalline{loopfooter} = \@loopfooter;
325 $globalline{total}= $grantotal;
326 $globalline{line} = $line;
327 $globalline{column} = $column;
328 push @mainloop,\%globalline;