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);
26 # use Smart::Comments;
28 # set the version for version checking
33 qw(get_report_types get_report_areas get_columns build_query get_criteria
34 save_report get_saved_reports execute_query get_saved_report create_compound run_compound);
38 [ 'borrowers', 'statistics','items', 'biblioitems' ]; # circulation
39 $table_areas{'2'} = [ 'items', 'biblioitems', 'biblio' ]; # catalogue
40 $table_areas{'3'} = [ 'borrowers', 'accountlines' ]; # patrons
41 $table_areas{'4'} = ['aqorders', 'biblio', 'items']; # acquisitions
45 'statistics.borrowernumber=borrowers.borrowernumber',
46 'items.itemnumber = statistics.itemnumber',
47 'biblioitems.biblioitemnumber = items.biblioitemnumber'
50 'items.biblioitemnumber=biblioitems.biblioitemnumber',
51 'biblioitems.biblionumber=biblio.biblionumber'
53 $keys{'3'} = ['borrowers.borrowernumber=accountlines.borrowernumber'];
55 'aqorders.biblionumber=biblio.biblionumber',
56 'biblio.biblionumber=items.biblionumber'
59 # have to do someting here to know if its dropdown, free text, date etc
63 'statistics.type', 'borrowers.categorycode',
64 'statistics.branch', 'biblioitems.itemtype',
65 'biblioitems.publicationyear|date',
66 'items.dateaccessioned|date'
69 [ 'biblioitems.itemtype', 'items.holdingbranch', 'items.homebranch' ,'items.itemlost'];
70 $criteria{'3'} = ['borrowers.branchcode'];
71 $criteria{'4'} = ['aqorders.datereceived|date'];
75 my $columns_def_file = "columns.def";
76 my $htdocs = C4::Context->config('intrahtdocs');
77 my $section='intranet';
78 my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section);
80 my $columns_def_file="$htdocs/$theme/$lang/$columns_def_file";
81 open (COLUMNS,$columns_def_file);
82 while (my $input = <COLUMNS>){
83 my @row =split(/\t/,$input);
84 $columns{$row[0]}=$row[1];
91 C4::Reports - Module for generating reports
106 =item get_report_types()
108 This will return a list of all the available report types
112 sub get_report_types {
113 my $dbh = C4::Context->dbh();
115 # FIXME these should be in the database perhaps
116 my @reports = ( 'Tabular', 'Summary', 'Matrix' );
118 for ( my $i = 0 ; $i < 3 ; $i++ ) {
120 $hashrep{id} = $i + 1;
121 $hashrep{name} = $reports[$i];
122 push @reports2, \%hashrep;
124 return ( \@reports2 );
128 =item get_report_areas()
130 This will return a list of all the available report areas
134 sub get_report_areas {
135 my $dbh = C4::Context->dbh();
137 # FIXME these should be in the database
138 my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions' );
140 for ( my $i = 0 ; $i < 4 ; $i++ ) {
142 $hashrep{id} = $i + 1;
143 $hashrep{name} = $reports[$i];
144 push @reports2, \%hashrep;
146 return ( \@reports2 );
150 =item get_all_tables()
152 This will return a list of all tables in the database
157 my $dbh = C4::Context->dbh();
158 my $query = "SHOW TABLES";
159 my $sth = $dbh->prepare($query);
162 while ( my $data = $sth->fetchrow_arrayref() ) {
163 push @tables, $data->[0];
170 =item get_columns($area)
172 This will return a list of all columns for a report area
178 # this calls the internal fucntion _get_columns
180 my $tables = $table_areas{$area};
182 foreach my $table (@$tables) {
183 my @columns = _get_columns($table);
184 push @allcolumns, @columns;
186 return ( \@allcolumns );
190 my ($tablename) = @_;
191 my $dbh = C4::Context->dbh();
192 my $sth = $dbh->prepare("show columns from $tablename");
196 $tablehash{'table'}=$tablename;
197 push @columns, \%tablehash;
198 while ( my $data = $sth->fetchrow_arrayref() ) {
200 $temphash{'name'} = "$tablename.$data->[0]";
201 $temphash{'description'} = $columns{"$tablename.$data->[0]"};
202 push @columns, \%temphash;
208 =item build_query($columns,$criteria,$orderby,$area)
210 This will build the sql needed to return the results asked for,
211 $columns is expected to be of the format tablename.columnname.
212 This is what get_columns returns.
217 my ( $columns, $criteria, $orderby, $area, $totals ) = @_;
219 my $keys = $keys{$area};
220 my $tables = $table_areas{$area};
223 _build_query( $tables, $columns, $criteria, $keys, $orderby, $totals );
228 my ( $tables, $columns, $criteria, $keys, $orderby, $totals ) = @_;
230 # $keys is an array of joining constraints
231 my $dbh = C4::Context->dbh();
232 my $joinedtables = join( ',', @$tables );
233 my $joinedcolumns = join( ',', @$columns );
234 my $joinedkeys = join( ' AND ', @$keys );
236 "SELECT $totals $joinedcolumns FROM $tables->[0] ";
237 for (my $i=1;$i<@$tables;$i++){
238 $query .= "LEFT JOIN $tables->[$i] on ($keys->[$i-1]) ";
242 $criteria =~ s/AND/WHERE/;
243 $query .= " $criteria";
247 my @totcolumns = split( ',', $totals );
248 foreach my $total (@totcolumns) {
249 if ( $total =~ /\((.*)\)/ ) {
250 if ( $groupby eq '' ) {
251 $groupby = " GROUP BY $1";
266 =item get_criteria($area);
268 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
274 my $dbh = C4::Context->dbh();
275 my $crit = $criteria{$area};
277 foreach my $localcrit (@$crit) {
278 my ( $value, $type ) = split( /\|/, $localcrit );
279 my ( $table, $column ) = split( /\./, $value );
280 if ( $type eq 'date' ) {
282 $temp{'name'} = $value;
284 $temp{'description'} = $columns{$value};
285 push @criteria_array, \%temp;
290 "SELECT distinct($column) as availablevalues FROM $table";
291 my $sth = $dbh->prepare($query);
294 while ( my $row = $sth->fetchrow_hashref() ) {
300 $temp{'name'} = $value;
301 $temp{'description'} = $columns{$value};
302 $temp{'values'} = \@values;
303 push @criteria_array, \%temp;
306 return ( \@criteria_array );
310 my ( $sql, $type, $format ) = @_;
311 my $dbh = C4::Context->dbh();
313 # take this line out when in production
315 my $sth = $dbh->prepare($sql);
317 my $colnames=$sth->{'NAME'};
319 my $row = join ('</th><th>',@$colnames);
320 $row = "<tr><th>$row</th></tr>";
322 $temphash{'row'} = $row;
323 push @results, \%temphash;
326 while ( my @data = $sth->fetchrow_array() ) {
330 my $row = join( '</td><td>', @data );
331 $row = "<tr><td>$row</td></tr>";
332 $temphash{'row'} = $row;
333 if ( $format eq 'text' ) {
334 $string .= "\n" . $row;
336 if ($format eq 'tab' ){
337 $row = join("\t",@data);
338 $string .="\n" . $row;
340 if ($format eq 'csv' ){
341 $row = join(",",@data);
342 $string .="\n" . $row;
345 push @results, \%temphash;
349 if ( $format eq 'text' || $format eq 'tab' || $format eq 'csv') {
353 return ( \@results );
357 =item save_report($sql,$name,$type,$notes)
359 Given some sql and a name this will saved it so that it can resued
364 my ( $sql, $name, $type, $notes ) = @_;
365 my $dbh = C4::Context->dbh();
367 "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,type,notes) VALUES (?,now(),now(),?,?,?,?)";
368 my $sth = $dbh->prepare($query);
369 $sth->execute( 0, $sql, $name, $type, $notes );
374 sub get_saved_reports {
375 my $dbh = C4::Context->dbh();
376 my $query = "SELECT * FROM saved_sql ORDER by date_created";
377 my $sth = $dbh->prepare($query);
380 while ( my $data = $sth->fetchrow_hashref() ) {
381 push @reports, $data;
384 return ( \@reports );
387 sub get_saved_report {
389 my $dbh = C4::Context->dbh();
390 my $query = " SELECT * FROM saved_sql WHERE id = ?";
391 my $sth = $dbh->prepare($query);
393 my $data = $sth->fetchrow_hashref();
395 return ( $data->{'savedsql'}, $data->{'type'} );
398 =item create_compound($masterID,$subreportID)
400 This will take 2 reports and create a compound report using both of them
404 sub create_compound {
405 my ($masterID,$subreportID) = @_;
406 my $dbh = C4::Context->dbh();
408 my ($mastersql,$mastertype) = get_saved_report($masterID);
409 my ($subsql,$subtype) = get_saved_report($subreportID);
411 # now we have to do some checking to see how these two will fit together
413 my ($mastertables,$subtables);
414 if ($mastersql =~ / from (.*) where /i){
417 if ($subsql =~ / from (.*) where /i){
420 return ($mastertables,$subtables);
425 Chris Cormack <crc@liblime.com>