New XML API
[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         
137         $template->param(
138                                         CGIextChoice => $CGIextChoice,
139                                         CGIsepChoice => $CGIsepChoice
140                                         );
141 output_html_with_http_headers $input, $cookie, $template->output;
142 }
143
144
145
146
147 sub calculate {
148         my ($line, $column, $filters) = @_;
149         my @mainloop;
150         my @loopfooter;
151         my @loopcol;
152         my @loopline;
153         my @looprow;
154         my %globalline;
155         my $grantotal =0;
156 # extract parameters
157         my $dbh = C4::Context->dbh;
158
159 # Filters
160 # Checking filters
161 #
162         my @loopfilter;
163         for (my $i=0;$i<=6;$i++) {
164                 my %cell;
165                 if ( @$filters[$i] ) {
166                         if (($i==1) and (@$filters[$i-1])) {
167                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
168                         }
169                         $cell{filter} .= @$filters[$i];
170                         $cell{crit} .="Issue From" if ($i==0);
171                         $cell{crit} .="Issue To" if ($i==1);
172                         $cell{crit} .="Return From" if ($i==2);
173                         $cell{crit} .="Return To" if ($i==3);
174                         $cell{crit} .="Branch" if ($i==4);
175                         $cell{crit} .="Doc Type" if ($i==5);
176                         $cell{crit} .="Bor Cat" if ($i==6);
177                         $cell{crit} .="Day" if ($i==7);
178                         $cell{crit} .="Month" if ($i==8);
179                         $cell{crit} .="Year" if ($i==9);
180                         push @loopfilter, \%cell;
181                 }
182         }
183         my $colfield;
184         my $colorder;
185         if ($column){
186                 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
187                 $column = "biblioitems.".$column if $column=~/itemtype/;
188                 $column = "borrowers.".$column if $column=~/categorycode/;
189                 my @colfilter ;
190                 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
191                 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
192                 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ )  ;
193                 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ )  ;
194                 $colfilter[0] = @$filters[4] if ($column =~ /branch/ )  ;
195                 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ )  ;
196                 $colfilter[0] = @$filters[6] if ($column =~ /category/ )  ;
197         #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
198                 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
199                 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
200                 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
201         #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
202                                                                                                 
203         # loop cols.
204                 if ($column eq "Day") {
205                         #Display by day
206                         $column = "issues.timestamp";
207                         $colfield .="dayname($column)";  
208                         $colorder .="weekday($column)";
209                 } elsif ($column eq "Month") {
210                         #Display by Month
211                         $column = "issues.timestamp";
212                         $colfield .="monthname($column)";  
213                         $colorder .="month($column)";  
214                 } elsif ($column eq "Year") {
215                         #Display by Year
216                         $column = "issues.timestamp";
217                         $colfield .="Year($column)";
218                         $colorder .= $column;
219                 } else {
220                         $colfield .= $column;
221                         $colorder .= $column;
222                 }  
223                 
224                 my $strsth2;
225                 $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";
226                 if (($column=~/timestamp/) or ($column=~/returndate/)){
227                         if ($colfilter[1] and ($colfilter[0])){
228                                 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
229                         } elsif ($colfilter[1]) {
230                                         $strsth2 .= " and $column < '$colfilter[1]' " ;
231                         } elsif ($colfilter[0]) {
232                                 $strsth2 .= " and $column > '$colfilter[0]' " ;
233                         }
234                 } elsif ($colfilter[0]) {
235                         $colfilter[0] =~ s/\*/%/g;
236                         $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
237                 }
238                 $strsth2 .=" group by $colfield";
239                 $strsth2 .=" order by $colorder";
240                 warn "". $strsth2;
241                 
242                 my $sth2 = $dbh->prepare( $strsth2 );
243                 if (( @colfilter ) and ($colfilter[1])){
244                         $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
245                 } elsif ($colfilter[0]) {
246                         $sth2->execute($colfilter[0]);
247                 } else {
248                         $sth2->execute;
249                 }
250                 
251         
252                 while (my ($celvalue) = $sth2->fetchrow) {
253                         my %cell;
254         #               my %ft;
255         #               warn "coltitle :".$celvalue;
256                         $cell{coltitle} = $celvalue;
257         #               $ft{totalcol} = 0;
258                         push @loopcol, \%cell;
259                 }
260         #       warn "fin des titres colonnes";
261         }
262         
263         my $i=0;
264 #       my @totalcol;
265         my $hilighted=-1;
266         
267         #Initialization of cell values.....
268         my @table;
269         
270 #       warn "init table";
271         for (my $i=1;$i<=$line;$i++) {
272                 foreach my $col ( @loopcol ) {
273 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
274                         $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
275                 }
276         }
277
278
279 # preparing calculation
280         my $strcalc ;
281         
282 # Processing average loanperiods
283         $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
284         $strcalc .= " , $colfield " if ($colfield);
285         $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";
286
287         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
288         $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
289         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
290         $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
291         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
292         $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
293         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
294         $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
295         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
296         $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
297         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
298         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
299         @$filters[6]=~ s/\*/%/g if (@$filters[6]);
300         $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
301         @$filters[7]=~ s/\*/%/g if (@$filters[7]);
302         $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
303         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
304         $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
305         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
306         $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
307         
308         $strcalc .= " group by biblio.biblionumber";
309         $strcalc .= ", $colfield" if ($column);
310         $strcalc .= " order by ";
311         $strcalc .= "$colfield, " if ($colfield);
312         $strcalc .= "RANK DESC ";
313         my $max;
314         if (@loopcol) {
315                 $max = $line*@loopcol;
316         } else { $max=$line;}
317         $strcalc .= " LIMIT 0,$max";
318         warn "SQL :". $strcalc;
319         
320         my $dbcalc = $dbh->prepare($strcalc);
321         $dbcalc->execute;
322 #       warn "filling table";
323         my $previous_col;
324         my $i=1;
325         while (my  @data = $dbcalc->fetchrow) {
326                 my ($row, $rank, $id, $col )=@data;
327                 $col = "zzEMPTY" if ($col eq undef);
328                 $i=1 if (($previous_col) and not($col eq $previous_col));
329                 $table[$i]->{$col}->{'name'}=$row;
330                 $table[$i]->{$col}->{'count'}=$rank;
331                 $table[$i]->{$col}->{'link'}=$id;
332 #               warn " ".$i." ".$col. " ".$row;
333                 $i++;
334                 $previous_col=$col;
335         }
336         
337         push @loopcol,{coltitle => "Global"} if not($column);
338         
339         for ($i=1; $i<=$line;$i++) {
340                 my @loopcell;
341                 warn " $i";
342                 #@loopcol ensures the order for columns is common with column titles
343                 # and the number matches the number of columns
344                 my $colcount=0;
345                 foreach my $col ( @loopcol ) {
346 #                       warn " colonne :$col->{coltitle}";
347                         my $value;
348                         my $count=0;
349                         my $link;
350                         if (@loopcol){
351                                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
352                                 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
353                                 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
354                         } else {
355                                 $value =$table[$i]->{"zzEMPTY"}->{'name'};
356                                 $count =$table[$i]->{"zzEMPTY"}->{'count'};
357                                 $link =$table[$i]->{"zzEMPTY"}->{'link'};
358                         }
359 #                       warn " ".$i ." value:$value count:$count reference:$link";
360                         push @loopcell, {value => $value, count =>$count, reference => $link} ;
361                 }
362                 #warn "row : $row colcount:$colcount";
363                 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
364                 push @looprow,{ 'rowtitle' => $i ,
365                                                 'loopcell' => \@loopcell,
366                                                 'hilighted' => ($hilighted >0),
367                                                 #'totalrow' => ($total)?sprintf("%.2f",$total):0
368                                         };
369                 $hilighted = -$hilighted;
370         }
371 #       
372                         
373
374         # the header of the table
375         $globalline{loopfilter}=\@loopfilter;
376         # the core of the table
377         $globalline{looprow} = \@looprow;
378         $globalline{loopcol} = \@loopcol;
379 #       # the foot (totals by borrower type)
380         $globalline{loopfooter} = \@loopfooter;
381         $globalline{total}= $grantotal;
382         $globalline{line} = $line;
383         $globalline{column} = $column;
384         push @mainloop,\%globalline;
385         return \@mainloop;
386 }
387
388 1;