adding a warn to log why we're redirecting to installer
[koha.git] / reports / cat_issues_top.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 C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Circulation;
30 use Date::Manip;
31 use C4::Members;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39
40 =over2
41
42 =cut
43
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/cat_issues_top.tmpl";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->param("Filter");
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 my $del = $input->param("sep");
54 #warn "calcul : ".$calc;
55 my ($template, $borrowernumber, $cookie)
56         = get_template_and_user({template_name => $fullreportname,
57                                 query => $input,
58                                 type => "intranet",
59                                 authnotrequired => 0,
60                                 flagsrequired => { reports => 1},
61                                 debug => 1,
62                                 });
63 $template->param(do_it => $do_it,
64                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
65                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
66                 IntranetNav => C4::Context->preference("IntranetNav"),
67                 );
68 if ($do_it) {
69 # Displaying results
70         my $results = calculate($limit, $column, \@filters);
71         if ($output eq "screen"){
72 # Printing results to screen
73                 $template->param(mainloop => $results,
74                                                 limit => $limit);
75                 output_html_with_http_headers $input, $cookie, $template->output;
76                 exit(1);
77         } else {
78 # Printing to a csv file
79                 print $input->header(-type => 'application/vnd.sun.xml.calc',
80                                      -encoding    => 'utf-8',
81                         -attachment=>"$basename.csv",
82                         -filename=>"$basename.csv" );
83                 my $cols = @$results[0]->{loopcol};
84                 my $lines = @$results[0]->{looprow};
85                 my $sep;
86                 $sep =C4::Context->preference("delimiter");
87 # header top-right
88                 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
89 # Other header
90                 foreach my $col ( @$cols ) {
91                         print $col->{coltitle}.$sep;
92                 }
93                 print "Total\n";
94 # Table
95                 foreach my $line ( @$lines ) {
96                         my $x = $line->{loopcell};
97                         print $line->{rowtitle}.$sep;
98                         foreach my $cell (@$x) {
99                                 print $cell->{value}.$sep;
100                         }
101                         print $line->{totalrow};
102                         print "\n";
103                 }
104 # footer
105                 print "TOTAL";
106                 $cols = @$results[0]->{loopfooter};
107                 foreach my $col ( @$cols ) {
108                         print $sep.$col->{totalcol};
109                 }
110                 print $sep.@$results[0]->{total};
111                 exit(1);
112         }
113 # Displaying choices
114 } else {
115         my $dbh = C4::Context->dbh;
116         my @values;
117         my %labels;
118         my %select;
119         my $req;
120         
121         my @mime = ( C4::Context->preference("MIME") );
122 #       foreach my $mime (@mime){
123 #               warn "".$mime;
124 #       }
125         
126         my $CGIextChoice=CGI::scrolling_list(
127                                 -name     => 'MIME',
128                                 -id       => 'MIME',
129                                 -values   => \@mime,
130                                 -size     => 1,
131                                 -multiple => 0 );
132         
133         my @dels = ( C4::Context->preference("delimiter") );
134         my $CGIsepChoice=CGI::scrolling_list(
135                                 -name     => 'sep',
136                                 -id       => 'sep',
137                                 -values   => \@dels,
138                                 -size     => 1,
139                                 -multiple => 0 );
140         #branch
141         my $branches = GetBranches;
142         my @branchloop;
143         foreach my $thisbranch (keys %$branches) {
144 #                       my $selected = 1 if $thisbranch eq $branch;
145                         my %row =(value => $thisbranch,
146 #                                                                       selected => $selected,
147                                                                         branchname => $branches->{$thisbranch}->{'branchname'},
148                                                         );
149                         push @branchloop, \%row;
150         }
151
152         #doctype
153         my $itemtypes = GetItemTypes;
154         my @itemtypeloop;
155         foreach my $thisitemtype (keys %$itemtypes) {
156 #                       my $selected = 1 if $thisbranch eq $branch;
157                         my %row =(value => $thisitemtype,
158 #                                                                       selected => $selected,
159                                                                         description => $itemtypes->{$thisitemtype}->{'description'},
160                                                         );
161                         push @itemtypeloop, \%row;
162         }
163         
164         #borcat
165         my ($codes,$labels) = GetborCatFromCatType(undef,undef);
166         my @borcatloop;
167         foreach my $thisborcat (sort keys %$labels) {
168  #                      my $selected = 1 if $thisbranch eq $branch;
169                         my %row =(value => $thisborcat,
170  #                                                                      selected => $selected,
171                                                                         description => $labels->{$thisborcat},
172                                                         );
173                         push @borcatloop, \%row;
174         }
175         
176         #Day
177         #Month
178         $template->param(
179                                         CGIextChoice => $CGIextChoice,
180                                         CGIsepChoice => $CGIsepChoice,
181                                         branchloop =>\@branchloop,
182                                         itemtypeloop =>\@itemtypeloop,
183                                         borcatloop =>\@borcatloop,
184                                         );
185 output_html_with_http_headers $input, $cookie, $template->output;
186 }
187
188
189
190
191 sub calculate {
192         my ($line, $column, $filters) = @_;
193         my @mainloop;
194         my @loopfooter;
195         my @loopcol;
196         my @loopline;
197         my @looprow;
198         my %globalline;
199         my $grantotal =0;
200 # extract parameters
201         my $dbh = C4::Context->dbh;
202
203 # Filters
204 # Checking filters
205 #
206         my @loopfilter;
207         for (my $i=0;$i<=6;$i++) {
208                 my %cell;
209                 if ( @$filters[$i] ) {
210                         if (($i==1) and (@$filters[$i-1])) {
211                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
212                         }
213                         $cell{filter} .= @$filters[$i];
214                         $cell{crit} .="Issue From" if ($i==0);
215                         $cell{crit} .="Issue To" if ($i==1);
216                         $cell{crit} .="Return From" if ($i==2);
217                         $cell{crit} .="Return To" if ($i==3);
218                         $cell{crit} .="Branch" if ($i==4);
219                         $cell{crit} .="Doc Type" if ($i==5);
220                         $cell{crit} .="Bor Cat" if ($i==6);
221                         $cell{crit} .="Day" if ($i==7);
222                         $cell{crit} .="Month" if ($i==8);
223                         $cell{crit} .="Year" if ($i==9);
224                         push @loopfilter, \%cell;
225                 }
226         }
227         my $colfield;
228         my $colorder;
229         if ($column){
230                 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
231                 $column = "biblioitems.".$column if $column=~/itemtype/;
232                 $column = "borrowers.".$column if $column=~/categorycode/;
233                 my @colfilter ;
234                 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
235                 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
236                 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ )  ;
237                 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ )  ;
238                 $colfilter[0] = @$filters[4] if ($column =~ /branch/ )  ;
239                 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ )  ;
240                 $colfilter[0] = @$filters[6] if ($column =~ /category/ )  ;
241         #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
242                 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
243                 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
244                 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
245         #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
246                                                                                                 
247         # loop cols.
248                 if ($column eq "Day") {
249                         #Display by day
250                         $column = "issues.timestamp";
251                         $colfield .="dayname($column)";  
252                         $colorder .="weekday($column)";
253                 } elsif ($column eq "Month") {
254                         #Display by Month
255                         $column = "issues.timestamp";
256                         $colfield .="monthname($column)";  
257                         $colorder .="month($column)";  
258                 } elsif ($column eq "Year") {
259                         #Display by Year
260                         $column = "issues.timestamp";
261                         $colfield .="Year($column)";
262                         $colorder .= $column;
263                 } else {
264                         $colfield .= $column;
265                         $colorder .= $column;
266                 }  
267                 
268                 my $strsth2;
269                 $strsth2 .= "select distinctrow $colfield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
270                 if (($column=~/timestamp/) or ($column=~/returndate/)){
271                         if ($colfilter[1] and ($colfilter[0])){
272                                 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
273                         } elsif ($colfilter[1]) {
274                                         $strsth2 .= " and $column < '$colfilter[1]' " ;
275                         } elsif ($colfilter[0]) {
276                                 $strsth2 .= " and $column > '$colfilter[0]' " ;
277                         }
278                 } elsif ($colfilter[0]) {
279                         $colfilter[0] =~ s/\*/%/g;
280                         $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
281                 }
282                 $strsth2 .=" group by $colfield";
283                 $strsth2 .=" order by $colorder";
284                 warn "". $strsth2;
285                 
286                 my $sth2 = $dbh->prepare( $strsth2 );
287                 if (( @colfilter ) and ($colfilter[1])){
288                         $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
289                 } elsif ($colfilter[0]) {
290                         $sth2->execute($colfilter[0]);
291                 } else {
292                         $sth2->execute;
293                 }
294                 
295         
296                 while (my ($celvalue) = $sth2->fetchrow) {
297                         my %cell;
298                         $cell{coltitle} = ($celvalue?$celvalue:"NULL");
299                         push @loopcol, \%cell;
300                 }
301         #       warn "fin des titres colonnes";
302         }
303         
304         my $i=0;
305 #       my @totalcol;
306         my $hilighted=-1;
307         
308         #Initialization of cell values.....
309         my @table;
310         
311 #       warn "init table";
312         for (my $i=1;$i<=$line;$i++) {
313                 foreach my $col ( @loopcol ) {
314 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
315                         $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
316                 }
317         }
318
319
320 # preparing calculation
321         my $strcalc ;
322         
323 # Processing average loanperiods
324         $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
325         $strcalc .= " , $colfield " if ($colfield);
326         $strcalc .= " FROM `issues`,borrowers,(items LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
327
328         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
329         $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
330         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
331         $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
332         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
333         $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
334         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
335         $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
336         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
337         $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
338         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
339         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
340         @$filters[6]=~ s/\*/%/g if (@$filters[6]);
341         $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
342         @$filters[7]=~ s/\*/%/g if (@$filters[7]);
343         $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
344         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
345         $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
346         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
347         $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
348         
349         $strcalc .= " group by biblio.biblionumber";
350         $strcalc .= ", $colfield" if ($column);
351         $strcalc .= " order by RANK DESC";
352         $strcalc .= ", $colfield " if ($colfield);
353 #       my $max;
354 #       if (@loopcol) {
355 #               $max = $line*@loopcol;
356 #       } else { $max=$line;}
357 #       $strcalc .= " LIMIT 0,$max";
358         warn "SQL :". $strcalc;
359         
360         my $dbcalc = $dbh->prepare($strcalc);
361         $dbcalc->execute;
362 #       warn "filling table";
363         my $previous_col;
364         my %indice;
365         while (my  @data = $dbcalc->fetchrow) {
366                 my ($row, $rank, $id, $col )=@data;
367                 $col = "zzEMPTY" if ($col eq undef);
368                 $indice{$col}=1 if (not($indice{$col}));
369                 $table[$indice{$col}]->{$col}->{'name'}=$row;
370                 $table[$indice{$col}]->{$col}->{'count'}=$rank;
371                 $table[$indice{$col}]->{$col}->{'link'}=$id;
372 #               warn " ".$i." ".$col. " ".$row;
373                 $indice{$col}++;
374         }
375         
376         push @loopcol,{coltitle => "Global"} if not($column);
377         
378         for ($i=1; $i<=$line;$i++) {
379                 my @loopcell;
380                 warn " $i";
381                 #@loopcol ensures the order for columns is common with column titles
382                 # and the number matches the number of columns
383                 my $colcount=0;
384                 foreach my $col ( @loopcol ) {
385 #                       warn " colonne :$col->{coltitle}";
386                         my $value;
387                         my $count=0;
388                         my $link;
389                         if (@loopcol){
390                                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
391                                 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
392                                 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
393                         } else {
394                                 $value =$table[$i]->{"zzEMPTY"}->{'name'};
395                                 $count =$table[$i]->{"zzEMPTY"}->{'count'};
396                                 $link =$table[$i]->{"zzEMPTY"}->{'link'};
397                         }
398 #                       warn " ".$i ." value:$value count:$count reference:$link";
399                         push @loopcell, {value => $value, count =>$count, reference => $link} ;
400                 }
401                 #warn "row : $row colcount:$colcount";
402                 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
403                 push @looprow,{ 'rowtitle' => $i ,
404                                                 'loopcell' => \@loopcell,
405                                                 'hilighted' => ($hilighted >0),
406                                                 #'totalrow' => ($total)?sprintf("%.2f",$total):0
407                                         };
408                 $hilighted = -$hilighted;
409         }
410 #       
411                         
412
413         # the header of the table
414         $globalline{loopfilter}=\@loopfilter;
415         # the core of the table
416         $globalline{looprow} = \@looprow;
417         $globalline{loopcol} = \@loopcol;
418 #       # the foot (totals by borrower type)
419         $globalline{loopfooter} = \@loopfooter;
420         $globalline{total}= $grantotal;
421         $globalline{line} = $line;
422         $globalline{column} = $column;
423         push @mainloop,\%globalline;
424         return \@mainloop;
425 }
426
427 1;