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 # (rch) biblioitems.lccn is mapped to lccn MARC21 010$a in default framework.
113 # This is not the LC Classification. It's the Control Number.
114 # So I'm just going to remove this bit. Call Number is handled in itemcallnumber.
117 # $req = $dbh->prepare( "select count(lccn) from biblioitems ");
120 # while (my ($value) =$req->fetchrow) {
121 # $hlghtlccn = !($hasdewey);
122 # $haslccn =1 if (($value>2) and (! $haslccn));
123 # $count++ if (($value) and (! $haslccn));
124 # push @select, $value;
126 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
128 # -values => \@select,
132 # No need to test for data here. If you don't have itemcallnumbers, you probably know it.
133 # FIXME: Hardcoding to 5 chars on itemcallnum.
138 $req = $dbh->prepare("select itemtype, description from itemtypes order by description");
140 my $CGIitemtype = $req->fetchall_arrayref({});
142 my $authvals = GetKohaAuthorisedValues
("items.ccode");
144 foreach (keys %$authvals) {
145 push @authvals, { code
=> $_, description
=> $authvals->{$_} };
149 my $branches=GetBranches
();
151 foreach (keys %$branches) {
152 my $thisbranch = ''; # FIXME: populate $thisbranch to preselect one
153 my %row = (branchcode
=> $_,
154 selected
=> ($thisbranch eq $_ ?
1 : 0),
155 branchname
=> $branches->{$_}->{'branchname'},
157 push @branchloop, \
%row;
160 my $locations = GetKohaAuthorisedValues
("items.location");
162 foreach (sort keys %$locations) {
163 push @locations, { code
=> $_, description
=> "$_ - " . $locations->{$_} };
166 my @mime = ( map { +{type
=>$_} } (split /[;:]/,C4
::Context
->preference("MIME")) );
167 my @delim = ( map { +{delim
=>$_} } (split //,C4
::Context
->preference("delimiter")) );
169 $template->param(hasdewey
=>$hasdewey,
172 CGIItemType
=> $CGIitemtype,
173 CGIBranch
=> \
@branchloop,
174 locationloop
=> \
@locations,
175 authvals
=> \
@authvals,
176 CGIextChoice
=> \
@mime,
177 CGIsepChoice
=> \
@delim,
181 output_html_with_http_headers
$input, $cookie, $template->output;
187 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
196 my $dbh = C4
::Context
->dbh;
202 for (my $i=0;$i<=12;$i++) {
204 if ( @
$filters[$i] ) {
205 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@
$filters[$i-1])) {
206 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
208 $cell{filter
} .= @
$filters[$i];
210 ($i== 0) ?
"Dewey Classification From" :
211 ($i== 1) ?
"Dewey Classification To" :
212 ($i== 2) ?
"Lccn Classification From" :
213 ($i== 3) ?
"Lccn Classification To" :
214 ($i== 4) ?
"Item CallNumber From" :
215 ($i== 5) ?
"Item CallNumber To" :
216 ($i== 6) ?
"Item type" :
217 ($i== 7) ?
"Publisher" :
218 ($i== 8) ?
"Publication year From" :
219 ($i== 9) ?
"Publication year To" :
220 ($i==10) ?
"Library :" :
221 ($i==11) ?
"Shelving Location :" :
222 ($i==12) ?
"Collection Code :" : '';
223 push @loopfilter, \
%cell;
227 # warn map {"filtres $_\n"} @filters[0..3];
230 $linefilter[0] = @
$filters[0] if ($line =~ /dewey/ ) ;
231 $linefilter[1] = @
$filters[1] if ($line =~ /dewey/ ) ;
232 $linefilter[0] = @
$filters[2] if ($line =~ /lccn/ ) ;
233 $linefilter[1] = @
$filters[3] if ($line =~ /lccn/ ) ;
234 $linefilter[0] = @
$filters[4] if ($line =~ /items\.itemcallnumber/ ) ;
235 $linefilter[1] = @
$filters[5] if ($line =~ /items\.itemcallnumber/ ) ;
236 $linefilter[0] = @
$filters[6] if ($line =~ /itemtype/ ) ;
237 $linefilter[0] = @
$filters[7] if ($line =~ /publishercode/ ) ;
238 $linefilter[0] = @
$filters[8] if ($line =~ /publicationyear/ ) ;
239 $linefilter[1] = @
$filters[9] if ($line =~ /publicationyear/ ) ;
240 $linefilter[0] = @
$filters[10] if ($line =~ /items\.homebranch/ ) ;
241 $linefilter[0] = @
$filters[11] if ($line =~ /items\.location/ ) ;
242 $linefilter[0] = @
$filters[12] if ($line =~ /items\.ccode/ ) ;
245 $colfilter[0] = @
$filters[0] if ($column =~ /dewey/ ) ;
246 $colfilter[1] = @
$filters[1] if ($column =~ /dewey/ ) ;
247 $colfilter[0] = @
$filters[2] if ($column =~ /lccn/ ) ;
248 $colfilter[1] = @
$filters[3] if ($column =~ /lccn/ ) ;
249 $colfilter[0] = @
$filters[4] if ($column =~ /items\.itemcallnumber/ ) ;
250 $colfilter[1] = @
$filters[5] if ($column =~ /items\.itemcallnumber/ ) ;
251 $colfilter[0] = @
$filters[6] if ($column =~ /itemtype/ ) ;
252 $colfilter[0] = @
$filters[7] if ($column =~ /publishercode/ ) ;
253 $colfilter[0] = @
$filters[8] if ($column =~ /publicationyear/ ) ;
254 $colfilter[1] = @
$filters[9] if ($column =~ /publicationyear/ ) ;
255 $colfilter[0] = @
$filters[10] if ($column =~ /items\.homebranch/ ) ;
256 $colfilter[0] = @
$filters[11] if ($column =~ /items\.location/ ) ;
257 $colfilter[0] = @
$filters[12] if ($column =~ /items\.ccode/ ) ;
261 if (($line =~/dewey/) and ($deweydigits)) {
262 $linefield .="left($line,$deweydigits)";
263 } elsif (($line=~/lccn/) and ($lccndigits)) {
264 $linefield .="left($line,$lccndigits)";
265 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
266 $linefield .="left($line,$cotedigits)";
272 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
275 $strsth .= " and $line >= ? " ;
276 $strsth .= " and $line <= ? " ;
277 } elsif ($linefilter[0]) {
278 $linefilter[0] =~ s/\*/%/g;
279 $strsth .= " and $line LIKE ? " ;
282 $strsth .=" order by $linefield";
283 $debug and print STDERR
"catalogue_stats SQL: $strsth\n";
285 my $sth = $dbh->prepare( $strsth );
286 if (( @linefilter ) and ($linefilter[1])){
287 $sth->execute($linefilter[0],$linefilter[1]);
288 } elsif ($linefilter[0]) {
289 $sth->execute($linefilter[0]);
293 while ( my ($celvalue) = $sth->fetchrow) {
296 $cell{rowtitle
} = $celvalue;
298 # $cell{rowtitle} = "";
301 push @loopline, \
%cell;
306 if (($column =~/dewey/) and ($deweydigits)) {
307 $colfield = "left($column,$deweydigits)";
308 }elsif (($column=~/lccn/) and ($lccndigits)) {
309 $colfield = "left($column,$lccndigits)";
310 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
311 $colfield = "left($column,$cotedigits)";
317 SELECT distinctrow $colfield
320 ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
321 WHERE $column IS NOT NULL ";
322 if (( @colfilter ) and ($colfilter[1])) {
323 $strsth2 .= " and $column> ? and $column< ?";
324 }elsif ($colfilter[0]){
325 $colfilter[0] =~ s/\*/%/g;
326 $strsth2 .= " and $column LIKE ? ";
328 $strsth2 .= " order by $colfield";
329 $debug and print STDERR
"SQL: $strsth2";
330 my $sth2 = $dbh->prepare( $strsth2 );
331 if ((@colfilter) and ($colfilter[1])) {
332 $sth2->execute($colfilter[0],$colfilter[1]);
333 } elsif ($colfilter[0]){
334 $sth2->execute($colfilter[0]);
338 while (my ($celvalue) = $sth2->fetchrow) {
342 $cell{coltitle
} = $celvalue;
344 # $cell{coltitle} = "";
347 push @loopcol, \
%cell;
354 #Initialization of cell values.....
357 foreach my $row ( @loopline ) {
358 foreach my $col ( @loopcol ) {
359 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
360 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
362 $table{$row->{rowtitle
}}->{totalrow
}=0;
365 # preparing calculation
366 my $strcalc .= "SELECT $linefield, $colfield, count(*) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
368 @
$filters[0]=~ s/\*/%/g;
369 $strcalc .= " AND dewey >" . @
$filters[0];
372 @
$filters[1]=~ s/\*/%/g ;
373 $strcalc .= " AND dewey <" . @
$filters[1];
376 @
$filters[2]=~ s/\*/%/g ;
377 $strcalc .= " AND lccn >" . @
$filters[2];
380 @
$filters[3]=~ s/\*/%/g;
381 $strcalc .= " AND lccn <" . @
$filters[3];
384 @
$filters[4]=~ s/\*/%/g ;
385 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@
$filters[4]);
389 @
$filters[5]=~ s/\*/%/g;
390 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@
$filters[5]);
394 @
$filters[6]=~ s/\*/%/g;
395 $strcalc .= " AND " .
396 (C4
::Context
::preference
('Item-level_itypes') ?
'items.itype' : 'biblioitems.itemtype')
397 . " LIKE '" . @
$filters[6] ."'";
401 @
$filters[7]=~ s/\*/%/g;
402 @
$filters[7].="%" unless @
$filters[7]=~/%/;
403 $strcalc .= " AND biblioitems.publishercode LIKE \"" . @
$filters[7] ."\"";
406 @
$filters[8]=~ s/\*/%/g;
407 $strcalc .= " AND publicationyear >" . @
$filters[8];
410 @
$filters[9]=~ s/\*/%/g;
411 $strcalc .= " AND publicationyear <" . @
$filters[9];
414 @
$filters[10]=~ s/\*/%/g;
415 $strcalc .= " AND items.homebranch LIKE '" . @
$filters[10] ."'";
418 @
$filters[11]=~ s/\*/%/g;
419 $strcalc .= " AND items.location LIKE '" . @
$filters[11] ."'";
422 @
$filters[12]=~ s/\*/%/g;
423 $strcalc .= " AND items.ccode LIKE '" . @
$filters[12] ."'";
426 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
427 $debug and warn "SQL: $strcalc";
428 my $dbcalc = $dbh->prepare($strcalc);
430 # warn "filling table";
433 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
434 # warn "filling table $row / $col / $value ";
435 $emptycol = 1 if (!defined($col));
436 $col = "zzEMPTY" if (!defined($col));
437 $row = "zzEMPTY" if (!defined($row));
439 $table{$row}->{$col}+=$value;
440 $table{$row}->{totalrow
}+=$value;
441 $grantotal += $value;
444 # my %cell = {rowtitle => 'zzROWEMPTY'};
445 # push @loopline,\%cell;
448 # %cell = {coltitle => "zzEMPTY"};
449 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
451 foreach my $row ( sort keys %table ) {
453 #@loopcol ensures the order for columns is common with column titles
454 # and the number matches the number of columns
455 foreach my $col ( @loopcol ) {
456 my $value =$table{$row}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
457 push @loopcell, {value
=> $value } ;
459 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?
"NULL":$row,
460 'loopcell' => \
@loopcell,
461 'hilighted' => ($hilighted *= -1 > 0),
462 'totalrow' => $table{$row}->{totalrow
}
466 # warn "footer processing";
467 foreach my $col ( @loopcol ) {
469 foreach my $row ( @looprow ) {
470 $total += $table{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
471 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
473 # warn "summ for column ".$col->{coltitle}." = ".$total;
474 push @loopfooter, {'totalcol' => $total};
478 # the header of the table
479 $globalline{loopfilter
}=\
@loopfilter;
480 # the core of the table
481 $globalline{looprow
} = \
@looprow;
482 $globalline{loopcol
} = \
@loopcol;
483 # # the foot (totals by borrower type)
484 $globalline{loopfooter
} = \
@loopfooter;
485 $globalline{total
}= $grantotal;
486 $globalline{line
} = $line;
487 $globalline{column
} = $column;
488 push @mainloop,\
%globalline;