MT2621 : Fix simple barcodes display
[koha.git] / reports / cat_issues_top.pl
blobd7f2e443f43d20d19bd505fb05db62b7d3db4d17
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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI;
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::Dates qw/format_date format_date_in_iso/;
32 use C4::Members;
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.tmpl";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->param("Filter");
50 $filters[0]=format_date_in_iso($filters[0]);
51 $filters[1]=format_date_in_iso($filters[1]);
52 $filters[2]=format_date_in_iso($filters[2]);
53 $filters[3]=format_date_in_iso($filters[3]);
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
56 my $mime = $input->param("MIME");
57 #warn "calcul : ".$calc;
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => $fullreportname,
60 query => $input,
61 type => "intranet",
62 authnotrequired => 0,
63 flagsrequired => { reports => '*'},
64 debug => 1,
65 });
66 our $sep = $input->param("sep");
67 $sep = "\t" if ($sep eq 'tabulation');
68 $template->param(do_it => $do_it,
69 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
71 if ($do_it) {
72 # Displaying results
73 my $results = calculate($limit, $column, \@filters);
74 if ($output eq "screen"){
75 # Printing results to screen
76 $template->param(mainloop => $results,
77 limit => $limit);
78 output_html_with_http_headers $input, $cookie, $template->output;
79 exit(1);
80 } else {
81 # Printing to a csv file
82 print $input->header(-type => 'application/vnd.sun.xml.calc',
83 -encoding => 'utf-8',
84 -attachment=>"$basename.csv",
85 -filename=>"$basename.csv" );
86 my $cols = @$results[0]->{loopcol};
87 my $lines = @$results[0]->{looprow};
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;
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;
102 print $line->{totalrow};
103 print "\n";
105 # footer
106 print "TOTAL";
107 $cols = @$results[0]->{loopfooter};
108 foreach my $col ( @$cols ) {
109 print $sep.$col->{totalcol};
111 print $sep.@$results[0]->{total};
112 exit(1);
114 # Displaying choices
115 } else {
116 my $dbh = C4::Context->dbh;
117 my @values;
118 my %labels;
119 my %select;
120 my $req;
122 my @mime = ( C4::Context->preference("MIME") );
123 # foreach my $mime (@mime){
124 # warn "".$mime;
127 my $CGIextChoice=CGI::scrolling_list(
128 -name => 'MIME',
129 -id => 'MIME',
130 -values => \@mime,
131 -size => 1,
132 -multiple => 0 );
134 my $CGIsepChoice=GetDelimiterChoices;
136 #doctype
137 my $itemtypes = GetItemTypes;
138 my @itemtypeloop;
139 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
140 my %row =(value => $thisitemtype,
141 description => $itemtypes->{$thisitemtype}->{'description'},
143 push @itemtypeloop, \%row;
146 #borcat
147 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
148 my @borcatloop;
149 foreach my $thisborcat (sort {$labels->{$a} cmp $labels->{$b}} keys %$labels) {
150 my %row =(value => $thisborcat,
151 description => $labels->{$thisborcat},
153 push @borcatloop, \%row;
156 #Day
157 #Month
158 $template->param(
159 CGIextChoice => $CGIextChoice,
160 CGIsepChoice => $CGIsepChoice,
161 branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}),
162 itemtypeloop =>\@itemtypeloop,
163 borcatloop =>\@borcatloop,
165 output_html_with_http_headers $input, $cookie, $template->output;
171 sub calculate {
172 my ($line, $column, $filters) = @_;
173 my @mainloop;
174 my @loopfooter;
175 my @loopcol;
176 my @loopline;
177 my @looprow;
178 my %globalline;
179 my $grantotal =0;
180 # extract parameters
181 my $dbh = C4::Context->dbh;
183 # Filters
184 # Checking filters
186 my @loopfilter;
187 for (my $i=0;$i<=6;$i++) {
188 my %cell;
189 if ( @$filters[$i] ) {
190 if (($i==1) and (@$filters[$i-1])) {
191 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
193 # format the dates filters, otherwise just fill as is
194 if ($i>=2) {
195 $cell{filter} .= @$filters[$i];
196 } else {
197 $cell{filter} .= format_date(@$filters[$i]);
198 } $cell{crit} .="Issue From" if ($i==0);
199 $cell{crit} .="Issue To" if ($i==1);
200 $cell{crit} .="Return From" if ($i==2);
201 $cell{crit} .="Return To" if ($i==3);
202 $cell{crit} .="Branch" if ($i==4);
203 $cell{crit} .="Doc Type" if ($i==5);
204 $cell{crit} .="Bor Cat" if ($i==6);
205 $cell{crit} .="Day" if ($i==7);
206 $cell{crit} .="Month" if ($i==8);
207 $cell{crit} .="Year" if ($i==9);
208 push @loopfilter, \%cell;
211 my $colfield;
212 my $colorder;
213 if ($column){
214 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
215 if($column=~/itemtype/){
216 $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
218 $column = "borrowers.".$column if $column=~/categorycode/;
219 my @colfilter ;
220 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
221 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
222 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
223 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
224 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
225 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
226 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
227 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
228 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
229 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
230 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
231 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
233 # loop cols.
234 if ($column eq "Day") {
235 #Display by day
236 $column = "old_issues.timestamp";
237 $colfield .="dayname($column)";
238 $colorder .="weekday($column)";
239 } elsif ($column eq "Month") {
240 #Display by Month
241 $column = "old_issues.timestamp";
242 $colfield .="monthname($column)";
243 $colorder .="month($column)";
244 } elsif ($column eq "Year") {
245 #Display by Year
246 $column = "old_issues.timestamp";
247 $colfield .="Year($column)";
248 $colorder .= $column;
249 } else {
250 $colfield .= $column;
251 $colorder .= $column;
254 my $strsth2;
255 $strsth2 .= "SELECT distinctrow $colfield
256 FROM `old_issues`
257 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
258 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
259 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
260 WHERE 1";
261 if (($column=~/timestamp/) or ($column=~/returndate/)){
262 if ($colfilter[1] and ($colfilter[0])){
263 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
264 } elsif ($colfilter[1]) {
265 $strsth2 .= " and $column < '$colfilter[1]' " ;
266 } elsif ($colfilter[0]) {
267 $strsth2 .= " and $column > '$colfilter[0]' " ;
269 } elsif ($colfilter[0]) {
270 $colfilter[0] =~ s/\*/%/g;
271 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
273 $strsth2 .=" group by $colfield";
274 $strsth2 .=" order by $colorder";
275 warn "". $strsth2;
277 my $sth2 = $dbh->prepare( $strsth2 );
278 if (( @colfilter ) and ($colfilter[1])){
279 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
280 } elsif ($colfilter[0]) {
281 $sth2->execute($colfilter[0]);
282 } else {
283 $sth2->execute;
287 while (my ($celvalue) = $sth2->fetchrow) {
288 my %cell;
289 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
290 push @loopcol, \%cell;
292 # warn "fin des titres colonnes";
295 my $i=0;
296 # my @totalcol;
297 my $hilighted=-1;
299 #Initialization of cell values.....
300 my @table;
302 # warn "init table";
303 for (my $i=1;$i<=$line;$i++) {
304 foreach my $col ( @loopcol ) {
305 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
306 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
311 # preparing calculation
312 my $strcalc ;
314 # Processing average loanperiods
315 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
316 $strcalc .= " , $colfield " if ($colfield);
317 $strcalc .= " FROM `old_issues`
318 LEFT JOIN items USING(itemnumber)
319 LEFT JOIN biblio USING(biblionumber)
320 LEFT JOIN biblioitems USING(biblionumber)
321 LEFT JOIN borrowers USING(borrowernumber)
322 WHERE 1";
324 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
325 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
326 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
327 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
328 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
329 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
330 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
331 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
332 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
333 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
334 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
335 if ( @$filters[5] ){
336 if(C4::Context->preference('item-level_itypes') ){
337 $strcalc .= " AND items.itype like "
338 }else{
339 $strcalc .= " AND biblioitems.itemtype like "
341 $strcalc .= "'" . @$filters[5] ."'" ;
343 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
344 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
345 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
346 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
347 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
348 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
349 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
350 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
352 $strcalc .= " group by biblio.biblionumber";
353 $strcalc .= ", $colfield" if ($column);
354 $strcalc .= " order by RANK DESC";
355 $strcalc .= ", $colfield " if ($colfield);
357 my $dbcalc = $dbh->prepare($strcalc);
358 $dbcalc->execute;
359 my $previous_col;
360 my %indice;
361 while (my @data = $dbcalc->fetchrow) {
362 my ($row, $rank, $id, $col )=@data;
363 $col = "zzEMPTY" if (!defined($col));
364 $indice{$col}=1 if (not($indice{$col}));
365 $table[$indice{$col}]->{$col}->{'name'}=$row;
366 $table[$indice{$col}]->{$col}->{'count'}=$rank;
367 $table[$indice{$col}]->{$col}->{'link'}=$id;
368 $indice{$col}++;
371 push @loopcol,{coltitle => "Global"} if not($column);
373 for ($i=1; $i<=$line;$i++) {
374 my @loopcell;
375 #@loopcol ensures the order for columns is common with column titles
376 # and the number matches the number of columns
377 my $colcount=0;
378 foreach my $col ( @loopcol ) {
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 push @loopcell, {value => $value, count =>$count, reference => $link} ;
393 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
394 push @looprow,{ 'rowtitle' => $i ,
395 'loopcell' => \@loopcell,
396 'hilighted' => ($hilighted >0),
398 $hilighted = -$hilighted;
403 # the header of the table
404 $globalline{loopfilter}=\@loopfilter;
405 # the core of the table
406 $globalline{looprow} = \@looprow;
407 $globalline{loopcol} = \@loopcol;
408 # # the foot (totals by borrower type)
409 $globalline{loopfooter} = \@loopfooter;
410 $globalline{total}= $grantotal;
411 $globalline{line} = $line;
412 $globalline{column} = $column;
413 push @mainloop,\%globalline;
414 return \@mainloop;