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
=> '*'},
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;
113 my $CGIextChoice = ( 'CSV' ); # FIXME translation
114 my $CGIsepChoice = GetDelimiterChoices
;
116 my $patron_categories = Koha
::Patron
::Categories
->search_limited({}, {order_by
=> ['categorycode']});
118 CGIextChoice
=> $CGIextChoice,
119 CGIsepChoice
=> $CGIsepChoice,
120 patron_categories
=> $patron_categories,
122 output_html_with_http_headers
$input, $cookie, $template->output;
127 my ($line, $column, $filters) = @_;
135 my $dbh = C4
::Context
->dbh;
141 for (my $i=0;$i<=2;$i++) {
143 if ( @
$filters[$i] ) {
144 if (($i==1) and (@
$filters[$i-1])) {
145 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
147 $cell{filter
} .= @
$filters[$i];
148 $cell{crit
} .="Bor Cat" if ($i==0);
149 $cell{crit
} .="Without issues since" if ($i==1);
150 push @loopfilter, \
%cell;
156 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
158 $colfilter[0] = @
$filters[0] if ($column =~ /category/ ) ;
159 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
160 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
163 $colfield .= $column;
164 $colorder .= $column;
167 $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
169 if ( $colfilter[0] ) {
170 $colfilter[0] =~ s/\*/%/g;
171 $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
172 push @query_args, $colfilter[0];
174 $strsth2 .=" group by " . $dbh->quote($colfield);
175 $strsth2 .=" order by " . $dbh->quote($colorder);
178 my $sth2 = $dbh->prepare( $strsth2 );
179 $sth2->execute( @query_args );
180 while (my ($celvalue) = $sth2->fetchrow) {
183 # warn "coltitle :".$celvalue;
184 $cell{coltitle
} = $celvalue;
186 push @loopcol, \
%cell;
188 # warn "fin des titres colonnes";
194 #Initialization of cell values.....
199 for (my $i=1;$i<=$line;$i++) {
200 foreach my $col ( @loopcol ) {
201 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"Global"}=0;
207 # preparing calculation
210 # Processing calculation
211 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
212 $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
213 $strcalc .= " FROM borrowers ";
214 $strcalc .= "WHERE 1 ";
216 if ( @
$filters[0] ) {
217 @
$filters[0]=~ s/\*/%/g;
218 $strcalc .= " AND borrowers.categorycode like ?";
219 push @query_args, @
$filters[0];
221 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
222 if ( @
$filters[1] ) {
223 $strcalc .= " AND issues.timestamp > ?";
224 push @query_args, @
$filters[1];
227 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
228 if ( @
$filters[1] ) {
229 $strcalc .= " AND old_issues.timestamp > ?";
230 push @query_args, @
$filters[1];
233 $strcalc .= " group by borrowers.borrowernumber";
234 $strcalc .= ", " . $dbh->quote($colfield) if ($column);
235 $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
239 $max = $line*@loopcol;
240 } else { $max=$line;}
241 $strcalc .= " LIMIT 0,$max";
244 my $dbcalc = $dbh->prepare($strcalc);
245 $dbcalc->execute( @query_args );
246 # warn "filling table";
249 while (my @data = $dbcalc->fetchrow) {
250 my ($row, $col )=@data;
251 $col = "zzEMPTY" if (!defined($col));
252 $i=1 if (($previous_col) and not($col eq $previous_col));
253 $table[$i]->{$col}=$row;
254 # warn " $i $col $row";
259 push @loopcol,{coltitle
=> "Global"} if not($column);
261 $max =(($line)?
$line:@table -1);
262 for ($i=1; $i<=$max;$i++) {
264 #@loopcol ensures the order for columns is common with column titles
265 # and the number matches the number of columns
267 foreach my $col ( @loopcol ) {
270 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}};
272 $value =$table[$i]->{"zzEMPTY"};
274 push @loopcell, {value
=> $value} ;
276 push @looprow,{ 'rowtitle' => $i ,
277 'loopcell' => \
@loopcell,
283 # the header of the table
284 $globalline{loopfilter
}=\
@loopfilter;
285 # the core of the table
286 $globalline{looprow
} = \
@looprow;
287 $globalline{loopcol
} = \
@loopcol;
288 # # the foot (totals by borrower type)
289 $globalline{loopfooter
} = \
@loopfooter;
290 $globalline{total
}= $grantotal;
291 $globalline{line
} = $line;
292 $globalline{column
} = $column;
293 push @mainloop,\
%globalline;