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'];
84 my $columns_def_file = "columns.def";
85 my $htdocs = C4::Context->config('intrahtdocs');
86 my $section='intranet';
88 my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section,$cgi);
90 my $full_path_to_columns_def_file="$htdocs/$theme/$lang/$columns_def_file";
91 open (COLUMNS,$full_path_to_columns_def_file);
92 while (my $input = <COLUMNS>){
93 my @row =split(/\t/,$input);
94 $columns{$row[0]}=$row[1];
101 C4::Reports - Module for generating reports
116 =item get_report_types()
118 This will return a list of all the available report types
122 sub get_report_types {
123 my $dbh = C4::Context->dbh();
125 # FIXME these should be in the database perhaps
126 my @reports = ( 'Tabular', 'Summary', 'Matrix' );
128 for ( my $i = 0 ; $i < 3 ; $i++ ) {
130 $hashrep{id} = $i + 1;
131 $hashrep{name} = $reports[$i];
132 push @reports2, \%hashrep;
134 return ( \@reports2 );
138 =item get_report_areas()
140 This will return a list of all the available report areas
144 sub get_report_areas {
145 my $dbh = C4::Context->dbh();
147 # FIXME these should be in the database
148 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
150 for ( my $i = 0 ; $i < 5 ; $i++ ) {
152 $hashrep{id} = $i + 1;
153 $hashrep{name} = $reports[$i];
154 push @reports2, \%hashrep;
156 return ( \@reports2 );
160 =item get_all_tables()
162 This will return a list of all tables in the database
167 my $dbh = C4::Context->dbh();
168 my $query = "SHOW TABLES";
169 my $sth = $dbh->prepare($query);
172 while ( my $data = $sth->fetchrow_arrayref() ) {
173 push @tables, $data->[0];
180 =item get_columns($area)
182 This will return a list of all columns for a report area
188 # this calls the internal fucntion _get_columns
190 my $tables = $table_areas{$area};
192 foreach my $table (@$tables) {
193 my @columns = _get_columns($table);
194 push @allcolumns, @columns;
196 return ( \@allcolumns );
200 my ($tablename) = @_;
201 my $dbh = C4::Context->dbh();
202 my $sth = $dbh->prepare("show columns from $tablename");
206 $tablehash{'table'}=$tablename;
207 push @columns, \%tablehash;
208 while ( my $data = $sth->fetchrow_arrayref() ) {
210 $temphash{'name'} = "$tablename.$data->[0]";
211 $temphash{'description'} = $columns{"$tablename.$data->[0]"};
212 push @columns, \%temphash;
218 =item build_query($columns,$criteria,$orderby,$area)
220 This will build the sql needed to return the results asked for,
221 $columns is expected to be of the format tablename.columnname.
222 This is what get_columns returns.
227 my ( $columns, $criteria, $orderby, $area, $totals, $definition ) = @_;
229 my $keys = $keys{$area};
230 my $tables = $table_areas{$area};
233 _build_query( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition );
238 my ( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition) = @_;
240 # $keys is an array of joining constraints
241 my $dbh = C4::Context->dbh();
242 my $joinedtables = join( ',', @$tables );
243 my $joinedcolumns = join( ',', @$columns );
244 my $joinedkeys = join( ' AND ', @$keys );
246 "SELECT $totals $joinedcolumns FROM $tables->[0] ";
247 for (my $i=1;$i<@$tables;$i++){
248 $query .= "LEFT JOIN $tables->[$i] on ($keys->[$i-1]) ";
252 $criteria =~ s/AND/WHERE/;
253 $query .= " $criteria";
256 my @definitions = split(',',$definition);
258 foreach my $def (@definitions){
259 my $defin=get_from_dictionary('',$def);
260 $deftext .=" ".$defin->[0]->{'saved_sql'};
262 if ($query =~ /WHERE/i){
266 $deftext =~ s/AND/WHERE/;
272 my @totcolumns = split( ',', $totals );
273 foreach my $total (@totcolumns) {
274 if ( $total =~ /\((.*)\)/ ) {
275 if ( $groupby eq '' ) {
276 $groupby = " GROUP BY $1";
291 =item get_criteria($area);
293 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
299 my $dbh = C4::Context->dbh();
300 my $crit = $criteria{$area};
302 foreach my $localcrit (@$crit) {
303 my ( $value, $type ) = split( /\|/, $localcrit );
304 my ( $table, $column ) = split( /\./, $value );
305 if ( $type eq 'date' ) {
307 $temp{'name'} = $value;
309 $temp{'description'} = $columns{$value};
310 push @criteria_array, \%temp;
315 "SELECT distinct($column) as availablevalues FROM $table";
316 my $sth = $dbh->prepare($query);
319 while ( my $row = $sth->fetchrow_hashref() ) {
325 $temp{'name'} = $value;
326 $temp{'description'} = $columns{$value};
327 $temp{'values'} = \@values;
328 push @criteria_array, \%temp;
331 return ( \@criteria_array );
335 my ( $sql, $type, $format, $id ) = @_;
336 my $dbh = C4::Context->dbh();
338 # take this line out when in production
339 if ($format eq 'url'){
344 my $sth = $dbh->prepare($sql);
346 my $colnames=$sth->{'NAME'};
350 $row = join ('</th><th>',@$colnames);
351 $row = "<tr><th>$row</th></tr>";
352 $temphash{'row'} = $row;
353 push @results, \%temphash;
356 while ( my @data = $sth->fetchrow_array() ) {
360 my $row = join( '</td><td>', @data );
361 $row = "<tr><td>$row</td></tr>";
362 $temphash{'row'} = $row;
363 if ( $format eq 'text' ) {
364 $string .= "\n" . $row;
366 if ($format eq 'tab' ){
367 $row = join("\t",@data);
368 $string .="\n" . $row;
370 if ($format eq 'csv' ){
371 $row = join(",",@data);
372 $string .="\n" . $row;
374 if ($format eq 'url'){
376 @$temphash{@$colnames}=@data;
377 push @xmlarray,$temphash;
379 push @results, \%temphash;
383 if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
386 elsif ($format eq 'url') {
387 my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
388 my $dump = new XML::Dumper;
389 my $xml = $dump->pl2xml( \@xmlarray );
390 store_results($id,$xml);
394 return ( \@results );
398 =item save_report($sql,$name,$type,$notes)
400 Given some sql and a name this will saved it so that it can resued
405 my ( $sql, $name, $type, $notes ) = @_;
406 my $dbh = C4::Context->dbh();
408 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes) VALUES (?,now(),now(),?,?,?,?)";
409 my $sth = $dbh->prepare($query);
410 $sth->execute( 0, $sql, $name, $type, $notes );
417 my $dbh = C4::Context->dbh();
418 my $query = "SELECT * FROM saved_reports WHERE report_id=?";
419 my $sth = $dbh->prepare($query);
421 if (my $data=$sth->fetchrow_hashref()){
422 my $query2 = "UPDATE saved_reports SET report=?,date_run=now() WHERE report_id=?";
423 my $sth2 = $dbh->prepare($query2);
424 $sth2->execute($xml,$id);
428 my $query2 = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
429 my $sth2 = $dbh->prepare($query2);
430 $sth2->execute($id,$xml);
438 my $dbh = C4::Context->dbh();
439 my $query = "SELECT * FROM saved_reports WHERE report_id = ?";
440 my $sth = $dbh->prepare($query);
442 my $data = $sth->fetchrow_hashref();
443 my $dump = new XML::Dumper;
444 my $perl = $dump->xml2pl( $data->{'report'} );
445 foreach my $row (@$perl) {
447 foreach my $key (keys %$row){
448 $htmlrow .= "<td>$row->{$key}</td>";
451 $row->{'row'} = $htmlrow;
454 $query = "SELECT * FROM saved_sql WHERE id = ?";
455 $sth = $dbh->prepare($query);
457 $data = $sth->fetchrow_hashref();
459 return ($perl,$data->{'report_name'},$data->{'notes'});
464 my $dbh = C4::Context->dbh();
465 my $query = "DELETE FROM saved_sql WHERE id = ?";
466 my $sth = $dbh->prepare($query);
471 sub get_saved_reports {
472 my $dbh = C4::Context->dbh();
473 my $query = "SELECT *,saved_sql.id AS id FROM saved_sql
474 LEFT JOIN saved_reports ON saved_reports.report_id = saved_sql.id
475 ORDER by date_created";
476 my $sth = $dbh->prepare($query);
479 while ( my $data = $sth->fetchrow_hashref() ) {
480 push @reports, $data;
483 return ( \@reports );
486 sub get_saved_report {
488 my $dbh = C4::Context->dbh();
489 my $query = " SELECT * FROM saved_sql WHERE id = ?";
490 my $sth = $dbh->prepare($query);
492 my $data = $sth->fetchrow_hashref();
494 return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
497 =item create_compound($masterID,$subreportID)
499 This will take 2 reports and create a compound report using both of them
503 sub create_compound {
504 my ($masterID,$subreportID) = @_;
505 my $dbh = C4::Context->dbh();
507 my ($mastersql,$mastertype) = get_saved_report($masterID);
508 my ($subsql,$subtype) = get_saved_report($subreportID);
510 # now we have to do some checking to see how these two will fit together
512 my ($mastertables,$subtables);
513 if ($mastersql =~ / from (.*) where /i){
516 if ($subsql =~ / from (.*) where /i){
519 return ($mastertables,$subtables);
522 =item get_column_type($column)
524 This takes a column name of the format table.column and will return what type it is
525 (free text, set values, date)
529 sub get_column_type {
530 my ($tablecolumn) = @_;
531 my ($table,$column) = split(/\./,$tablecolumn);
532 my $dbh = C4::Context->dbh();
536 # mysql doesnt support a column selection, set column to %
538 my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
539 while (my $info = $sth->fetchrow_hashref()){
540 if ($info->{'COLUMN_NAME'} eq $column){
542 if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
543 $info->{'TYPE_NAME'} = 'distinct';
545 return $info->{'TYPE_NAME'};
551 =item get_distinct_values($column)
553 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop
554 with the distinct values of the column
558 sub get_distinct_values {
559 my ($tablecolumn) = @_;
560 my ($table,$column) = split(/\./,$tablecolumn);
561 my $dbh = C4::Context->dbh();
563 "SELECT distinct($column) as availablevalues FROM $table";
564 my $sth = $dbh->prepare($query);
567 while ( my $row = $sth->fetchrow_hashref() ) {
574 sub save_dictionary {
575 my ($name,$description,$sql,$area) = @_;
576 my $dbh = C4::Context->dbh();
577 my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
578 VALUES (?,?,?,?,now(),now())";
579 my $sth = $dbh->prepare($query);
580 $sth->execute($name,$description,$sql,$area) || return 0;
585 sub get_from_dictionary {
587 my $dbh = C4::Context->dbh();
588 my $query = "SELECT * FROM reports_dictionary";
590 $query.= " WHERE area = ?";
593 $query.= " WHERE id = ?"
595 my $sth = $dbh->prepare($query);
600 $sth->execute($area);
606 while (my $data = $sth->fetchrow_hashref()){
614 sub delete_definition {
616 my $dbh = C4::Context->dbh();
617 my $query = "DELETE FROM reports_dictionary WHERE id = ?";
618 my $sth = $dbh->prepare($query);
625 my $dbh = C4::Context->dbh();
626 my $query = "SELECT * FROM saved_sql WHERE id = ?";
627 my $sth = $dbh->prepare($query);
629 my $data=$sth->fetchrow_hashref();
631 return $data->{'savedsql'};
639 Chris Cormack <crc@liblime.com>