Bug 3587 - OPACdefaultSortField options error
[koha.git] / reports / cat_issues_top.pl
blob12c888444734b8d5e57181edc5c7627217946f9f
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25 use C4::Branch; # GetBranches
26 use C4::Output;
27 use C4::Koha;
28 use C4::Circulation;
29 use C4::Reports;
30 use C4::Dates qw/format_date format_date_in_iso/;
31 use C4::Members;
33 =head1 NAME
35 plugin that shows a stats on borrowers
37 =head1 DESCRIPTION
39 =over 2
41 =cut
43 my $input = new CGI;
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/cat_issues_top.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 $filters[0]=format_date_in_iso($filters[0]);
50 $filters[1]=format_date_in_iso($filters[1]);
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
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 our $sep = $input->param("sep");
64 $sep = "\t" if ($sep eq 'tabulation');
65 $template->param(do_it => $do_it,
66 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
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 # header top-right
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 # Other header
88 foreach my $col ( @$cols ) {
89 print $col->{coltitle}.$sep;
91 print "Total\n";
92 # Table
93 foreach my $line ( @$lines ) {
94 my $x = $line->{loopcell};
95 print $line->{rowtitle}.$sep;
96 foreach my $cell (@$x) {
97 print $cell->{value}.$sep;
99 print $line->{totalrow};
100 print "\n";
102 # footer
103 print "TOTAL";
104 $cols = @$results[0]->{loopfooter};
105 foreach my $col ( @$cols ) {
106 print $sep.$col->{totalcol};
108 print $sep.@$results[0]->{total};
109 exit(1);
111 # Displaying choices
112 } else {
113 my $dbh = C4::Context->dbh;
114 my @values;
115 my %labels;
116 my %select;
117 my $req;
119 my @mime = ( C4::Context->preference("MIME") );
120 # foreach my $mime (@mime){
121 # warn "".$mime;
124 my $CGIextChoice=CGI::scrolling_list(
125 -name => 'MIME',
126 -id => 'MIME',
127 -values => \@mime,
128 -size => 1,
129 -multiple => 0 );
131 my $CGIsepChoice=GetDelimiterChoices;
133 #doctype
134 my $itemtypes = GetItemTypes;
135 my @itemtypeloop;
136 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
137 my %row =(value => $thisitemtype,
138 description => $itemtypes->{$thisitemtype}->{'description'},
140 push @itemtypeloop, \%row;
143 #borcat
144 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
145 my @borcatloop;
146 foreach my $thisborcat (sort {$labels->{$a} cmp $labels->{$b}} keys %$labels) {
147 my %row =(value => $thisborcat,
148 description => $labels->{$thisborcat},
150 push @borcatloop, \%row;
153 #Day
154 #Month
155 $template->param(
156 CGIextChoice => $CGIextChoice,
157 CGIsepChoice => $CGIsepChoice,
158 branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}),
159 itemtypeloop =>\@itemtypeloop,
160 borcatloop =>\@borcatloop,
162 output_html_with_http_headers $input, $cookie, $template->output;
168 sub calculate {
169 my ($line, $column, $filters) = @_;
170 my @mainloop;
171 my @loopfooter;
172 my @loopcol;
173 my @loopline;
174 my @looprow;
175 my %globalline;
176 my $grantotal =0;
177 # extract parameters
178 my $dbh = C4::Context->dbh;
180 # Filters
181 # Checking filters
183 my @loopfilter;
184 for (my $i=0;$i<=6;$i++) {
185 my %cell;
186 if ( @$filters[$i] ) {
187 if (($i==1) and (@$filters[$i-1])) {
188 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
190 # format the dates filters, otherwise just fill as is
191 if ($i>=2) {
192 $cell{filter} .= @$filters[$i];
193 } else {
194 $cell{filter} .= format_date(@$filters[$i]);
195 } $cell{crit} .="Issue From" if ($i==0);
196 $cell{crit} .="Issue To" if ($i==1);
197 $cell{crit} .="Return From" if ($i==2);
198 $cell{crit} .="Return To" if ($i==3);
199 $cell{crit} .="Branch" if ($i==4);
200 $cell{crit} .="Doc Type" if ($i==5);
201 $cell{crit} .="Bor Cat" if ($i==6);
202 $cell{crit} .="Day" if ($i==7);
203 $cell{crit} .="Month" if ($i==8);
204 $cell{crit} .="Year" if ($i==9);
205 push @loopfilter, \%cell;
208 my $colfield;
209 my $colorder;
210 if ($column){
211 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
212 $column = "biblioitems.".$column if $column=~/itemtype/;
213 $column = "borrowers.".$column if $column=~/categorycode/;
214 my @colfilter ;
215 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
216 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
217 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
218 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
219 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
220 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
221 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
222 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
223 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
224 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
225 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
226 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
228 # loop cols.
229 if ($column eq "Day") {
230 #Display by day
231 $column = "old_issues.timestamp";
232 $colfield .="dayname($column)";
233 $colorder .="weekday($column)";
234 } elsif ($column eq "Month") {
235 #Display by Month
236 $column = "old_issues.timestamp";
237 $colfield .="monthname($column)";
238 $colorder .="month($column)";
239 } elsif ($column eq "Year") {
240 #Display by Year
241 $column = "old_issues.timestamp";
242 $colfield .="Year($column)";
243 $colorder .= $column;
244 } else {
245 $colfield .= $column;
246 $colorder .= $column;
249 my $strsth2;
250 $strsth2 .= "SELECT distinctrow $colfield
251 FROM `old_issues`
252 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
253 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
254 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
255 WHERE 1";
256 if (($column=~/timestamp/) or ($column=~/returndate/)){
257 if ($colfilter[1] and ($colfilter[0])){
258 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
259 } elsif ($colfilter[1]) {
260 $strsth2 .= " and $column < '$colfilter[1]' " ;
261 } elsif ($colfilter[0]) {
262 $strsth2 .= " and $column > '$colfilter[0]' " ;
264 } elsif ($colfilter[0]) {
265 $colfilter[0] =~ s/\*/%/g;
266 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
268 $strsth2 .=" group by $colfield";
269 $strsth2 .=" order by $colorder";
270 warn "". $strsth2;
272 my $sth2 = $dbh->prepare( $strsth2 );
273 if (( @colfilter ) and ($colfilter[1])){
274 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
275 } elsif ($colfilter[0]) {
276 $sth2->execute($colfilter[0]);
277 } else {
278 $sth2->execute;
282 while (my ($celvalue) = $sth2->fetchrow) {
283 my %cell;
284 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
285 push @loopcol, \%cell;
287 # warn "fin des titres colonnes";
290 my $i=0;
291 # my @totalcol;
292 my $hilighted=-1;
294 #Initialization of cell values.....
295 my @table;
297 # warn "init table";
298 for (my $i=1;$i<=$line;$i++) {
299 foreach my $col ( @loopcol ) {
300 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
301 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
306 # preparing calculation
307 my $strcalc ;
309 # Processing average loanperiods
310 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
311 $strcalc .= " , $colfield " if ($colfield);
312 $strcalc .= " FROM `old_issues`
313 LEFT JOIN borrowers ON old_issues.borrowernumber=borrowers.borrowernumber
314 LEFT JOIN (items
315 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber)
316 ON items.itemnumber=old_issues.itemnumber
317 LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber)
318 WHERE 1";
320 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
321 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
322 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
323 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
324 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
325 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
326 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
327 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
328 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
329 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
330 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
331 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
332 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
333 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
334 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
335 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
336 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
337 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
338 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
339 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
341 $strcalc .= " group by biblio.biblionumber";
342 $strcalc .= ", $colfield" if ($column);
343 $strcalc .= " order by RANK DESC";
344 $strcalc .= ", $colfield " if ($colfield);
346 # my $max;
347 # if (@loopcol) {
348 # $max = $line*@loopcol;
349 # } else { $max=$line;}
350 # $strcalc .= " LIMIT 0,$max";
351 warn "SQL :". $strcalc;
353 my $dbcalc = $dbh->prepare($strcalc);
354 $dbcalc->execute;
355 # warn "filling table";
356 my $previous_col;
357 my %indice;
358 while (my @data = $dbcalc->fetchrow) {
359 my ($row, $rank, $id, $col )=@data;
360 $col = "zzEMPTY" if (!defined($col));
361 $indice{$col}=1 if (not($indice{$col}));
362 $table[$indice{$col}]->{$col}->{'name'}=$row;
363 $table[$indice{$col}]->{$col}->{'count'}=$rank;
364 $table[$indice{$col}]->{$col}->{'link'}=$id;
365 # warn " ".$i." ".$col. " ".$row;
366 $indice{$col}++;
369 push @loopcol,{coltitle => "Global"} if not($column);
371 for ($i=1; $i<=$line;$i++) {
372 my @loopcell;
373 warn " $i";
374 #@loopcol ensures the order for columns is common with column titles
375 # and the number matches the number of columns
376 my $colcount=0;
377 foreach my $col ( @loopcol ) {
378 # warn " colonne :$col->{coltitle}";
379 my $value;
380 my $count=0;
381 my $link;
382 if (@loopcol){
383 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
384 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
385 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
386 } else {
387 $value =$table[$i]->{"zzEMPTY"}->{'name'};
388 $count =$table[$i]->{"zzEMPTY"}->{'count'};
389 $link =$table[$i]->{"zzEMPTY"}->{'link'};
391 # warn " ".$i ." value:$value count:$count reference:$link";
392 push @loopcell, {value => $value, count =>$count, reference => $link} ;
394 #warn "row : $row colcount:$colcount";
395 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
396 push @looprow,{ 'rowtitle' => $i ,
397 'loopcell' => \@loopcell,
398 'hilighted' => ($hilighted >0),
399 #'totalrow' => ($total)?sprintf("%.2f",$total):0
401 $hilighted = -$hilighted;
406 # the header of the table
407 $globalline{loopfilter}=\@loopfilter;
408 # the core of the table
409 $globalline{looprow} = \@looprow;
410 $globalline{loopcol} = \@loopcol;
411 # # the foot (totals by borrower type)
412 $globalline{loopfooter} = \@loopfooter;
413 $globalline{total}= $grantotal;
414 $globalline{line} = $line;
415 $globalline{column} = $column;
416 push @mainloop,\%globalline;
417 return \@mainloop;