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 my $mime = $input->param("MIME");
53 our $sep = $input->param("sep") || '';
54 $sep = "\t" if ($sep eq 'tabulation');
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user
({template_name
=> $fullreportname,
60 flagsrequired
=> {reports
=> '*'},
63 $template->param(do_it
=> $do_it,
64 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
68 my $results = calculate
($limit, $column, \
@filters);
69 if ($output eq "screen"){
70 # Printing results to screen
71 $template->param(mainloop
=> $results);
72 output_html_with_http_headers
$input, $cookie, $template->output;
75 # Printing to a csv file
76 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
78 -attachment
=>"$basename.csv",
79 -filename
=>"$basename.csv" );
80 my $cols = @
$results[0]->{loopcol
};
81 my $lines = @
$results[0]->{looprow
};
83 print "num /". @
$results[0]->{column
} .$sep;
85 foreach my $col ( @
$cols ) {
86 print $col->{coltitle
}.$sep;
90 foreach my $line ( @
$lines ) {
91 my $x = $line->{loopcell
};
92 print $line->{rowtitle
}.$sep;
93 foreach my $cell (@
$x) {
94 my $cellvalue = defined $cell->{value
} ?
$cell->{value
}.$sep : ''.$sep;
97 # print $line->{totalrow};
102 $cols = @
$results[0]->{loopfooter
};
103 foreach my $col ( @
$cols ) {
104 print $sep.$col->{totalcol
};
106 print $sep.@
$results[0]->{total
};
111 my $dbh = C4
::Context
->dbh;
117 my @mime = ( C4
::Context
->preference("MIME") );
118 # foreach my $mime (@mime){
122 my $CGIextChoice=CGI
::scrolling_list
(
129 my $CGIsepChoice = GetDelimiterChoices
;
131 my ($codes,$labels) = GetborCatFromCatType
(undef,undef);
133 foreach my $thisborcat (sort keys %$labels) {
134 my %row =(value
=> $thisborcat,
135 description
=> $labels->{$thisborcat},
137 push @borcatloop, \
%row;
140 CGIextChoice
=> $CGIextChoice,
141 CGIsepChoice
=> $CGIsepChoice,
142 borcatloop
=>\
@borcatloop,
144 output_html_with_http_headers
$input, $cookie, $template->output;
149 my ($line, $column, $filters) = @_;
158 my $dbh = C4
::Context
->dbh;
164 for (my $i=0;$i<=2;$i++) {
166 if ( @
$filters[$i] ) {
167 if (($i==1) and (@
$filters[$i-1])) {
168 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
170 $cell{filter
} .= @
$filters[$i];
171 $cell{crit
} .="Bor Cat" if ($i==0);
172 $cell{crit
} .="Without issues since" if ($i==1);
173 push @loopfilter, \
%cell;
179 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
181 $colfilter[0] = @
$filters[0] if ($column =~ /category/ ) ;
182 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
183 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
186 $colfield .= $column;
187 $colorder .= $column;
190 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `old_issues` USING(borrowernumber)";
192 $colfilter[0] =~ s/\*/%/g;
193 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
195 $strsth2 .=" group by $colfield";
196 $strsth2 .=" order by $colorder";
199 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] );
238 my $strqueryfilter = "SELECT DISTINCT borrowernumber FROM old_issues WHERE borrowernumber IS NOT NULL ";
240 my $strqueryfilter .= "AND old_issues.timestamp> @$filters[1] ";
242 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
243 $strcalc .= " group by borrowers.borrowernumber";
244 $strcalc .= ", $colfield" if ($column);
245 $strcalc .= " order by $colfield " if ($colfield);
249 $max = $line*@loopcol;
250 } else { $max=$line;}
251 $strcalc .= " LIMIT 0,$max";
254 my $dbcalc = $dbh->prepare($strcalc);
256 # warn "filling table";
259 while (my @data = $dbcalc->fetchrow) {
260 my ($row, $col )=@data;
261 $col = "zzEMPTY" if (!defined($col));
262 $i=1 if (($previous_col) and not($col eq $previous_col));
263 $table[$i]->{$col}=$row;
264 # warn " $i $col $row";
269 push @loopcol,{coltitle
=> "Global"} if not($column);
271 $max =(($line)?
$line:@table -1);
272 for ($i=1; $i<=$max;$i++) {
274 #@loopcol ensures the order for columns is common with column titles
275 # and the number matches the number of columns
277 foreach my $col ( @loopcol ) {
280 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}};
282 $value =$table[$i]->{"zzEMPTY"};
284 push @loopcell, {value
=> $value} ;
286 push @looprow,{ 'rowtitle' => $i ,
287 'loopcell' => \
@loopcell,
293 # the header of the table
294 $globalline{loopfilter
}=\
@loopfilter;
295 # the core of the table
296 $globalline{looprow
} = \
@looprow;
297 $globalline{loopcol
} = \
@loopcol;
298 # # the foot (totals by borrower type)
299 $globalline{loopfooter
} = \
@loopfooter;
300 $globalline{total
}= $grantotal;
301 $globalline{line
} = $line;
302 $globalline{column
} = $column;
303 push @mainloop,\
%globalline;