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
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
25 use C4::Branch; # GetBranches
32 plugin that shows a stats on borrowers
42 my $fullreportname = "reports/catalogue_stats.tmpl";
43 my $do_it = $input->param('do_it');
44 my $line = $input->param("Line");
45 my $column = $input->param("Column");
46 my @filters = $input->param("Filter");
47 my $deweydigits = $input->param("deweydigits");
48 my $lccndigits = $input->param("lccndigits");
49 my $cotedigits = $input->param("cotedigits");
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 my $del = $input->param("sep");
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => $fullreportname,
60 flagsrequired => {reports => 1},
63 $template->param(do_it => $do_it);
65 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
66 if ($output eq "screen"){
67 $template->param(mainloop => $results);
68 output_html_with_http_headers $input, $cookie, $template->output;
71 print $input->header(-type => 'application/vnd.sun.xml.calc',
73 -attachment=>"$basename.csv",
74 -name=>"$basename.csv" );
75 my $cols = @$results[0]->{loopcol};
76 my $lines = @$results[0]->{looprow};
77 my $sep = C4::Context->preference("delimiter");
78 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
79 foreach my $col ( @$cols ) {
80 print $col->{coltitle}.$sep;
83 foreach my $line ( @$lines ) {
84 my $x = $line->{loopcell};
85 print $line->{rowtitle}.$sep;
86 foreach my $cell (@$x) {
87 print $cell->{value}.$sep;
89 print $line->{totalrow};
93 $cols = @$results[0]->{loopfooter};
94 foreach my $col ( @$cols ) {
95 print $sep.$col->{totalcol};
97 print $sep.@$results[0]->{total};
101 my $dbh = C4::Context->dbh;
107 # FIXME: no such field "dewey"
108 # $req = $dbh->prepare("select count(dewey) from biblioitems ");
112 # while (my ($value) =$req->fetchrow) {
113 # if (($value>2) and (! $hasdewey)) {
117 # push @select, $value;
119 # my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
121 # -values => \@select,
125 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
126 # This is not the LC Classification. It's the Control Number.
127 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
130 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
133 # while (my ($value) =$req->fetchrow) {
134 # $hlghtlccn = !($hasdewey);
135 # $haslccn =1 if (($value>2) and (! $haslccn));
136 # $count++ if (($value) and (! $haslccn));
137 # push @select, $value;
139 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
141 # -values => \@select,
145 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
146 # FIXME: Hardcoding to 5 chars on itemcallnum.
151 # $req = $dbh->prepare("select count(itemcallnumber) from items");
154 # while (my ($value) =$req->fetchrow) {
155 # $hascote =1 if (($value>2) and (! $hascote));
156 # $count++ if (($value) and (! $hascote));
157 # $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
158 ## push @select, $value;
160 # my $CGIcote=CGI::scrolling_list( -name => 'Filter',
162 # -values => \@select,
165 # my $hglghtDT = ++$count % 2;
166 # my $hglghtPub = ++$count % 2;
167 # my $hglghtPY = ++$count % 2;
168 # my $hglghtHB = ++$count % 2;
169 # my $hglghtLOC = ++$count % 2;
170 # warn "highlightDT " .$hglghtDT;
171 # warn "highlightPub " .$hglghtPub;
172 # warn "highlightPY " .$hglghtPY;
173 # warn "highlightHB " .$hglghtHB;
174 # warn "highlightLOC " .$hglghtLOC;
177 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
179 my $CGIitemtype = $req->fetchall_arrayref({});
181 my $authvals = GetKohaAuthorisedValues("items.ccode");
183 foreach (keys %$authvals) {
184 push @authvals, { code => $_, description => $authvals->{$_} };
187 # $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
191 # while (my ($value) =$req->fetchrow) {
192 # push @select, $value;
194 # my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
196 # -values => \@select,
200 my $branches=GetBranches();
202 foreach (keys %$branches) {
203 my $thisbranch = ''; # FIXME: populate $thisbranch to preselect one
204 my %row = (branchcode => $_,
205 selected => ($thisbranch eq $_ ? 1 : 0),
206 branchname => $branches->{$_}->{'branchname'},
208 push @branchloop, \%row;
211 $req = $dbh->prepare("select distinctrow location from items order by location");
213 my $locationloop = $req->fetchall_arrayref({});
214 my $locations = GetKohaAuthorisedValues("items.location");
216 foreach (sort keys %$locations) {
217 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
220 my @mime = ( map { +{type =>$_} } (split /[;:]/,C4::Context->preference("MIME")) );
221 my @delim = ( map { +{delim=>$_} } (split //,C4::Context->preference("delimiter")) );
223 $template->param(hasdewey=>$hasdewey,
224 # CGIFromDeweyClass => $CGIdewey,
225 # CGIToDeweyClass => $CGIdewey,
227 # hlghtlccn => $hlghtlccn,
228 # CGIFromLoCClass => $CGIlccn,
229 # CGIToLoCClass => $CGIlccn,
231 # hlghtcote => $hlghtcote,
232 # hglghtDT => $hglghtDT,
233 # hglghtPub => $hglghtPub,
234 # hglghtPY => $hglghtPY,
235 # hglghtHB => $hglghtHB,
236 # hglghtLOC => $hglghtLOC,
237 # CGIFromCoteClass => $CGIcote,
238 # CGIToCoteClass => $CGIcote,
239 CGIItemType => $CGIitemtype,
240 # CGIFromPublicationYear => $CGIpublicationyear,
241 # CGIToPublicationYear => $CGIpublicationyear,
242 # CGIPublisher => $CGIpublisher,
243 CGIBranch => \@branchloop,
244 locationloop => \@locations,
245 authvals => \@authvals,
246 CGIextChoice => \@mime,
247 CGIsepChoice => \@delim,
251 output_html_with_http_headers $input, $cookie, $template->output;
257 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
266 my $dbh = C4::Context->dbh;
272 for (my $i=0;$i<=12;$i++) {
274 if ( @$filters[$i] ) {
275 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
276 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
278 $cell{filter} .= @$filters[$i];
280 ($i== 0) ? "Dewey Classification From" :
281 ($i== 1) ? "Dewey Classification To" :
282 ($i== 2) ? "Lccn Classification From" :
283 ($i== 3) ? "Lccn Classification To" :
284 ($i== 4) ? "Item CallNumber From" :
285 ($i== 5) ? "Item CallNumber To" :
286 ($i== 6) ? "Item type" :
287 ($i== 7) ? "Publisher" :
288 ($i== 8) ? "Publication year From" :
289 ($i== 9) ? "Publication year To" :
290 ($i==10) ? "Library :" :
291 ($i==11) ? "Shelving Location :" :
292 ($i==12) ? "Collection Code :" : '';
293 push @loopfilter, \%cell;
297 # warn map {"filtres $_\n"} @filters[0..3];
300 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
301 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
302 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
303 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
304 $linefilter[0] = @$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
305 $linefilter[1] = @$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
306 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
307 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
308 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
309 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
310 $linefilter[0] = @$filters[10] if ($line =~ /items\.homebranch/ ) ;
311 $linefilter[0] = @$filters[11] if ($line =~ /items\.location/ ) ;
312 $linefilter[0] = @$filters[12] if ($line =~ /items\.ccode/ ) ;
315 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
316 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
317 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
318 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
319 $colfilter[0] = @$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
320 $colfilter[1] = @$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
321 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
322 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
323 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
324 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
325 $colfilter[0] = @$filters[10] if ($column =~ /items\.homebranch/ ) ;
326 $colfilter[0] = @$filters[11] if ($column =~ /items\.location/ ) ;
327 $colfilter[0] = @$filters[12] if ($column =~ /items\.ccode/ ) ;
331 if (($line =~/dewey/) and ($deweydigits)) {
332 $linefield .="left($line,$deweydigits)";
333 } elsif (($line=~/lccn/) and ($lccndigits)) {
334 $linefield .="left($line,$lccndigits)";
335 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
336 $linefield .="left($line,$cotedigits)";
342 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
345 $strsth .= " and $line >= ? " ;
346 $strsth .= " and $line <= ? " ;
347 } elsif ($linefilter[0]) {
348 $linefilter[0] =~ s/\*/%/g;
349 $strsth .= " and $line LIKE ? " ;
352 $strsth .=" order by $linefield";
353 $debug and print STDERR "catalogue_stats SQL: $strsth\n";
355 my $sth = $dbh->prepare( $strsth );
356 if (( @linefilter ) and ($linefilter[1])){
357 $sth->execute($linefilter[0],$linefilter[1]);
358 } elsif ($linefilter[0]) {
359 $sth->execute($linefilter[0]);
363 while ( my ($celvalue) = $sth->fetchrow) {
366 $cell{rowtitle} = $celvalue;
368 # $cell{rowtitle} = "";
371 push @loopline, \%cell;
376 if (($column =~/dewey/) and ($deweydigits)) {
377 $colfield = "left($column,$deweydigits)";
378 }elsif (($column=~/lccn/) and ($lccndigits)) {
379 $colfield = "left($column,$lccndigits)";
380 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
381 $colfield = "left($column,$cotedigits)";
387 SELECT distinctrow $colfield
390 ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
391 WHERE $column IS NOT NULL ";
392 if (( @colfilter ) and ($colfilter[1])) {
393 $strsth2 .= " and $column> ? and $column< ?";
394 }elsif ($colfilter[0]){
395 $colfilter[0] =~ s/\*/%/g;
396 $strsth2 .= " and $column LIKE ? ";
398 $strsth2 .= " order by $colfield";
399 $debug and print STDERR "SQL: $strsth2";
400 my $sth2 = $dbh->prepare( $strsth2 );
401 if ((@colfilter) and ($colfilter[1])) {
402 $sth2->execute($colfilter[0],$colfilter[1]);
403 } elsif ($colfilter[0]){
404 $sth2->execute($colfilter[0]);
408 while (my ($celvalue) = $sth2->fetchrow) {
412 $cell{coltitle} = $celvalue;
414 # $cell{coltitle} = "";
417 push @loopcol, \%cell;
424 #Initialization of cell values.....
427 foreach my $row ( @loopline ) {
428 foreach my $col ( @loopcol ) {
429 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
430 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
432 $table{$row->{rowtitle}}->{totalrow}=0;
435 # preparing calculation
436 my $strcalc .= "SELECT $linefield, $colfield, count(*) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
438 @$filters[0]=~ s/\*/%/g;
439 $strcalc .= " AND dewey >" . @$filters[0];
442 @$filters[1]=~ s/\*/%/g ;
443 $strcalc .= " AND dewey <" . @$filters[1];
446 @$filters[2]=~ s/\*/%/g ;
447 $strcalc .= " AND lccn >" . @$filters[2];
450 @$filters[3]=~ s/\*/%/g;
451 $strcalc .= " AND lccn <" . @$filters[3];
454 @$filters[4]=~ s/\*/%/g ;
455 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]);
459 @$filters[5]=~ s/\*/%/g;
460 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]);
464 @$filters[6]=~ s/\*/%/g;
465 $strcalc .= " AND " .
466 (C4::Context::preference('Item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype')
467 . " LIKE '" . @$filters[6] ."'";
471 @$filters[7]=~ s/\*/%/g;
472 @$filters[7].="%" unless @$filters[7]=~/%/;
473 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @$filters[7] ."\"";
476 @$filters[8]=~ s/\*/%/g;
477 $strcalc .= " AND publicationyear >" . @$filters[8];
480 @$filters[9]=~ s/\*/%/g;
481 $strcalc .= " AND publicationyear <" . @$filters[9];
484 @$filters[10]=~ s/\*/%/g;
485 $strcalc .= " AND items.homebranch LIKE '" . @$filters[10] ."'";
488 @$filters[11]=~ s/\*/%/g;
489 $strcalc .= " AND items.location LIKE '" . @$filters[11] ."'";
492 @$filters[12]=~ s/\*/%/g;
493 $strcalc .= " AND items.ccode LIKE '" . @$filters[12] ."'";
496 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
497 $debug and warn "SQL: $strcalc";
498 my $dbcalc = $dbh->prepare($strcalc);
500 # warn "filling table";
503 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
504 # warn "filling table $row / $col / $value ";
505 $emptycol = 1 if ($col eq undef);
506 $col = "zzEMPTY" if ($col eq undef);
507 $row = "zzEMPTY" if ($row eq undef);
509 $table{$row}->{$col}+=$value;
510 $table{$row}->{totalrow}+=$value;
511 $grantotal += $value;
514 # my %cell = {rowtitle => 'zzROWEMPTY'};
515 # push @loopline,\%cell;
518 # %cell = {coltitle => "zzEMPTY"};
519 push @loopcol,{coltitle => "NULL"} if ($emptycol);
521 foreach my $row ( sort keys %table ) {
523 #@loopcol ensures the order for columns is common with column titles
524 # and the number matches the number of columns
525 foreach my $col ( @loopcol ) {
526 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
527 push @loopcell, {value => $value } ;
529 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
530 'loopcell' => \@loopcell,
531 'hilighted' => ($hilighted *= -1 > 0),
532 'totalrow' => $table{$row}->{totalrow}
536 # warn "footer processing";
537 foreach my $col ( @loopcol ) {
539 foreach my $row ( @looprow ) {
540 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
541 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
543 # warn "summ for column ".$col->{coltitle}." = ".$total;
544 push @loopfooter, {'totalcol' => $total};
548 # the header of the table
549 $globalline{loopfilter}=\@loopfilter;
550 # the core of the table
551 $globalline{looprow} = \@looprow;
552 $globalline{loopcol} = \@loopcol;
553 # # the foot (totals by borrower type)
554 $globalline{loopfooter} = \@loopfooter;
555 $globalline{total}= $grantotal;
556 $globalline{line} = $line;
557 $globalline{column} = $column;
558 push @mainloop,\%globalline;