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