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>.
23 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Reports qw( GetDelimiterChoices );
28 use Koha::Patron::Categories;
32 reports/borrowers_out.pl
36 Plugin that shows a stats on borrowers
41 my $do_it=$input->param('do_it');
42 my $fullreportname = "reports/borrowers_out.tt";
43 my $limit = $input->param("Limit");
44 my $column = $input->param("Criteria");
45 my @filters = $input->multi_param("Filter");
47 my $output = $input->param("output");
48 my $basename = $input->param("basename");
49 our $sep = C4::Context->csv_delimiter(scalar $input->param("sep"));
50 my ($template, $borrowernumber, $cookie)
51 = get_template_and_user({template_name => $fullreportname,
54 flagsrequired => {reports => '*'},
56 $template->param(do_it => $do_it,
60 my $results = calculate($limit, $column, \@filters);
61 if ($output eq "screen"){
62 # Printing results to screen
63 $template->param(mainloop => $results);
64 output_html_with_http_headers $input, $cookie, $template->output;
67 # Printing to a csv file
68 print $input->header(-type => 'application/vnd.sun.xml.calc',
70 -attachment=>"$basename.csv",
71 -filename=>"$basename.csv" );
72 my $cols = @$results[0]->{loopcol};
73 my $lines = @$results[0]->{looprow};
75 print "num /". @$results[0]->{column} .$sep;
77 foreach my $col ( @$cols ) {
78 print $col->{coltitle}.$sep;
82 foreach my $line ( @$lines ) {
83 my $x = $line->{loopcell};
84 print $line->{rowtitle}.$sep;
85 foreach my $cell (@$x) {
86 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
89 # print $line->{totalrow};
94 $cols = @$results[0]->{loopfooter};
95 foreach my $col ( @$cols ) {
96 print $sep.$col->{totalcol};
98 print $sep.@$results[0]->{total};
103 my $dbh = C4::Context->dbh;
105 my $CGIextChoice = ( 'CSV' ); # FIXME translation
106 my $CGIsepChoice = GetDelimiterChoices;
108 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['categorycode']});
110 CGIextChoice => $CGIextChoice,
111 CGIsepChoice => $CGIsepChoice,
112 patron_categories => $patron_categories,
114 output_html_with_http_headers $input, $cookie, $template->output;
119 my ($line, $column, $filters) = @_;
127 my $dbh = C4::Context->dbh;
133 for (my $i=0;$i<=2;$i++) {
135 if ( @$filters[$i] ) {
136 if (($i==1) and (@$filters[$i-1])) {
137 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
139 $cell{filter} .= @$filters[$i];
140 $cell{crit} .="Bor Cat" if ($i==0);
141 $cell{crit} .="Without issues since" if ($i==1);
142 push @loopfilter, \%cell;
148 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
150 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
151 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
152 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
155 $colfield .= $column;
156 $colorder .= $column;
159 $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
161 if ( $colfilter[0] ) {
162 $colfilter[0] =~ s/\*/%/g;
163 $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
164 push @query_args, $colfilter[0];
166 $strsth2 .=" group by " . $dbh->quote($colfield);
167 $strsth2 .=" order by " . $dbh->quote($colorder);
170 my $sth2 = $dbh->prepare( $strsth2 );
171 $sth2->execute( @query_args );
172 while (my ($celvalue) = $sth2->fetchrow) {
175 # warn "coltitle :".$celvalue;
176 $cell{coltitle} = $celvalue;
178 push @loopcol, \%cell;
180 # warn "fin des titres colonnes";
186 #Initialization of cell values.....
191 for (my $i=1;$i<=$line;$i++) {
192 foreach my $col ( @loopcol ) {
193 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
199 # preparing calculation
202 # Processing calculation
203 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
204 $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
205 $strcalc .= " FROM borrowers ";
206 $strcalc .= "WHERE 1 ";
207 if(C4::Context->preference('IndependentBranches') && !C4::Context->IsSuperLibrarian()){
208 $strcalc .= "AND branchcode = '".C4::Context->userenv->{branch}."' ";
213 if ( @$filters[0] ) {
214 @$filters[0]=~ s/\*/%/g;
215 $strcalc .= " AND borrowers.categorycode like ?";
216 push @query_args, @$filters[0];
218 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
219 if ( @$filters[1] ) {
220 $strcalc .= " AND issues.timestamp > ?";
221 push @query_args, @$filters[1];
224 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
225 if ( @$filters[1] ) {
226 $strcalc .= " AND old_issues.timestamp > ?";
227 push @query_args, @$filters[1];
230 $strcalc .= " group by borrowers.borrowernumber";
231 $strcalc .= ", " . $dbh->quote($colfield) if ($column);
232 $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
236 $max = $line*@loopcol;
237 } else { $max=$line;}
238 $strcalc .= " LIMIT 0,$max";
241 my $dbcalc = $dbh->prepare($strcalc);
242 $dbcalc->execute( @query_args );
243 # warn "filling table";
246 while (my @data = $dbcalc->fetchrow) {
247 my ($row, $col )=@data;
248 $col = "zzEMPTY" if (!defined($col));
249 $i=1 if (($previous_col) and not($col eq $previous_col));
250 $table[$i]->{$col}=$row;
251 # warn " $i $col $row";
256 push @loopcol,{coltitle => "Global"} if not($column);
258 $max =(($line)?$line:@table -1);
259 for ($i=1; $i<=$max;$i++) {
261 #@loopcol ensures the order for columns is common with column titles
262 # and the number matches the number of columns
264 foreach my $col ( @loopcol ) {
267 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
269 $value =$table[$i]->{"zzEMPTY"};
271 push @loopcell, {value => $value} ;
273 push @looprow,{ 'rowtitle' => $i ,
274 'loopcell' => \@loopcell,
280 # the header of the table
281 $globalline{loopfilter}=\@loopfilter;
282 # the core of the table
283 $globalline{looprow} = \@looprow;
284 $globalline{loopcol} = \@loopcol;
285 # # the foot (totals by borrower type)
286 $globalline{loopfooter} = \@loopfooter;
287 $globalline{total}= $grantotal;
288 $globalline{line} = $line;
289 $globalline{column} = $column;
290 push @mainloop,\%globalline;