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);
27 # use Smart::Comments;
30 # set the version for version checking
35 qw(get_report_types get_report_areas get_columns build_query get_criteria
36 save_report get_saved_reports execute_query get_saved_report create_compound run_compound
37 get_column_type get_distinct_values save_dictionary get_from_dictionary
38 delete_definition delete_report);
42 [ 'borrowers', 'statistics','items', 'biblioitems' ]; # circulation
43 $table_areas{'2'} = [ 'items', 'biblioitems', 'biblio' ]; # catalogue
44 $table_areas{'3'} = [ 'borrowers', 'accountlines' ]; # patrons
45 $table_areas{'4'} = ['aqorders', 'biblio', 'items']; # acquisitions
49 'statistics.borrowernumber=borrowers.borrowernumber',
50 'items.itemnumber = statistics.itemnumber',
51 'biblioitems.biblioitemnumber = items.biblioitemnumber'
54 'items.biblioitemnumber=biblioitems.biblioitemnumber',
55 'biblioitems.biblionumber=biblio.biblionumber'
57 $keys{'3'} = ['borrowers.borrowernumber=accountlines.borrowernumber'];
59 'aqorders.biblionumber=biblio.biblionumber',
60 'biblio.biblionumber=items.biblionumber'
63 # have to do someting here to know if its dropdown, free text, date etc
67 'statistics.type', 'borrowers.categorycode',
68 'statistics.branch', 'biblioitems.itemtype',
69 'biblioitems.publicationyear|date',
70 'items.dateaccessioned|date'
73 [ 'biblioitems.itemtype', 'items.holdingbranch', 'items.homebranch' ,'items.itemlost'];
74 $criteria{'3'} = ['borrowers.branchcode'];
75 $criteria{'4'} = ['aqorders.datereceived|date'];
79 my $columns_def_file = "columns.def";
80 my $htdocs = C4::Context->config('intrahtdocs');
81 my $section='intranet';
82 my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section);
84 my $columns_def_file="$htdocs/$theme/$lang/$columns_def_file";
85 open (COLUMNS,$columns_def_file);
86 while (my $input = <COLUMNS>){
87 my @row =split(/\t/,$input);
88 $columns{$row[0]}=$row[1];
95 C4::Reports - Module for generating reports
110 =item get_report_types()
112 This will return a list of all the available report types
116 sub get_report_types {
117 my $dbh = C4::Context->dbh();
119 # FIXME these should be in the database perhaps
120 my @reports = ( 'Tabular', 'Summary', 'Matrix' );
122 for ( my $i = 0 ; $i < 3 ; $i++ ) {
124 $hashrep{id} = $i + 1;
125 $hashrep{name} = $reports[$i];
126 push @reports2, \%hashrep;
128 return ( \@reports2 );
132 =item get_report_areas()
134 This will return a list of all the available report areas
138 sub get_report_areas {
139 my $dbh = C4::Context->dbh();
141 # FIXME these should be in the database
142 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions' );
144 for ( my $i = 0 ; $i < 4 ; $i++ ) {
146 $hashrep{id} = $i + 1;
147 $hashrep{name} = $reports[$i];
148 push @reports2, \%hashrep;
150 return ( \@reports2 );
154 =item get_all_tables()
156 This will return a list of all tables in the database
161 my $dbh = C4::Context->dbh();
162 my $query = "SHOW TABLES";
163 my $sth = $dbh->prepare($query);
166 while ( my $data = $sth->fetchrow_arrayref() ) {
167 push @tables, $data->[0];
174 =item get_columns($area)
176 This will return a list of all columns for a report area
182 # this calls the internal fucntion _get_columns
184 my $tables = $table_areas{$area};
186 foreach my $table (@$tables) {
187 my @columns = _get_columns($table);
188 push @allcolumns, @columns;
190 return ( \@allcolumns );
194 my ($tablename) = @_;
195 my $dbh = C4::Context->dbh();
196 my $sth = $dbh->prepare("show columns from $tablename");
200 $tablehash{'table'}=$tablename;
201 push @columns, \%tablehash;
202 while ( my $data = $sth->fetchrow_arrayref() ) {
204 $temphash{'name'} = "$tablename.$data->[0]";
205 $temphash{'description'} = $columns{"$tablename.$data->[0]"};
206 push @columns, \%temphash;
212 =item build_query($columns,$criteria,$orderby,$area)
214 This will build the sql needed to return the results asked for,
215 $columns is expected to be of the format tablename.columnname.
216 This is what get_columns returns.
221 my ( $columns, $criteria, $orderby, $area, $totals, $definition ) = @_;
223 my $keys = $keys{$area};
224 my $tables = $table_areas{$area};
227 _build_query( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition );
232 my ( $tables, $columns, $criteria, $keys, $orderby, $totals, $definition) = @_;
234 # $keys is an array of joining constraints
235 my $dbh = C4::Context->dbh();
236 my $joinedtables = join( ',', @$tables );
237 my $joinedcolumns = join( ',', @$columns );
238 my $joinedkeys = join( ' AND ', @$keys );
240 "SELECT $totals $joinedcolumns FROM $tables->[0] ";
241 for (my $i=1;$i<@$tables;$i++){
242 $query .= "LEFT JOIN $tables->[$i] on ($keys->[$i-1]) ";
246 $criteria =~ s/AND/WHERE/;
247 $query .= " $criteria";
250 my @definitions = split(',',$definition);
252 foreach my $def (@definitions){
253 my $defin=get_from_dictionary('',$def);
254 $deftext .=" ".$defin->[0]->{'saved_sql'};
256 if ($query =~ /WHERE/i){
260 $deftext =~ s/AND/WHERE/;
266 my @totcolumns = split( ',', $totals );
267 foreach my $total (@totcolumns) {
268 if ( $total =~ /\((.*)\)/ ) {
269 if ( $groupby eq '' ) {
270 $groupby = " GROUP BY $1";
285 =item get_criteria($area);
287 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
293 my $dbh = C4::Context->dbh();
294 my $crit = $criteria{$area};
296 foreach my $localcrit (@$crit) {
297 my ( $value, $type ) = split( /\|/, $localcrit );
298 my ( $table, $column ) = split( /\./, $value );
299 if ( $type eq 'date' ) {
301 $temp{'name'} = $value;
303 $temp{'description'} = $columns{$value};
304 push @criteria_array, \%temp;
309 "SELECT distinct($column) as availablevalues FROM $table";
310 my $sth = $dbh->prepare($query);
313 while ( my $row = $sth->fetchrow_hashref() ) {
319 $temp{'name'} = $value;
320 $temp{'description'} = $columns{$value};
321 $temp{'values'} = \@values;
322 push @criteria_array, \%temp;
325 return ( \@criteria_array );
329 my ( $sql, $type, $format, $id ) = @_;
330 my $dbh = C4::Context->dbh();
332 # take this line out when in production
333 if ($format eq 'url'){
338 my $sth = $dbh->prepare($sql);
340 my $colnames=$sth->{'NAME'};
344 $row = join ('</th><th>',@$colnames);
345 $row = "<tr><th>$row</th></tr>";
346 $temphash{'row'} = $row;
347 push @results, \%temphash;
351 while ( my @data = $sth->fetchrow_array() ) {
355 my $row = join( '</td><td>', @data );
356 $row = "<tr><td>$row</td></tr>";
357 $temphash{'row'} = $row;
358 if ( $format eq 'text' ) {
359 $string .= "\n" . $row;
361 if ($format eq 'tab' ){
362 $row = join("\t",@data);
363 $string .="\n" . $row;
365 if ($format eq 'csv' ){
366 $row = join(",",@data);
367 $string .="\n" . $row;
369 if ($format eq 'url'){
371 @$temphash{@$colnames}=@data;
372 $xmlhash{$i}=$temphash;
375 push @results, \%temphash;
379 if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv' ) {
382 elsif ($format eq 'url') {
384 my $xml = XMLout(\%xmlhash);
385 store_results($id,$xml);
389 return ( \@results );
393 =item save_report($sql,$name,$type,$notes)
395 Given some sql and a name this will saved it so that it can resued
400 my ( $sql, $name, $type, $notes ) = @_;
401 my $dbh = C4::Context->dbh();
403 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes) VALUES (?,now(),now(),?,?,?,?)";
404 my $sth = $dbh->prepare($query);
405 $sth->execute( 0, $sql, $name, $type, $notes );
412 my $dbh = C4::Context->dbh();
413 my $query = "INSERT INTO saved_reports (report_id,report,date_run) VALUES (?,?,now())";
414 my $sth = $dbh->prepare($query);
415 $sth->execute($id,$xml);
421 my $dbh = C4::Context->dbh();
422 my $query = "DELETE FROM saved_sql WHERE id = ?";
423 my $sth = $dbh->prepare($query);
428 sub get_saved_reports {
429 my $dbh = C4::Context->dbh();
430 my $query = "SELECT * FROM saved_sql ORDER by date_created";
431 my $sth = $dbh->prepare($query);
434 while ( my $data = $sth->fetchrow_hashref() ) {
435 push @reports, $data;
438 return ( \@reports );
441 sub get_saved_report {
443 my $dbh = C4::Context->dbh();
444 my $query = " SELECT * FROM saved_sql WHERE id = ?";
445 my $sth = $dbh->prepare($query);
447 my $data = $sth->fetchrow_hashref();
449 return ( $data->{'savedsql'}, $data->{'type'} );
452 =item create_compound($masterID,$subreportID)
454 This will take 2 reports and create a compound report using both of them
458 sub create_compound {
459 my ($masterID,$subreportID) = @_;
460 my $dbh = C4::Context->dbh();
462 my ($mastersql,$mastertype) = get_saved_report($masterID);
463 my ($subsql,$subtype) = get_saved_report($subreportID);
465 # now we have to do some checking to see how these two will fit together
467 my ($mastertables,$subtables);
468 if ($mastersql =~ / from (.*) where /i){
471 if ($subsql =~ / from (.*) where /i){
474 return ($mastertables,$subtables);
477 =item get_column_type($column)
479 This takes a column name of the format table.column and will return what type it is
480 (free text, set values, date)
484 sub get_column_type {
485 my ($tablecolumn) = @_;
486 my ($table,$column) = split(/\./,$tablecolumn);
487 my $dbh = C4::Context->dbh();
491 # mysql doesnt support a column selection, set column to %
493 my $sth = $dbh->column_info( $catalog, $schema, $table, $tempcolumn ) || die $dbh->errstr;
494 while (my $info = $sth->fetchrow_hashref()){
495 if ($info->{'COLUMN_NAME'} eq $column){
497 if ($info->{'TYPE_NAME'} eq 'CHAR' || $info->{'TYPE_NAME'} eq 'VARCHAR'){
498 $info->{'TYPE_NAME'} = 'distinct';
500 return $info->{'TYPE_NAME'};
506 =item get_distinct_values($column)
508 Given a column name, return an arrary ref of hashrefs suitable for use as a tmpl_loop
509 with the distinct values of the column
513 sub get_distinct_values {
514 my ($tablecolumn) = @_;
515 my ($table,$column) = split(/\./,$tablecolumn);
516 my $dbh = C4::Context->dbh();
518 "SELECT distinct($column) as availablevalues FROM $table";
519 my $sth = $dbh->prepare($query);
522 while ( my $row = $sth->fetchrow_hashref() ) {
529 sub save_dictionary {
530 my ($name,$description,$sql,$area) = @_;
531 my $dbh = C4::Context->dbh();
532 my $query = "INSERT INTO reports_dictionary (name,description,saved_sql,area,date_created,date_modified)
533 VALUES (?,?,?,?,now(),now())";
534 my $sth = $dbh->prepare($query);
535 $sth->execute($name,$description,$sql,$area) || return 0;
540 sub get_from_dictionary {
542 my $dbh = C4::Context->dbh();
543 my $query = "SELECT * FROM reports_dictionary";
545 $query.= " WHERE area = ?";
548 $query.= " WHERE id = ?"
550 my $sth = $dbh->prepare($query);
555 $sth->execute($area);
561 while (my $data = $sth->fetchrow_hashref()){
569 sub delete_definition {
571 my $dbh = C4::Context->dbh();
572 my $query = "DELETE FROM reports_dictionary WHERE id = ?";
573 my $sth = $dbh->prepare($query);
579 Chris Cormack <crc@liblime.com>