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};
177 foreach my $table (@$tables) {
178 my @columns = _get_columns($table,$cgi);
179 push @allcolumns, @columns;
181 return ( \@allcolumns );
185 my ($tablename,$cgi) = @_;
186 my $dbh = C4::Context->dbh();
187 my $sth = $dbh->prepare("show columns from $tablename");
190 my $column_defs = _get_column_defs($cgi);
192 $tablehash{'table'}=$tablename;
193 push @columns, \%tablehash;
194 while ( my $data = $sth->fetchrow_arrayref() ) {
196 $temphash{'name'} = "$tablename.$data->[0]";
197 $temphash{'description'} = $column_defs->{"$tablename.$data->[0]"};
198 push @columns, \%temphash;
204 =item build_query($columns,$criteria,$orderby,$area)
206 This will build the sql needed to return the results asked for,
207 $columns is expected to be of the format tablename.columnname.
208 This is what get_columns returns.
213 my ( $columns, $criteria, $orderby, $area, $totals, $definition ) = @_;
215 my $keys = $keys{$area};
216 my $tables = $table_areas{$area};
219 _build_query( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition );
224 my ( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition) = @_;
226 # $keys is an array of joining constraints
227 my $dbh = C4::Context->dbh();
228 my $joinedtables = join( ',', @$tables );
229 my $joinedcolumns = join( ',', @$columns );
230 my $joinedkeys = join( ' AND ', @$keys );
232 "SELECT $totals $joinedcolumns FROM $tables->[0] ";
233 for (my $i=1;$i<@$tables;$i++){
234 $query .= "LEFT JOIN $tables->[$i] on ($keys->[$i-1]) ";
238 $criteria =~ s/AND/WHERE/;
239 $query .= " $criteria";
242 my @definitions = split(',',$definition);
244 foreach my $def (@definitions){
245 my $defin=get_from_dictionary('',$def);
246 $deftext .=" ".$defin->[0]->{'saved_sql'};
248 if ($query =~ /WHERE/i){
252 $deftext =~ s/AND/WHERE/;
258 my @totcolumns = split( ',', $totals );
259 foreach my $total (@totcolumns) {
260 if ( $total =~ /\((.*)\)/ ) {
261 if ( $groupby eq '' ) {
262 $groupby = " GROUP BY $1";
277 =item get_criteria($area,$cgi);
279 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
284 my ($area,$cgi) = @_;
285 my $dbh = C4::Context->dbh();
286 my $crit = $criteria{$area};
287 my $column_defs = _get_column_defs($cgi);
289 foreach my $localcrit (@$crit) {
290 my ( $value, $type ) = split( /\|/, $localcrit );
291 my ( $table, $column ) = split( /\./, $value );
292 if ( $type eq 'date' ) {
294 $temp{'name'} = $value;
296 $temp{'description'} = $column_defs->{$value};
297 push @criteria_array, \%temp;
302 "SELECT distinct($column) as availablevalues FROM $table";
303 my $sth = $dbh->prepare($query);
306 while ( my $row = $sth->fetchrow_hashref() ) {
312 $temp{'name'} = $value;
313 $temp{'description'} = $column_defs->{$value};
314 $temp{'values'} = \@values;
315 push @criteria_array, \%temp;
318 return ( \@criteria_array );
322 my ( $sql, $type, $format, $id ) = @_;
323 my $dbh = C4::Context->dbh();
325 # take this line out when in production
326 if ($format eq 'url'){
331 my $sth = $dbh->prepare($sql);
333 my $colnames=$sth->{'NAME'};
337 $row = join ('</th><th>',@$colnames);
338 $row = "<tr><th>$row</th></tr>";
339 $temphash{'row'} = $row;
340 push @results, \%temphash;
343 while ( my @data = $sth->fetchrow_array() ) {
347 my $row = join( '</td><td>', @data );
348 $row = "<tr><td>$row</td></tr>";
349 $temphash{'row'} = $row;
350 if ( $format eq 'text' ) {
351 $string .= "\n" . $row;
353 if ($format eq 'tab' ){
354 $row = join("\t",@data);
355 $string .="\n" . $row;
357 if ($format eq 'csv' ){
358 $row = join(",",@data);
359 $string .="\n" . $row;
361 if ($format eq 'url'){
363 @$temphash{@$colnames}=@data;
364 push @xmlarray,$temphash;
366 push @results, \%temphash;
370 if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
373 elsif ($format eq 'url') {
374 my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
375 my $dump = new XML::Dumper;
376 my $xml = $dump->pl2xml( \@xmlarray );
377 store_results($id,$xml);
381 return ( \@results );
385 =item save_report($sql,$name,$type,$notes)
387 Given some sql and a name this will saved it so that it can resued
392 my ( $sql, $name, $type, $notes ) = @_;
393 my $dbh = C4::Context->dbh();
395 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes) VALUES (?,now(),now(),?,?,?,?)";
396 my $sth = $dbh->prepare($query);
397 $sth->execute( 0, $sql, $name, $type, $notes );
404 my $dbh = C4::Context->dbh();
405 my $query = "SELECT * FROM saved_reports WHERE report_id=?";
406 my $sth = $dbh->prepare($query);
408 if (my $data=$sth->fetchrow_hashref()){
409 my $query2 = "UPDATE saved_reports SET report=?,date_run=now() WHERE report_id=?";
410 my $sth2 = $dbh->prepare($query2);
411 $sth2->execute($xml,$id);
415 my $query2 = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
416 my $sth2 = $dbh->prepare($query2);
417 $sth2->execute($id,$xml);
425 my $dbh = C4::Context->dbh();
426 my $query = "SELECT * FROM saved_reports WHERE report_id = ?";
427 my $sth = $dbh->prepare($query);
429 my $data = $sth->fetchrow_hashref();
430 my $dump = new XML::Dumper;
431 my $perl = $dump->xml2pl( $data->{'report'} );
432 foreach my $row (@$perl) {
434 foreach my $key (keys %$row){
435 $htmlrow .= "<td>$row->{$key}</td>";
438 $row->{'row'} = $htmlrow;
441 $query = "SELECT * FROM saved_sql WHERE id = ?";
442 $sth = $dbh->prepare($query);
444 $data = $sth->fetchrow_hashref();
446 return ($perl,$data->{'report_name'},$data->{'notes'});
451 my $dbh = C4::Context->dbh();
452 my $query = "DELETE FROM saved_sql WHERE id = ?";
453 my $sth = $dbh->prepare($query);
458 sub get_saved_reports {
459 my $dbh = C4::Context->dbh();
460 my $query = "SELECT *,saved_sql.id AS id FROM saved_sql
461 LEFT JOIN saved_reports ON saved_reports.report_id = saved_sql.id
462 ORDER by date_created";
463 my $sth = $dbh->prepare($query);
466 while ( my $data = $sth->fetchrow_hashref() ) {
467 push @reports, $data;
470 return ( \@reports );
473 sub get_saved_report {
475 my $dbh = C4::Context->dbh();
476 my $query = " SELECT * FROM saved_sql WHERE id = ?";
477 my $sth = $dbh->prepare($query);
479 my $data = $sth->fetchrow_hashref();
481 return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
484 =item create_compound($masterID,$subreportID)
486 This will take 2 reports and create a compound report using both of them
490 sub create_compound {
491 my ($masterID,$subreportID) = @_;
492 my $dbh = C4::Context->dbh();
494 my ($mastersql,$mastertype) = get_saved_report($masterID);
495 my ($subsql,$subtype) = get_saved_report($subreportID);
497 # now we have to do some checking to see how these two will fit together
499 my ($mastertables,$subtables);
500 if ($mastersql =~ / from (.*) where /i){
503 if ($subsql =~ / from (.*) where /i){
506 return ($mastertables,$subtables);
509 =item get_column_type($column)
511 This takes a column name of the format table.column and will return what type it is
512 (free text, set values, date)
516 sub get_column_type {
517 my ($tablecolumn) = @_;
518 my ($table,$column) = split(/\./,$tablecolumn);
519 my $dbh = C4::Context->dbh();
523 # mysql doesnt support a column selection, set column to %
525 my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
526 while (my $info = $sth->fetchrow_hashref()){
527 if ($info->{'COLUMN_NAME'} eq $column){
529 if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
530 $info->{'TYPE_NAME'} = 'distinct';
532 return $info->{'TYPE_NAME'};
538 =item get_distinct_values($column)
540 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop
541 with the distinct values of the column
545 sub get_distinct_values {
546 my ($tablecolumn) = @_;
547 my ($table,$column) = split(/\./,$tablecolumn);
548 my $dbh = C4::Context->dbh();
550 "SELECT distinct($column) as availablevalues FROM $table";
551 my $sth = $dbh->prepare($query);
554 while ( my $row = $sth->fetchrow_hashref() ) {
561 sub save_dictionary {
562 my ($name,$description,$sql,$area) = @_;
563 my $dbh = C4::Context->dbh();
564 my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
565 VALUES (?,?,?,?,now(),now())";
566 my $sth = $dbh->prepare($query);
567 $sth->execute($name,$description,$sql,$area) || return 0;
572 sub get_from_dictionary {
574 my $dbh = C4::Context->dbh();
575 my $query = "SELECT * FROM reports_dictionary";
577 $query.= " WHERE area = ?";
580 $query.= " WHERE id = ?"
582 my $sth = $dbh->prepare($query);
587 $sth->execute($area);
593 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
594 while (my $data = $sth->fetchrow_hashref()){
595 $data->{'areaname'}=$reports[$data->{'area'}-1];
603 sub delete_definition {
605 my $dbh = C4::Context->dbh();
606 my $query = "DELETE FROM reports_dictionary WHERE id = ?";
607 my $sth = $dbh->prepare($query);
614 my $dbh = C4::Context->dbh();
615 my $query = "SELECT * FROM saved_sql WHERE id = ?";
616 my $sth = $dbh->prepare($query);
618 my $data=$sth->fetchrow_hashref();
620 return $data->{'savedsql'};
623 sub _get_column_defs {
626 my $columns_def_file = "columns.def";
627 my $htdocs = C4::Context->config('intrahtdocs');
628 my $section='intranet';
629 my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section,$cgi);
631 my $full_path_to_columns_def_file="$htdocs/$theme/$lang/$columns_def_file";
632 open (COLUMNS,$full_path_to_columns_def_file);
633 while (my $input = <COLUMNS>){
634 my @row =split(/\t/,$input);
635 $columns{$row[0]}=$row[1];
646 Chris Cormack <crc@liblime.com>