reintroducing size for fields in addbiblio
[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 C4::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Circulation;
30 use C4::Date;
31 use C4::Members;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39 =over 2
40
41 =cut
42
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 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 => { reports => 1},
62                 debug => 1,
63                 });
64 $template->param(do_it => $do_it,
65         DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
66         );
67 if ($do_it) {
68 # Displaying results
69     my $results = calculate($limit, $column, \@filters);
70     if ($output eq "screen"){
71 # Printing results to screen
72         $template->param(mainloop => $results,
73                         limit => $limit);
74         output_html_with_http_headers $input, $cookie, $template->output;
75         exit(1);
76     } else {
77 # Printing to a csv file
78         print $input->header(-type => 'application/vnd.sun.xml.calc',
79                             -encoding    => 'utf-8',
80                             -attachment=>"$basename.csv",
81                             -filename=>"$basename.csv" );
82         my $cols = @$results[0]->{loopcol};
83         my $lines = @$results[0]->{looprow};
84         my $sep;
85         $sep =C4::Context->preference("delimiter");
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;
91         }
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;
99             }
100             print $line->{totalrow};
101             print "\n";
102         }
103 # footer
104         print "TOTAL";
105         $cols = @$results[0]->{loopfooter};
106         foreach my $col ( @$cols ) {
107             print $sep.$col->{totalcol};
108         }
109         print $sep.@$results[0]->{total};
110         exit(1);
111     }
112 # Displaying choices
113 } else {
114     my $dbh = C4::Context->dbh;
115     my @values;
116     my %labels;
117     my %select;
118     my $req;
119     
120     my @mime = ( C4::Context->preference("MIME") );
121 #       foreach my $mime (@mime){
122 #               warn "".$mime;
123 #       }
124     
125     my $CGIextChoice=CGI::scrolling_list(
126                 -name     => 'MIME',
127                 -id       => 'MIME',
128                 -values   => \@mime,
129                 -size     => 1,
130                 -multiple => 0 );
131     
132     my @dels = ( C4::Context->preference("delimiter") );
133     my $CGIsepChoice=CGI::scrolling_list(
134                 -name     => 'sep',
135                 -id       => 'sep',
136                 -values   => \@dels,
137                 -size     => 1,
138                 -multiple => 0 );
139     #branch
140     my $branches = GetBranches;
141     my @branchloop;
142     foreach my $thisbranch (keys %$branches) {
143 #                       my $selected = 1 if $thisbranch eq $branch;
144             my %row =(value => $thisbranch,
145 #                                                                       selected => $selected,
146                                     branchname => $branches->{$thisbranch}->{'branchname'},
147                             );
148             push @branchloop, \%row;
149     }
150
151     #doctype
152     my $itemtypes = GetItemTypes;
153     my @itemtypeloop;
154     foreach my $thisitemtype (keys %$itemtypes) {
155 #                       my $selected = 1 if $thisbranch eq $branch;
156             my %row =(value => $thisitemtype,
157 #                                                                       selected => $selected,
158                                     description => $itemtypes->{$thisitemtype}->{'description'},
159                             );
160             push @itemtypeloop, \%row;
161     }
162     
163     #borcat
164     my ($codes,$labels) = GetborCatFromCatType(undef,undef);
165     my @borcatloop;
166     foreach my $thisborcat (sort keys %$labels) {
167 #                       my $selected = 1 if $thisbranch eq $branch;
168             my %row =(value => $thisborcat,
169 #                                                                       selected => $selected,
170                                     description => $labels->{$thisborcat},
171                             );
172             push @borcatloop, \%row;
173     }
174     
175     #Day
176     #Month
177     $template->param(
178                     CGIextChoice => $CGIextChoice,
179                     CGIsepChoice => $CGIsepChoice,
180                     branchloop =>\@branchloop,
181                     itemtypeloop =>\@itemtypeloop,
182                     borcatloop =>\@borcatloop,
183                     );
184 output_html_with_http_headers $input, $cookie, $template->output;
185 }
186
187
188
189
190 sub calculate {
191     my ($line, $column, $filters) = @_;
192     my @mainloop;
193     my @loopfooter;
194     my @loopcol;
195     my @loopline;
196     my @looprow;
197     my %globalline;
198     my $grantotal =0;
199 # extract parameters
200     my $dbh = C4::Context->dbh;
201
202 # Filters
203 # Checking filters
204 #
205     my @loopfilter;
206     for (my $i=0;$i<=6;$i++) {
207         my %cell;
208         if ( @$filters[$i] ) {
209             if (($i==1) and (@$filters[$i-1])) {
210                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
211             }
212             # format the dates filters, otherwise just fill as is
213             if ($i>=2) {
214                 $cell{filter} .= @$filters[$i];
215             } else {
216                 $cell{filter} .= format_date(@$filters[$i]);
217             }            $cell{crit} .="Issue From" if ($i==0);
218             $cell{crit} .="Issue To" if ($i==1);
219             $cell{crit} .="Return From" if ($i==2);
220             $cell{crit} .="Return To" if ($i==3);
221             $cell{crit} .="Branch" if ($i==4);
222             $cell{crit} .="Doc Type" if ($i==5);
223             $cell{crit} .="Bor Cat" if ($i==6);
224             $cell{crit} .="Day" if ($i==7);
225             $cell{crit} .="Month" if ($i==8);
226             $cell{crit} .="Year" if ($i==9);
227             push @loopfilter, \%cell;
228         }
229     }
230     my $colfield;
231     my $colorder;
232     if ($column){
233         $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
234         $column = "biblioitems.".$column if $column=~/itemtype/;
235         $column = "borrowers.".$column if $column=~/categorycode/;
236         my @colfilter ;
237         $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
238         $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
239         $colfilter[0] = @$filters[2] if ($column =~ /returndate/ )  ;
240         $colfilter[1] = @$filters[3] if ($column =~ /returndate/ )  ;
241         $colfilter[0] = @$filters[4] if ($column =~ /branch/ )  ;
242         $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ )  ;
243         $colfilter[0] = @$filters[6] if ($column =~ /category/ )  ;
244     #   $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
245         $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
246         $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
247         $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
248     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
249                                                 
250     # loop cols.
251         if ($column eq "Day") {
252             #Display by day
253             $column = "issues.timestamp";
254             $colfield .="dayname($column)";  
255             $colorder .="weekday($column)";
256         } elsif ($column eq "Month") {
257             #Display by Month
258             $column = "issues.timestamp";
259             $colfield .="monthname($column)";  
260             $colorder .="month($column)";  
261         } elsif ($column eq "Year") {
262             #Display by Year
263             $column = "issues.timestamp";
264             $colfield .="Year($column)";
265             $colorder .= $column;
266         } else {
267             $colfield .= $column;
268             $colorder .= $column;
269         }  
270         
271         my $strsth2;
272         $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";
273         if (($column=~/timestamp/) or ($column=~/returndate/)){
274             if ($colfilter[1] and ($colfilter[0])){
275                 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
276             } elsif ($colfilter[1]) {
277                     $strsth2 .= " and $column < '$colfilter[1]' " ;
278             } elsif ($colfilter[0]) {
279                 $strsth2 .= " and $column > '$colfilter[0]' " ;
280             }
281         } elsif ($colfilter[0]) {
282             $colfilter[0] =~ s/\*/%/g;
283             $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
284         }
285         $strsth2 .=" group by $colfield";
286         $strsth2 .=" order by $colorder";
287         warn "". $strsth2;
288         
289         my $sth2 = $dbh->prepare( $strsth2 );
290         if (( @colfilter ) and ($colfilter[1])){
291             $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
292         } elsif ($colfilter[0]) {
293             $sth2->execute($colfilter[0]);
294         } else {
295             $sth2->execute;
296         }
297         
298     
299         while (my ($celvalue) = $sth2->fetchrow) {
300             my %cell;
301             $cell{coltitle} = ($celvalue?$celvalue:"NULL");
302             push @loopcol, \%cell;
303         }
304     #   warn "fin des titres colonnes";
305     }
306     
307     my $i=0;
308 #       my @totalcol;
309     my $hilighted=-1;
310     
311     #Initialization of cell values.....
312     my @table;
313     
314 #       warn "init table";
315     for (my $i=1;$i<=$line;$i++) {
316         foreach my $col ( @loopcol ) {
317 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
318             $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
319         }
320     }
321
322
323 # preparing calculation
324     my $strcalc ;
325     
326 # Processing average loanperiods
327     $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
328     $strcalc .= " , $colfield " if ($colfield);
329     $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";
330
331     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
332     $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
333     @$filters[1]=~ s/\*/%/g if (@$filters[1]);
334     $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
335     @$filters[2]=~ s/\*/%/g if (@$filters[2]);
336     $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
337     @$filters[3]=~ s/\*/%/g if (@$filters[3]);
338     $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
339     @$filters[4]=~ s/\*/%/g if (@$filters[4]);
340     $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
341     @$filters[5]=~ s/\*/%/g if (@$filters[5]);
342     $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$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(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
347     @$filters[8]=~ s/\*/%/g if (@$filters[8]);
348     $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
349     @$filters[9]=~ s/\*/%/g if (@$filters[9]);
350     $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
351     
352     $strcalc .= " group by biblio.biblionumber";
353     $strcalc .= ", $colfield" if ($column);
354     $strcalc .= " order by RANK DESC";
355     $strcalc .= ", $colfield " if ($colfield);
356 #       my $max;
357 #       if (@loopcol) {
358 #               $max = $line*@loopcol;
359 #       } else { $max=$line;}
360 #       $strcalc .= " LIMIT 0,$max";
361     warn "SQL :". $strcalc;
362     
363     my $dbcalc = $dbh->prepare($strcalc);
364     $dbcalc->execute;
365 #       warn "filling table";
366     my $previous_col;
367     my %indice;
368     while (my  @data = $dbcalc->fetchrow) {
369         my ($row, $rank, $id, $col )=@data;
370         $col = "zzEMPTY" if ($col eq undef);
371         $indice{$col}=1 if (not($indice{$col}));
372         $table[$indice{$col}]->{$col}->{'name'}=$row;
373         $table[$indice{$col}]->{$col}->{'count'}=$rank;
374         $table[$indice{$col}]->{$col}->{'link'}=$id;
375 #               warn " ".$i." ".$col. " ".$row;
376         $indice{$col}++;
377     }
378     
379     push @loopcol,{coltitle => "Global"} if not($column);
380     
381     for ($i=1; $i<=$line;$i++) {
382         my @loopcell;
383         warn " $i";
384         #@loopcol ensures the order for columns is common with column titles
385         # and the number matches the number of columns
386         my $colcount=0;
387         foreach my $col ( @loopcol ) {
388 #                       warn " colonne :$col->{coltitle}";
389             my $value;
390             my $count=0;
391             my $link;
392             if (@loopcol){
393                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
394                 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
395                 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
396             } else {
397                 $value =$table[$i]->{"zzEMPTY"}->{'name'};
398                 $count =$table[$i]->{"zzEMPTY"}->{'count'};
399                 $link =$table[$i]->{"zzEMPTY"}->{'link'};
400             }
401 #                       warn " ".$i ." value:$value count:$count reference:$link";
402             push @loopcell, {value => $value, count =>$count, reference => $link} ;
403         }
404         #warn "row : $row colcount:$colcount";
405         #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
406         push @looprow,{ 'rowtitle' => $i ,
407                         'loopcell' => \@loopcell,
408                         'hilighted' => ($hilighted >0),
409                         #'totalrow' => ($total)?sprintf("%.2f",$total):0
410                     };
411         $hilighted = -$hilighted;
412     }
413 #       
414             
415
416     # the header of the table
417     $globalline{loopfilter}=\@loopfilter;
418     # the core of the table
419     $globalline{looprow} = \@looprow;
420     $globalline{loopcol} = \@loopcol;
421 #       # the foot (totals by borrower type)
422     $globalline{loopfooter} = \@loopfooter;
423     $globalline{total}= $grantotal;
424     $globalline{line} = $line;
425     $globalline{column} = $column;
426     push @mainloop,\%globalline;
427     return \@mainloop;
428 }
429
430 1;