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',
73 'statistics.branch', 'biblioitems.itemtype',
74 'biblioitems.publicationyear|date',
75 'items.dateaccessioned|date'
78 [ 'biblioitems.itemtype', 'items.holdingbranch', 'items.homebranch' ,'items.itemlost'];
79 $criteria{'3'} = ['borrowers.branchcode'];
80 $criteria{'4'} = ['aqorders.datereceived|date'];
81 $criteria{'5'} = ['borrowers.branchcode'];
86 C4::Reports - Module for generating reports
101 =item get_report_types()
103 This will return a list of all the available report types
107 sub get_report_types
{
108 my $dbh = C4
::Context
->dbh();
110 # FIXME these should be in the database perhaps
111 my @reports = ( 'Tabular', 'Summary', 'Matrix' );
113 for ( my $i = 0 ; $i < 3 ; $i++ ) {
115 $hashrep{id
} = $i + 1;
116 $hashrep{name
} = $reports[$i];
117 push @reports2, \
%hashrep;
119 return ( \
@reports2 );
123 =item get_report_areas()
125 This will return a list of all the available report areas
129 sub get_report_areas
{
130 my $dbh = C4
::Context
->dbh();
132 # FIXME these should be in the database
133 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
135 for ( my $i = 0 ; $i < 5 ; $i++ ) {
137 $hashrep{id
} = $i + 1;
138 $hashrep{name
} = $reports[$i];
139 push @reports2, \
%hashrep;
141 return ( \
@reports2 );
145 =item get_all_tables()
147 This will return a list of all tables in the database
152 my $dbh = C4
::Context
->dbh();
153 my $query = "SHOW TABLES";
154 my $sth = $dbh->prepare($query);
157 while ( my $data = $sth->fetchrow_arrayref() ) {
158 push @tables, $data->[0];
165 =item get_columns($area)
167 This will return a list of all columns for a report area
173 # this calls the internal fucntion _get_columns
174 my ($area,$cgi) = @_;
175 my $tables = $table_areas{$area};
178 foreach my $table (@
$tables) {
179 my @columns = _get_columns
($table,$cgi, $first);
181 push @allcolumns, @columns;
183 return ( \
@allcolumns );
187 my ($tablename,$cgi, $first) = @_;
188 my $dbh = C4
::Context
->dbh();
189 my $sth = $dbh->prepare("show columns from $tablename");
192 my $column_defs = _get_column_defs
($cgi);
194 $tablehash{'table'}=$tablename;
195 $tablehash{'__first__'} = $first;
196 push @columns, \
%tablehash;
197 while ( my $data = $sth->fetchrow_arrayref() ) {
199 $temphash{'name'} = "$tablename.$data->[0]";
200 $temphash{'description'} = $column_defs->{"$tablename.$data->[0]"};
201 push @columns, \
%temphash;
207 =item build_query($columns,$criteria,$orderby,$area)
209 This will build the sql needed to return the results asked for,
210 $columns is expected to be of the format tablename.columnname.
211 This is what get_columns returns.
216 my ( $columns, $criteria, $orderby, $area, $totals, $definition ) = @_;
218 my $keys = $keys{$area};
219 my $tables = $table_areas{$area};
222 _build_query
( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition );
227 my ( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition) = @_;
229 # $keys is an array of joining constraints
230 my $dbh = C4
::Context
->dbh();
231 my $joinedtables = join( ',', @
$tables );
232 my $joinedcolumns = join( ',', @
$columns );
233 my $joinedkeys = join( ' AND ', @
$keys );
235 "SELECT $totals $joinedcolumns FROM $tables->[0] ";
236 for (my $i=1;$i<@
$tables;$i++){
237 $query .= "LEFT JOIN $tables->[$i] on ($keys->[$i-1]) ";
241 $criteria =~ s/AND/WHERE/;
242 $query .= " $criteria";
245 my @definitions = split(',',$definition);
247 foreach my $def (@definitions){
248 my $defin=get_from_dictionary
('',$def);
249 $deftext .=" ".$defin->[0]->{'saved_sql'};
251 if ($query =~ /WHERE/i){
255 $deftext =~ s/AND/WHERE/;
261 my @totcolumns = split( ',', $totals );
262 foreach my $total (@totcolumns) {
263 if ( $total =~ /\((.*)\)/ ) {
264 if ( $groupby eq '' ) {
265 $groupby = " GROUP BY $1";
280 =item get_criteria($area,$cgi);
282 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
287 my ($area,$cgi) = @_;
288 my $dbh = C4
::Context
->dbh();
289 my $crit = $criteria{$area};
290 my $column_defs = _get_column_defs
($cgi);
292 foreach my $localcrit (@
$crit) {
293 my ( $value, $type ) = split( /\|/, $localcrit );
294 my ( $table, $column ) = split( /\./, $value );
295 if ( $type eq 'date' ) {
297 $temp{'name'} = $value;
299 $temp{'description'} = $column_defs->{$value};
300 push @criteria_array, \
%temp;
305 "SELECT distinct($column) as availablevalues FROM $table";
306 my $sth = $dbh->prepare($query);
309 while ( my $row = $sth->fetchrow_hashref() ) {
315 $temp{'name'} = $value;
316 $temp{'description'} = $column_defs->{$value};
317 $temp{'values'} = \
@values;
318 push @criteria_array, \
%temp;
321 return ( \
@criteria_array );
325 my ( $sql, $type, $format, $id ) = @_;
326 my $dbh = C4
::Context
->dbh();
328 # take this line out when in production
329 if ($format eq 'csv' or $format eq 'tab'){
334 my $sth = $dbh->prepare($sql);
336 my $colnames=$sth->{'NAME'};
340 $row = join ('</th><th>',@
$colnames);
341 $row = "<tr><th>$row</th></tr>";
342 $temphash{'row'} = $row;
343 push @results, \
%temphash;
346 while ( my @data = $sth->fetchrow_array() ) {
350 my $row = join( '</td><td>', @data );
351 $row = "<tr><td>$row</td></tr>";
352 $temphash{'row'} = $row;
353 if ( $format eq 'text' ) {
354 $string .= "\n" . $row;
356 if ($format eq 'tab' ){
357 $row = join("\t",@data);
358 $string .="\n" . $row;
360 if ($format eq 'csv' ){
361 $row = join(",",@data);
362 $string .="\n" . $row;
364 if ($format eq 'url'){
366 @
$temphash{@
$colnames}=@data;
367 push @xmlarray,$temphash;
369 push @results, \
%temphash;
373 if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
376 elsif ($format eq 'url') {
377 my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
378 my $dump = new XML
::Dumper
;
379 my $xml = $dump->pl2xml( \
@xmlarray );
380 store_results
($id,$xml);
384 return ( \
@results );
388 =item save_report($sql,$name,$type,$notes)
390 Given some sql and a name this will saved it so that it can resued
395 my ( $sql, $name, $type, $notes ) = @_;
396 my $dbh = C4
::Context
->dbh();
398 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes) VALUES (?,now(),now(),?,?,?,?)";
399 my $sth = $dbh->prepare($query);
400 $sth->execute( 0, $sql, $name, $type, $notes );
407 my $dbh = C4
::Context
->dbh();
408 my $query = "SELECT * FROM saved_reports WHERE report_id=?";
409 my $sth = $dbh->prepare($query);
411 if (my $data=$sth->fetchrow_hashref()){
412 my $query2 = "UPDATE saved_reports SET report=?,date_run=now() WHERE report_id=?";
413 my $sth2 = $dbh->prepare($query2);
414 $sth2->execute($xml,$id);
418 my $query2 = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
419 my $sth2 = $dbh->prepare($query2);
420 $sth2->execute($id,$xml);
428 my $dbh = C4
::Context
->dbh();
429 my $query = "SELECT * FROM saved_reports WHERE report_id = ?";
430 my $sth = $dbh->prepare($query);
432 my $data = $sth->fetchrow_hashref();
433 my $dump = new XML
::Dumper
;
434 my $perl = $dump->xml2pl( $data->{'report'} );
435 foreach my $row (@
$perl) {
437 foreach my $key (keys %$row){
438 $htmlrow .= "<td>$row->{$key}</td>";
441 $row->{'row'} = $htmlrow;
444 $query = "SELECT * FROM saved_sql WHERE id = ?";
445 $sth = $dbh->prepare($query);
447 $data = $sth->fetchrow_hashref();
449 return ($perl,$data->{'report_name'},$data->{'notes'});
454 my $dbh = C4
::Context
->dbh();
455 my $query = "DELETE FROM saved_sql WHERE id = ?";
456 my $sth = $dbh->prepare($query);
461 sub get_saved_reports
{
462 my $dbh = C4
::Context
->dbh();
463 my $query = "SELECT *,saved_sql.id AS id FROM saved_sql
464 LEFT JOIN saved_reports ON saved_reports.report_id = saved_sql.id
465 ORDER by date_created";
466 my $sth = $dbh->prepare($query);
469 while ( my $data = $sth->fetchrow_hashref() ) {
470 push @reports, $data;
473 return ( \
@reports );
476 sub get_saved_report
{
478 my $dbh = C4
::Context
->dbh();
479 my $query = " SELECT * FROM saved_sql WHERE id = ?";
480 my $sth = $dbh->prepare($query);
482 my $data = $sth->fetchrow_hashref();
484 return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
487 =item create_compound($masterID,$subreportID)
489 This will take 2 reports and create a compound report using both of them
493 sub create_compound
{
494 my ($masterID,$subreportID) = @_;
495 my $dbh = C4
::Context
->dbh();
497 my ($mastersql,$mastertype) = get_saved_report
($masterID);
498 my ($subsql,$subtype) = get_saved_report
($subreportID);
500 # now we have to do some checking to see how these two will fit together
502 my ($mastertables,$subtables);
503 if ($mastersql =~ / from (.*) where /i){
506 if ($subsql =~ / from (.*) where /i){
509 return ($mastertables,$subtables);
512 =item get_column_type($column)
514 This takes a column name of the format table.column and will return what type it is
515 (free text, set values, date)
519 sub get_column_type
{
520 my ($tablecolumn) = @_;
521 my ($table,$column) = split(/\./,$tablecolumn);
522 my $dbh = C4
::Context
->dbh();
526 # mysql doesnt support a column selection, set column to %
528 my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
529 while (my $info = $sth->fetchrow_hashref()){
530 if ($info->{'COLUMN_NAME'} eq $column){
532 if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
533 $info->{'TYPE_NAME'} = 'distinct';
535 return $info->{'TYPE_NAME'};
541 =item get_distinct_values($column)
543 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop
544 with the distinct values of the column
548 sub get_distinct_values
{
549 my ($tablecolumn) = @_;
550 my ($table,$column) = split(/\./,$tablecolumn);
551 my $dbh = C4
::Context
->dbh();
553 "SELECT distinct($column) as availablevalues FROM $table";
554 my $sth = $dbh->prepare($query);
557 while ( my $row = $sth->fetchrow_hashref() ) {
564 sub save_dictionary
{
565 my ($name,$description,$sql,$area) = @_;
566 my $dbh = C4
::Context
->dbh();
567 my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
568 VALUES (?,?,?,?,now(),now())";
569 my $sth = $dbh->prepare($query);
570 $sth->execute($name,$description,$sql,$area) || return 0;
575 sub get_from_dictionary
{
577 my $dbh = C4
::Context
->dbh();
578 my $query = "SELECT * FROM reports_dictionary";
580 $query.= " WHERE area = ?";
583 $query.= " WHERE id = ?"
585 my $sth = $dbh->prepare($query);
590 $sth->execute($area);
596 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
597 while (my $data = $sth->fetchrow_hashref()){
598 $data->{'areaname'}=$reports[$data->{'area'}-1];
606 sub delete_definition
{
608 my $dbh = C4
::Context
->dbh();
609 my $query = "DELETE FROM reports_dictionary WHERE id = ?";
610 my $sth = $dbh->prepare($query);
617 my $dbh = C4
::Context
->dbh();
618 my $query = "SELECT * FROM saved_sql WHERE id = ?";
619 my $sth = $dbh->prepare($query);
621 my $data=$sth->fetchrow_hashref();
623 return $data->{'savedsql'};
626 sub _get_column_defs
{
629 my $columns_def_file = "columns.def";
630 my $htdocs = C4
::Context
->config('intrahtdocs');
631 my $section='intranet';
632 my ($theme, $lang) = themelanguage
($htdocs, $columns_def_file, $section,$cgi);
634 my $full_path_to_columns_def_file="$htdocs/$theme/$lang/$columns_def_file";
635 open (COLUMNS
,$full_path_to_columns_def_file);
636 while (my $input = <COLUMNS
>){
637 my @row =split(/\t/,$input);
638 $columns{$row[0]}=$row[1];
649 Chris Cormack <crc@liblime.com>