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