Bug 25898: Prohibit indirect object notation
[koha.git] / reports / cat_issues_top.pl
blob28025cce67a8c6572cd74bae3b1eb3244d0effb9
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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>.
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;
33 =head1 NAME
35 plugin that shows a stats on borrowers
37 =head1 DESCRIPTION
39 =cut
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' } ); };
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 debug => 1,
60 });
61 our $sep = $input->param("sep");
62 $sep = "\t" if ($sep eq 'tabulation');
63 $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,
71 limit => $limit);
72 output_html_with_http_headers $input, $cookie, $template->output;
73 exit;
74 } else {
75 # Printing to a csv file
76 print $input->header(-type => 'application/vnd.sun.xml.calc',
77 -encoding => 'utf-8',
78 -attachment=>"$basename.csv",
79 -filename=>"$basename.csv" );
80 my $cols = @$results[0]->{loopcol};
81 my $lines = @$results[0]->{looprow};
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;
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 print $cell->{count} // '';
97 print "\n";
99 exit;
101 # Displaying choices
102 } else {
103 my $dbh = C4::Context->dbh;
105 my $CGIextChoice = ( 'CSV' ); # FIXME translation
106 my $CGIsepChoice=GetDelimiterChoices;
108 #doctype
109 my $itemtypes = Koha::ItemTypes->search_with_localization;
111 #ccode
112 my $ccodes = GetAuthorisedValues('CCODE');
113 my @ccodeloop;
114 for my $thisccode (@$ccodes) {
115 my %row = (value => $thisccode->{authorised_value},
116 description => $thisccode->{lib},
118 push @ccodeloop, \%row;
121 @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
123 #shelvingloc
124 my $shelvinglocs = GetAuthorisedValues('LOC');
125 my @shelvinglocloop;
126 for my $thisloc (@$shelvinglocs) {
127 my %row = (value => $thisloc->{authorised_value},
128 description => $thisloc->{lib},
130 push @shelvinglocloop, \%row;
133 @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
135 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
137 $template->param(
138 CGIextChoice => $CGIextChoice,
139 CGIsepChoice => $CGIsepChoice,
140 itemtypes => $itemtypes,
141 ccodeloop =>\@ccodeloop,
142 shelvinglocloop =>\@shelvinglocloop,
143 patron_categories => $patron_categories,
145 output_html_with_http_headers $input, $cookie, $template->output;
151 sub calculate {
152 my ($line, $column, $filters) = @_;
153 my @mainloop;
154 my @loopcol;
155 my @looprow;
156 my %globalline;
157 my $grantotal =0;
158 # extract parameters
159 my $dbh = C4::Context->dbh;
161 # Filters
162 # Checking filters
164 my @loopfilter;
165 for (my $i=0;$i<=12;$i++) {
166 my %cell;
167 if ( @$filters[$i] ) {
168 if (($i==1) and (@$filters[$i-1])) {
169 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
171 # format the dates filters, otherwise just fill as is
172 if ($i>=2) {
173 $cell{filter} .= @$filters[$i];
174 } else {
175 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
176 if ( @$filters[$i] );
178 $cell{crit} .="Issue From" if ($i==0);
179 $cell{crit} .="Issue To" if ($i==1);
180 $cell{crit} .="Return From" if ($i==2);
181 $cell{crit} .="Return To" if ($i==3);
182 $cell{crit} .="Branch" if ($i==4);
183 $cell{crit} .="Doc Type" if ($i==5);
184 $cell{crit} .="Call number" if ($i==6);
185 $cell{crit} .="Collection code" if ($i==7);
186 $cell{crit} .="Shelving location" if ($i==8);
187 $cell{crit} .="Bor Cat" if ($i==9);
188 $cell{crit} .="Day" if ($i==10);
189 $cell{crit} .="Month" if ($i==11);
190 $cell{crit} .="Year" if ($i==12);
191 push @loopfilter, \%cell;
194 my $colfield;
195 my $colorder;
196 if ($column){
197 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/issuedate/));
198 if($column=~/itemtype/){
199 $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
201 $column = "borrowers.".$column if $column=~/categorycode/;
202 my @colfilter ;
203 $colfilter[0] = @$filters[0] if ($column =~ /issuedate/ ) ;
204 $colfilter[1] = @$filters[1] if ($column =~ /issuedate/ ) ;
205 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
206 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
207 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
208 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
209 # These limits does not currently exist, maybe later?
210 # $colfilter[0] = @$filters[6] if ($column =~ /ccode/ ) ;
211 # $colfilter[0] = @$filters[7] if ($column =~ /location/ ) ;
212 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
213 # This commented out row (sort2) was not removed when adding new filters for ccode, shelving location and call number
214 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
215 $colfilter[0] = @$filters[9] if ($column =~ /issuedate/ ) ;
216 $colfilter[0] = @$filters[10] if ($column =~ /issuedate/ ) ;
217 $colfilter[0] = @$filters[11] if ($column =~ /issuedate/ ) ;
218 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
220 # loop cols.
221 if ($column eq "Day") {
222 #Display by day
223 $column = "old_issues.issuedate";
224 $colfield .="dayname($column)";
225 $colorder .="weekday($column)";
226 } elsif ($column eq "Month") {
227 #Display by Month
228 $column = "old_issues.issuedate";
229 $colfield .="monthname($column)";
230 $colorder .="month($column)";
231 } elsif ($column eq "Year") {
232 #Display by Year
233 $column = "old_issues.issuedate";
234 $colfield .="Year($column)";
235 $colorder .= $column;
236 } else {
237 $colfield .= $column;
238 $colorder .= $column;
241 my $strsth2;
242 $strsth2 .= "SELECT distinctrow $colfield
243 FROM `old_issues`
244 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
245 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
246 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
247 WHERE 1";
248 if (($column=~/issuedate/) or ($column=~/returndate/)){
249 if ($colfilter[1] and ($colfilter[0])){
250 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
251 } elsif ($colfilter[1]) {
252 $strsth2 .= " and $column < '$colfilter[1]' " ;
253 } elsif ($colfilter[0]) {
254 $strsth2 .= " and $column > '$colfilter[0]' " ;
256 } elsif ($colfilter[0]) {
257 $colfilter[0] =~ s/\*/%/g;
258 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
260 $strsth2 .=" group by $colfield";
261 $strsth2 .=" order by $colorder";
263 my $sth2 = $dbh->prepare( $strsth2 );
264 if (( @colfilter ) and ($colfilter[1])){
265 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
266 } elsif ($colfilter[0]) {
267 $sth2->execute($colfilter[0]);
268 } else {
269 $sth2->execute;
273 while (my ($celvalue) = $sth2->fetchrow) {
274 my %cell;
275 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
276 push @loopcol, \%cell;
278 # warn "fin des titres colonnes";
281 my $i=0;
282 # my @totalcol;
283 my $hilighted=-1;
285 #Initialization of cell values.....
286 my @table;
288 # warn "init table";
289 for (my $i=1;$i<=$line;$i++) {
290 foreach my $col ( @loopcol ) {
291 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
292 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
297 # preparing calculation
298 my $strcalc ;
300 # Processing average loanperiods
301 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
302 $strcalc .= ", itemcallnumber as CALLNUM";
303 $strcalc .= ", ccode as CCODE";
304 $strcalc .= ", location as LOC";
305 $strcalc .= " , $colfield " if ($colfield);
306 $strcalc .= " FROM `old_issues`
307 LEFT JOIN items USING(itemnumber)
308 LEFT JOIN biblio USING(biblionumber)
309 LEFT JOIN biblioitems USING(biblionumber)
310 LEFT JOIN borrowers USING(borrowernumber)
311 WHERE 1";
313 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
314 $strcalc .= " AND old_issues.issuedate > '" . @$filters[0] ."'" if ( @$filters[0] );
315 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
316 $strcalc .= " AND old_issues.issuedate < '" . @$filters[1] ."'" if ( @$filters[1] );
317 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
318 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
319 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
320 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
321 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
322 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
323 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
324 if ( @$filters[5] ){
325 if(C4::Context->preference('item-level_itypes') ){
326 $strcalc .= " AND items.itype like "
327 }else{
328 $strcalc .= " AND biblioitems.itemtype like "
330 $strcalc .= "'" . @$filters[5] ."'" ;
332 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
333 $strcalc .= " AND itemcallnumber like '" . @$filters[6] ."'" if ( @$filters[6] );
334 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
335 $strcalc .= " AND ccode like '" . @$filters[7] ."'" if ( @$filters[7] );
336 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
337 $strcalc .= " AND location like '" . @$filters[8] ."'" if ( @$filters[8] );
338 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
339 $strcalc .= " AND borrowers.categorycode like '" . @$filters[9] ."'" if ( @$filters[9] );
340 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
341 $strcalc .= " AND dayname(old_issues.issuedate) like '" . @$filters[10]."'" if (@$filters[10]);
342 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
343 $strcalc .= " AND monthname(old_issues.issuedate) like '" . @$filters[11]."'" if (@$filters[11]);
344 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
345 $strcalc .= " AND year(old_issues.issuedate) like '" . @$filters[12] ."'" if ( @$filters[12] );
347 $strcalc .= " group by biblio.biblionumber";
348 $strcalc .= ", $colfield" if ($column);
349 $strcalc .= " order by RANK DESC";
350 $strcalc .= ", $colfield " if ($colfield);
352 my $dbcalc = $dbh->prepare($strcalc);
353 $dbcalc->execute;
354 my %indice;
355 while (my @data = $dbcalc->fetchrow) {
356 my ($row, $rank, $id, $callnum, $ccode, $loc, $col )=@data;
357 $col = "zzEMPTY" if (!defined($col));
358 $indice{$col}=1 if (not($indice{$col}));
359 $table[$indice{$col}]->{$col}->{'name'}=$row;
360 $table[$indice{$col}]->{$col}->{'count'}=$rank;
361 $table[$indice{$col}]->{$col}->{'link'}=$id;
362 $indice{$col}++;
365 push @loopcol,{coltitle => "Global"} if not($column);
367 for ($i=1; $i<=$line;$i++) {
368 my @loopcell;
369 #@loopcol 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 my $value;
374 my $count=0;
375 my $link;
376 if (@loopcol){
377 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
378 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
379 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
380 } else {
381 $value =$table[$i]->{"zzEMPTY"}->{'name'};
382 $count =$table[$i]->{"zzEMPTY"}->{'count'};
383 $link =$table[$i]->{"zzEMPTY"}->{'link'};
385 push @loopcell, {value => $value, count =>$count, reference => $link} ;
387 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
388 push @looprow,{ 'rowtitle' => $i ,
389 'loopcell' => \@loopcell,
390 'hilighted' => ($hilighted >0),
392 $hilighted = -$hilighted;
397 # the header of the table
398 $globalline{loopfilter}=\@loopfilter;
399 # the core of the table
400 $globalline{looprow} = \@looprow;
401 $globalline{loopcol} = \@loopcol;
402 # # the foot (totals by borrower type)
403 $globalline{total}= $grantotal;
404 $globalline{line} = $line;
405 $globalline{column} = $column;
406 push @mainloop,\%globalline;
407 return \@mainloop;