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