Bug 28591: Don't pass debug to get_template_and_user
[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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Koha;
27 use C4::Circulation;
28 use C4::Reports;
29 use C4::Members;
30 use Koha::DateUtils;
31 use Koha::ItemTypes;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39 =cut
40
41 my $input = CGI->new;
42 my $do_it=$input->param('do_it');
43 my $fullreportname = "reports/cat_issues_top.tt";
44 my $limit = $input->param("Limit");
45 my $column = $input->param("Criteria");
46 my @filters = $input->multi_param("Filter");
47 foreach ( @filters[0..3] ) {
48     $_ and $_ = eval { output_pref( { dt => dt_from_string ( $_ ), dateonly => 1, dateformat => 'iso' } ); };
49 }
50
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 #warn "calcul : ".$calc;
54 my ($template, $borrowernumber, $cookie)
55     = get_template_and_user({template_name => $fullreportname,
56                 query => $input,
57                 type => "intranet",
58                 flagsrequired => { reports => '*'},
59                 });
60 our $sep     = $input->param("sep");
61 $sep = "\t" if ($sep eq 'tabulation');
62 $template->param(do_it => $do_it,
63         );
64 if ($do_it) {
65 # Displaying results
66     my $results = calculate($limit, $column, \@filters);
67     if ($output eq "screen"){
68 # Printing results to screen
69         $template->param(mainloop => $results,
70                         limit => $limit);
71         output_html_with_http_headers $input, $cookie, $template->output;
72         exit;
73     } else {
74 # Printing to a csv file
75         print $input->header(-type => 'application/vnd.sun.xml.calc',
76                             -encoding    => 'utf-8',
77                             -attachment=>"$basename.csv",
78                             -filename=>"$basename.csv" );
79         my $cols = @$results[0]->{loopcol};
80         my $lines = @$results[0]->{looprow};
81 # header top-right
82         print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
83 # Other header
84         foreach my $col ( @$cols ) {
85             print $col->{coltitle}.$sep;
86         }
87         print "Total\n";
88 # Table
89         foreach my $line ( @$lines ) {
90             my $x = $line->{loopcell};
91             print $line->{rowtitle}.$sep;
92             foreach my $cell (@$x) {
93                 print $cell->{value}.$sep;
94                 print $cell->{count} // '';
95             }
96             print "\n";
97         }
98         exit;
99     }
100 # Displaying choices
101 } else {
102     my $dbh = C4::Context->dbh;
103     
104     my $CGIextChoice = ( 'CSV' ); # FIXME translation
105     my $CGIsepChoice=GetDelimiterChoices;
106
107     #doctype
108     my $itemtypes = Koha::ItemTypes->search_with_localization;
109
110     #ccode
111     my $ccodes = GetAuthorisedValues('CCODE');
112     my @ccodeloop;
113     for my $thisccode (@$ccodes) {
114             my %row = (value => $thisccode->{authorised_value},
115                        description => $thisccode->{lib},
116                             );
117             push @ccodeloop, \%row;
118     }
119
120     @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
121
122     #shelvingloc
123     my $shelvinglocs = GetAuthorisedValues('LOC');
124     my @shelvinglocloop;
125     for my $thisloc (@$shelvinglocs) {
126             my %row = (value => $thisloc->{authorised_value},
127                        description => $thisloc->{lib},
128                             );
129             push @shelvinglocloop, \%row;
130     }
131
132     @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
133
134     my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['categorycode']});
135
136     $template->param(
137                     CGIextChoice => $CGIextChoice,
138                     CGIsepChoice => $CGIsepChoice,
139                     itemtypes => $itemtypes,
140                     ccodeloop =>\@ccodeloop,
141                     shelvinglocloop =>\@shelvinglocloop,
142                     patron_categories => $patron_categories,
143                     );
144 output_html_with_http_headers $input, $cookie, $template->output;
145 }
146
147
148
149
150 sub calculate {
151     my ($line, $column, $filters) = @_;
152     my @mainloop;
153     my @loopcol;
154     my @looprow;
155     my %globalline;
156     my $grantotal =0;
157 # extract parameters
158     my $dbh = C4::Context->dbh;
159
160 # Filters
161 # Checking filters
162 #
163     my @loopfilter;
164     for (my $i=0;$i<=12;$i++) {
165         my %cell;
166         if ( @$filters[$i] ) {
167             if (($i==1) and (@$filters[$i-1])) {
168                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
169             }
170             # format the dates filters, otherwise just fill as is
171             if ($i>=2) {
172                 $cell{filter} .= @$filters[$i];
173             } else {
174                 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
175                    if ( @$filters[$i] );
176             }
177             $cell{crit} .="Issue From" if ($i==0);
178             $cell{crit} .="Issue To" if ($i==1);
179             $cell{crit} .="Return From" if ($i==2);
180             $cell{crit} .="Return To" if ($i==3);
181             $cell{crit} .="Branch" if ($i==4);
182             $cell{crit} .="Doc Type" if ($i==5);
183             $cell{crit} .="Call number" if ($i==6);
184             $cell{crit} .="Collection code" if ($i==7);
185             $cell{crit} .="Shelving location" if ($i==8);
186             $cell{crit} .="Bor Cat" if ($i==9);
187             $cell{crit} .="Day" if ($i==10);
188             $cell{crit} .="Month" if ($i==11);
189             $cell{crit} .="Year" if ($i==12);
190             push @loopfilter, \%cell;
191         }
192     }
193     my $colfield;
194     my $colorder;
195     if ($column){
196         $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/issuedate/));
197         if($column=~/itemtype/){
198             $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
199         }
200         $column = "borrowers.".$column if $column=~/categorycode/;
201         my @colfilter ;
202         $colfilter[0] = @$filters[0] if ($column =~ /issuedate/ )  ;
203         $colfilter[1] = @$filters[1] if ($column =~ /issuedate/ )  ;
204         $colfilter[0] = @$filters[2] if ($column =~ /returndate/ )  ;
205         $colfilter[1] = @$filters[3] if ($column =~ /returndate/ )  ;
206         $colfilter[0] = @$filters[4] if ($column =~ /branch/ )  ;
207         $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ )  ;
208       # These limits does not currently exist, maybe later?
209       # $colfilter[0] = @$filters[6] if ($column =~ /ccode/ )  ;
210       # $colfilter[0] = @$filters[7] if ($column =~ /location/ )  ;
211         $colfilter[0] = @$filters[8] if ($column =~ /category/ )  ;
212       # This commented out row (sort2) was not removed when adding new filters for ccode, shelving location and call number
213       # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
214         $colfilter[0] = @$filters[9] if ($column =~ /issuedate/ ) ;
215         $colfilter[0] = @$filters[10] if ($column =~ /issuedate/ ) ;
216         $colfilter[0] = @$filters[11] if ($column =~ /issuedate/ ) ;
217     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
218                                                 
219     # loop cols.
220         if ($column eq "Day") {
221             #Display by day
222             $column = "old_issues.issuedate";
223             $colfield .="dayname($column)";  
224             $colorder .="weekday($column)";
225         } elsif ($column eq "Month") {
226             #Display by Month
227             $column = "old_issues.issuedate";
228             $colfield .="monthname($column)";  
229             $colorder .="month($column)";  
230         } elsif ($column eq "Year") {
231             #Display by Year
232             $column = "old_issues.issuedate";
233             $colfield .="Year($column)";
234             $colorder .= $column;
235         } else {
236             $colfield .= $column;
237             $colorder .= $column;
238         }  
239         
240         my $strsth2;
241         $strsth2 .= "SELECT distinctrow $colfield 
242                      FROM `old_issues` 
243                      LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber 
244                      LEFT JOIN items ON old_issues.itemnumber=items.itemnumber 
245                      LEFT JOIN biblioitems  ON biblioitems.biblioitemnumber=items.biblioitemnumber 
246                      WHERE 1";
247         if (($column=~/issuedate/) or ($column=~/returndate/)){
248             if ($colfilter[1] and ($colfilter[0])){
249                 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
250             } elsif ($colfilter[1]) {
251                     $strsth2 .= " and $column < '$colfilter[1]' " ;
252             } elsif ($colfilter[0]) {
253                 $strsth2 .= " and $column > '$colfilter[0]' " ;
254             }
255         } elsif ($colfilter[0]) {
256             $colfilter[0] =~ s/\*/%/g;
257             $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
258         }
259         $strsth2 .=" group by $colfield";
260         $strsth2 .=" order by $colorder";
261         
262         my $sth2 = $dbh->prepare( $strsth2 );
263         if (( @colfilter ) and ($colfilter[1])){
264             $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
265         } elsif ($colfilter[0]) {
266             $sth2->execute($colfilter[0]);
267         } else {
268             $sth2->execute;
269         }
270         
271     
272         while (my ($celvalue) = $sth2->fetchrow) {
273             my %cell;
274             $cell{coltitle} = ($celvalue?$celvalue:"NULL");
275             push @loopcol, \%cell;
276         }
277     #   warn "fin des titres colonnes";
278     }
279     
280     my $i=0;
281 #       my @totalcol;
282     my $hilighted=-1;
283     
284     #Initialization of cell values.....
285     my @table;
286     
287 #       warn "init table";
288     for (my $i=1;$i<=$line;$i++) {
289         foreach my $col ( @loopcol ) {
290 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
291             $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
292         }
293     }
294
295
296 # preparing calculation
297     my $strcalc ;
298     
299 # Processing average loanperiods
300     $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
301     $strcalc .= ", itemcallnumber as CALLNUM";
302     $strcalc .= ", ccode as CCODE";
303     $strcalc .= ", location as LOC";
304     $strcalc .= " , $colfield " if ($colfield);
305     $strcalc .= " FROM `old_issues` 
306                   LEFT JOIN items USING(itemnumber) 
307                   LEFT JOIN biblio USING(biblionumber) 
308                   LEFT JOIN biblioitems USING(biblionumber)
309                   LEFT JOIN borrowers USING(borrowernumber)
310                   WHERE 1";
311
312     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
313     $strcalc .= " AND old_issues.issuedate > '" . @$filters[0] ."'" if ( @$filters[0] );
314     @$filters[1]=~ s/\*/%/g if (@$filters[1]);
315     $strcalc .= " AND old_issues.issuedate < '" . @$filters[1] ."'" if ( @$filters[1] );
316     @$filters[2]=~ s/\*/%/g if (@$filters[2]);
317     $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
318     @$filters[3]=~ s/\*/%/g if (@$filters[3]);
319     $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
320     @$filters[4]=~ s/\*/%/g if (@$filters[4]);
321     $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
322     @$filters[5]=~ s/\*/%/g if (@$filters[5]);
323     if ( @$filters[5] ){
324         if(C4::Context->preference('item-level_itypes') ){
325             $strcalc .= " AND items.itype like "
326         }else{
327             $strcalc .= " AND biblioitems.itemtype like "
328         } 
329         $strcalc .= "'" . @$filters[5] ."'" ;
330     }
331     @$filters[6]=~ s/\*/%/g if (@$filters[6]);
332     $strcalc .= " AND itemcallnumber like '" . @$filters[6] ."'" if ( @$filters[6] );
333     @$filters[7]=~ s/\*/%/g if (@$filters[7]);
334     $strcalc .= " AND ccode like '" . @$filters[7] ."'" if ( @$filters[7] );
335     @$filters[8]=~ s/\*/%/g if (@$filters[8]);
336     $strcalc .= " AND location like '" . @$filters[8] ."'" if ( @$filters[8] );
337     @$filters[9]=~ s/\*/%/g if (@$filters[9]);
338     $strcalc .= " AND borrowers.categorycode like '" . @$filters[9] ."'" if ( @$filters[9] );
339     @$filters[10]=~ s/\*/%/g if (@$filters[10]);
340     $strcalc .= " AND dayname(old_issues.issuedate) like '" . @$filters[10]."'" if (@$filters[10]);
341     @$filters[11]=~ s/\*/%/g if (@$filters[11]);
342     $strcalc .= " AND monthname(old_issues.issuedate) like '" . @$filters[11]."'" if (@$filters[11]);
343     @$filters[12]=~ s/\*/%/g if (@$filters[12]);
344     $strcalc .= " AND year(old_issues.issuedate) like '" . @$filters[12] ."'" if ( @$filters[12] );
345     
346     $strcalc .= " group by biblio.biblionumber";
347     $strcalc .= ", $colfield" if ($column);
348     $strcalc .= " order by RANK DESC";
349     $strcalc .= ", $colfield " if ($colfield);
350     
351     my $dbcalc = $dbh->prepare($strcalc);
352     $dbcalc->execute;
353     my %indice;
354     while (my  @data = $dbcalc->fetchrow) {
355         my ($row, $rank, $id, $callnum, $ccode, $loc, $col )=@data;
356         $col = "zzEMPTY" if (!defined($col));
357         $indice{$col}=1 if (not($indice{$col}));
358         $table[$indice{$col}]->{$col}->{'name'}=$row;
359         $table[$indice{$col}]->{$col}->{'count'}=$rank;
360         $table[$indice{$col}]->{$col}->{'link'}=$id;
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         #@loopcol ensures the order for columns is common with column titles
369         # and the number matches the number of columns
370         my $colcount=0;
371         foreach my $col ( @loopcol ) {
372             my $value;
373             my $count=0;
374             my $link;
375             if (@loopcol){
376                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
377                 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
378                 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
379             } else {
380                 $value =$table[$i]->{"zzEMPTY"}->{'name'};
381                 $count =$table[$i]->{"zzEMPTY"}->{'count'};
382                 $link =$table[$i]->{"zzEMPTY"}->{'link'};
383             }
384             push @loopcell, {value => $value, count =>$count, reference => $link} ;
385         }
386         #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
387         push @looprow,{ 'rowtitle' => $i ,
388                         'loopcell' => \@loopcell,
389                         'hilighted' => ($hilighted >0),
390                     };
391         $hilighted = -$hilighted;
392     }
393 #       
394             
395
396     # the header of the table
397     $globalline{loopfilter}=\@loopfilter;
398     # the core of the table
399     $globalline{looprow} = \@looprow;
400     $globalline{loopcol} = \@loopcol;
401 #       # the foot (totals by borrower type)
402     $globalline{total}= $grantotal;
403     $globalline{line} = $line;
404     $globalline{column} = $column;
405     push @mainloop,\%globalline;
406     return \@mainloop;
407 }
408
409 1;