Bug 15066: Make transfer rotating collection works under Plack
[koha.git] / reports / borrowers_out.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
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/;
32
33 =head1 NAME
34
35 reports/borrowers_out.pl
36
37 =head1 DESCRIPTION
38
39 Plugin that shows a stats on borrowers
40
41 =cut
42
43 my $input = new CGI;
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/borrowers_out.tt";
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,
63         );
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;
85         }
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;
94             }
95 #            print $line->{totalrow};
96             print "\n";
97         }
98 # footer
99         print "TOTAL";
100         $cols = @$results[0]->{loopfooter};
101         foreach my $col ( @$cols ) {
102             print $sep.$col->{totalcol};
103         }
104         print $sep.@$results[0]->{total};
105         exit;
106     }
107 # Displaying choices
108 } else {
109     my $dbh = C4::Context->dbh;
110     my @values;
111     my %labels;
112     my %select;
113     my $req;
114     
115     my $CGIextChoice = ( 'CSV' ); # FIXME translation
116         my $CGIsepChoice = GetDelimiterChoices;
117     
118     my ($codes,$labels) = GetborCatFromCatType(undef,undef);
119     my @borcatloop;
120     foreach my $thisborcat (sort keys %$labels) {
121             my %row =(value => $thisborcat,
122                                     description => $labels->{$thisborcat},
123                             );
124             push @borcatloop, \%row;
125     }
126     $template->param(
127                     CGIextChoice => $CGIextChoice,
128                     CGIsepChoice => $CGIsepChoice,
129                     borcatloop =>\@borcatloop,
130                     );
131 output_html_with_http_headers $input, $cookie, $template->output;
132 }
133
134
135 sub calculate {
136     my ($line, $column, $filters) = @_;
137     my @mainloop;
138     my @loopfooter;
139     my @loopcol;
140     my @loopline;
141     my @looprow;
142     my %globalline;
143     my $grantotal =0;
144 # extract parameters
145     my $dbh = C4::Context->dbh;
146
147 # Filters
148 # Checking filters
149 #
150     my @loopfilter;
151     for (my $i=0;$i<=2;$i++) {
152         my %cell;
153         if ( @$filters[$i] ) {
154             if (($i==1) and (@$filters[$i-1])) {
155                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
156             }
157             $cell{filter} .= @$filters[$i];
158             $cell{crit} .="Bor Cat" if ($i==0);
159             $cell{crit} .="Without issues since" if ($i==1);
160             push @loopfilter, \%cell;
161         }
162     }
163     my $colfield;
164     my $colorder;
165     if ($column){
166         $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
167         my @colfilter ;
168         $colfilter[0] = @$filters[0] if ($column =~ /category/ )  ;
169     #   $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
170     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
171                                                 
172     # loop cols.
173         $colfield .= $column;
174         $colorder .= $column;
175         
176         my $strsth2;
177         $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
178         my @query_args;
179         if ( $colfilter[0] ) {
180             $colfilter[0] =~ s/\*/%/g;
181             $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
182             push @query_args, $colfilter[0];
183         }
184         $strsth2 .=" group by " . $dbh->quote($colfield);
185         $strsth2 .=" order by " . $dbh->quote($colorder);
186         # warn "". $strsth2;
187         
188         my $sth2 = $dbh->prepare( $strsth2 );
189         $sth2->execute( @query_args );
190         while (my ($celvalue) = $sth2->fetchrow) {
191             my %cell;
192     #           my %ft;
193     #           warn "coltitle :".$celvalue;
194             $cell{coltitle} = $celvalue;
195     #           $ft{totalcol} = 0;
196             push @loopcol, \%cell;
197         }
198     #   warn "fin des titres colonnes";
199     }
200     
201     my $i=0;
202 #       my @totalcol;
203     
204     #Initialization of cell values.....
205     my @table;
206     
207 #       warn "init table";
208     if($line) {
209         for (my $i=1;$i<=$line;$i++) {
210             foreach my $col ( @loopcol ) {
211                 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
212             }
213         }
214     }
215
216
217 # preparing calculation
218     my $strcalc ;
219     
220 # Processing calculation
221     $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
222     $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
223     $strcalc .= " FROM borrowers ";
224     $strcalc .= "WHERE 1 ";
225     my @query_args;
226     if ( @$filters[0] ) {
227         @$filters[0]=~ s/\*/%/g;
228         $strcalc .= " AND borrowers.categorycode like ?";
229         push @query_args, @$filters[0];
230     }
231     $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
232     if ( @$filters[1] ) {
233         $strcalc .= " AND issues.timestamap > ?";
234         push @query_args, @$filters[1];
235     }
236     $strcalc .= ") ";
237     $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
238     if ( @$filters[1] ) {
239         $strcalc .= " AND old_issues.timestamp > ?";
240         push @query_args, @$filters[1];
241     }
242     $strcalc .= ") ";
243     $strcalc .= " group by borrowers.borrowernumber";
244     $strcalc .= ", " . $dbh->quote($colfield) if ($column);
245     $strcalc .= " order by " . $dbh->quote($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";
252      } 
253     
254     my $dbcalc = $dbh->prepare($strcalc);
255     $dbcalc->execute( @query_args );
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;
267     }
268     
269     push @loopcol,{coltitle => "Global"} if not($column);
270     
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"};
283             }
284             push @loopcell, {value => $value} ;
285         }
286         push @looprow,{ 'rowtitle' => $i ,
287                         'loopcell' => \@loopcell,
288                     };
289     }
290     
291             
292
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;
305 }
306
307 1;
308 __END__