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