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