Bug 16844: (follow-up of 15656) Remove export of GetMemberRelatives from C4::Members
[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 Koha::DateUtils;
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->multi_param("Filter");
49 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
50     if ( $filters[1] );
51
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 our $sep     = $input->param("sep") || '';
55 $sep = "\t" if ($sep eq 'tabulation');
56 my ($template, $borrowernumber, $cookie)
57     = get_template_and_user({template_name => $fullreportname,
58                 query => $input,
59                 type => "intranet",
60                 authnotrequired => 0,
61                 flagsrequired => {reports => '*'},
62                 debug => 1,
63                 });
64 $template->param(do_it => $do_it,
65         );
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;
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;
87         }
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;
96             }
97 #            print $line->{totalrow};
98             print "\n";
99         }
100 # footer
101         print "TOTAL";
102         $cols = @$results[0]->{loopfooter};
103         foreach my $col ( @$cols ) {
104             print $sep.$col->{totalcol};
105         }
106         print $sep.@$results[0]->{total};
107         exit;
108     }
109 # Displaying choices
110 } else {
111     my $dbh = C4::Context->dbh;
112     my @values;
113     my %labels;
114     my %select;
115     my $req;
116     
117     my $CGIextChoice = ( 'CSV' ); # FIXME translation
118         my $CGIsepChoice = GetDelimiterChoices;
119     
120     my ($codes,$labels) = GetborCatFromCatType(undef,undef);
121     my @borcatloop;
122     foreach my $thisborcat (sort keys %$labels) {
123             my %row =(value => $thisborcat,
124                                     description => $labels->{$thisborcat},
125                             );
126             push @borcatloop, \%row;
127     }
128     $template->param(
129                     CGIextChoice => $CGIextChoice,
130                     CGIsepChoice => $CGIsepChoice,
131                     borcatloop =>\@borcatloop,
132                     );
133 output_html_with_http_headers $input, $cookie, $template->output;
134 }
135
136
137 sub calculate {
138     my ($line, $column, $filters) = @_;
139     my @mainloop;
140     my @loopfooter;
141     my @loopcol;
142     my @loopline;
143     my @looprow;
144     my %globalline;
145     my $grantotal =0;
146 # extract parameters
147     my $dbh = C4::Context->dbh;
148
149 # Filters
150 # Checking filters
151 #
152     my @loopfilter;
153     for (my $i=0;$i<=2;$i++) {
154         my %cell;
155         if ( @$filters[$i] ) {
156             if (($i==1) and (@$filters[$i-1])) {
157                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
158             }
159             $cell{filter} .= @$filters[$i];
160             $cell{crit} .="Bor Cat" if ($i==0);
161             $cell{crit} .="Without issues since" if ($i==1);
162             push @loopfilter, \%cell;
163         }
164     }
165     my $colfield;
166     my $colorder;
167     if ($column){
168         $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
169         my @colfilter ;
170         $colfilter[0] = @$filters[0] if ($column =~ /category/ )  ;
171     #   $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
172     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
173                                                 
174     # loop cols.
175         $colfield .= $column;
176         $colorder .= $column;
177         
178         my $strsth2;
179         $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
180         my @query_args;
181         if ( $colfilter[0] ) {
182             $colfilter[0] =~ s/\*/%/g;
183             $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
184             push @query_args, $colfilter[0];
185         }
186         $strsth2 .=" group by " . $dbh->quote($colfield);
187         $strsth2 .=" order by " . $dbh->quote($colorder);
188         # warn "". $strsth2;
189         
190         my $sth2 = $dbh->prepare( $strsth2 );
191         $sth2->execute( @query_args );
192         while (my ($celvalue) = $sth2->fetchrow) {
193             my %cell;
194     #           my %ft;
195     #           warn "coltitle :".$celvalue;
196             $cell{coltitle} = $celvalue;
197     #           $ft{totalcol} = 0;
198             push @loopcol, \%cell;
199         }
200     #   warn "fin des titres colonnes";
201     }
202     
203     my $i=0;
204 #       my @totalcol;
205     
206     #Initialization of cell values.....
207     my @table;
208     
209 #       warn "init table";
210     if($line) {
211         for (my $i=1;$i<=$line;$i++) {
212             foreach my $col ( @loopcol ) {
213                 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
214             }
215         }
216     }
217
218
219 # preparing calculation
220     my $strcalc ;
221     
222 # Processing calculation
223     $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
224     $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
225     $strcalc .= " FROM borrowers ";
226     $strcalc .= "WHERE 1 ";
227     my @query_args;
228     if ( @$filters[0] ) {
229         @$filters[0]=~ s/\*/%/g;
230         $strcalc .= " AND borrowers.categorycode like ?";
231         push @query_args, @$filters[0];
232     }
233     $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
234     if ( @$filters[1] ) {
235         $strcalc .= " AND issues.timestamp > ?";
236         push @query_args, @$filters[1];
237     }
238     $strcalc .= ") ";
239     $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
240     if ( @$filters[1] ) {
241         $strcalc .= " AND old_issues.timestamp > ?";
242         push @query_args, @$filters[1];
243     }
244     $strcalc .= ") ";
245     $strcalc .= " group by borrowers.borrowernumber";
246     $strcalc .= ", " . $dbh->quote($colfield) if ($column);
247     $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
248     my $max;
249     if ($line) {
250         if (@loopcol) {
251             $max = $line*@loopcol;
252         } else { $max=$line;}
253         $strcalc .= " LIMIT 0,$max";
254      } 
255     
256     my $dbcalc = $dbh->prepare($strcalc);
257     $dbcalc->execute( @query_args );
258 #       warn "filling table";
259     my $previous_col;
260     $i=1;
261     while (my  @data = $dbcalc->fetchrow) {
262         my ($row, $col )=@data;
263         $col = "zzEMPTY" if (!defined($col));
264         $i=1 if (($previous_col) and not($col eq $previous_col));
265         $table[$i]->{$col}=$row;
266 #               warn " $i $col $row";
267         $i++;
268         $previous_col=$col;
269     }
270     
271     push @loopcol,{coltitle => "Global"} if not($column);
272     
273     $max =(($line)?$line:@table -1);
274     for ($i=1; $i<=$max;$i++) {
275         my @loopcell;
276         #@loopcol ensures the order for columns is common with column titles
277         # and the number matches the number of columns
278         my $colcount=0;
279         foreach my $col ( @loopcol ) {
280             my $value;
281             if (@loopcol){
282                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
283             } else {
284                 $value =$table[$i]->{"zzEMPTY"};
285             }
286             push @loopcell, {value => $value} ;
287         }
288         push @looprow,{ 'rowtitle' => $i ,
289                         'loopcell' => \@loopcell,
290                     };
291     }
292     
293             
294
295     # the header of the table
296     $globalline{loopfilter}=\@loopfilter;
297     # the core of the table
298     $globalline{looprow} = \@looprow;
299     $globalline{loopcol} = \@loopcol;
300 #       # the foot (totals by borrower type)
301     $globalline{loopfooter} = \@loopfooter;
302     $globalline{total}= $grantotal;
303     $globalline{line} = $line;
304     $globalline{column} = $column;
305     push @mainloop,\%globalline;
306     return \@mainloop;
307 }
308
309 1;
310 __END__