Bug 17469: Add missing sample notices to fr-CA web installer
[koha.git] / reports / cat_issues_top.pl
blob08b185234d71dce5f3a97f926dab27c0a49e7504
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 strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Circulation;
29 use C4::Reports;
30 use C4::Members;
31 use Koha::DateUtils;
33 =head1 NAME
35 plugin that shows a stats on borrowers
37 =head1 DESCRIPTION
39 =cut
41 my $input = new CGI;
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 authnotrequired => 0,
59 flagsrequired => { reports => '*'},
60 debug => 1,
61 });
62 our $sep = $input->param("sep");
63 $sep = "\t" if ($sep eq 'tabulation');
64 $template->param(do_it => $do_it,
66 if ($do_it) {
67 # Displaying results
68 my $results = calculate($limit, $column, \@filters);
69 if ($output eq "screen"){
70 # Printing results to screen
71 $template->param(mainloop => $results,
72 limit => $limit);
73 output_html_with_http_headers $input, $cookie, $template->output;
74 exit;
75 } else {
76 # Printing to a csv file
77 print $input->header(-type => 'application/vnd.sun.xml.calc',
78 -encoding => 'utf-8',
79 -attachment=>"$basename.csv",
80 -filename=>"$basename.csv" );
81 my $cols = @$results[0]->{loopcol};
82 my $lines = @$results[0]->{looprow};
83 # header top-right
84 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
85 # Other header
86 foreach my $col ( @$cols ) {
87 print $col->{coltitle}.$sep;
89 print "Total\n";
90 # Table
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
98 print "\n";
100 # footer
101 print "TOTAL";
102 $cols = @$results[0]->{loopfooter};
103 foreach my $col ( @$cols ) {
104 print $sep.$col->{totalcol};
106 print $sep.@$results[0]->{total};
107 exit;
109 # Displaying choices
110 } else {
111 my $dbh = C4::Context->dbh;
112 my @values;
113 my %labels;
114 my %select;
115 my $req;
117 my $CGIextChoice = ( 'CSV' ); # FIXME translation
118 my $CGIsepChoice=GetDelimiterChoices;
120 #doctype
121 my $itemtypes = GetItemTypes;
122 my @itemtypeloop;
123 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description}} keys %$itemtypes) {
124 my %row =(value => $thisitemtype,
125 description => $itemtypes->{$thisitemtype}->{translated_description},
127 push @itemtypeloop, \%row;
130 #ccode
131 my $ccodes = GetAuthorisedValues('CCODE');
132 my @ccodeloop;
133 for my $thisccode (@$ccodes) {
134 my %row = (value => $thisccode->{authorised_value},
135 description => $thisccode->{lib},
137 push @ccodeloop, \%row;
140 @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
142 #shelvingloc
143 my $shelvinglocs = GetAuthorisedValues('LOC');
144 my @shelvinglocloop;
145 for my $thisloc (@$shelvinglocs) {
146 my %row = (value => $thisloc->{authorised_value},
147 description => $thisloc->{lib},
149 push @shelvinglocloop, \%row;
152 @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
154 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
156 $template->param(
157 CGIextChoice => $CGIextChoice,
158 CGIsepChoice => $CGIsepChoice,
159 itemtypeloop =>\@itemtypeloop,
160 ccodeloop =>\@ccodeloop,
161 shelvinglocloop =>\@shelvinglocloop,
162 patron_categories => $patron_categories,
164 output_html_with_http_headers $input, $cookie, $template->output;
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;
182 # Filters
183 # Checking filters
185 my @loopfilter;
186 for (my $i=0;$i<=12;$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]) ;
192 # format the dates filters, otherwise just fill as is
193 if ($i>=2) {
194 $cell{filter} .= @$filters[$i];
195 } else {
196 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
197 if ( @$filters[$i] );
199 $cell{crit} .="Issue From" if ($i==0);
200 $cell{crit} .="Issue To" if ($i==1);
201 $cell{crit} .="Return From" if ($i==2);
202 $cell{crit} .="Return To" if ($i==3);
203 $cell{crit} .="Branch" if ($i==4);
204 $cell{crit} .="Doc Type" if ($i==5);
205 $cell{crit} .="Call number" if ($i==6);
206 $cell{crit} .="Collection code" if ($i==7);
207 $cell{crit} .="Shelving location" if ($i==8);
208 $cell{crit} .="Bor Cat" if ($i==9);
209 $cell{crit} .="Day" if ($i==10);
210 $cell{crit} .="Month" if ($i==11);
211 $cell{crit} .="Year" if ($i==12);
212 push @loopfilter, \%cell;
215 my $colfield;
216 my $colorder;
217 if ($column){
218 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
219 if($column=~/itemtype/){
220 $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
222 $column = "borrowers.".$column if $column=~/categorycode/;
223 my @colfilter ;
224 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
225 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
226 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
227 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
228 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
229 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
230 # These limits does not currently exist, maybe later?
231 # $colfilter[0] = @$filters[6] if ($column =~ /ccode/ ) ;
232 # $colfilter[0] = @$filters[7] if ($column =~ /location/ ) ;
233 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
234 # This commented out row (sort2) was not removed when adding new filters for ccode, shelving location and call number
235 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
236 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
237 $colfilter[0] = @$filters[10] if ($column =~ /timestamp/ ) ;
238 $colfilter[0] = @$filters[11] if ($column =~ /timestamp/ ) ;
239 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
241 # loop cols.
242 if ($column eq "Day") {
243 #Display by day
244 $column = "old_issues.timestamp";
245 $colfield .="dayname($column)";
246 $colorder .="weekday($column)";
247 } elsif ($column eq "Month") {
248 #Display by Month
249 $column = "old_issues.timestamp";
250 $colfield .="monthname($column)";
251 $colorder .="month($column)";
252 } elsif ($column eq "Year") {
253 #Display by Year
254 $column = "old_issues.timestamp";
255 $colfield .="Year($column)";
256 $colorder .= $column;
257 } else {
258 $colfield .= $column;
259 $colorder .= $column;
262 my $strsth2;
263 $strsth2 .= "SELECT distinctrow $colfield
264 FROM `old_issues`
265 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
266 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
267 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
268 WHERE 1";
269 if (($column=~/timestamp/) or ($column=~/returndate/)){
270 if ($colfilter[1] and ($colfilter[0])){
271 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
272 } elsif ($colfilter[1]) {
273 $strsth2 .= " and $column < '$colfilter[1]' " ;
274 } elsif ($colfilter[0]) {
275 $strsth2 .= " and $column > '$colfilter[0]' " ;
277 } elsif ($colfilter[0]) {
278 $colfilter[0] =~ s/\*/%/g;
279 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
281 $strsth2 .=" group by $colfield";
282 $strsth2 .=" order by $colorder";
284 my $sth2 = $dbh->prepare( $strsth2 );
285 if (( @colfilter ) and ($colfilter[1])){
286 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
287 } elsif ($colfilter[0]) {
288 $sth2->execute($colfilter[0]);
289 } else {
290 $sth2->execute;
294 while (my ($celvalue) = $sth2->fetchrow) {
295 my %cell;
296 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
297 push @loopcol, \%cell;
299 # warn "fin des titres colonnes";
302 my $i=0;
303 # my @totalcol;
304 my $hilighted=-1;
306 #Initialization of cell values.....
307 my @table;
309 # warn "init table";
310 for (my $i=1;$i<=$line;$i++) {
311 foreach my $col ( @loopcol ) {
312 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
313 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
318 # preparing calculation
319 my $strcalc ;
321 # Processing average loanperiods
322 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
323 $strcalc .= ", itemcallnumber as CALLNUM";
324 $strcalc .= ", ccode as CCODE";
325 $strcalc .= ", location as LOC";
326 $strcalc .= " , $colfield " if ($colfield);
327 $strcalc .= " FROM `old_issues`
328 LEFT JOIN items USING(itemnumber)
329 LEFT JOIN biblio USING(biblionumber)
330 LEFT JOIN biblioitems USING(biblionumber)
331 LEFT JOIN borrowers USING(borrowernumber)
332 WHERE 1";
334 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
335 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
336 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
337 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
338 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
339 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
340 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
341 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
342 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
343 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
344 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
345 if ( @$filters[5] ){
346 if(C4::Context->preference('item-level_itypes') ){
347 $strcalc .= " AND items.itype like "
348 }else{
349 $strcalc .= " AND biblioitems.itemtype like "
351 $strcalc .= "'" . @$filters[5] ."'" ;
353 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
354 $strcalc .= " AND itemcallnumber like '" . @$filters[6] ."'" if ( @$filters[6] );
355 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
356 $strcalc .= " AND ccode like '" . @$filters[7] ."'" if ( @$filters[7] );
357 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
358 $strcalc .= " AND location like '" . @$filters[8] ."'" if ( @$filters[8] );
359 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
360 $strcalc .= " AND borrowers.categorycode like '" . @$filters[9] ."'" if ( @$filters[9] );
361 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
362 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[10]."'" if (@$filters[10]);
363 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
364 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[11]."'" if (@$filters[11]);
365 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
366 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[12] ."'" if ( @$filters[12] );
368 $strcalc .= " group by biblio.biblionumber";
369 $strcalc .= ", $colfield" if ($column);
370 $strcalc .= " order by RANK DESC";
371 $strcalc .= ", $colfield " if ($colfield);
373 my $dbcalc = $dbh->prepare($strcalc);
374 $dbcalc->execute;
375 my $previous_col;
376 my %indice;
377 while (my @data = $dbcalc->fetchrow) {
378 my ($row, $rank, $id, $callnum, $ccode, $loc, $col )=@data;
379 $col = "zzEMPTY" if (!defined($col));
380 $indice{$col}=1 if (not($indice{$col}));
381 $table[$indice{$col}]->{$col}->{'name'}=$row;
382 $table[$indice{$col}]->{$col}->{'count'}=$rank;
383 $table[$indice{$col}]->{$col}->{'link'}=$id;
384 $indice{$col}++;
387 push @loopcol,{coltitle => "Global"} if not($column);
389 for ($i=1; $i<=$line;$i++) {
390 my @loopcell;
391 #@loopcol ensures the order for columns is common with column titles
392 # and the number matches the number of columns
393 my $colcount=0;
394 foreach my $col ( @loopcol ) {
395 my $value;
396 my $count=0;
397 my $link;
398 if (@loopcol){
399 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
400 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
401 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
402 } else {
403 $value =$table[$i]->{"zzEMPTY"}->{'name'};
404 $count =$table[$i]->{"zzEMPTY"}->{'count'};
405 $link =$table[$i]->{"zzEMPTY"}->{'link'};
407 push @loopcell, {value => $value, count =>$count, reference => $link} ;
409 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
410 push @looprow,{ 'rowtitle' => $i ,
411 'loopcell' => \@loopcell,
412 'hilighted' => ($hilighted >0),
414 $hilighted = -$hilighted;
419 # the header of the table
420 $globalline{loopfilter}=\@loopfilter;
421 # the core of the table
422 $globalline{looprow} = \@looprow;
423 $globalline{loopcol} = \@loopcol;
424 # # the foot (totals by borrower type)
425 $globalline{loopfooter} = \@loopfooter;
426 $globalline{total}= $grantotal;
427 $globalline{line} = $line;
428 $globalline{column} = $column;
429 push @mainloop,\%globalline;
430 return \@mainloop;