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