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>.
32 use Koha::Patron::Categories;
36 reports/borrowers_out.pl
40 Plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/borrowers_out.tt";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->multi_param("Filter");
50 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 our $sep = $input->param("sep") || '';
56 $sep = "\t" if ($sep eq 'tabulation');
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
61 flagsrequired => {reports => '*'},
63 $template->param(do_it => $do_it,
67 my $results = calculate($limit, $column, \@filters);
68 if ($output eq "screen"){
69 # Printing results to screen
70 $template->param(mainloop => $results);
71 output_html_with_http_headers $input, $cookie, $template->output;
74 # Printing to a csv file
75 print $input->header(-type => 'application/vnd.sun.xml.calc',
77 -attachment=>"$basename.csv",
78 -filename=>"$basename.csv" );
79 my $cols = @$results[0]->{loopcol};
80 my $lines = @$results[0]->{looprow};
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 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
96 # print $line->{totalrow};
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
110 my $dbh = C4::Context->dbh;
112 my $CGIextChoice = ( 'CSV' ); # FIXME translation
113 my $CGIsepChoice = GetDelimiterChoices;
115 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['categorycode']});
117 CGIextChoice => $CGIextChoice,
118 CGIsepChoice => $CGIsepChoice,
119 patron_categories => $patron_categories,
121 output_html_with_http_headers $input, $cookie, $template->output;
126 my ($line, $column, $filters) = @_;
134 my $dbh = C4::Context->dbh;
140 for (my $i=0;$i<=2;$i++) {
142 if ( @$filters[$i] ) {
143 if (($i==1) and (@$filters[$i-1])) {
144 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
146 $cell{filter} .= @$filters[$i];
147 $cell{crit} .="Bor Cat" if ($i==0);
148 $cell{crit} .="Without issues since" if ($i==1);
149 push @loopfilter, \%cell;
155 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
157 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
158 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
159 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
162 $colfield .= $column;
163 $colorder .= $column;
166 $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
168 if ( $colfilter[0] ) {
169 $colfilter[0] =~ s/\*/%/g;
170 $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
171 push @query_args, $colfilter[0];
173 $strsth2 .=" group by " . $dbh->quote($colfield);
174 $strsth2 .=" order by " . $dbh->quote($colorder);
177 my $sth2 = $dbh->prepare( $strsth2 );
178 $sth2->execute( @query_args );
179 while (my ($celvalue) = $sth2->fetchrow) {
182 # warn "coltitle :".$celvalue;
183 $cell{coltitle} = $celvalue;
185 push @loopcol, \%cell;
187 # warn "fin des titres colonnes";
193 #Initialization of cell values.....
198 for (my $i=1;$i<=$line;$i++) {
199 foreach my $col ( @loopcol ) {
200 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
206 # preparing calculation
209 # Processing calculation
210 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
211 $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
212 $strcalc .= " FROM borrowers ";
213 $strcalc .= "WHERE 1 ";
215 if ( @$filters[0] ) {
216 @$filters[0]=~ s/\*/%/g;
217 $strcalc .= " AND borrowers.categorycode like ?";
218 push @query_args, @$filters[0];
220 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
221 if ( @$filters[1] ) {
222 $strcalc .= " AND issues.timestamp > ?";
223 push @query_args, @$filters[1];
226 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
227 if ( @$filters[1] ) {
228 $strcalc .= " AND old_issues.timestamp > ?";
229 push @query_args, @$filters[1];
232 $strcalc .= " group by borrowers.borrowernumber";
233 $strcalc .= ", " . $dbh->quote($colfield) if ($column);
234 $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
238 $max = $line*@loopcol;
239 } else { $max=$line;}
240 $strcalc .= " LIMIT 0,$max";
243 my $dbcalc = $dbh->prepare($strcalc);
244 $dbcalc->execute( @query_args );
245 # warn "filling table";
248 while (my @data = $dbcalc->fetchrow) {
249 my ($row, $col )=@data;
250 $col = "zzEMPTY" if (!defined($col));
251 $i=1 if (($previous_col) and not($col eq $previous_col));
252 $table[$i]->{$col}=$row;
253 # warn " $i $col $row";
258 push @loopcol,{coltitle => "Global"} if not($column);
260 $max =(($line)?$line:@table -1);
261 for ($i=1; $i<=$max;$i++) {
263 #@loopcol ensures the order for columns is common with column titles
264 # and the number matches the number of columns
266 foreach my $col ( @loopcol ) {
269 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
271 $value =$table[$i]->{"zzEMPTY"};
273 push @loopcell, {value => $value} ;
275 push @looprow,{ 'rowtitle' => $i ,
276 'loopcell' => \@loopcell,
282 # the header of the table
283 $globalline{loopfilter}=\@loopfilter;
284 # the core of the table
285 $globalline{looprow} = \@looprow;
286 $globalline{loopcol} = \@loopcol;
287 # # the foot (totals by borrower type)
288 $globalline{loopfooter} = \@loopfooter;
289 $globalline{total}= $grantotal;
290 $globalline{line} = $line;
291 $globalline{column} = $column;
292 push @mainloop,\%globalline;