start of big MARC21 authorities work
[koha.git] / reports / issues_stats.pl
blob61b9ce76fef1988b34be9c3d07855cdb0af1a3c2
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 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.
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.
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
21 use strict;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25 use C4::Branch; # GetBranches
26 use C4::Koha;
27 use C4::Output;
28 use C4::Circulation;
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use Date::Manip;
32 =head1 NAME
34 plugin that shows a stats on borrowers
36 =head1 DESCRIPTION
38 =over 2
40 =cut
42 my $input = new CGI;
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/issues_stats.tmpl";
45 my $line = $input->param("Line");
46 my $column = $input->param("Column");
47 my @filters = $input->param("Filter");
48 $filters[0]=format_date_in_iso($filters[0]);
49 $filters[1]=format_date_in_iso($filters[1]);
50 my $podsp = $input->param("DisplayBy");
51 my $type = $input->param("PeriodTypeSel");
52 my $daysel = $input->param("PeriodDaySel");
53 my $monthsel = $input->param("PeriodMonthSel");
54 my $calc = $input->param("Cellvalue");
55 my $output = $input->param("output");
56 my $basename = $input->param("basename");
57 my $mime = $input->param("MIME");
58 my $del = $input->param("sep");
59 #warn "calcul : ".$calc;
60 my ($template, $borrowernumber, $cookie)
61 = get_template_and_user({template_name => $fullreportname,
62 query => $input,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => {reports => 1},
66 debug => 1,
67 });
68 $template->param(do_it => $do_it,
69 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
71 if ($do_it) {
72 # Displaying results
73 my $results = calculate($line, $column, $podsp, $type, $daysel, $monthsel, $calc, \@filters);
74 if ($output eq "screen"){
75 # Printing results to screen
76 $template->param(mainloop => $results);
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 my $sep;
88 $sep =C4::Context->preference("delimiter");
89 # header top-right
90 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
91 # Other header
92 foreach my $col ( @$cols ) {
93 print $col->{coltitle}.$sep;
95 print "Total\n";
96 # Table
97 foreach my $line ( @$lines ) {
98 my $x = $line->{loopcell};
99 print $line->{rowtitle}.$sep;
100 foreach my $cell (@$x) {
101 print $cell->{value}.$sep;
103 print $line->{totalrow};
104 print "\n";
106 # footer
107 print "TOTAL";
108 $cols = @$results[0]->{loopfooter};
109 foreach my $col ( @$cols ) {
110 print $sep.$col->{totalcol};
112 print $sep.@$results[0]->{total};
113 exit(1);
115 # Displaying choices
116 } else {
117 my $dbh = C4::Context->dbh;
118 my @values;
119 my %labels;
120 my %select;
121 my $req;
122 $req = $dbh->prepare("select distinctrow categorycode,description from categories order by description");
123 $req->execute;
124 my @select;
125 push @select,"";
126 $select{""}="";
127 while (my ($value, $desc) =$req->fetchrow) {
128 push @select, $value;
129 $select{$value}=$desc;
131 my $CGIBorCat=CGI::scrolling_list( -name => 'Filter',
132 -id => 'borcat',
133 -values => \@select,
134 -labels => \%select,
135 -size => 1,
136 -multiple => 0 );
138 $req = $dbh->prepare( "select distinctrow itemtype,description from itemtypes order by description");
139 $req->execute;
140 undef @select;
141 undef %select;
142 push @select,"";
143 $select{""}="";
144 while (my ($value,$desc) =$req->fetchrow) {
145 push @select, $value;
146 $select{$value}=$desc;
148 my $CGIItemTypes=CGI::scrolling_list( -name => 'Filter',
149 -id => 'itemtype',
150 -values => \@select,
151 -labels => \%select,
152 -size => 1,
153 -multiple => 0 );
155 $req = $dbh->prepare("select distinctrow sort1 from borrowers where sort1 is not null order by sort1");
156 $req->execute;
157 undef @select;
158 push @select,"";
159 my $hassort1;
160 while (my ($value) =$req->fetchrow) {
161 $hassort1 =1 if ($value);
162 push @select, $value;
164 my $branches=GetBranches();
165 my @select_branch;
166 my %select_branches;
167 push @select_branch,"";
168 $select_branches{""} = "";
169 foreach my $branch (keys %$branches) {
170 push @select_branch, $branch;
171 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
173 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
174 -id => 'branch',
175 -values => \@select_branch,
176 -labels => \%select_branches,
177 -size => 1,
178 -multiple => 0 );
180 my $CGISort1=CGI::scrolling_list( -name => 'Filter',
181 -id => 'sort1',
182 -values => \@select,
183 -size => 1,
184 -multiple => 0 );
186 $req = $dbh->prepare("select distinctrow sort2 from borrowers where sort2 is not null order by sort2");
187 $req->execute;
188 undef @select;
189 push @select,"";
190 my $hassort2;
191 my $hglghtsort2;
192 while (my ($value) =$req->fetchrow) {
193 $hassort2 =1 if ($value);
194 $hglghtsort2= !($hassort1);
195 push @select, $value;
197 my $CGISort2=CGI::scrolling_list( -name => 'Filter',
198 -id => 'sort2',
199 -values => \@select,
200 -size => 1,
201 -multiple => 0 );
202 # location list
203 $req = $dbh->prepare("select distinctrow location from items order by location");
204 $req->execute;
205 undef @select;
206 push @select,"";
207 while (my ($value) =$req->fetchrow) {
208 push @select, $value;
210 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
211 -id => 'location',
212 -values => \@select,
213 -size => 1,
214 -multiple => 0 );
215 # various
216 my @mime = ( C4::Context->preference("MIME") );
218 my $CGIextChoice=CGI::scrolling_list(
219 -name => 'MIME',
220 -id => 'MIME',
221 -values => \@mime,
222 -size => 1,
223 -multiple => 0 );
225 my @dels = ( C4::Context->preference("delimiter") );
226 my $CGIsepChoice=CGI::scrolling_list(
227 -name => 'sep',
228 -id => 'sep',
229 -values => \@dels,
230 -size => 1,
231 -multiple => 0 );
233 $template->param(
234 CGIBorCat => $CGIBorCat,
235 CGIItemType => $CGIItemTypes,
236 CGIBranch => $CGIBranch,
237 hassort1=> $hassort1,
238 hassort2=> $hassort2,
239 HlghtSort2 => $hglghtsort2,
240 CGISort1 => $CGISort1,
241 CGISort2 => $CGISort2,
242 CGIextChoice => $CGIextChoice,
243 CGIsepChoice => $CGIsepChoice,
244 CGILocation => $CGIlocation,
246 output_html_with_http_headers $input, $cookie, $template->output;
252 sub calculate {
253 my ($line, $column, $dsp, $type,$daysel,$monthsel ,$process, $filters) = @_;
254 my @mainloop;
255 my @loopfooter;
256 my @loopcol;
257 my @loopline;
258 my @looprow;
259 my %globalline;
260 my $grantotal =0;
261 # extract parameters
262 my $dbh = C4::Context->dbh;
264 # Filters
265 # Checking filters
267 my @loopfilter;
268 for (my $i=0;$i<=9;$i++) {
269 my %cell;
270 if ( @$filters[$i] ) {
271 if (($i==1) and (@$filters[$i-1])) {
272 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
274 # format the dates filters, otherwise just fill as is
275 if ($i>=2) {
276 $cell{filter} .= @$filters[$i];
277 } else {
278 $cell{filter} .= format_date(@$filters[$i]);
280 $cell{crit} .="Period From" if ($i==0);
281 $cell{crit} .="Period To" if ($i==1);
282 $cell{crit} .="Borrower Cat=" if ($i==2);
283 $cell{crit} .="Doc Type=" if ($i==3);
284 $cell{crit} .="Branch=" if ($i==4);
285 $cell{crit} .="Location=" if ($i==5);
286 $cell{crit} .="Item callnumber>=" if ($i==6);
287 $cell{crit} .="Item callnumber<" if ($i==7);
288 $cell{crit} .="sort1=" if ($i==8);
289 $cell{crit} .="sort2=" if ($i==9);
291 push @loopfilter, \%cell;
294 push @loopfilter,{crit=>"Issue|Return ",filter=>$type};
295 push @loopfilter,{crit=>"Display by ",filter=>$dsp} if ($dsp);
296 push @loopfilter,{crit=>"Select Day ",filter=>$daysel} if ($daysel);
297 push @loopfilter,{crit=>"Select Month ",filter=>$monthsel} if ($monthsel);
300 my @linefilter;
301 # warn "filtres ".@filters[0];
302 # warn "filtres ".@filters[1];
303 # warn "filtres ".@filters[2];
304 # warn "filtres ".@filters[3];
306 $linefilter[0] = @$filters[0] if ($line =~ /datetime/ ) ;
307 $linefilter[1] = @$filters[1] if ($line =~ /datetime/ ) ;
308 $linefilter[0] = @$filters[2] if ($line =~ /category/ ) ;
309 $linefilter[0] = @$filters[3] if ($line =~ /itemtype/ ) ;
310 $linefilter[0] = @$filters[4] if ($line =~ /branch/ ) ;
311 $linefilter[0] = @$filters[5] if ($line =~ /location/ ) ;
312 $linefilter[0] = @$filters[6] if ($line =~ /sort1/ ) ;
313 $linefilter[0] = @$filters[7] if ($line =~ /sort2/ ) ;
315 my @colfilter ;
316 $colfilter[0] = @$filters[0] if ($column =~ /datetime/) ;
317 $colfilter[1] = @$filters[1] if ($column =~ /datetime/) ;
318 $colfilter[0] = @$filters[2] if ($column =~ /category/) ;
319 $colfilter[0] = @$filters[3] if ($column =~ /itemtype/) ;
320 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
321 $colfilter[0] = @$filters[5] if ($column =~ /location/ ) ;
322 $colfilter[0] = @$filters[6] if ($column =~ /sort1/ ) ;
323 $colfilter[0] = @$filters[7] if ($column =~ /sort2/ ) ;
324 # 1st, loop rows.
325 my $linefield;
326 if (($line =~/datetime/) and ($dsp == 1)) {
327 #Display by day
328 $linefield .="dayname($line)";
329 } elsif (($line=~/datetime/) and ($dsp == 2)) {
330 #Display by Month
331 $linefield .="monthname($line)";
332 } elsif (($line=~/datetime/) and ($dsp == 3)) {
333 #Display by Year
334 $linefield .="Year($line)";
335 } elsif ($line=~/datetime/) {
336 $linefield .= 'date_format(`datetime`,"%Y-%m-%d")';
337 } else {
338 $linefield .= $line;
340 my $lineorder = $linefield;
341 $lineorder = "weekday($line)" if $linefield =~ /dayname/;
342 $lineorder = "month($line)" if $linefield =~ "^month";
343 $lineorder = $linefield if (not ($linefield =~ "^month") and not($linefield =~ /dayname/));
345 my $strsth;
346 $strsth .= "select distinctrow $linefield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $line is not null ";
348 if ($line=~/datetime/) {
349 if ($linefilter[1] and ($linefilter[0])){
350 $strsth .= " and $line between ? and ? " ;
351 } elsif ($linefilter[1]) {
352 $strsth .= " and $line < ? " ;
353 } elsif ($linefilter[0]) {
354 $strsth .= " and $line > ? " ;
356 $strsth .= " and type ='".$type."' " if $type;
357 $strsth .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
358 $strsth .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
359 } elsif ($linefilter[0]) {
360 $linefilter[0] =~ s/\*/%/g;
361 $strsth .= " and $line LIKE ? " ;
363 $strsth .=" group by $linefield";
364 $strsth .=" order by $lineorder";
365 # warn "". $strsth;
367 my $sth = $dbh->prepare( $strsth );
368 if (( @linefilter ) and ($linefilter[1])){
369 $sth->execute("'".$linefilter[0]."'","'".$linefilter[1]."'");
370 } elsif ($linefilter[0]) {
371 $sth->execute($linefilter[0]);
372 } else {
373 $sth->execute;
376 while ( my ($celvalue) = $sth->fetchrow) {
377 my %cell;
378 if ($celvalue) {
379 $cell{rowtitle} = $celvalue;
380 } else {
381 $cell{rowtitle} = "";
383 $cell{totalrow} = 0;
384 push @loopline, \%cell;
387 # 2nd, loop cols.
388 my $colfield;
389 my $colorder;
390 if (($column =~/datetime/) and ($dsp == 1)) {
391 #Display by day
392 $colfield .="dayname($column)";
393 } elsif (($column=~/datetime/) and ($dsp == 2)) {
394 #Display by Month
395 $colfield .="monthname($column)";
396 } elsif (($column=~/datetime/) and ($dsp == 3)) {
397 #Display by Year
398 $colfield .="Year($column)";
399 } elsif ($column=~/datetime/) {
400 $colfield .='date_format(`datetime`,"%Y-%m-%d")';
401 } else {
402 $colfield .= $column;
404 $colorder = "weekday($line)" if $colfield =~ "^dayname";
405 $colorder = "month($line)" if $colfield =~ "^month";
406 $colorder = $colfield if (not ($colfield =~ "^month") and not($colfield =~ "^dayname"));
408 my $strsth2;
409 $strsth2 .= "select distinctrow $colfield from statistics, borrowers where (statistics.borrowernumber=borrowers.borrowernumber) and $column is not null ";
411 if ($column=~/datetime/){
412 if (($colfilter[1]) and ($colfilter[0])){
413 $strsth2 .= " and $column between ? and ? " ;
414 } elsif ($colfilter[1]) {
415 $strsth2 .= " and $column < ? " ;
416 } elsif ($colfilter[0]) {
417 $strsth2 .= " and $column > ? " ;
419 $strsth2 .= " and type ='".$type."' " if $type;
420 $strsth2 .= " and dayname(datetime) ='". $daysel ."' " if $daysel;
421 $strsth2 .= " and monthname(datetime) ='". $monthsel ."' " if $monthsel;
422 } elsif ($colfilter[0]) {
423 $colfilter[0] =~ s/\*/%/g;
424 $strsth2 .= " and $column LIKE ? " ;
426 $strsth2 .=" group by $colfield";
427 $strsth2 .=" order by $colorder";
428 # warn "". $strsth2;
430 my $sth2 = $dbh->prepare( $strsth2 );
431 if (( @colfilter ) and ($colfilter[1])){
432 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
433 } elsif ($colfilter[0]) {
434 $sth2->execute($colfilter[0]);
435 } else {
436 $sth2->execute;
440 while (my ($celvalue) = $sth2->fetchrow) {
441 my %cell;
442 my %ft;
443 # warn "coltitle :".$celvalue;
444 $cell{coltitle} = $celvalue;
445 $ft{totalcol} = 0;
446 push @loopcol, \%cell;
448 # warn "fin des titres colonnes";
450 my $i=0;
451 my @totalcol;
452 my $hilighted=-1;
454 #Initialization of cell values.....
455 my %table;
456 # warn "init table";
457 foreach my $row ( @loopline ) {
458 foreach my $col ( @loopcol ) {
459 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
460 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
462 $table{$row->{rowtitle}}->{totalrow}=0;
465 # preparing calculation
466 my $strcalc ;
468 $strcalc .= "SELECT $linefield, $colfield, ";
469 $strcalc .= "COUNT( * ) " if ($process ==1);
470 if ($process ==2){
471 $strcalc .= "(COUNT(DISTINCT borrowers.borrowernumber))" ;
473 if ($process ==3){
474 $strcalc .= "(COUNT(DISTINCT issues.itemnumber))" ;
476 if ($process ==4){
477 my $rqbookcount = $dbh->prepare("SELECT count(*) FROM items");
478 $rqbookcount->execute;
479 my ($bookcount) = $rqbookcount->fetchrow;
480 $strcalc .= "100*(COUNT(DISTINCT issues.itemnumber))/ $bookcount " ;
482 $strcalc .= "FROM statistics ";
483 $strcalc .= "LEFT JOIN borrowers ON statistics.borrowernumber=borrowers.borrowernumber ";
484 $strcalc .= "LEFT JOIN items ON statistics.itemnumber=items.itemnumber " if @$filters[5] or @$filters[6];
486 $strcalc .= "WHERE 1=1 ";
487 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
488 $strcalc .= " AND statistics.datetime > '" . @$filters[0] ."'" if ( @$filters[0] );
489 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
490 $strcalc .= " AND statistics.datetime < '" . @$filters[1] ."'" if ( @$filters[1] );
491 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
492 $strcalc .= " AND borrowers.categorycode like '" . @$filters[2] ."'" if ( @$filters[2] );
493 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
494 $strcalc .= " AND statistics.itemtype like '" . @$filters[3] ."'" if ( @$filters[3] );
495 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
496 $strcalc .= " AND statistics.branch like '" . @$filters[4] ."'" if ( @$filters[4] );
497 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
498 $strcalc .= " AND items.location like '" . @$filters[5] ."'" if ( @$filters[5] );
499 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
500 $strcalc .= " AND items.itemcallnumber >='" . @$filters[6] ."'" if ( @$filters[6] );
501 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
502 $strcalc .= " AND items.itemcallnumber <'" . @$filters[7] ."'" if ( @$filters[7] );
503 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
504 $strcalc .= " AND borrowers.sort1 like '" . @$filters[8] ."'" if ( @$filters[8] );
505 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
506 $strcalc .= " AND borrowers.sort2 like '" . @$filters[9] ."'" if ( @$filters[9] );
507 $strcalc .= " AND dayname(datetime) like '" . $daysel ."'" if ( $daysel );
508 $strcalc .= " AND monthname(datetime) like '" . $monthsel ."'" if ( $monthsel );
509 $strcalc .= " AND statistics.type like '" . $type ."'" if ( $type );
511 $strcalc .= " group by $linefield, $colfield order by $lineorder,$colorder";
512 warn "". $strcalc;
513 my $dbcalc = $dbh->prepare($strcalc);
514 $dbcalc->execute;
515 # warn "filling table";
516 my $emptycol;
517 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
518 # warn "filling table $row / $col / $value ";
519 $emptycol = 1 if ($col eq undef);
520 $col = "zzEMPTY" if ($col eq undef);
521 $row = "zzEMPTY" if ($row eq undef);
523 $table{$row}->{$col}+=$value;
524 $table{$row}->{totalrow}+=$value;
525 $grantotal += $value;
527 push @loopcol,{coltitle => "NULL"} if ($emptycol);
529 foreach my $row (@loopline) {
530 my @loopcell;
531 #@loopcol ensures the order for columns is common with column titles
532 # and the number matches the number of columns
533 foreach my $col ( @loopcol ) {
534 my $value =$table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
535 push @loopcell, {value => $value } ;
537 push @looprow,{ 'rowtitle' => ($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle},
538 'loopcell' => \@loopcell,
539 'hilighted' => ($hilighted >0),
540 'totalrow' => $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{totalrow}
542 $hilighted = -$hilighted;
545 # warn "footer processing";
546 foreach my $col ( @loopcol ) {
547 my $total=0;
548 foreach my $row ( @looprow ) {
549 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
550 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
552 # warn "summ for column ".$col->{coltitle}." = ".$total;
553 push @loopfooter, {'totalcol' => $total};
557 # the header of the table
558 $globalline{loopfilter}=\@loopfilter;
559 # the core of the table
560 $globalline{looprow} = \@looprow;
561 $globalline{loopcol} = \@loopcol;
562 # # the foot (totals by borrower type)
563 $globalline{loopfooter} = \@loopfooter;
564 $globalline{total}= $grantotal;
565 $globalline{line} = $line;
566 $globalline{column} = $column;
567 push @mainloop,\%globalline;
568 return \@mainloop;