Bug 16460: Update MARC21 frameworks to Update No. 22 (April 2016)
[koha.git] / reports / cat_issues_top.pl
blob949afb70acd59ac70f7cad8860064c9a2b0d90b8
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::Branch; # GetBranches
27 use C4::Output;
28 use C4::Koha;
29 use C4::Circulation;
30 use C4::Reports;
31 use C4::Members;
32 use Koha::DateUtils;
34 =head1 NAME
36 plugin that shows a stats on borrowers
38 =head1 DESCRIPTION
40 =over 2
42 =cut
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/cat_issues_top.tt";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->multi_param("Filter");
50 foreach ( @filters[0..3] ) {
51 $_ and $_ = eval { output_pref( { dt => dt_from_string ( $_ ), dateonly => 1, dateformat => 'iso' } ); };
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
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,
69 if ($do_it) {
70 # Displaying results
71 my $results = calculate($limit, $column, \@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop => $results,
75 limit => $limit);
76 output_html_with_http_headers $input, $cookie, $template->output;
77 exit;
78 } else {
79 # Printing to a csv file
80 print $input->header(-type => 'application/vnd.sun.xml.calc',
81 -encoding => 'utf-8',
82 -attachment=>"$basename.csv",
83 -filename=>"$basename.csv" );
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
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;
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;
100 print $line->{totalrow};
101 print "\n";
103 # footer
104 print "TOTAL";
105 $cols = @$results[0]->{loopfooter};
106 foreach my $col ( @$cols ) {
107 print $sep.$col->{totalcol};
109 print $sep.@$results[0]->{total};
110 exit;
112 # Displaying choices
113 } else {
114 my $dbh = C4::Context->dbh;
115 my @values;
116 my %labels;
117 my %select;
118 my $req;
120 my $CGIextChoice = ( 'CSV' ); # FIXME translation
121 my $CGIsepChoice=GetDelimiterChoices;
123 #doctype
124 my $itemtypes = GetItemTypes;
125 my @itemtypeloop;
126 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description}} keys %$itemtypes) {
127 my %row =(value => $thisitemtype,
128 description => $itemtypes->{$thisitemtype}->{translated_description},
130 push @itemtypeloop, \%row;
133 #ccode
134 my $ccodes = GetAuthorisedValues('CCODE');
135 my @ccodeloop;
136 for my $thisccode (@$ccodes) {
137 my %row = (value => $thisccode->{authorised_value},
138 description => $thisccode->{lib},
140 push @ccodeloop, \%row;
143 @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
145 #shelvingloc
146 my $shelvinglocs = GetAuthorisedValues('LOC');
147 my @shelvinglocloop;
148 for my $thisloc (@$shelvinglocs) {
149 my %row = (value => $thisloc->{authorised_value},
150 description => $thisloc->{lib},
152 push @shelvinglocloop, \%row;
155 @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
157 #borcat
158 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
159 my @borcatloop;
160 foreach my $thisborcat (sort {$labels->{$a} cmp $labels->{$b}} keys %$labels) {
161 my %row =(value => $thisborcat,
162 description => $labels->{$thisborcat},
164 push @borcatloop, \%row;
167 #Day
168 #Month
169 $template->param(
170 CGIextChoice => $CGIextChoice,
171 CGIsepChoice => $CGIsepChoice,
172 branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}),
173 itemtypeloop =>\@itemtypeloop,
174 ccodeloop =>\@ccodeloop,
175 shelvinglocloop =>\@shelvinglocloop,
176 borcatloop =>\@borcatloop,
178 output_html_with_http_headers $input, $cookie, $template->output;
184 sub calculate {
185 my ($line, $column, $filters) = @_;
186 my @mainloop;
187 my @loopfooter;
188 my @loopcol;
189 my @loopline;
190 my @looprow;
191 my %globalline;
192 my $grantotal =0;
193 # extract parameters
194 my $dbh = C4::Context->dbh;
196 # Filters
197 # Checking filters
199 my @loopfilter;
200 for (my $i=0;$i<=12;$i++) {
201 my %cell;
202 if ( @$filters[$i] ) {
203 if (($i==1) and (@$filters[$i-1])) {
204 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
206 # format the dates filters, otherwise just fill as is
207 if ($i>=2) {
208 $cell{filter} .= @$filters[$i];
209 } else {
210 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
211 if ( @$filters[$i] );
213 $cell{crit} .="Issue From" if ($i==0);
214 $cell{crit} .="Issue To" if ($i==1);
215 $cell{crit} .="Return From" if ($i==2);
216 $cell{crit} .="Return To" if ($i==3);
217 $cell{crit} .="Branch" if ($i==4);
218 $cell{crit} .="Doc Type" if ($i==5);
219 $cell{crit} .="Call number" if ($i==6);
220 $cell{crit} .="Collection code" if ($i==7);
221 $cell{crit} .="Shelving location" if ($i==8);
222 $cell{crit} .="Bor Cat" if ($i==9);
223 $cell{crit} .="Day" if ($i==10);
224 $cell{crit} .="Month" if ($i==11);
225 $cell{crit} .="Year" if ($i==12);
226 push @loopfilter, \%cell;
229 my $colfield;
230 my $colorder;
231 if ($column){
232 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
233 if($column=~/itemtype/){
234 $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
236 $column = "borrowers.".$column if $column=~/categorycode/;
237 my @colfilter ;
238 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
239 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
240 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
241 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
242 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
243 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
244 # These limits does not currently exist, maybe later?
245 # $colfilter[0] = @$filters[6] if ($column =~ /ccode/ ) ;
246 # $colfilter[0] = @$filters[7] if ($column =~ /location/ ) ;
247 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
248 # This commented out row (sort2) was not removed when adding new filters for ccode, shelving location and call number
249 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
250 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
251 $colfilter[0] = @$filters[10] if ($column =~ /timestamp/ ) ;
252 $colfilter[0] = @$filters[11] if ($column =~ /timestamp/ ) ;
253 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
255 # loop cols.
256 if ($column eq "Day") {
257 #Display by day
258 $column = "old_issues.timestamp";
259 $colfield .="dayname($column)";
260 $colorder .="weekday($column)";
261 } elsif ($column eq "Month") {
262 #Display by Month
263 $column = "old_issues.timestamp";
264 $colfield .="monthname($column)";
265 $colorder .="month($column)";
266 } elsif ($column eq "Year") {
267 #Display by Year
268 $column = "old_issues.timestamp";
269 $colfield .="Year($column)";
270 $colorder .= $column;
271 } else {
272 $colfield .= $column;
273 $colorder .= $column;
276 my $strsth2;
277 $strsth2 .= "SELECT distinctrow $colfield
278 FROM `old_issues`
279 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
280 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
281 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
282 WHERE 1";
283 if (($column=~/timestamp/) or ($column=~/returndate/)){
284 if ($colfilter[1] and ($colfilter[0])){
285 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
286 } elsif ($colfilter[1]) {
287 $strsth2 .= " and $column < '$colfilter[1]' " ;
288 } elsif ($colfilter[0]) {
289 $strsth2 .= " and $column > '$colfilter[0]' " ;
291 } elsif ($colfilter[0]) {
292 $colfilter[0] =~ s/\*/%/g;
293 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
295 $strsth2 .=" group by $colfield";
296 $strsth2 .=" order by $colorder";
298 my $sth2 = $dbh->prepare( $strsth2 );
299 if (( @colfilter ) and ($colfilter[1])){
300 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
301 } elsif ($colfilter[0]) {
302 $sth2->execute($colfilter[0]);
303 } else {
304 $sth2->execute;
308 while (my ($celvalue) = $sth2->fetchrow) {
309 my %cell;
310 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
311 push @loopcol, \%cell;
313 # warn "fin des titres colonnes";
316 my $i=0;
317 # my @totalcol;
318 my $hilighted=-1;
320 #Initialization of cell values.....
321 my @table;
323 # warn "init table";
324 for (my $i=1;$i<=$line;$i++) {
325 foreach my $col ( @loopcol ) {
326 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
327 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
332 # preparing calculation
333 my $strcalc ;
335 # Processing average loanperiods
336 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
337 $strcalc .= ", itemcallnumber as CALLNUM";
338 $strcalc .= ", ccode as CCODE";
339 $strcalc .= ", location as LOC";
340 $strcalc .= " , $colfield " if ($colfield);
341 $strcalc .= " FROM `old_issues`
342 LEFT JOIN items USING(itemnumber)
343 LEFT JOIN biblio USING(biblionumber)
344 LEFT JOIN biblioitems USING(biblionumber)
345 LEFT JOIN borrowers USING(borrowernumber)
346 WHERE 1";
348 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
349 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
350 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
351 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
352 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
353 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
354 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
355 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
356 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
357 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
358 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
359 if ( @$filters[5] ){
360 if(C4::Context->preference('item-level_itypes') ){
361 $strcalc .= " AND items.itype like "
362 }else{
363 $strcalc .= " AND biblioitems.itemtype like "
365 $strcalc .= "'" . @$filters[5] ."'" ;
367 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
368 $strcalc .= " AND itemcallnumber like '" . @$filters[6] ."'" if ( @$filters[6] );
369 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
370 $strcalc .= " AND ccode like '" . @$filters[7] ."'" if ( @$filters[7] );
371 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
372 $strcalc .= " AND location like '" . @$filters[8] ."'" if ( @$filters[8] );
373 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
374 $strcalc .= " AND borrowers.categorycode like '" . @$filters[9] ."'" if ( @$filters[9] );
375 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
376 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[10]."'" if (@$filters[10]);
377 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
378 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[11]."'" if (@$filters[11]);
379 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
380 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[12] ."'" if ( @$filters[12] );
382 $strcalc .= " group by biblio.biblionumber";
383 $strcalc .= ", $colfield" if ($column);
384 $strcalc .= " order by RANK DESC";
385 $strcalc .= ", $colfield " if ($colfield);
387 my $dbcalc = $dbh->prepare($strcalc);
388 $dbcalc->execute;
389 my $previous_col;
390 my %indice;
391 while (my @data = $dbcalc->fetchrow) {
392 my ($row, $rank, $id, $callnum, $ccode, $loc, $col )=@data;
393 $col = "zzEMPTY" if (!defined($col));
394 $indice{$col}=1 if (not($indice{$col}));
395 $table[$indice{$col}]->{$col}->{'name'}=$row;
396 $table[$indice{$col}]->{$col}->{'count'}=$rank;
397 $table[$indice{$col}]->{$col}->{'link'}=$id;
398 $indice{$col}++;
401 push @loopcol,{coltitle => "Global"} if not($column);
403 for ($i=1; $i<=$line;$i++) {
404 my @loopcell;
405 #@loopcol ensures the order for columns is common with column titles
406 # and the number matches the number of columns
407 my $colcount=0;
408 foreach my $col ( @loopcol ) {
409 my $value;
410 my $count=0;
411 my $link;
412 if (@loopcol){
413 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
414 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
415 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
416 } else {
417 $value =$table[$i]->{"zzEMPTY"}->{'name'};
418 $count =$table[$i]->{"zzEMPTY"}->{'count'};
419 $link =$table[$i]->{"zzEMPTY"}->{'link'};
421 push @loopcell, {value => $value, count =>$count, reference => $link} ;
423 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
424 push @looprow,{ 'rowtitle' => $i ,
425 'loopcell' => \@loopcell,
426 'hilighted' => ($hilighted >0),
428 $hilighted = -$hilighted;
433 # the header of the table
434 $globalline{loopfilter}=\@loopfilter;
435 # the core of the table
436 $globalline{looprow} = \@looprow;
437 $globalline{loopcol} = \@loopcol;
438 # # the foot (totals by borrower type)
439 $globalline{loopfooter} = \@loopfooter;
440 $globalline{total}= $grantotal;
441 $globalline{line} = $line;
442 $globalline{column} = $column;
443 push @mainloop,\%globalline;
444 return \@mainloop;