3 # Copyright 2007 Liblime Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
23 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # use Smart::Comments;
32 # set the version for version checking
37 get_report_types get_report_areas get_columns build_query get_criteria
38 save_report get_saved_reports execute_query get_saved_report create_compound run_compound
39 get_column_type get_distinct_values save_dictionary get_from_dictionary
40 delete_definition delete_report format_results get_sql
46 [ 'borrowers', 'statistics','items', 'biblioitems' ]; # circulation
47 $table_areas{'2'} = [ 'items', 'biblioitems', 'biblio' ]; # catalogue
48 $table_areas{'3'} = [ 'borrowers' ]; # patrons
49 $table_areas{'4'} = ['aqorders', 'biblio', 'items']; # acquisitions
50 $table_areas{'5'} = [ 'borrowers', 'accountlines' ]; # accounts
53 'statistics.borrowernumber=borrowers.borrowernumber',
54 'items.itemnumber = statistics.itemnumber',
55 'biblioitems.biblioitemnumber = items.biblioitemnumber'
58 'items.biblioitemnumber=biblioitems.biblioitemnumber',
59 'biblioitems.biblionumber=biblio.biblionumber'
63 'aqorders.biblionumber=biblio.biblionumber',
64 'biblio.biblionumber=items.biblionumber'
66 $keys{'5'} = ['borrowers.borrowernumber=accountlines.borrowernumber'];
68 # have to do someting here to know if its dropdown, free text, date etc
72 'statistics.type', 'borrowers.categorycode',
74 'biblioitems.publicationyear|date',
75 'items.dateaccessioned|date'
78 [ 'items.holdingbranch', 'items.homebranch' ,'items.itemlost', 'items.location', 'items.ccode'];
79 $criteria{'3'} = ['borrowers.branchcode'];
80 $criteria{'4'} = ['aqorders.datereceived|date'];
81 $criteria{'5'} = ['borrowers.branchcode'];
83 if (C4
::Context
->preference('item-level_itypes')) {
84 unshift @
{ $criteria{'1'} }, 'items.itype';
85 unshift @
{ $criteria{'2'} }, 'items.itype';
87 unshift @
{ $criteria{'1'} }, 'biblioitems.itemtype';
88 unshift @
{ $criteria{'2'} }, 'biblioitems.itemtype';
93 C4::Reports - Module for generating reports
108 =item get_report_types()
110 This will return a list of all the available report types
114 sub get_report_types
{
115 my $dbh = C4
::Context
->dbh();
117 # FIXME these should be in the database perhaps
118 my @reports = ( 'Tabular', 'Summary', 'Matrix' );
120 for ( my $i = 0 ; $i < 3 ; $i++ ) {
122 $hashrep{id
} = $i + 1;
123 $hashrep{name
} = $reports[$i];
124 push @reports2, \
%hashrep;
126 return ( \
@reports2 );
130 =item get_report_areas()
132 This will return a list of all the available report areas
136 sub get_report_areas
{
137 my $dbh = C4
::Context
->dbh();
139 # FIXME these should be in the database
140 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
142 for ( my $i = 0 ; $i < 5 ; $i++ ) {
144 $hashrep{id
} = $i + 1;
145 $hashrep{name
} = $reports[$i];
146 push @reports2, \
%hashrep;
148 return ( \
@reports2 );
152 =item get_all_tables()
154 This will return a list of all tables in the database
159 my $dbh = C4
::Context
->dbh();
160 my $query = "SHOW TABLES";
161 my $sth = $dbh->prepare($query);
164 while ( my $data = $sth->fetchrow_arrayref() ) {
165 push @tables, $data->[0];
172 =item get_columns($area)
174 This will return a list of all columns for a report area
180 # this calls the internal fucntion _get_columns
181 my ($area,$cgi) = @_;
182 my $tables = $table_areas{$area};
185 foreach my $table (@
$tables) {
186 my @columns = _get_columns
($table,$cgi, $first);
188 push @allcolumns, @columns;
190 return ( \
@allcolumns );
194 my ($tablename,$cgi, $first) = @_;
195 my $dbh = C4
::Context
->dbh();
196 my $sth = $dbh->prepare("show columns from $tablename");
199 my $column_defs = _get_column_defs
($cgi);
201 $tablehash{'table'}=$tablename;
202 $tablehash{'__first__'} = $first;
203 push @columns, \
%tablehash;
204 while ( my $data = $sth->fetchrow_arrayref() ) {
206 $temphash{'name'} = "$tablename.$data->[0]";
207 $temphash{'description'} = $column_defs->{"$tablename.$data->[0]"};
208 push @columns, \
%temphash;
214 =item build_query($columns,$criteria,$orderby,$area)
216 This will build the sql needed to return the results asked for,
217 $columns is expected to be of the format tablename.columnname.
218 This is what get_columns returns.
223 my ( $columns, $criteria, $orderby, $area, $totals, $definition ) = @_;
225 my $keys = $keys{$area};
226 my $tables = $table_areas{$area};
229 _build_query
( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition );
234 my ( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition) = @_;
236 # $keys is an array of joining constraints
237 my $dbh = C4
::Context
->dbh();
238 my $joinedtables = join( ',', @
$tables );
239 my $joinedcolumns = join( ',', @
$columns );
240 my $joinedkeys = join( ' AND ', @
$keys );
242 "SELECT $totals $joinedcolumns FROM $tables->[0] ";
243 for (my $i=1;$i<@
$tables;$i++){
244 $query .= "LEFT JOIN $tables->[$i] on ($keys->[$i-1]) ";
248 $criteria =~ s/AND/WHERE/;
249 $query .= " $criteria";
252 my @definitions = split(',',$definition);
254 foreach my $def (@definitions){
255 my $defin=get_from_dictionary
('',$def);
256 $deftext .=" ".$defin->[0]->{'saved_sql'};
258 if ($query =~ /WHERE/i){
262 $deftext =~ s/AND/WHERE/;
268 my @totcolumns = split( ',', $totals );
269 foreach my $total (@totcolumns) {
270 if ( $total =~ /\((.*)\)/ ) {
271 if ( $groupby eq '' ) {
272 $groupby = " GROUP BY $1";
287 =item get_criteria($area,$cgi);
289 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
294 my ($area,$cgi) = @_;
295 my $dbh = C4
::Context
->dbh();
296 my $crit = $criteria{$area};
297 my $column_defs = _get_column_defs
($cgi);
299 foreach my $localcrit (@
$crit) {
300 my ( $value, $type ) = split( /\|/, $localcrit );
301 my ( $table, $column ) = split( /\./, $value );
302 if ( $type eq 'date' ) {
304 $temp{'name'} = $value;
306 $temp{'description'} = $column_defs->{$value};
307 push @criteria_array, \
%temp;
312 "SELECT distinct($column) as availablevalues FROM $table";
313 my $sth = $dbh->prepare($query);
316 while ( my $row = $sth->fetchrow_hashref() ) {
322 $temp{'name'} = $value;
323 $temp{'description'} = $column_defs->{$value};
324 $temp{'values'} = \
@values;
325 push @criteria_array, \
%temp;
328 return ( \
@criteria_array );
332 my ( $sql, $type, $format, $id ) = @_;
333 my $dbh = C4
::Context
->dbh();
335 # take this line out when in production
336 if ($format eq 'csv' or $format eq 'tab'){
341 my $sth = $dbh->prepare($sql);
343 my $colnames=$sth->{'NAME'};
347 $row = join ('</th><th>',@
$colnames);
348 $row = "<tr><th>$row</th></tr>";
349 $temphash{'row'} = $row;
350 push @results, \
%temphash;
353 while ( my @data = $sth->fetchrow_array() ) {
357 my $row = join( '</td><td>', @data );
358 $row = "<tr><td>$row</td></tr>";
359 $temphash{'row'} = $row;
360 if ( $format eq 'text' ) {
361 $string .= "\n" . $row;
363 if ($format eq 'tab' ){
364 $row = join("\t",@data);
365 $string .="\n" . $row;
367 if ($format eq 'csv' ){
368 $row = join(",",@data);
369 $string .="\n" . $row;
371 if ($format eq 'url'){
373 @
$temphash{@
$colnames}=@data;
374 push @xmlarray,$temphash;
376 push @results, \
%temphash;
380 if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
383 elsif ($format eq 'url') {
384 my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
385 my $dump = new XML
::Dumper
;
386 my $xml = $dump->pl2xml( \
@xmlarray );
387 store_results
($id,$xml);
391 return ( \
@results );
395 =item save_report($sql,$name,$type,$notes)
397 Given some sql and a name this will saved it so that it can resued
402 my ( $sql, $name, $type, $notes ) = @_;
403 my $dbh = C4
::Context
->dbh();
405 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes) VALUES (?,now(),now(),?,?,?,?)";
406 my $sth = $dbh->prepare($query);
407 $sth->execute( 0, $sql, $name, $type, $notes );
414 my $dbh = C4
::Context
->dbh();
415 my $query = "SELECT * FROM saved_reports WHERE report_id=?";
416 my $sth = $dbh->prepare($query);
418 if (my $data=$sth->fetchrow_hashref()){
419 my $query2 = "UPDATE saved_reports SET report=?,date_run=now() WHERE report_id=?";
420 my $sth2 = $dbh->prepare($query2);
421 $sth2->execute($xml,$id);
425 my $query2 = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
426 my $sth2 = $dbh->prepare($query2);
427 $sth2->execute($id,$xml);
435 my $dbh = C4
::Context
->dbh();
436 my $query = "SELECT * FROM saved_reports WHERE report_id = ?";
437 my $sth = $dbh->prepare($query);
439 my $data = $sth->fetchrow_hashref();
440 my $dump = new XML
::Dumper
;
441 my $perl = $dump->xml2pl( $data->{'report'} );
442 foreach my $row (@
$perl) {
444 foreach my $key (keys %$row){
445 $htmlrow .= "<td>$row->{$key}</td>";
448 $row->{'row'} = $htmlrow;
451 $query = "SELECT * FROM saved_sql WHERE id = ?";
452 $sth = $dbh->prepare($query);
454 $data = $sth->fetchrow_hashref();
456 return ($perl,$data->{'report_name'},$data->{'notes'});
461 my $dbh = C4
::Context
->dbh();
462 my $query = "DELETE FROM saved_sql WHERE id = ?";
463 my $sth = $dbh->prepare($query);
468 sub get_saved_reports
{
469 my $dbh = C4
::Context
->dbh();
470 my $query = "SELECT *,saved_sql.id AS id FROM saved_sql
471 LEFT JOIN saved_reports ON saved_reports.report_id = saved_sql.id
472 ORDER by date_created";
473 my $sth = $dbh->prepare($query);
476 while ( my $data = $sth->fetchrow_hashref() ) {
477 push @reports, $data;
480 return ( \
@reports );
483 sub get_saved_report
{
485 my $dbh = C4
::Context
->dbh();
486 my $query = " SELECT * FROM saved_sql WHERE id = ?";
487 my $sth = $dbh->prepare($query);
489 my $data = $sth->fetchrow_hashref();
491 return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
494 =item create_compound($masterID,$subreportID)
496 This will take 2 reports and create a compound report using both of them
500 sub create_compound
{
501 my ($masterID,$subreportID) = @_;
502 my $dbh = C4
::Context
->dbh();
504 my ($mastersql,$mastertype) = get_saved_report
($masterID);
505 my ($subsql,$subtype) = get_saved_report
($subreportID);
507 # now we have to do some checking to see how these two will fit together
509 my ($mastertables,$subtables);
510 if ($mastersql =~ / from (.*) where /i){
513 if ($subsql =~ / from (.*) where /i){
516 return ($mastertables,$subtables);
519 =item get_column_type($column)
521 This takes a column name of the format table.column and will return what type it is
522 (free text, set values, date)
526 sub get_column_type
{
527 my ($tablecolumn) = @_;
528 my ($table,$column) = split(/\./,$tablecolumn);
529 my $dbh = C4
::Context
->dbh();
533 # mysql doesnt support a column selection, set column to %
535 my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
536 while (my $info = $sth->fetchrow_hashref()){
537 if ($info->{'COLUMN_NAME'} eq $column){
539 if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
540 $info->{'TYPE_NAME'} = 'distinct';
542 return $info->{'TYPE_NAME'};
548 =item get_distinct_values($column)
550 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop
551 with the distinct values of the column
555 sub get_distinct_values
{
556 my ($tablecolumn) = @_;
557 my ($table,$column) = split(/\./,$tablecolumn);
558 my $dbh = C4
::Context
->dbh();
560 "SELECT distinct($column) as availablevalues FROM $table";
561 my $sth = $dbh->prepare($query);
564 while ( my $row = $sth->fetchrow_hashref() ) {
571 sub save_dictionary
{
572 my ($name,$description,$sql,$area) = @_;
573 my $dbh = C4
::Context
->dbh();
574 my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
575 VALUES (?,?,?,?,now(),now())";
576 my $sth = $dbh->prepare($query);
577 $sth->execute($name,$description,$sql,$area) || return 0;
582 sub get_from_dictionary
{
584 my $dbh = C4
::Context
->dbh();
585 my $query = "SELECT * FROM reports_dictionary";
587 $query.= " WHERE area = ?";
590 $query.= " WHERE id = ?"
592 my $sth = $dbh->prepare($query);
597 $sth->execute($area);
603 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
604 while (my $data = $sth->fetchrow_hashref()){
605 $data->{'areaname'}=$reports[$data->{'area'}-1];
613 sub delete_definition
{
615 my $dbh = C4
::Context
->dbh();
616 my $query = "DELETE FROM reports_dictionary WHERE id = ?";
617 my $sth = $dbh->prepare($query);
624 my $dbh = C4
::Context
->dbh();
625 my $query = "SELECT * FROM saved_sql WHERE id = ?";
626 my $sth = $dbh->prepare($query);
628 my $data=$sth->fetchrow_hashref();
630 return $data->{'savedsql'};
633 sub _get_column_defs
{
636 my $columns_def_file = "columns.def";
637 my $htdocs = C4
::Context
->config('intrahtdocs');
638 my $section='intranet';
639 my ($theme, $lang) = themelanguage
($htdocs, $columns_def_file, $section,$cgi);
641 my $full_path_to_columns_def_file="$htdocs/$theme/$lang/$columns_def_file";
642 open (COLUMNS
,$full_path_to_columns_def_file);
643 while (my $input = <COLUMNS
>){
644 my @row =split(/\t/,$input);
645 $columns{$row[0]}=$row[1];
656 Chris Cormack <crc@liblime.com>