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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
31 use C4
::Dates qw
/format_date_in_iso/;
35 reports/borrowers_out.pl
39 Plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/borrowers_out.tt";
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,
66 my $results = calculate
($limit, $column, \
@filters);
67 if ($output eq "screen"){
68 # Printing results to screen
69 $template->param(mainloop
=> $results);
70 output_html_with_http_headers
$input, $cookie, $template->output;
73 # Printing to a csv file
74 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
76 -attachment
=>"$basename.csv",
77 -filename
=>"$basename.csv" );
78 my $cols = @
$results[0]->{loopcol
};
79 my $lines = @
$results[0]->{looprow
};
81 print "num /". @
$results[0]->{column
} .$sep;
83 foreach my $col ( @
$cols ) {
84 print $col->{coltitle
}.$sep;
88 foreach my $line ( @
$lines ) {
89 my $x = $line->{loopcell
};
90 print $line->{rowtitle
}.$sep;
91 foreach my $cell (@
$x) {
92 my $cellvalue = defined $cell->{value
} ?
$cell->{value
}.$sep : ''.$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 $CGIextChoice = ( 'CSV' ); # FIXME translation
116 my $CGIsepChoice = GetDelimiterChoices
;
118 my ($codes,$labels) = GetborCatFromCatType
(undef,undef);
120 foreach my $thisborcat (sort keys %$labels) {
121 my %row =(value
=> $thisborcat,
122 description
=> $labels->{$thisborcat},
124 push @borcatloop, \
%row;
127 CGIextChoice
=> $CGIextChoice,
128 CGIsepChoice
=> $CGIsepChoice,
129 borcatloop
=>\
@borcatloop,
131 output_html_with_http_headers
$input, $cookie, $template->output;
136 my ($line, $column, $filters) = @_;
145 my $dbh = C4
::Context
->dbh;
151 for (my $i=0;$i<=2;$i++) {
153 if ( @
$filters[$i] ) {
154 if (($i==1) and (@
$filters[$i-1])) {
155 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
157 $cell{filter
} .= @
$filters[$i];
158 $cell{crit
} .="Bor Cat" if ($i==0);
159 $cell{crit
} .="Without issues since" if ($i==1);
160 push @loopfilter, \
%cell;
166 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
168 $colfilter[0] = @
$filters[0] if ($column =~ /category/ ) ;
169 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
170 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
173 $colfield .= $column;
174 $colorder .= $column;
177 $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
179 if ( $colfilter[0] ) {
180 $colfilter[0] =~ s/\*/%/g;
181 $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
182 push @query_args, $colfilter[0];
184 $strsth2 .=" group by " . $dbh->quote($colfield);
185 $strsth2 .=" order by " . $dbh->quote($colorder);
188 my $sth2 = $dbh->prepare( $strsth2 );
189 $sth2->execute( @query_args );
190 while (my ($celvalue) = $sth2->fetchrow) {
193 # warn "coltitle :".$celvalue;
194 $cell{coltitle
} = $celvalue;
196 push @loopcol, \
%cell;
198 # warn "fin des titres colonnes";
204 #Initialization of cell values.....
209 for (my $i=1;$i<=$line;$i++) {
210 foreach my $col ( @loopcol ) {
211 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"Global"}=0;
217 # preparing calculation
220 # Processing calculation
221 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
222 $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
223 $strcalc .= " FROM borrowers ";
224 $strcalc .= "WHERE 1 ";
226 if ( @
$filters[0] ) {
227 @
$filters[0]=~ s/\*/%/g;
228 $strcalc .= " AND borrowers.categorycode like ?";
229 push @query_args, @
$filters[0];
231 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
232 if ( @
$filters[1] ) {
233 $strcalc .= " AND issues.timestamap > ?";
234 push @query_args, @
$filters[1];
237 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
238 if ( @
$filters[1] ) {
239 $strcalc .= " AND old_issues.timestamp > ?";
240 push @query_args, @
$filters[1];
243 $strcalc .= " group by borrowers.borrowernumber";
244 $strcalc .= ", " . $dbh->quote($colfield) if ($column);
245 $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
249 $max = $line*@loopcol;
250 } else { $max=$line;}
251 $strcalc .= " LIMIT 0,$max";
254 my $dbcalc = $dbh->prepare($strcalc);
255 $dbcalc->execute( @query_args );
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;