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