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
28 use C4::Dates qw/format_date_in_iso/;
32 plugin that shows a stats on borrowers
41 my $do_it=$input->param('do_it');
42 my $fullreportname = "reports/borrowers_out.tmpl";
43 my $limit = $input->param("Limit");
44 my $column = $input->param("Criteria");
45 my @filters = $input->param("Filter");
46 my $output = $input->param("output");
47 my $basename = $input->param("basename");
48 my $mime = $input->param("MIME");
49 my $del = $input->param("sep");
50 my ($template, $borrowernumber, $cookie)
51 = get_template_and_user({template_name => $fullreportname,
55 flagsrequired => {reports => 1},
58 $template->param(do_it => $do_it,
59 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
63 my $results = calculate($limit, $column, \@filters);
64 if ($output eq "screen"){
65 # Printing results to screen
66 $template->param(mainloop => $results);
67 output_html_with_http_headers $input, $cookie, $template->output;
70 # Printing to a csv file
71 print $input->header(-type => 'application/vnd.sun.xml.calc',
73 -attachment=>"$basename.csv",
74 -filename=>"$basename.csv" );
75 my $cols = @$results[0]->{loopcol};
76 my $lines = @$results[0]->{looprow};
78 $sep =C4::Context->preference("delimiter");
80 print "num /". @$results[0]->{column} .$sep;
82 foreach my $col ( @$cols ) {
83 print $col->{coltitle}.$sep;
87 foreach my $line ( @$lines ) {
88 my $x = $line->{loopcell};
89 print $line->{rowtitle}.$sep;
90 foreach my $cell (@$x) {
91 print $cell->{value}.$sep;
93 print $line->{totalrow};
98 $cols = @$results[0]->{loopfooter};
99 foreach my $col ( @$cols ) {
100 print $sep.$col->{totalcol};
102 print $sep.@$results[0]->{total};
107 my $dbh = C4::Context->dbh;
113 my @mime = ( C4::Context->preference("MIME") );
114 # foreach my $mime (@mime){
118 my $CGIextChoice=CGI::scrolling_list(
125 my @dels = ( C4::Context->preference("delimiter") );
126 my $CGIsepChoice=CGI::scrolling_list(
133 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
135 foreach my $thisborcat (sort keys %$labels) {
136 my %row =(value => $thisborcat,
137 description => $labels->{$thisborcat},
139 push @borcatloop, \%row;
142 CGIextChoice => $CGIextChoice,
143 CGIsepChoice => $CGIsepChoice,
144 borcatloop =>\@borcatloop,
146 output_html_with_http_headers $input, $cookie, $template->output;
151 my ($line, $column, $filters) = @_;
160 my $dbh = C4::Context->dbh;
166 for (my $i=0;$i<=2;$i++) {
168 if ( @$filters[$i] ) {
169 if (($i==1) and (@$filters[$i-1])) {
170 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
172 $cell{filter} .= @$filters[$i];
173 $cell{crit} .="Bor Cat" if ($i==0);
174 $cell{crit} .="Without issues since" if ($i==1);
175 push @loopfilter, \%cell;
181 $column = "borrowers.".$column if $column=~/categorycode/;
183 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
184 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
185 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
188 $colfield .= $column;
189 $colorder .= $column;
192 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `old_issues` ON issues.borrowernumber=borrowers.borrowernumber";
194 $colfilter[0] =~ s/\*/%/g;
195 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
197 $strsth2 .=" group by $colfield";
198 $strsth2 .=" order by $colorder";
201 my $sth2 = $dbh->prepare( $strsth2 );
204 while (my ($celvalue) = $sth2->fetchrow) {
207 # warn "coltitle :".$celvalue;
208 $cell{coltitle} = $celvalue;
210 push @loopcol, \%cell;
212 # warn "fin des titres colonnes";
219 #Initialization of cell values.....
223 for (my $i=1;$i<=$line;$i++) {
224 foreach my $col ( @loopcol ) {
225 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
226 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
231 # preparing calculation
234 # Processing calculation
235 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
236 $strcalc .= " , $colfield " if ($colfield);
237 $strcalc .= " FROM borrowers ";
238 $strcalc .= "WHERE 1 ";
239 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
240 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
242 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM old_issues where old_issues.timestamp> @$filters[1] ";
243 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM old_issues where old_issues.timestamp> ".format_date_in_iso(@$filters[1]));
244 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
246 # $queryfilter->execute(@$filters[1]);
247 # while (my ($borrowernumber)=$queryfilter->fetchrow){
248 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
251 my $strqueryfilter="SELECT DISTINCT borrowernumber FROM old_issues ";
252 my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM old_issues ");
253 $queryfilter->execute;
254 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
255 # while (my ($borrowernumber)=$queryfilter->fetchrow){
256 # $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
259 $strcalc .= " group by borrowers.borrowernumber";
260 $strcalc .= ", $colfield" if ($column);
261 $strcalc .= " order by $colfield " if ($colfield);
264 $max = $line*@loopcol;
265 } else { $max=$line;}
266 $strcalc .= " LIMIT 0,$max" if ($line);
267 warn "SQL :". $strcalc;
269 my $dbcalc = $dbh->prepare($strcalc);
271 # warn "filling table";
274 while (my @data = $dbcalc->fetchrow) {
275 my ($row, $col )=@data;
276 $col = "zzEMPTY" if (!defined($col));
277 $i=1 if (($previous_col) and not($col eq $previous_col));
278 $table[$i]->{$col}=$row;
279 # warn " $i $col $row";
284 push @loopcol,{coltitle => "Global"} if not($column);
286 $max =(($line)?$line:@table -1);
287 for ($i=1; $i<=$max;$i++) {
289 #@loopcol ensures the order for columns is common with column titles
290 # and the number matches the number of columns
292 foreach my $col ( @loopcol ) {
295 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
297 $value =$table[$i]->{"zzEMPTY"};
299 push @loopcell, {value => $value} ;
301 push @looprow,{ 'rowtitle' => $i ,
302 'loopcell' => \@loopcell,
303 'hilighted' => ($hilighted >0),
305 $hilighted = -$hilighted;
310 # the header of the table
311 $globalline{loopfilter}=\@loopfilter;
312 # the core of the table
313 $globalline{looprow} = \@looprow;
314 $globalline{loopcol} = \@loopcol;
315 # # the foot (totals by borrower type)
316 $globalline{loopfooter} = \@loopfooter;
317 $globalline{total}= $grantotal;
318 $globalline{line} = $line;
319 $globalline{column} = $column;
320 push @mainloop,\%globalline;