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