fixing help feature with new template structure
[koha.git] / reports / bor_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 CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Context;
27 use C4::Branch; # GetBranches
28 use C4::Koha;
29 use C4::Circulation;
30 use C4::Members;
31 use Date::Manip;
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/bor_issues_top.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 my $output = $input->param("output");
50 my $basename = $input->param("basename");
51 my $mime = $input->param("MIME");
52 my $del = $input->param("sep");
53 #warn "calcul : ".$calc;
54 my ($template, $borrowernumber, $cookie)
55         = get_template_and_user({template_name => $fullreportname,
56                                 query => $input,
57                                 type => "intranet",
58                                 authnotrequired => 0,
59                                 flagsrequired => {reports => 1},
60                                 debug => 1,
61                                 });
62 $template->param(do_it => $do_it,
63                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
64                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
65                 IntranetNav => C4::Context->preference("IntranetNav"),
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         
124         my $CGIextChoice=CGI::scrolling_list(
125                                 -name     => 'MIME',
126                                 -id       => 'MIME',
127                                 -values   => \@mime,
128                                 -size     => 1,
129                                 -multiple => 0 );
130         
131         my @dels = ( C4::Context->preference("delimiter") );
132         my $CGIsepChoice=CGI::scrolling_list(
133                                 -name     => 'sep',
134                                 -id       => 'sep',
135                                 -values   => \@dels,
136                                 -size     => 1,
137                                 -multiple => 0 );
138         #branch
139         my $branches = GetBranches;
140         my @branchloop;
141         foreach my $thisbranch (keys %$branches) {
142 #                       my $selected = 1 if $thisbranch eq $branch;
143                         my %row =(value => $thisbranch,
144 #                                                                       selected => $selected,
145                                                                         branchname => $branches->{$thisbranch}->{'branchname'},
146                                                         );
147                         push @branchloop, \%row;
148         }
149
150         #doctype
151         my $itemtypes = GetItemTypes;
152         my @itemtypeloop;
153         foreach my $thisitemtype (keys %$itemtypes) {
154 #                       my $selected = 1 if $thisbranch eq $branch;
155                         my %row =(value => $thisitemtype,
156 #                                                                       selected => $selected,
157                                                                         description => $itemtypes->{$thisitemtype}->{'description'},
158                                                         );
159                         push @itemtypeloop, \%row;
160         }
161         
162         #borcat
163         my ($codes,$labels) = GetborCatFromCatType(undef,undef);
164         my @borcatloop;
165         foreach my $thisborcat (sort keys %$labels) {
166  #                      my $selected = 1 if $thisbranch eq $branch;
167                         my %row =(value => $thisborcat,
168  #                                                                      selected => $selected,
169                                                                         description => $labels->{$thisborcat},
170                                                         );
171                         push @borcatloop, \%row;
172         }
173         
174         #Day
175         #Month
176         $template->param(
177                                         CGIextChoice => $CGIextChoice,
178                                         CGIsepChoice => $CGIsepChoice,
179                                         branchloop =>\@branchloop,
180                                         itemtypeloop =>\@itemtypeloop,
181                                         borcatloop =>\@borcatloop,
182                                         );
183 output_html_with_http_headers $input, $cookie, $template->output;
184 }
185
186
187
188
189 sub calculate {
190         my ($line, $column, $filters) = @_;
191         my @mainloop;
192         my @loopfooter;
193         my @loopcol;
194         my @loopline;
195         my @looprow;
196         my %globalline;
197         my $grantotal =0;
198 # extract parameters
199         my $dbh = C4::Context->dbh;
200
201 # Filters
202 # Checking filters
203 #
204         my @loopfilter;
205         for (my $i=0;$i<=6;$i++) {
206                 my %cell;
207                 if ( @$filters[$i] ) {
208                         if (($i==1) and (@$filters[$i-1])) {
209                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
210                         }
211                         $cell{filter} .= @$filters[$i];
212                         $cell{crit} .="Issue From" if ($i==0);
213                         $cell{crit} .="Issue To" if ($i==1);
214                         $cell{crit} .="Return From" if ($i==2);
215                         $cell{crit} .="Return To" if ($i==3);
216                         $cell{crit} .="Branch" if ($i==4);
217                         $cell{crit} .="Doc Type" if ($i==5);
218                         $cell{crit} .="Bor Cat" if ($i==6);
219                         $cell{crit} .="Day" if ($i==7);
220                         $cell{crit} .="Month" if ($i==8);
221                         $cell{crit} .="Year" if ($i==9);
222                         push @loopfilter, \%cell;
223                 }
224         }
225         my $colfield;
226         my $colorder;
227         if ($column){
228                 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
229                 $column = "biblioitems.".$column if $column=~/itemtype/;
230                 $column = "borrowers.".$column if $column=~/categorycode/;
231                 my @colfilter ;
232                 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ )  ;
233                 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ )  ;
234                 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ )  ;
235                 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ )  ;
236                 $colfilter[0] = @$filters[4] if ($column =~ /branch/ )  ;
237                 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ )  ;
238                 $colfilter[0] = @$filters[6] if ($column =~ /category/ )  ;
239         #       $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
240                 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
241                 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
242                 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
243         #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
244                                                                                                 
245         # loop cols.
246                 if ($column eq "Day") {
247                         #Display by day
248                         $column = "issues.timestamp";
249                         $colfield .="dayname($column)";  
250                         $colorder .="weekday($column)";
251                 } elsif ($column eq "Month") {
252                         #Display by Month
253                         $column = "issues.timestamp";
254                         $colfield .="monthname($column)";  
255                         $colorder .="month($column)";  
256                 } elsif ($column eq "Year") {
257                         #Display by Year
258                         $column = "issues.timestamp";
259                         $colfield .="Year($column)";
260                         $colorder .= $column;
261                 } else {
262                         $colfield .= $column;
263                         $colorder .= $column;
264                 }  
265                 
266                 my $strsth2;
267                 $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";
268                 if (($column=~/timestamp/) or ($column=~/returndate/)){
269                         if ($colfilter[1] and ($colfilter[0])){
270                                 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
271                         } elsif ($colfilter[1]) {
272                                         $strsth2 .= " and $column < '$colfilter[1]' " ;
273                         } elsif ($colfilter[0]) {
274                                 $strsth2 .= " and $column > '$colfilter[0]' " ;
275                         }
276                 } elsif ($colfilter[0]) {
277                         $colfilter[0] =~ s/\*/%/g;
278                         $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
279                 }
280                 $strsth2 .=" group by $colfield";
281                 $strsth2 .=" order by $colorder";
282                 warn "". $strsth2;
283                 
284                 my $sth2 = $dbh->prepare( $strsth2 );
285                 $sth2->execute;
286         
287                 while (my ($celvalue) = $sth2->fetchrow) {
288                         my %cell;
289                         $cell{'coltitle'} = ($celvalue?$celvalue:"NULL");
290                         push @loopcol, \%cell;
291                 }
292         #       warn "fin des titres colonnes";
293         }
294         
295         my $i=0;
296 #       my @totalcol;
297         my $hilighted=-1;
298         
299         #Initialization of cell values.....
300         my @table;
301         
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;
307                 }
308         }
309
310
311 # preparing calculation
312         my $strcalc ;
313         
314 # Processing average loanperiods
315         $strcalc .= "SELECT  CONCAT(borrowers.surname , \"\\t\",borrowers.firstname),  COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
316         $strcalc .= " , $colfield " if ($colfield);
317         $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";
318
319         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
320         $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
321         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
322         $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
323         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
324         $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
325         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
326         $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
327         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
328         $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
329         @$filters[5]=~ s/\*/%/g if (@$filters[5]);
330         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$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(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
335         @$filters[8]=~ s/\*/%/g if (@$filters[8]);
336         $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
337         @$filters[9]=~ s/\*/%/g if (@$filters[9]);
338         $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
339         
340         $strcalc .= " group by borrowers.borrowernumber";
341         $strcalc .= ", $colfield" if ($column);
342         $strcalc .= " order by RANK DESC";
343         $strcalc .= ",$colfield " if ($colfield);
344 #       my $max;
345 #       if (@loopcol) {
346 #               $max = $line*@loopcol;
347 #       } else { $max=$line;}
348 #       $strcalc .= " LIMIT 0,$max";
349         warn "SQL :". $strcalc;
350         
351         my $dbcalc = $dbh->prepare($strcalc);
352         $dbcalc->execute;
353 #       warn "filling table";
354         my $previous_col;
355         my %indice;
356         while (my  @data = $dbcalc->fetchrow) {
357                 my ($row, $rank, $id, $col )=@data;
358                 $col = "zzEMPTY" if ($col eq undef);
359                 $indice{$col}=1 if (not($indice{$col}));
360                 $table[$indice{$col}]->{$col}->{'name'}=$row;
361                 $table[$indice{$col}]->{$col}->{'count'}=$rank;
362                 $table[$indice{$col}]->{$col}->{'link'}=$id;
363 #               warn " ".$i." ".$col. " ".$row;
364                 $indice{$col}++;
365         }
366         
367         push @loopcol,{coltitle => "Global"} if not($column);
368         
369         for ($i=1; $i<=$line;$i++) {
370                 my @loopcell;
371                 warn " $i";
372                 #@loborrowersopcol ensures the order for columns is common with column titles
373                 # and the number matches the number of columns
374                 my $colcount=0;
375                 foreach my $col ( @loopcol ) {
376 #                       warn " colonne :$col->{coltitle}";
377                         my $value;
378                         my $count=0;
379                         my $link;
380                         if (@loopcol){
381                                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
382                                 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
383                                 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
384                         } else {
385                                 $value =$table[$i]->{"zzEMPTY"}->{'name'};
386                                 $count =$table[$i]->{"zzEMPTY"}->{'count'};
387                                 $link =$table[$i]->{"zzEMPTY"}->{'link'};
388                         }
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
401         # the header of the table
402         $globalline{loopfilter}=\@loopfilter;
403         # the core of the table
404         $globalline{looprow} = \@looprow;
405         $globalline{loopcol} = \@loopcol;
406 #       # the foot (totals by borrower type)
407         $globalline{loopfooter} = \@loopfooter;
408         $globalline{total}= $grantotal;
409         $globalline{line} = $line;
410         $globalline{column} = $column;
411         push @mainloop,\%globalline;
412         return \@mainloop;
413 }
414
415 1;