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
22 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 # use Smart::Comments;
31 # set the version for version checking
36 get_report_types get_report_areas get_columns build_query get_criteria
37 save_report get_saved_reports execute_query get_saved_report create_compound run_compound
38 get_column_type get_distinct_values save_dictionary get_from_dictionary
39 delete_definition delete_report format_results get_sql
45 [ 'borrowers', 'statistics','items', 'biblioitems' ]; # circulation
46 $table_areas{'2'} = [ 'items', 'biblioitems', 'biblio' ]; # catalogue
47 $table_areas{'3'} = [ 'borrowers' ]; # patrons
48 $table_areas{'4'} = ['aqorders', 'biblio', 'items']; # acquisitions
49 $table_areas{'5'} = [ 'borrowers', 'accountlines' ]; # accounts
52 'statistics.borrowernumber=borrowers.borrowernumber',
53 'items.itemnumber = statistics.itemnumber',
54 'biblioitems.biblioitemnumber = items.biblioitemnumber'
57 'items.biblioitemnumber=biblioitems.biblioitemnumber',
58 'biblioitems.biblionumber=biblio.biblionumber'
62 'aqorders.biblionumber=biblio.biblionumber',
63 'biblio.biblionumber=items.biblionumber'
65 $keys{'5'} = ['borrowers.borrowernumber=accountlines.borrowernumber'];
67 # have to do someting here to know if its dropdown, free text, date etc
71 'statistics.type', 'borrowers.categorycode',
72 'statistics.branch', 'biblioitems.itemtype',
73 'biblioitems.publicationyear|date',
74 'items.dateaccessioned|date'
77 [ 'biblioitems.itemtype', 'items.holdingbranch', 'items.homebranch' ,'items.itemlost'];
78 $criteria{'3'} = ['borrowers.branchcode'];
79 $criteria{'4'} = ['aqorders.datereceived|date'];
80 $criteria{'5'} = ['borrowers.branchcode'];
83 my $columns_def_file = "columns.def";
84 my $htdocs = C4::Context->config('intrahtdocs');
85 my $section='intranet';
86 my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section);
88 my $full_path_to_columns_def_file="$htdocs/$theme/$lang/$columns_def_file";
89 open (COLUMNS,$full_path_to_columns_def_file);
90 while (my $input = <COLUMNS>){
91 my @row =split(/\t/,$input);
92 $columns{$row[0]}=$row[1];
99 C4::Reports - Module for generating reports
114 =item get_report_types()
116 This will return a list of all the available report types
120 sub get_report_types {
121 my $dbh = C4::Context->dbh();
123 # FIXME these should be in the database perhaps
124 my @reports = ( 'Tabular', 'Summary', 'Matrix' );
126 for ( my $i = 0 ; $i < 3 ; $i++ ) {
128 $hashrep{id} = $i + 1;
129 $hashrep{name} = $reports[$i];
130 push @reports2, \%hashrep;
132 return ( \@reports2 );
136 =item get_report_areas()
138 This will return a list of all the available report areas
142 sub get_report_areas {
143 my $dbh = C4::Context->dbh();
145 # FIXME these should be in the database
146 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
148 for ( my $i = 0 ; $i < 5 ; $i++ ) {
150 $hashrep{id} = $i + 1;
151 $hashrep{name} = $reports[$i];
152 push @reports2, \%hashrep;
154 return ( \@reports2 );
158 =item get_all_tables()
160 This will return a list of all tables in the database
165 my $dbh = C4::Context->dbh();
166 my $query = "SHOW TABLES";
167 my $sth = $dbh->prepare($query);
170 while ( my $data = $sth->fetchrow_arrayref() ) {
171 push @tables, $data->[0];
178 =item get_columns($area)
180 This will return a list of all columns for a report area
186 # this calls the internal fucntion _get_columns
188 my $tables = $table_areas{$area};
190 foreach my $table (@$tables) {
191 my @columns = _get_columns($table);
192 push @allcolumns, @columns;
194 return ( \@allcolumns );
198 my ($tablename) = @_;
199 my $dbh = C4::Context->dbh();
200 my $sth = $dbh->prepare("show columns from $tablename");
204 $tablehash{'table'}=$tablename;
205 push @columns, \%tablehash;
206 while ( my $data = $sth->fetchrow_arrayref() ) {
208 $temphash{'name'} = "$tablename.$data->[0]";
209 $temphash{'description'} = $columns{"$tablename.$data->[0]"};
210 push @columns, \%temphash;
216 =item build_query($columns,$criteria,$orderby,$area)
218 This will build the sql needed to return the results asked for,
219 $columns is expected to be of the format tablename.columnname.
220 This is what get_columns returns.
225 my ( $columns, $criteria, $orderby, $area, $totals, $definition ) = @_;
227 my $keys = $keys{$area};
228 my $tables = $table_areas{$area};
231 _build_query( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition );
236 my ( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition) = @_;
238 # $keys is an array of joining constraints
239 my $dbh = C4::Context->dbh();
240 my $joinedtables = join( ',', @$tables );
241 my $joinedcolumns = join( ',', @$columns );
242 my $joinedkeys = join( ' AND ', @$keys );
244 "SELECT $totals $joinedcolumns FROM $tables->[0] ";
245 for (my $i=1;$i<@$tables;$i++){
246 $query .= "LEFT JOIN $tables->[$i] on ($keys->[$i-1]) ";
250 $criteria =~ s/AND/WHERE/;
251 $query .= " $criteria";
254 my @definitions = split(',',$definition);
256 foreach my $def (@definitions){
257 my $defin=get_from_dictionary('',$def);
258 $deftext .=" ".$defin->[0]->{'saved_sql'};
260 if ($query =~ /WHERE/i){
264 $deftext =~ s/AND/WHERE/;
270 my @totcolumns = split( ',', $totals );
271 foreach my $total (@totcolumns) {
272 if ( $total =~ /\((.*)\)/ ) {
273 if ( $groupby eq '' ) {
274 $groupby = " GROUP BY $1";
289 =item get_criteria($area);
291 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
297 my $dbh = C4::Context->dbh();
298 my $crit = $criteria{$area};
300 foreach my $localcrit (@$crit) {
301 my ( $value, $type ) = split( /\|/, $localcrit );
302 my ( $table, $column ) = split( /\./, $value );
303 if ( $type eq 'date' ) {
305 $temp{'name'} = $value;
307 $temp{'description'} = $columns{$value};
308 push @criteria_array, \%temp;
313 "SELECT distinct($column) as availablevalues FROM $table";
314 my $sth = $dbh->prepare($query);
317 while ( my $row = $sth->fetchrow_hashref() ) {
323 $temp{'name'} = $value;
324 $temp{'description'} = $columns{$value};
325 $temp{'values'} = \@values;
326 push @criteria_array, \%temp;
329 return ( \@criteria_array );
333 my ( $sql, $type, $format, $id ) = @_;
334 my $dbh = C4::Context->dbh();
336 # take this line out when in production
337 if ($format eq 'url'){
342 my $sth = $dbh->prepare($sql);
344 my $colnames=$sth->{'NAME'};
348 $row = join ('</th><th>',@$colnames);
349 $row = "<tr><th>$row</th></tr>";
350 $temphash{'row'} = $row;
351 push @results, \%temphash;
354 while ( my @data = $sth->fetchrow_array() ) {
358 my $row = join( '</td><td>', @data );
359 $row = "<tr><td>$row</td></tr>";
360 $temphash{'row'} = $row;
361 if ( $format eq 'text' ) {
362 $string .= "\n" . $row;
364 if ($format eq 'tab' ){
365 $row = join("\t",@data);
366 $string .="\n" . $row;
368 if ($format eq 'csv' ){
369 $row = join(",",@data);
370 $string .="\n" . $row;
372 if ($format eq 'url'){
374 @$temphash{@$colnames}=@data;
375 push @xmlarray,$temphash;
377 push @results, \%temphash;
381 if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
384 elsif ($format eq 'url') {
385 my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
386 my $dump = new XML::Dumper;
387 my $xml = $dump->pl2xml( \@xmlarray );
388 store_results($id,$xml);
392 return ( \@results );
396 =item save_report($sql,$name,$type,$notes)
398 Given some sql and a name this will saved it so that it can resued
403 my ( $sql, $name, $type, $notes ) = @_;
404 my $dbh = C4::Context->dbh();
406 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes) VALUES (?,now(),now(),?,?,?,?)";
407 my $sth = $dbh->prepare($query);
408 $sth->execute( 0, $sql, $name, $type, $notes );
415 my $dbh = C4::Context->dbh();
416 my $query = "SELECT * FROM saved_reports WHERE report_id=?";
417 my $sth = $dbh->prepare($query);
419 if (my $data=$sth->fetchrow_hashref()){
420 my $query2 = "UPDATE saved_reports SET report=?,date_run=now() WHERE report_id=?";
421 my $sth2 = $dbh->prepare($query2);
422 $sth2->execute($xml,$id);
426 my $query2 = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
427 my $sth2 = $dbh->prepare($query2);
428 $sth2->execute($id,$xml);
436 my $dbh = C4::Context->dbh();
437 my $query = "SELECT * FROM saved_reports WHERE report_id = ?";
438 my $sth = $dbh->prepare($query);
440 my $data = $sth->fetchrow_hashref();
441 my $dump = new XML::Dumper;
442 my $perl = $dump->xml2pl( $data->{'report'} );
443 foreach my $row (@$perl) {
445 foreach my $key (keys %$row){
446 $htmlrow .= "<td>$row->{$key}</td>";
449 $row->{'row'} = $htmlrow;
452 $query = "SELECT * FROM saved_sql WHERE id = ?";
453 $sth = $dbh->prepare($query);
455 $data = $sth->fetchrow_hashref();
457 return ($perl,$data->{'report_name'},$data->{'notes'});
462 my $dbh = C4::Context->dbh();
463 my $query = "DELETE FROM saved_sql WHERE id = ?";
464 my $sth = $dbh->prepare($query);
469 sub get_saved_reports {
470 my $dbh = C4::Context->dbh();
471 my $query = "SELECT *,saved_sql.id AS id FROM saved_sql
472 LEFT JOIN saved_reports ON saved_reports.report_id = saved_sql.id
473 ORDER by date_created";
474 my $sth = $dbh->prepare($query);
477 while ( my $data = $sth->fetchrow_hashref() ) {
478 push @reports, $data;
481 return ( \@reports );
484 sub get_saved_report {
486 my $dbh = C4::Context->dbh();
487 my $query = " SELECT * FROM saved_sql WHERE id = ?";
488 my $sth = $dbh->prepare($query);
490 my $data = $sth->fetchrow_hashref();
492 return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
495 =item create_compound($masterID,$subreportID)
497 This will take 2 reports and create a compound report using both of them
501 sub create_compound {
502 my ($masterID,$subreportID) = @_;
503 my $dbh = C4::Context->dbh();
505 my ($mastersql,$mastertype) = get_saved_report($masterID);
506 my ($subsql,$subtype) = get_saved_report($subreportID);
508 # now we have to do some checking to see how these two will fit together
510 my ($mastertables,$subtables);
511 if ($mastersql =~ / from (.*) where /i){
514 if ($subsql =~ / from (.*) where /i){
517 return ($mastertables,$subtables);
520 =item get_column_type($column)
522 This takes a column name of the format table.column and will return what type it is
523 (free text, set values, date)
527 sub get_column_type {
528 my ($tablecolumn) = @_;
529 my ($table,$column) = split(/\./,$tablecolumn);
530 my $dbh = C4::Context->dbh();
534 # mysql doesnt support a column selection, set column to %
536 my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
537 while (my $info = $sth->fetchrow_hashref()){
538 if ($info->{'COLUMN_NAME'} eq $column){
540 if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
541 $info->{'TYPE_NAME'} = 'distinct';
543 return $info->{'TYPE_NAME'};
549 =item get_distinct_values($column)
551 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop
552 with the distinct values of the column
556 sub get_distinct_values {
557 my ($tablecolumn) = @_;
558 my ($table,$column) = split(/\./,$tablecolumn);
559 my $dbh = C4::Context->dbh();
561 "SELECT distinct($column) as availablevalues FROM $table";
562 my $sth = $dbh->prepare($query);
565 while ( my $row = $sth->fetchrow_hashref() ) {
572 sub save_dictionary {
573 my ($name,$description,$sql,$area) = @_;
574 my $dbh = C4::Context->dbh();
575 my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
576 VALUES (?,?,?,?,now(),now())";
577 my $sth = $dbh->prepare($query);
578 $sth->execute($name,$description,$sql,$area) || return 0;
583 sub get_from_dictionary {
585 my $dbh = C4::Context->dbh();
586 my $query = "SELECT * FROM reports_dictionary";
588 $query.= " WHERE area = ?";
591 $query.= " WHERE id = ?"
593 my $sth = $dbh->prepare($query);
598 $sth->execute($area);
604 while (my $data = $sth->fetchrow_hashref()){
612 sub delete_definition {
614 my $dbh = C4::Context->dbh();
615 my $query = "DELETE FROM reports_dictionary WHERE id = ?";
616 my $sth = $dbh->prepare($query);
623 my $dbh = C4::Context->dbh();
624 my $query = "SELECT * FROM saved_sql WHERE id = ?";
625 my $sth = $dbh->prepare($query);
627 my $data=$sth->fetchrow_hashref();
629 return $data->{'savedsql'};
637 Chris Cormack <crc@liblime.com>