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