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;
31 # set the version for version checking
36 qw(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 );
43 [ 'borrowers', 'statistics','items', 'biblioitems' ]; # circulation
44 $table_areas{'2'} = [ 'items', 'biblioitems', 'biblio' ]; # catalogue
45 $table_areas{'3'} = [ 'borrowers' ]; # patrons
46 $table_areas{'4'} = ['aqorders', 'biblio', 'items']; # acquisitions
47 $table_areas{'5'} = [ 'borrowers', 'accountlines' ]; # accounts
50 'statistics.borrowernumber=borrowers.borrowernumber',
51 'items.itemnumber = statistics.itemnumber',
52 'biblioitems.biblioitemnumber = items.biblioitemnumber'
55 'items.biblioitemnumber=biblioitems.biblioitemnumber',
56 'biblioitems.biblionumber=biblio.biblionumber'
60 'aqorders.biblionumber=biblio.biblionumber',
61 'biblio.biblionumber=items.biblionumber'
63 $keys{'5'} = ['borrowers.borrowernumber=accountlines.borrowernumber'];
65 # have to do someting here to know if its dropdown, free text, date etc
69 'statistics.type', 'borrowers.categorycode',
70 'statistics.branch', 'biblioitems.itemtype',
71 'biblioitems.publicationyear|date',
72 'items.dateaccessioned|date'
75 [ 'biblioitems.itemtype', 'items.holdingbranch', 'items.homebranch' ,'items.itemlost'];
76 $criteria{'3'} = ['borrowers.branchcode'];
77 $criteria{'4'} = ['aqorders.datereceived|date'];
78 $criteria{'5'} = ['borrowers.branchcode'];
81 my $columns_def_file = "columns.def";
82 my $htdocs = C4::Context->config('intrahtdocs');
83 my $section='intranet';
84 my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section);
86 my $columns_def_file="$htdocs/$theme/$lang/$columns_def_file";
87 open (COLUMNS,$columns_def_file);
88 while (my $input = <COLUMNS>){
89 my @row =split(/\t/,$input);
90 $columns{$row[0]}=$row[1];
97 C4::Reports - Module for generating reports
112 =item get_report_types()
114 This will return a list of all the available report types
118 sub get_report_types {
119 my $dbh = C4::Context->dbh();
121 # FIXME these should be in the database perhaps
122 my @reports = ( 'Tabular', 'Summary', 'Matrix' );
124 for ( my $i = 0 ; $i < 3 ; $i++ ) {
126 $hashrep{id} = $i + 1;
127 $hashrep{name} = $reports[$i];
128 push @reports2, \%hashrep;
130 return ( \@reports2 );
134 =item get_report_areas()
136 This will return a list of all the available report areas
140 sub get_report_areas {
141 my $dbh = C4::Context->dbh();
143 # FIXME these should be in the database
144 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
146 for ( my $i = 0 ; $i < 5 ; $i++ ) {
148 $hashrep{id} = $i + 1;
149 $hashrep{name} = $reports[$i];
150 push @reports2, \%hashrep;
152 return ( \@reports2 );
156 =item get_all_tables()
158 This will return a list of all tables in the database
163 my $dbh = C4::Context->dbh();
164 my $query = "SHOW TABLES";
165 my $sth = $dbh->prepare($query);
168 while ( my $data = $sth->fetchrow_arrayref() ) {
169 push @tables, $data->[0];
176 =item get_columns($area)
178 This will return a list of all columns for a report area
184 # this calls the internal fucntion _get_columns
186 my $tables = $table_areas{$area};
188 foreach my $table (@$tables) {
189 my @columns = _get_columns($table);
190 push @allcolumns, @columns;
192 return ( \@allcolumns );
196 my ($tablename) = @_;
197 my $dbh = C4::Context->dbh();
198 my $sth = $dbh->prepare("show columns from $tablename");
202 $tablehash{'table'}=$tablename;
203 push @columns, \%tablehash;
204 while ( my $data = $sth->fetchrow_arrayref() ) {
206 $temphash{'name'} = "$tablename.$data->[0]";
207 $temphash{'description'} = $columns{"$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);
289 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
295 my $dbh = C4::Context->dbh();
296 my $crit = $criteria{$area};
298 foreach my $localcrit (@$crit) {
299 my ( $value, $type ) = split( /\|/, $localcrit );
300 my ( $table, $column ) = split( /\./, $value );
301 if ( $type eq 'date' ) {
303 $temp{'name'} = $value;
305 $temp{'description'} = $columns{$value};
306 push @criteria_array, \%temp;
311 "SELECT distinct($column) as availablevalues FROM $table";
312 my $sth = $dbh->prepare($query);
315 while ( my $row = $sth->fetchrow_hashref() ) {
321 $temp{'name'} = $value;
322 $temp{'description'} = $columns{$value};
323 $temp{'values'} = \@values;
324 push @criteria_array, \%temp;
327 return ( \@criteria_array );
331 my ( $sql, $type, $format, $id ) = @_;
332 my $dbh = C4::Context->dbh();
334 # take this line out when in production
335 if ($format eq 'url'){
340 my $sth = $dbh->prepare($sql);
342 my $colnames=$sth->{'NAME'};
346 $row = join ('</th><th>',@$colnames);
347 $row = "<tr><th>$row</th></tr>";
348 $temphash{'row'} = $row;
349 push @results, \%temphash;
352 while ( my @data = $sth->fetchrow_array() ) {
356 my $row = join( '</td><td>', @data );
357 $row = "<tr><td>$row</td></tr>";
358 $temphash{'row'} = $row;
359 if ( $format eq 'text' ) {
360 $string .= "\n" . $row;
362 if ($format eq 'tab' ){
363 $row = join("\t",@data);
364 $string .="\n" . $row;
366 if ($format eq 'csv' ){
367 $row = join(",",@data);
368 $string .="\n" . $row;
370 if ($format eq 'url'){
372 @$temphash{@$colnames}=@data;
373 push @xmlarray,$temphash;
375 push @results, \%temphash;
379 if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
382 elsif ($format eq 'url') {
383 my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
384 my $dump = new XML::Dumper;
385 my $xml = $dump->pl2xml( \@xmlarray );
386 store_results($id,$xml);
390 return ( \@results );
394 =item save_report($sql,$name,$type,$notes)
396 Given some sql and a name this will saved it so that it can resued
401 my ( $sql, $name, $type, $notes ) = @_;
402 my $dbh = C4::Context->dbh();
404 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes) VALUES (?,now(),now(),?,?,?,?)";
405 my $sth = $dbh->prepare($query);
406 $sth->execute( 0, $sql, $name, $type, $notes );
413 my $dbh = C4::Context->dbh();
414 my $query = "SELECT * FROM saved_reports WHERE report_id=?";
415 my $sth = $dbh->prepare($query);
417 if (my $data=$sth->fetchrow_hashref()){
418 my $query2 = "UPDATE saved_reports SET report=?,date_run=now() WHERE report_id=?";
419 my $sth2 = $dbh->prepare($query2);
420 $sth2->execute($xml,$id);
424 my $query2 = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
425 my $sth2 = $dbh->prepare($query2);
426 $sth2->execute($id,$xml);
434 my $dbh = C4::Context->dbh();
435 my $query = "SELECT * FROM saved_reports WHERE report_id = ?";
436 my $sth = $dbh->prepare($query);
438 my $data = $sth->fetchrow_hashref();
439 my $dump = new XML::Dumper;
440 my $perl = $dump->xml2pl( $data->{'report'} );
441 foreach my $row (@$perl) {
443 foreach my $key (keys %$row){
444 $htmlrow .= "<td>$row->{$key}</td>";
447 $row->{'row'} = $htmlrow;
450 $query = "SELECT * FROM saved_sql WHERE id = ?";
451 $sth = $dbh->prepare($query);
453 $data = $sth->fetchrow_hashref();
455 return ($perl,$data->{'report_name'},$data->{'notes'});
460 my $dbh = C4::Context->dbh();
461 my $query = "DELETE FROM saved_sql WHERE id = ?";
462 my $sth = $dbh->prepare($query);
467 sub get_saved_reports {
468 my $dbh = C4::Context->dbh();
469 my $query = "SELECT *,saved_sql.id AS id FROM saved_sql
470 LEFT JOIN saved_reports ON saved_reports.report_id = saved_sql.id
471 ORDER by date_created";
472 my $sth = $dbh->prepare($query);
475 while ( my $data = $sth->fetchrow_hashref() ) {
476 push @reports, $data;
479 return ( \@reports );
482 sub get_saved_report {
484 my $dbh = C4::Context->dbh();
485 my $query = " SELECT * FROM saved_sql WHERE id = ?";
486 my $sth = $dbh->prepare($query);
488 my $data = $sth->fetchrow_hashref();
490 return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
493 =item create_compound($masterID,$subreportID)
495 This will take 2 reports and create a compound report using both of them
499 sub create_compound {
500 my ($masterID,$subreportID) = @_;
501 my $dbh = C4::Context->dbh();
503 my ($mastersql,$mastertype) = get_saved_report($masterID);
504 my ($subsql,$subtype) = get_saved_report($subreportID);
506 # now we have to do some checking to see how these two will fit together
508 my ($mastertables,$subtables);
509 if ($mastersql =~ / from (.*) where /i){
512 if ($subsql =~ / from (.*) where /i){
515 return ($mastertables,$subtables);
518 =item get_column_type($column)
520 This takes a column name of the format table.column and will return what type it is
521 (free text, set values, date)
525 sub get_column_type {
526 my ($tablecolumn) = @_;
527 my ($table,$column) = split(/\./,$tablecolumn);
528 my $dbh = C4::Context->dbh();
532 # mysql doesnt support a column selection, set column to %
534 my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
535 while (my $info = $sth->fetchrow_hashref()){
536 if ($info->{'COLUMN_NAME'} eq $column){
538 if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
539 $info->{'TYPE_NAME'} = 'distinct';
541 return $info->{'TYPE_NAME'};
547 =item get_distinct_values($column)
549 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop
550 with the distinct values of the column
554 sub get_distinct_values {
555 my ($tablecolumn) = @_;
556 my ($table,$column) = split(/\./,$tablecolumn);
557 my $dbh = C4::Context->dbh();
559 "SELECT distinct($column) as availablevalues FROM $table";
560 my $sth = $dbh->prepare($query);
563 while ( my $row = $sth->fetchrow_hashref() ) {
570 sub save_dictionary {
571 my ($name,$description,$sql,$area) = @_;
572 my $dbh = C4::Context->dbh();
573 my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
574 VALUES (?,?,?,?,now(),now())";
575 my $sth = $dbh->prepare($query);
576 $sth->execute($name,$description,$sql,$area) || return 0;
581 sub get_from_dictionary {
583 my $dbh = C4::Context->dbh();
584 my $query = "SELECT * FROM reports_dictionary";
586 $query.= " WHERE area = ?";
589 $query.= " WHERE id = ?"
591 my $sth = $dbh->prepare($query);
596 $sth->execute($area);
602 while (my $data = $sth->fetchrow_hashref()){
610 sub delete_definition {
612 my $dbh = C4::Context->dbh();
613 my $query = "DELETE FROM reports_dictionary WHERE id = ?";
614 my $sth = $dbh->prepare($query);
621 my $dbh = C4::Context->dbh();
622 my $query = "SELECT * FROM saved_sql WHERE id = ?";
623 my $sth = $dbh->prepare($query);
625 my $data=$sth->fetchrow_hashref();
627 return $data->{'savedsql'};
632 Chris Cormack <crc@liblime.com>