Bug 24157: Add new method Acq::Invoice::Adjustement->fund
[koha.git] / reports / borrowers_out.pl
blob31ba73aa8b5758e544ab839d6ef57b86b8ac6373
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Circulation;
28 use C4::Reports;
29 use C4::Members;
31 use Koha::DateUtils;
32 use Koha::Patron::Categories;
34 =head1 NAME
36 reports/borrowers_out.pl
38 =head1 DESCRIPTION
40 Plugin that shows a stats on borrowers
42 =cut
44 my $input = new CGI;
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' } ); }
51 if ( $filters[1] );
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,
59 query => $input,
60 type => "intranet",
61 authnotrequired => 0,
62 flagsrequired => {reports => '*'},
63 debug => 1,
64 });
65 $template->param(do_it => $do_it,
67 if ($do_it) {
68 # Displaying results
69 my $results = calculate($limit, $column, \@filters);
70 if ($output eq "screen"){
71 # Printing results to screen
72 $template->param(mainloop => $results);
73 output_html_with_http_headers $input, $cookie, $template->output;
74 exit;
75 } else {
76 # Printing to a csv file
77 print $input->header(-type => 'application/vnd.sun.xml.calc',
78 -encoding => 'utf-8',
79 -attachment=>"$basename.csv",
80 -filename=>"$basename.csv" );
81 my $cols = @$results[0]->{loopcol};
82 my $lines = @$results[0]->{looprow};
83 # header top-right
84 print "num /". @$results[0]->{column} .$sep;
85 # Other header
86 foreach my $col ( @$cols ) {
87 print $col->{coltitle}.$sep;
89 print "Total\n";
90 # Table
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
96 print $cellvalue;
98 # print $line->{totalrow};
99 print "\n";
101 # footer
102 print "TOTAL";
103 $cols = @$results[0]->{loopfooter};
104 foreach my $col ( @$cols ) {
105 print $sep.$col->{totalcol};
107 print $sep.@$results[0]->{total};
108 exit;
110 # Displaying choices
111 } else {
112 my $dbh = C4::Context->dbh;
114 my $CGIextChoice = ( 'CSV' ); # FIXME translation
115 my $CGIsepChoice = GetDelimiterChoices;
117 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
118 $template->param(
119 CGIextChoice => $CGIextChoice,
120 CGIsepChoice => $CGIsepChoice,
121 patron_categories => $patron_categories,
123 output_html_with_http_headers $input, $cookie, $template->output;
127 sub calculate {
128 my ($line, $column, $filters) = @_;
129 my @mainloop;
130 my @loopfooter;
131 my @loopcol;
132 my @looprow;
133 my %globalline;
134 my $grantotal =0;
135 # extract parameters
136 my $dbh = C4::Context->dbh;
138 # Filters
139 # Checking filters
141 my @loopfilter;
142 for (my $i=0;$i<=2;$i++) {
143 my %cell;
144 if ( @$filters[$i] ) {
145 if (($i==1) and (@$filters[$i-1])) {
146 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
148 $cell{filter} .= @$filters[$i];
149 $cell{crit} .="Bor Cat" if ($i==0);
150 $cell{crit} .="Without issues since" if ($i==1);
151 push @loopfilter, \%cell;
154 my $colfield;
155 my $colorder;
156 if ($column){
157 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
158 my @colfilter ;
159 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
160 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
161 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
163 # loop cols.
164 $colfield .= $column;
165 $colorder .= $column;
167 my $strsth2;
168 $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
169 my @query_args;
170 if ( $colfilter[0] ) {
171 $colfilter[0] =~ s/\*/%/g;
172 $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
173 push @query_args, $colfilter[0];
175 $strsth2 .=" group by " . $dbh->quote($colfield);
176 $strsth2 .=" order by " . $dbh->quote($colorder);
177 # warn "". $strsth2;
179 my $sth2 = $dbh->prepare( $strsth2 );
180 $sth2->execute( @query_args );
181 while (my ($celvalue) = $sth2->fetchrow) {
182 my %cell;
183 # my %ft;
184 # warn "coltitle :".$celvalue;
185 $cell{coltitle} = $celvalue;
186 # $ft{totalcol} = 0;
187 push @loopcol, \%cell;
189 # warn "fin des titres colonnes";
192 my $i=0;
193 # my @totalcol;
195 #Initialization of cell values.....
196 my @table;
198 # warn "init table";
199 if($line) {
200 for (my $i=1;$i<=$line;$i++) {
201 foreach my $col ( @loopcol ) {
202 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
208 # preparing calculation
209 my $strcalc ;
211 # Processing calculation
212 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
213 $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
214 $strcalc .= " FROM borrowers ";
215 $strcalc .= "WHERE 1 ";
216 my @query_args;
217 if ( @$filters[0] ) {
218 @$filters[0]=~ s/\*/%/g;
219 $strcalc .= " AND borrowers.categorycode like ?";
220 push @query_args, @$filters[0];
222 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
223 if ( @$filters[1] ) {
224 $strcalc .= " AND issues.timestamp > ?";
225 push @query_args, @$filters[1];
227 $strcalc .= ") ";
228 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
229 if ( @$filters[1] ) {
230 $strcalc .= " AND old_issues.timestamp > ?";
231 push @query_args, @$filters[1];
233 $strcalc .= ") ";
234 $strcalc .= " group by borrowers.borrowernumber";
235 $strcalc .= ", " . $dbh->quote($colfield) if ($column);
236 $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
237 my $max;
238 if ($line) {
239 if (@loopcol) {
240 $max = $line*@loopcol;
241 } else { $max=$line;}
242 $strcalc .= " LIMIT 0,$max";
245 my $dbcalc = $dbh->prepare($strcalc);
246 $dbcalc->execute( @query_args );
247 # warn "filling table";
248 my $previous_col;
249 $i=1;
250 while (my @data = $dbcalc->fetchrow) {
251 my ($row, $col )=@data;
252 $col = "zzEMPTY" if (!defined($col));
253 $i=1 if (($previous_col) and not($col eq $previous_col));
254 $table[$i]->{$col}=$row;
255 # warn " $i $col $row";
256 $i++;
257 $previous_col=$col;
260 push @loopcol,{coltitle => "Global"} if not($column);
262 $max =(($line)?$line:@table -1);
263 for ($i=1; $i<=$max;$i++) {
264 my @loopcell;
265 #@loopcol ensures the order for columns is common with column titles
266 # and the number matches the number of columns
267 my $colcount=0;
268 foreach my $col ( @loopcol ) {
269 my $value;
270 if (@loopcol){
271 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
272 } else {
273 $value =$table[$i]->{"zzEMPTY"};
275 push @loopcell, {value => $value} ;
277 push @looprow,{ 'rowtitle' => $i ,
278 'loopcell' => \@loopcell,
284 # the header of the table
285 $globalline{loopfilter}=\@loopfilter;
286 # the core of the table
287 $globalline{looprow} = \@looprow;
288 $globalline{loopcol} = \@loopcol;
289 # # the foot (totals by borrower type)
290 $globalline{loopfooter} = \@loopfooter;
291 $globalline{total}= $grantotal;
292 $globalline{line} = $line;
293 $globalline{column} = $column;
294 push @mainloop,\%globalline;
295 return \@mainloop;
299 __END__