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>.
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] = eval { output_pref
( { dt
=> dt_from_string
( $filters[1]), dateonly
=> 1, dateformat
=> 'iso' } ); }
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 our $sep = $input->param("sep") || '';
55 $sep = "\t" if ($sep eq 'tabulation');
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user
({template_name
=> $fullreportname,
61 flagsrequired
=> {reports
=> '*'},
64 $template->param(do_it
=> $do_it,
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 $CGIextChoice = ( 'CSV' ); # FIXME translation
118 my $CGIsepChoice = GetDelimiterChoices
;
120 my ($codes,$labels) = GetborCatFromCatType
(undef,undef);
122 foreach my $thisborcat (sort keys %$labels) {
123 my %row =(value
=> $thisborcat,
124 description
=> $labels->{$thisborcat},
126 push @borcatloop, \
%row;
129 CGIextChoice
=> $CGIextChoice,
130 CGIsepChoice
=> $CGIsepChoice,
131 borcatloop
=>\
@borcatloop,
133 output_html_with_http_headers
$input, $cookie, $template->output;
138 my ($line, $column, $filters) = @_;
147 my $dbh = C4
::Context
->dbh;
153 for (my $i=0;$i<=2;$i++) {
155 if ( @
$filters[$i] ) {
156 if (($i==1) and (@
$filters[$i-1])) {
157 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
159 $cell{filter
} .= @
$filters[$i];
160 $cell{crit
} .="Bor Cat" if ($i==0);
161 $cell{crit
} .="Without issues since" if ($i==1);
162 push @loopfilter, \
%cell;
168 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
170 $colfilter[0] = @
$filters[0] if ($column =~ /category/ ) ;
171 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
172 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
175 $colfield .= $column;
176 $colorder .= $column;
179 $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
181 if ( $colfilter[0] ) {
182 $colfilter[0] =~ s/\*/%/g;
183 $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
184 push @query_args, $colfilter[0];
186 $strsth2 .=" group by " . $dbh->quote($colfield);
187 $strsth2 .=" order by " . $dbh->quote($colorder);
190 my $sth2 = $dbh->prepare( $strsth2 );
191 $sth2->execute( @query_args );
192 while (my ($celvalue) = $sth2->fetchrow) {
195 # warn "coltitle :".$celvalue;
196 $cell{coltitle
} = $celvalue;
198 push @loopcol, \
%cell;
200 # warn "fin des titres colonnes";
206 #Initialization of cell values.....
211 for (my $i=1;$i<=$line;$i++) {
212 foreach my $col ( @loopcol ) {
213 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"Global"}=0;
219 # preparing calculation
222 # Processing calculation
223 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
224 $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
225 $strcalc .= " FROM borrowers ";
226 $strcalc .= "WHERE 1 ";
228 if ( @
$filters[0] ) {
229 @
$filters[0]=~ s/\*/%/g;
230 $strcalc .= " AND borrowers.categorycode like ?";
231 push @query_args, @
$filters[0];
233 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
234 if ( @
$filters[1] ) {
235 $strcalc .= " AND issues.timestamp > ?";
236 push @query_args, @
$filters[1];
239 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
240 if ( @
$filters[1] ) {
241 $strcalc .= " AND old_issues.timestamp > ?";
242 push @query_args, @
$filters[1];
245 $strcalc .= " group by borrowers.borrowernumber";
246 $strcalc .= ", " . $dbh->quote($colfield) if ($column);
247 $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
251 $max = $line*@loopcol;
252 } else { $max=$line;}
253 $strcalc .= " LIMIT 0,$max";
256 my $dbcalc = $dbh->prepare($strcalc);
257 $dbcalc->execute( @query_args );
258 # warn "filling table";
261 while (my @data = $dbcalc->fetchrow) {
262 my ($row, $col )=@data;
263 $col = "zzEMPTY" if (!defined($col));
264 $i=1 if (($previous_col) and not($col eq $previous_col));
265 $table[$i]->{$col}=$row;
266 # warn " $i $col $row";
271 push @loopcol,{coltitle
=> "Global"} if not($column);
273 $max =(($line)?
$line:@table -1);
274 for ($i=1; $i<=$max;$i++) {
276 #@loopcol ensures the order for columns is common with column titles
277 # and the number matches the number of columns
279 foreach my $col ( @loopcol ) {
282 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}};
284 $value =$table[$i]->{"zzEMPTY"};
286 push @loopcell, {value
=> $value} ;
288 push @looprow,{ 'rowtitle' => $i ,
289 'loopcell' => \
@loopcell,
295 # the header of the table
296 $globalline{loopfilter
}=\
@loopfilter;
297 # the core of the table
298 $globalline{looprow
} = \
@looprow;
299 $globalline{loopcol
} = \
@loopcol;
300 # # the foot (totals by borrower type)
301 $globalline{loopfooter
} = \
@loopfooter;
302 $globalline{total
}= $grantotal;
303 $globalline{line
} = $line;
304 $globalline{column
} = $column;
305 push @mainloop,\
%globalline;