English intranet updates
[koha.git] / reports / cat_issues_top.pl
blobd07335e5246291fffb42a178f18bd13974a32e6f
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";
276 my $sth2 = $dbh->prepare( $strsth2 );
277 if (( @colfilter ) and ($colfilter[1])){
278 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
279 } elsif ($colfilter[0]) {
280 $sth2->execute($colfilter[0]);
281 } else {
282 $sth2->execute;
286 while (my ($celvalue) = $sth2->fetchrow) {
287 my %cell;
288 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
289 push @loopcol, \%cell;
291 # warn "fin des titres colonnes";
294 my $i=0;
295 # my @totalcol;
296 my $hilighted=-1;
298 #Initialization of cell values.....
299 my @table;
301 # warn "init table";
302 for (my $i=1;$i<=$line;$i++) {
303 foreach my $col ( @loopcol ) {
304 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
305 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
310 # preparing calculation
311 my $strcalc ;
313 # Processing average loanperiods
314 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
315 $strcalc .= " , $colfield " if ($colfield);
316 $strcalc .= " FROM `old_issues`
317 LEFT JOIN items USING(itemnumber)
318 LEFT JOIN biblio USING(biblionumber)
319 LEFT JOIN biblioitems USING(biblionumber)
320 LEFT JOIN borrowers USING(borrowernumber)
321 WHERE 1";
323 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
324 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
325 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
326 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
327 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
328 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
329 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
330 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
331 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
332 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
333 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
334 if ( @$filters[5] ){
335 if(C4::Context->preference('item-level_itypes') ){
336 $strcalc .= " AND items.itype like "
337 }else{
338 $strcalc .= " AND biblioitems.itemtype like "
340 $strcalc .= "'" . @$filters[5] ."'" ;
342 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
343 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
344 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
345 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
346 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
347 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
348 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
349 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
351 $strcalc .= " group by biblio.biblionumber";
352 $strcalc .= ", $colfield" if ($column);
353 $strcalc .= " order by RANK DESC";
354 $strcalc .= ", $colfield " if ($colfield);
356 my $dbcalc = $dbh->prepare($strcalc);
357 $dbcalc->execute;
358 my $previous_col;
359 my %indice;
360 while (my @data = $dbcalc->fetchrow) {
361 my ($row, $rank, $id, $col )=@data;
362 $col = "zzEMPTY" if (!defined($col));
363 $indice{$col}=1 if (not($indice{$col}));
364 $table[$indice{$col}]->{$col}->{'name'}=$row;
365 $table[$indice{$col}]->{$col}->{'count'}=$rank;
366 $table[$indice{$col}]->{$col}->{'link'}=$id;
367 $indice{$col}++;
370 push @loopcol,{coltitle => "Global"} if not($column);
372 for ($i=1; $i<=$line;$i++) {
373 my @loopcell;
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 my $value;
379 my $count=0;
380 my $link;
381 if (@loopcol){
382 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
383 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
384 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
385 } else {
386 $value =$table[$i]->{"zzEMPTY"}->{'name'};
387 $count =$table[$i]->{"zzEMPTY"}->{'count'};
388 $link =$table[$i]->{"zzEMPTY"}->{'link'};
390 push @loopcell, {value => $value, count =>$count, reference => $link} ;
392 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
393 push @looprow,{ 'rowtitle' => $i ,
394 'loopcell' => \@loopcell,
395 'hilighted' => ($hilighted >0),
397 $hilighted = -$hilighted;
402 # the header of the table
403 $globalline{loopfilter}=\@loopfilter;
404 # the core of the table
405 $globalline{looprow} = \@looprow;
406 $globalline{loopcol} = \@loopcol;
407 # # the foot (totals by borrower type)
408 $globalline{loopfooter} = \@loopfooter;
409 $globalline{total}= $grantotal;
410 $globalline{line} = $line;
411 $globalline{column} = $column;
412 push @mainloop,\%globalline;
413 return \@mainloop;