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