Bug 3856: Tools Menu in Alpha Order - resubmission.
[koha.git] / reports / borrowers_out.pl
blobe6990291cdd6a5b218bf5badf1eb01da68c79598
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use warnings;
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Output;
28 use C4::Circulation;
29 use C4::Reports;
30 use C4::Members;
31 use C4::Dates qw/format_date_in_iso/;
33 =head1 NAME
35 plugin that shows a stats on borrowers
37 =head1 DESCRIPTION
39 =over 2
41 =cut
43 my $input = new CGI;
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/borrowers_out.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 $filters[1] = format_date_in_iso($filters[1]) if $filters[1];
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 our $sep = $input->param("sep") || '';
54 $sep = "\t" if ($sep eq 'tabulation');
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => $fullreportname,
57 query => $input,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => {reports => '*'},
61 debug => 1,
62 });
63 $template->param(do_it => $do_it,
64 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
66 if ($do_it) {
67 # Displaying results
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;
73 exit(1);
74 } else {
75 # Printing to a csv file
76 print $input->header(-type => 'application/vnd.sun.xml.calc',
77 -encoding => 'utf-8',
78 -attachment=>"$basename.csv",
79 -filename=>"$basename.csv" );
80 my $cols = @$results[0]->{loopcol};
81 my $lines = @$results[0]->{looprow};
82 # header top-right
83 print "num /". @$results[0]->{column} .$sep;
84 # Other header
85 foreach my $col ( @$cols ) {
86 print $col->{coltitle}.$sep;
88 print "Total\n";
89 # Table
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;
95 print $cellvalue;
97 # print $line->{totalrow};
98 print "\n";
100 # footer
101 print "TOTAL";
102 $cols = @$results[0]->{loopfooter};
103 foreach my $col ( @$cols ) {
104 print $sep.$col->{totalcol};
106 print $sep.@$results[0]->{total};
107 exit(1);
109 # Displaying choices
110 } else {
111 my $dbh = C4::Context->dbh;
112 my @values;
113 my %labels;
114 my %select;
115 my $req;
117 my @mime = ( C4::Context->preference("MIME") );
118 # foreach my $mime (@mime){
119 # warn "".$mime;
122 my $CGIextChoice=CGI::scrolling_list(
123 -name => 'MIME',
124 -id => 'MIME',
125 -values => \@mime,
126 -size => 1,
127 -multiple => 0 );
129 my $CGIsepChoice = GetDelimiterChoices;
131 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
132 my @borcatloop;
133 foreach my $thisborcat (sort keys %$labels) {
134 my %row =(value => $thisborcat,
135 description => $labels->{$thisborcat},
137 push @borcatloop, \%row;
139 $template->param(
140 CGIextChoice => $CGIextChoice,
141 CGIsepChoice => $CGIsepChoice,
142 borcatloop =>\@borcatloop,
144 output_html_with_http_headers $input, $cookie, $template->output;
148 sub calculate {
149 my ($line, $column, $filters) = @_;
150 my @mainloop;
151 my @loopfooter;
152 my @loopcol;
153 my @loopline;
154 my @looprow;
155 my %globalline;
156 my $grantotal =0;
157 # extract parameters
158 my $dbh = C4::Context->dbh;
160 # Filters
161 # Checking filters
163 my @loopfilter;
164 for (my $i=0;$i<=2;$i++) {
165 my %cell;
166 if ( @$filters[$i] ) {
167 if (($i==1) and (@$filters[$i-1])) {
168 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
170 $cell{filter} .= @$filters[$i];
171 $cell{crit} .="Bor Cat" if ($i==0);
172 $cell{crit} .="Without issues since" if ($i==1);
173 push @loopfilter, \%cell;
176 my $colfield;
177 my $colorder;
178 if ($column){
179 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
180 my @colfilter ;
181 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
182 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
183 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
185 # loop cols.
186 $colfield .= $column;
187 $colorder .= $column;
189 my $strsth2;
190 $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `old_issues` USING(borrowernumber)";
191 if ($colfilter[0]) {
192 $colfilter[0] =~ s/\*/%/g;
193 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
195 $strsth2 .=" group by $colfield";
196 $strsth2 .=" order by $colorder";
197 # warn "". $strsth2;
199 my $sth2 = $dbh->prepare( $strsth2 );
200 $sth2->execute;
201 while (my ($celvalue) = $sth2->fetchrow) {
202 my %cell;
203 # my %ft;
204 # warn "coltitle :".$celvalue;
205 $cell{coltitle} = $celvalue;
206 # $ft{totalcol} = 0;
207 push @loopcol, \%cell;
209 # warn "fin des titres colonnes";
212 my $i=0;
213 # my @totalcol;
215 #Initialization of cell values.....
216 my @table;
218 # warn "init table";
219 if($line) {
220 for (my $i=1;$i<=$line;$i++) {
221 foreach my $col ( @loopcol ) {
222 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
228 # preparing calculation
229 my $strcalc ;
231 # Processing calculation
232 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
233 $strcalc .= " , $colfield " if ($colfield);
234 $strcalc .= " FROM borrowers ";
235 $strcalc .= "WHERE 1 ";
236 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
237 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
238 my $strqueryfilter = "SELECT DISTINCT borrowernumber FROM old_issues WHERE borrowernumber IS NOT NULL ";
239 if (@$filters[1]){
240 my $strqueryfilter .= "AND old_issues.timestamp> @$filters[1] ";
242 $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
243 $strcalc .= " group by borrowers.borrowernumber";
244 $strcalc .= ", $colfield" if ($column);
245 $strcalc .= " order by $colfield " if ($colfield);
246 my $max;
247 if ($line) {
248 if (@loopcol) {
249 $max = $line*@loopcol;
250 } else { $max=$line;}
251 $strcalc .= " LIMIT 0,$max";
254 my $dbcalc = $dbh->prepare($strcalc);
255 $dbcalc->execute;
256 # warn "filling table";
257 my $previous_col;
258 $i=1;
259 while (my @data = $dbcalc->fetchrow) {
260 my ($row, $col )=@data;
261 $col = "zzEMPTY" if (!defined($col));
262 $i=1 if (($previous_col) and not($col eq $previous_col));
263 $table[$i]->{$col}=$row;
264 # warn " $i $col $row";
265 $i++;
266 $previous_col=$col;
269 push @loopcol,{coltitle => "Global"} if not($column);
271 $max =(($line)?$line:@table -1);
272 for ($i=1; $i<=$max;$i++) {
273 my @loopcell;
274 #@loopcol ensures the order for columns is common with column titles
275 # and the number matches the number of columns
276 my $colcount=0;
277 foreach my $col ( @loopcol ) {
278 my $value;
279 if (@loopcol){
280 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
281 } else {
282 $value =$table[$i]->{"zzEMPTY"};
284 push @loopcell, {value => $value} ;
286 push @looprow,{ 'rowtitle' => $i ,
287 'loopcell' => \@loopcell,
293 # the header of the table
294 $globalline{loopfilter}=\@loopfilter;
295 # the core of the table
296 $globalline{looprow} = \@looprow;
297 $globalline{loopcol} = \@loopcol;
298 # # the foot (totals by borrower type)
299 $globalline{loopfooter} = \@loopfooter;
300 $globalline{total}= $grantotal;
301 $globalline{line} = $line;
302 $globalline{column} = $column;
303 push @mainloop,\%globalline;
304 return \@mainloop;
308 __END__