Fix number of tests for db_structure.t
[koha.git] / reports / cat_issues_top.pl
blob8edd63a2c5259ec2983cd9f34a9b15d0c3a8a6b1
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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Circulation;
30 use C4::Reports;
31 use C4::Members;
32 use Koha::DateUtils;
34 =head1 NAME
36 plugin that shows a stats on borrowers
38 =head1 DESCRIPTION
40 =over 2
42 =cut
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/cat_issues_top.tt";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->multi_param("Filter");
50 foreach ( @filters[0..3] ) {
51 $_ and $_ = eval { output_pref( { dt => dt_from_string ( $_ ), dateonly => 1, dateformat => 'iso' } ); };
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
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 => { reports => '*'},
63 debug => 1,
64 });
65 our $sep = $input->param("sep");
66 $sep = "\t" if ($sep eq 'tabulation');
67 $template->param(do_it => $do_it,
69 if ($do_it) {
70 # Displaying results
71 my $results = calculate($limit, $column, \@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop => $results,
75 limit => $limit);
76 output_html_with_http_headers $input, $cookie, $template->output;
77 exit;
78 } else {
79 # Printing to a csv file
80 print $input->header(-type => 'application/vnd.sun.xml.calc',
81 -encoding => 'utf-8',
82 -attachment=>"$basename.csv",
83 -filename=>"$basename.csv" );
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
86 # header top-right
87 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
88 # Other header
89 foreach my $col ( @$cols ) {
90 print $col->{coltitle}.$sep;
92 print "Total\n";
93 # Table
94 foreach my $line ( @$lines ) {
95 my $x = $line->{loopcell};
96 print $line->{rowtitle}.$sep;
97 foreach my $cell (@$x) {
98 print $cell->{value}.$sep;
100 print $line->{totalrow};
101 print "\n";
103 # footer
104 print "TOTAL";
105 $cols = @$results[0]->{loopfooter};
106 foreach my $col ( @$cols ) {
107 print $sep.$col->{totalcol};
109 print $sep.@$results[0]->{total};
110 exit;
112 # Displaying choices
113 } else {
114 my $dbh = C4::Context->dbh;
115 my @values;
116 my %labels;
117 my %select;
118 my $req;
120 my $CGIextChoice = ( 'CSV' ); # FIXME translation
121 my $CGIsepChoice=GetDelimiterChoices;
123 #doctype
124 my $itemtypes = GetItemTypes;
125 my @itemtypeloop;
126 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description}} keys %$itemtypes) {
127 my %row =(value => $thisitemtype,
128 description => $itemtypes->{$thisitemtype}->{translated_description},
130 push @itemtypeloop, \%row;
133 #borcat
134 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
135 my @borcatloop;
136 foreach my $thisborcat (sort {$labels->{$a} cmp $labels->{$b}} keys %$labels) {
137 my %row =(value => $thisborcat,
138 description => $labels->{$thisborcat},
140 push @borcatloop, \%row;
143 #Day
144 #Month
145 $template->param(
146 CGIextChoice => $CGIextChoice,
147 CGIsepChoice => $CGIsepChoice,
148 branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}),
149 itemtypeloop =>\@itemtypeloop,
150 borcatloop =>\@borcatloop,
152 output_html_with_http_headers $input, $cookie, $template->output;
158 sub calculate {
159 my ($line, $column, $filters) = @_;
160 my @mainloop;
161 my @loopfooter;
162 my @loopcol;
163 my @loopline;
164 my @looprow;
165 my %globalline;
166 my $grantotal =0;
167 # extract parameters
168 my $dbh = C4::Context->dbh;
170 # Filters
171 # Checking filters
173 my @loopfilter;
174 for (my $i=0;$i<=6;$i++) {
175 my %cell;
176 if ( @$filters[$i] ) {
177 if (($i==1) and (@$filters[$i-1])) {
178 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
180 # format the dates filters, otherwise just fill as is
181 if ($i>=2) {
182 $cell{filter} .= @$filters[$i];
183 } else {
184 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
185 if ( @$filters[$i] );
187 $cell{crit} .="Issue From" if ($i==0);
188 $cell{crit} .="Issue To" if ($i==1);
189 $cell{crit} .="Return From" if ($i==2);
190 $cell{crit} .="Return To" if ($i==3);
191 $cell{crit} .="Branch" if ($i==4);
192 $cell{crit} .="Doc Type" if ($i==5);
193 $cell{crit} .="Bor Cat" if ($i==6);
194 $cell{crit} .="Day" if ($i==7);
195 $cell{crit} .="Month" if ($i==8);
196 $cell{crit} .="Year" if ($i==9);
197 push @loopfilter, \%cell;
200 my $colfield;
201 my $colorder;
202 if ($column){
203 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
204 if($column=~/itemtype/){
205 $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
207 $column = "borrowers.".$column if $column=~/categorycode/;
208 my @colfilter ;
209 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
210 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
211 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
212 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
213 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
214 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
215 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
216 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
217 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
218 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
219 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
220 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
222 # loop cols.
223 if ($column eq "Day") {
224 #Display by day
225 $column = "old_issues.timestamp";
226 $colfield .="dayname($column)";
227 $colorder .="weekday($column)";
228 } elsif ($column eq "Month") {
229 #Display by Month
230 $column = "old_issues.timestamp";
231 $colfield .="monthname($column)";
232 $colorder .="month($column)";
233 } elsif ($column eq "Year") {
234 #Display by Year
235 $column = "old_issues.timestamp";
236 $colfield .="Year($column)";
237 $colorder .= $column;
238 } else {
239 $colfield .= $column;
240 $colorder .= $column;
243 my $strsth2;
244 $strsth2 .= "SELECT distinctrow $colfield
245 FROM `old_issues`
246 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
247 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
248 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
249 WHERE 1";
250 if (($column=~/timestamp/) or ($column=~/returndate/)){
251 if ($colfilter[1] and ($colfilter[0])){
252 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
253 } elsif ($colfilter[1]) {
254 $strsth2 .= " and $column < '$colfilter[1]' " ;
255 } elsif ($colfilter[0]) {
256 $strsth2 .= " and $column > '$colfilter[0]' " ;
258 } elsif ($colfilter[0]) {
259 $colfilter[0] =~ s/\*/%/g;
260 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
262 $strsth2 .=" group by $colfield";
263 $strsth2 .=" order by $colorder";
265 my $sth2 = $dbh->prepare( $strsth2 );
266 if (( @colfilter ) and ($colfilter[1])){
267 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
268 } elsif ($colfilter[0]) {
269 $sth2->execute($colfilter[0]);
270 } else {
271 $sth2->execute;
275 while (my ($celvalue) = $sth2->fetchrow) {
276 my %cell;
277 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
278 push @loopcol, \%cell;
280 # warn "fin des titres colonnes";
283 my $i=0;
284 # my @totalcol;
285 my $hilighted=-1;
287 #Initialization of cell values.....
288 my @table;
290 # warn "init table";
291 for (my $i=1;$i<=$line;$i++) {
292 foreach my $col ( @loopcol ) {
293 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
294 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
299 # preparing calculation
300 my $strcalc ;
302 # Processing average loanperiods
303 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
304 $strcalc .= " , $colfield " if ($colfield);
305 $strcalc .= " FROM `old_issues`
306 LEFT JOIN items USING(itemnumber)
307 LEFT JOIN biblio USING(biblionumber)
308 LEFT JOIN biblioitems USING(biblionumber)
309 LEFT JOIN borrowers USING(borrowernumber)
310 WHERE 1";
312 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
313 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
314 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
315 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
316 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
317 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
318 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
319 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
320 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
321 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
322 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
323 if ( @$filters[5] ){
324 if(C4::Context->preference('item-level_itypes') ){
325 $strcalc .= " AND items.itype like "
326 }else{
327 $strcalc .= " AND biblioitems.itemtype like "
329 $strcalc .= "'" . @$filters[5] ."'" ;
331 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
332 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
333 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
334 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
335 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
336 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
337 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
338 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
340 $strcalc .= " group by biblio.biblionumber";
341 $strcalc .= ", $colfield" if ($column);
342 $strcalc .= " order by RANK DESC";
343 $strcalc .= ", $colfield " if ($colfield);
345 my $dbcalc = $dbh->prepare($strcalc);
346 $dbcalc->execute;
347 my $previous_col;
348 my %indice;
349 while (my @data = $dbcalc->fetchrow) {
350 my ($row, $rank, $id, $col )=@data;
351 $col = "zzEMPTY" if (!defined($col));
352 $indice{$col}=1 if (not($indice{$col}));
353 $table[$indice{$col}]->{$col}->{'name'}=$row;
354 $table[$indice{$col}]->{$col}->{'count'}=$rank;
355 $table[$indice{$col}]->{$col}->{'link'}=$id;
356 $indice{$col}++;
359 push @loopcol,{coltitle => "Global"} if not($column);
361 for ($i=1; $i<=$line;$i++) {
362 my @loopcell;
363 #@loopcol ensures the order for columns is common with column titles
364 # and the number matches the number of columns
365 my $colcount=0;
366 foreach my $col ( @loopcol ) {
367 my $value;
368 my $count=0;
369 my $link;
370 if (@loopcol){
371 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
372 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
373 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
374 } else {
375 $value =$table[$i]->{"zzEMPTY"}->{'name'};
376 $count =$table[$i]->{"zzEMPTY"}->{'count'};
377 $link =$table[$i]->{"zzEMPTY"}->{'link'};
379 push @loopcell, {value => $value, count =>$count, reference => $link} ;
381 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
382 push @looprow,{ 'rowtitle' => $i ,
383 'loopcell' => \@loopcell,
384 'hilighted' => ($hilighted >0),
386 $hilighted = -$hilighted;
391 # the header of the table
392 $globalline{loopfilter}=\@loopfilter;
393 # the core of the table
394 $globalline{looprow} = \@looprow;
395 $globalline{loopcol} = \@loopcol;
396 # # the foot (totals by borrower type)
397 $globalline{loopfooter} = \@loopfooter;
398 $globalline{total}= $grantotal;
399 $globalline{line} = $line;
400 $globalline{column} = $column;
401 push @mainloop,\%globalline;
402 return \@mainloop;