Bug 20783: Use iframe to embed Youtube videos
[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;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Circulation;
28 use C4::Reports;
29 use C4::Members;
30
31 use Koha::DateUtils;
32 use Koha::Patron::Categories;
33
34 =head1 NAME
35
36 reports/borrowers_out.pl
37
38 =head1 DESCRIPTION
39
40 Plugin that shows a stats on borrowers
41
42 =cut
43
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/borrowers_out.tt";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->multi_param("Filter");
50 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
51     if ( $filters[1] );
52
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 our $sep     = $input->param("sep") || '';
56 $sep = "\t" if ($sep eq 'tabulation');
57 my ($template, $borrowernumber, $cookie)
58     = get_template_and_user({template_name => $fullreportname,
59                 query => $input,
60                 type => "intranet",
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 $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
121     $template->param(
122                     CGIextChoice => $CGIextChoice,
123                     CGIsepChoice => $CGIsepChoice,
124                     patron_categories => $patron_categories,
125                     );
126 output_html_with_http_headers $input, $cookie, $template->output;
127 }
128
129
130 sub calculate {
131     my ($line, $column, $filters) = @_;
132     my @mainloop;
133     my @loopfooter;
134     my @loopcol;
135     my @loopline;
136     my @looprow;
137     my %globalline;
138     my $grantotal =0;
139 # extract parameters
140     my $dbh = C4::Context->dbh;
141
142 # Filters
143 # Checking filters
144 #
145     my @loopfilter;
146     for (my $i=0;$i<=2;$i++) {
147         my %cell;
148         if ( @$filters[$i] ) {
149             if (($i==1) and (@$filters[$i-1])) {
150                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
151             }
152             $cell{filter} .= @$filters[$i];
153             $cell{crit} .="Bor Cat" if ($i==0);
154             $cell{crit} .="Without issues since" if ($i==1);
155             push @loopfilter, \%cell;
156         }
157     }
158     my $colfield;
159     my $colorder;
160     if ($column){
161         $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
162         my @colfilter ;
163         $colfilter[0] = @$filters[0] if ($column =~ /category/ )  ;
164     #   $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
165     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
166                                                 
167     # loop cols.
168         $colfield .= $column;
169         $colorder .= $column;
170         
171         my $strsth2;
172         $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
173         my @query_args;
174         if ( $colfilter[0] ) {
175             $colfilter[0] =~ s/\*/%/g;
176             $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
177             push @query_args, $colfilter[0];
178         }
179         $strsth2 .=" group by " . $dbh->quote($colfield);
180         $strsth2 .=" order by " . $dbh->quote($colorder);
181         # warn "". $strsth2;
182         
183         my $sth2 = $dbh->prepare( $strsth2 );
184         $sth2->execute( @query_args );
185         while (my ($celvalue) = $sth2->fetchrow) {
186             my %cell;
187     #           my %ft;
188     #           warn "coltitle :".$celvalue;
189             $cell{coltitle} = $celvalue;
190     #           $ft{totalcol} = 0;
191             push @loopcol, \%cell;
192         }
193     #   warn "fin des titres colonnes";
194     }
195     
196     my $i=0;
197 #       my @totalcol;
198     
199     #Initialization of cell values.....
200     my @table;
201     
202 #       warn "init table";
203     if($line) {
204         for (my $i=1;$i<=$line;$i++) {
205             foreach my $col ( @loopcol ) {
206                 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
207             }
208         }
209     }
210
211
212 # preparing calculation
213     my $strcalc ;
214     
215 # Processing calculation
216     $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
217     $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
218     $strcalc .= " FROM borrowers ";
219     $strcalc .= "WHERE 1 ";
220     my @query_args;
221     if ( @$filters[0] ) {
222         @$filters[0]=~ s/\*/%/g;
223         $strcalc .= " AND borrowers.categorycode like ?";
224         push @query_args, @$filters[0];
225     }
226     $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
227     if ( @$filters[1] ) {
228         $strcalc .= " AND issues.timestamp > ?";
229         push @query_args, @$filters[1];
230     }
231     $strcalc .= ") ";
232     $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
233     if ( @$filters[1] ) {
234         $strcalc .= " AND old_issues.timestamp > ?";
235         push @query_args, @$filters[1];
236     }
237     $strcalc .= ") ";
238     $strcalc .= " group by borrowers.borrowernumber";
239     $strcalc .= ", " . $dbh->quote($colfield) if ($column);
240     $strcalc .= " order by " . $dbh->quote($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";
247      } 
248     
249     my $dbcalc = $dbh->prepare($strcalc);
250     $dbcalc->execute( @query_args );
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;
262     }
263     
264     push @loopcol,{coltitle => "Global"} if not($column);
265     
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"};
278             }
279             push @loopcell, {value => $value} ;
280         }
281         push @looprow,{ 'rowtitle' => $i ,
282                         'loopcell' => \@loopcell,
283                     };
284     }
285     
286             
287
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;
300 }
301
302 1;
303 __END__