french translation, updated
[koha.git] / reports / borrowers_out.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Circulation;
28 use C4::Members;
29 use C4::Date;
30
31 =head1 NAME
32
33 plugin that shows a stats on borrowers
34
35 =head1 DESCRIPTION
36
37 =over 2
38
39 =cut
40
41 my $input = new CGI;
42 my $do_it=$input->param('do_it');
43 my $fullreportname = "reports/borrowers_out.tmpl";
44 my $limit = $input->param("Limit");
45 my $column = $input->param("Criteria");
46 my @filters = $input->param("Filter");
47 my $output = $input->param("output");
48 my $basename = $input->param("basename");
49 my $mime = $input->param("MIME");
50 my $del = $input->param("sep");
51 #warn "calcul : ".$calc;
52 my ($template, $borrowernumber, $cookie)
53     = get_template_and_user({template_name => $fullreportname,
54                 query => $input,
55                 type => "intranet",
56                 authnotrequired => 0,
57                 flagsrequired => {reports => 1},
58                 debug => 1,
59                 });
60 $template->param(do_it => $do_it,
61         DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
62         );
63 if ($do_it) {
64 # Displaying results
65     my $results = calculate($limit, $column, \@filters);
66     if ($output eq "screen"){
67 # Printing results to screen
68         $template->param(mainloop => $results);
69         output_html_with_http_headers $input, $cookie, $template->output;
70         exit(1);
71     } else {
72 # Printing to a csv file
73         print $input->header(-type => 'application/vnd.sun.xml.calc',
74                             -encoding    => 'utf-8',
75                             -attachment=>"$basename.csv",
76                             -filename=>"$basename.csv" );
77         my $cols = @$results[0]->{loopcol};
78         my $lines = @$results[0]->{looprow};
79         my $sep;
80         $sep =C4::Context->preference("delimiter");
81 # header top-right
82         print "num /". @$results[0]->{column} .$sep;
83 # Other header
84         foreach my $col ( @$cols ) {
85             print $col->{coltitle}.$sep;
86         }
87         print "Total\n";
88 # Table
89         foreach my $line ( @$lines ) {
90             my $x = $line->{loopcell};
91             print $line->{rowtitle}.$sep;
92             foreach my $cell (@$x) {
93                 print $cell->{value}.$sep;
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(1);
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 @mime = ( C4::Context->preference("MIME") );
116 #       foreach my $mime (@mime){
117 #               warn "".$mime;
118 #       }
119     
120     my $CGIextChoice=CGI::scrolling_list(
121                 -name     => 'MIME',
122                 -id       => 'MIME',
123                 -values   => \@mime,
124                 -size     => 1,
125                 -multiple => 0 );
126     
127     my @dels = ( C4::Context->preference("delimiter") );
128     my $CGIsepChoice=CGI::scrolling_list(
129                 -name     => 'sep',
130                 -id       => 'sep',
131                 -values   => \@dels,
132                 -size     => 1,
133                 -multiple => 0 );
134     
135     my ($codes,$labels) = GetborCatFromCatType(undef,undef);
136     my @borcatloop;
137     foreach my $thisborcat (sort keys %$labels) {
138 #                       my $selected = 1 if $thisbranch eq $branch;
139             my %row =(value => $thisborcat,
140 #                                                                       selected => $selected,
141                                     description => $labels->{$thisborcat},
142                             );
143             push @borcatloop, \%row;
144     }
145     $template->param(
146                     CGIextChoice => $CGIextChoice,
147                     CGIsepChoice => $CGIsepChoice,
148                     borcatloop =>\@borcatloop,
149                     );
150 output_html_with_http_headers $input, $cookie, $template->output;
151 }
152
153
154
155
156 sub calculate {
157     my ($line, $column, $filters) = @_;
158     my @mainloop;
159     my @loopfooter;
160     my @loopcol;
161     my @loopline;
162     my @looprow;
163     my %globalline;
164     my $grantotal =0;
165 # extract parameters
166     my $dbh = C4::Context->dbh;
167
168 # Filters
169 # Checking filters
170 #
171     my @loopfilter;
172     for (my $i=0;$i<=2;$i++) {
173         my %cell;
174         if ( @$filters[$i] ) {
175             if (($i==1) and (@$filters[$i-1])) {
176                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
177             }
178             $cell{filter} .= @$filters[$i];
179             $cell{crit} .="Bor Cat" if ($i==0);
180             $cell{crit} .="Without issues since" if ($i==1);
181             push @loopfilter, \%cell;
182         }
183     }
184     my $colfield;
185     my $colorder;
186     if ($column){
187         $column = "borrowers.".$column if $column=~/categorycode/;
188         my @colfilter ;
189         $colfilter[0] = @$filters[0] if ($column =~ /category/ )  ;
190     #   $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
191     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
192                                                 
193     # loop cols.
194         $colfield .= $column;
195         $colorder .= $column;
196         
197         my $strsth2;
198         $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `issues` ON issues.borrowernumber=borrowers.borrowernumber";
199         if ($colfilter[0]) {
200             $colfilter[0] =~ s/\*/%/g;
201             $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
202         }
203         $strsth2 .=" group by $colfield";
204         $strsth2 .=" order by $colorder";
205         warn "". $strsth2;
206         
207         my $sth2 = $dbh->prepare( $strsth2 );
208         $sth2->execute;
209
210         
211     
212         while (my ($celvalue) = $sth2->fetchrow) {
213             my %cell;
214     #           my %ft;
215     #           warn "coltitle :".$celvalue;
216             $cell{coltitle} = $celvalue;
217     #           $ft{totalcol} = 0;
218             push @loopcol, \%cell;
219         }
220     #   warn "fin des titres colonnes";
221     }
222     
223     my $i=0;
224 #       my @totalcol;
225     my $hilighted=-1;
226     
227     #Initialization of cell values.....
228     my @table;
229     
230 #       warn "init table";
231     for (my $i=1;$i<=$line;$i++) {
232         foreach my $col ( @loopcol ) {
233 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
234             $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
235         }
236     }
237
238
239 # preparing calculation
240     my $strcalc ;
241     
242 # Processing calculation
243     $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
244     $strcalc .= " , $colfield " if ($colfield);
245     $strcalc .= " FROM borrowers ";
246     $strcalc .= "WHERE 1 ";
247     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
248     $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
249     if (@$filters[1]){
250         my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> @$filters[1] ";
251         my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues where issues.timestamp> ".format_date_in_iso(@$filters[1]));
252         $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
253         
254 #               $queryfilter->execute(@$filters[1]);
255 #               while (my ($borrowernumber)=$queryfilter->fetchrow){
256 #                       $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
257 #               }
258     } else {
259         my $strqueryfilter="SELECT DISTINCT borrowernumber FROM issues ";
260         my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM issues ");
261         $queryfilter->execute;
262         $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
263 #               while (my ($borrowernumber)=$queryfilter->fetchrow){
264 #                       $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
265 #               }
266     }
267     $strcalc .= " group by borrowers.borrowernumber";
268     $strcalc .= ", $colfield" if ($column);
269     $strcalc .= " order by $colfield " if ($colfield);
270     my $max;
271     if (@loopcol) {
272         $max = $line*@loopcol;
273     } else { $max=$line;}
274     $strcalc .= " LIMIT 0,$max" if ($line);
275     warn "SQL :". $strcalc;
276     
277     my $dbcalc = $dbh->prepare($strcalc);
278     $dbcalc->execute;
279 #       warn "filling table";
280     my $previous_col;
281     my $i=1;
282     while (my  @data = $dbcalc->fetchrow) {
283         my ($row, $col )=@data;
284         $col = "zzEMPTY" if ($col eq undef);
285         $i=1 if (($previous_col) and not($col eq $previous_col));
286         $table[$i]->{$col}=$row;
287 #               warn " ".$i." ".$col. " ".$row;
288         $i++;
289         $previous_col=$col;
290     }
291     
292     push @loopcol,{coltitle => "Global"} if not($column);
293     
294     my $max =(($line)?$line:@table -1);
295     for ($i=1; $i<=$max;$i++) {
296         my @loopcell;
297         #@loopcol ensures the order for columns is common with column titles
298         # and the number matches the number of columns
299         my $colcount=0;
300         foreach my $col ( @loopcol ) {
301             my $value;
302             if (@loopcol){
303                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
304             } else {
305                 $value =$table[$i]->{"zzEMPTY"};
306             }
307             push @loopcell, {value => $value} ;
308         }
309         push @looprow,{ 'rowtitle' => $i ,
310                         'loopcell' => \@loopcell,
311                         'hilighted' => ($hilighted >0),
312                     };
313         $hilighted = -$hilighted;
314     }
315     
316             
317
318     # the header of the table
319     $globalline{loopfilter}=\@loopfilter;
320     # the core of the table
321     $globalline{looprow} = \@looprow;
322     $globalline{loopcol} = \@loopcol;
323 #       # the foot (totals by borrower type)
324     $globalline{loopfooter} = \@loopfooter;
325     $globalline{total}= $grantotal;
326     $globalline{line} = $line;
327     $globalline{column} = $column;
328     push @mainloop,\%globalline;
329     return \@mainloop;
330 }
331
332 1;