Bug 9788: Improvements when calling GetReservesFromItemnumber
[koha.git] / reports / borrowers_out.pl
blob55243b355a144fd75f3940ed7cf3850234d686a5
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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 our $sep = $input->param("sep") || '';
53 $sep = "\t" if ($sep eq 'tabulation');
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user({template_name => $fullreportname,
56 query => $input,
57 type => "intranet",
58 authnotrequired => 0,
59 flagsrequired => {reports => '*'},
60 debug => 1,
61 });
62 $template->param(do_it => $do_it,
64 if ($do_it) {
65 # Displaying results
66 my $results = calculate($limit, $column, \@filters);
67 if ($output eq "screen"){
68 # Printing results to screen
69 $template->param(mainloop => $results);
70 output_html_with_http_headers $input, $cookie, $template->output;
71 exit;
72 } else {
73 # Printing to a csv file
74 print $input->header(-type => 'application/vnd.sun.xml.calc',
75 -encoding => 'utf-8',
76 -attachment=>"$basename.csv",
77 -filename=>"$basename.csv" );
78 my $cols = @$results[0]->{loopcol};
79 my $lines = @$results[0]->{looprow};
80 # header top-right
81 print "num /". @$results[0]->{column} .$sep;
82 # Other header
83 foreach my $col ( @$cols ) {
84 print $col->{coltitle}.$sep;
86 print "Total\n";
87 # Table
88 foreach my $line ( @$lines ) {
89 my $x = $line->{loopcell};
90 print $line->{rowtitle}.$sep;
91 foreach my $cell (@$x) {
92 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
93 print $cellvalue;
95 # print $line->{totalrow};
96 print "\n";
98 # footer
99 print "TOTAL";
100 $cols = @$results[0]->{loopfooter};
101 foreach my $col ( @$cols ) {
102 print $sep.$col->{totalcol};
104 print $sep.@$results[0]->{total};
105 exit;
107 # Displaying choices
108 } else {
109 my $dbh = C4::Context->dbh;
110 my @values;
111 my %labels;
112 my %select;
113 my $req;
115 my $CGIextChoice=CGI::scrolling_list(
116 -name => 'MIME',
117 -id => 'MIME',
118 -values => ['CSV'], # FIXME translation
119 -size => 1,
120 -multiple => 0 );
122 my $CGIsepChoice = GetDelimiterChoices;
124 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
125 my @borcatloop;
126 foreach my $thisborcat (sort keys %$labels) {
127 my %row =(value => $thisborcat,
128 description => $labels->{$thisborcat},
130 push @borcatloop, \%row;
132 $template->param(
133 CGIextChoice => $CGIextChoice,
134 CGIsepChoice => $CGIsepChoice,
135 borcatloop =>\@borcatloop,
137 output_html_with_http_headers $input, $cookie, $template->output;
141 sub calculate {
142 my ($line, $column, $filters) = @_;
143 my @mainloop;
144 my @loopfooter;
145 my @loopcol;
146 my @loopline;
147 my @looprow;
148 my %globalline;
149 my $grantotal =0;
150 # extract parameters
151 my $dbh = C4::Context->dbh;
153 # Filters
154 # Checking filters
156 my @loopfilter;
157 for (my $i=0;$i<=2;$i++) {
158 my %cell;
159 if ( @$filters[$i] ) {
160 if (($i==1) and (@$filters[$i-1])) {
161 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
163 $cell{filter} .= @$filters[$i];
164 $cell{crit} .="Bor Cat" if ($i==0);
165 $cell{crit} .="Without issues since" if ($i==1);
166 push @loopfilter, \%cell;
169 my $colfield;
170 my $colorder;
171 if ($column){
172 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
173 my @colfilter ;
174 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
175 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
176 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
178 # loop cols.
179 $colfield .= $column;
180 $colorder .= $column;
182 my $strsth2;
183 $strsth2 .= "select distinct $colfield FROM borrowers WHERE 1";
184 if ($colfilter[0]) {
185 $colfilter[0] =~ s/\*/%/g;
186 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
188 $strsth2 .=" group by $colfield";
189 $strsth2 .=" order by $colorder";
190 # warn "". $strsth2;
192 my $sth2 = $dbh->prepare( $strsth2 );
193 $sth2->execute;
194 while (my ($celvalue) = $sth2->fetchrow) {
195 my %cell;
196 # my %ft;
197 # warn "coltitle :".$celvalue;
198 $cell{coltitle} = $celvalue;
199 # $ft{totalcol} = 0;
200 push @loopcol, \%cell;
202 # warn "fin des titres colonnes";
205 my $i=0;
206 # my @totalcol;
208 #Initialization of cell values.....
209 my @table;
211 # warn "init table";
212 if($line) {
213 for (my $i=1;$i<=$line;$i++) {
214 foreach my $col ( @loopcol ) {
215 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
221 # preparing calculation
222 my $strcalc ;
224 # Processing calculation
225 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
226 $strcalc .= " , $colfield " if ($colfield);
227 $strcalc .= " FROM borrowers ";
228 $strcalc .= "WHERE 1 ";
229 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
230 $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
232 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
233 $strcalc .= " AND issues.timestamp> '" . @$filters[1] . "'" if (@$filters[1]);
234 $strcalc .= ") ";
235 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
236 $strcalc .= " AND old_issues.timestamp> '" . @$filters[1] . "'" if (@$filters[1]);
237 $strcalc .= ") ";
238 $strcalc .= " group by borrowers.borrowernumber";
239 $strcalc .= ", $colfield" if ($column);
240 $strcalc .= " order by $colfield " if ($colfield);
241 my $max;
242 if ($line) {
243 if (@loopcol) {
244 $max = $line*@loopcol;
245 } else { $max=$line;}
246 $strcalc .= " LIMIT 0,$max";
249 my $dbcalc = $dbh->prepare($strcalc);
250 $dbcalc->execute;
251 # warn "filling table";
252 my $previous_col;
253 $i=1;
254 while (my @data = $dbcalc->fetchrow) {
255 my ($row, $col )=@data;
256 $col = "zzEMPTY" if (!defined($col));
257 $i=1 if (($previous_col) and not($col eq $previous_col));
258 $table[$i]->{$col}=$row;
259 # warn " $i $col $row";
260 $i++;
261 $previous_col=$col;
264 push @loopcol,{coltitle => "Global"} if not($column);
266 $max =(($line)?$line:@table -1);
267 for ($i=1; $i<=$max;$i++) {
268 my @loopcell;
269 #@loopcol ensures the order for columns is common with column titles
270 # and the number matches the number of columns
271 my $colcount=0;
272 foreach my $col ( @loopcol ) {
273 my $value;
274 if (@loopcol){
275 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
276 } else {
277 $value =$table[$i]->{"zzEMPTY"};
279 push @loopcell, {value => $value} ;
281 push @looprow,{ 'rowtitle' => $i ,
282 'loopcell' => \@loopcell,
288 # the header of the table
289 $globalline{loopfilter}=\@loopfilter;
290 # the core of the table
291 $globalline{looprow} = \@looprow;
292 $globalline{loopcol} = \@loopcol;
293 # # the foot (totals by borrower type)
294 $globalline{loopfooter} = \@loopfooter;
295 $globalline{total}= $grantotal;
296 $globalline{line} = $line;
297 $globalline{column} = $column;
298 push @mainloop,\%globalline;
299 return \@mainloop;
303 __END__