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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Text::CSV::Encoded;
23 use Encode qw( decode );
26 use C4::Reports::Guided qw( delete_report get_report_areas convert_sql update_sql get_saved_reports get_results ValidateSQLParameters format_results get_report_types get_columns get_from_dictionary get_criteria build_query save_report execute_query nb_rows get_report_groups );
28 use C4::Auth qw( get_template_and_user get_session );
29 use C4::Output qw( pagination_bar output_html_with_http_headers );
32 use C4::Log qw( logaction );
33 use Koha::AuthorisedValue;
34 use Koha::AuthorisedValues;
35 use Koha::BiblioFrameworks;
37 use Koha::Patron::Categories;
38 use Koha::SharedContent;
39 use Koha::Util::OpenDocument qw( generate_ods );
40 use Koha::Notice::Templates;
41 use Koha::TemplateUtils qw( process_tt );
42 use C4::ClassSource qw( GetClassSources );
51 Script to control the guided report creation
56 my $usecache = Koha::Caches->get_instance->memcached_cache;
58 my $op = $input->param('op') // '';
60 if ( ( $op eq 'add_form' ) || ( $op eq 'add_form_sql' ) || ( $op eq 'edit_form' )
61 || ( $op eq 'duplicate' ) ) {
62 $flagsrequired = 'create_reports';
64 elsif ( $op eq 'list' ) {
65 $flagsrequired = 'execute_reports';
67 elsif ( $op eq 'delete' ) {
68 $flagsrequired = 'delete_reports';
74 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
76 template_name => "reports/guided_reports_start.tt",
79 flagsrequired => { reports => $flagsrequired },
82 my $session_id = $input->cookie('CGISESSID');
83 my $session = $session_id ? get_session($session_id) : undef;
85 $template->param( templates => Koha::Notice::Templates->search( { module => 'report' } ) );
88 if ( $input->param("filter_set") or $input->param('clear_filters') ) {
90 $filter->{$_} = $input->param("filter_$_") foreach qw/date author keyword group subgroup/;
91 $session->param('report_filter', $filter) if $session;
92 $template->param( 'filter_set' => 1 );
94 elsif ($session and not $input->param('clear_filters')) {
95 $filter = $session->param('report_filter');
100 $template->param( 'start' => 1 );
103 elsif ( $op eq 'add_form' ) {
105 $template->param( 'build1' => 1 );
107 'areas' => get_report_areas(),
108 'usecache' => $usecache,
109 'cache_expiry' => 300,
113 elsif ( $op eq 'cud-delete') {
114 my @ids = $input->multi_param('id');
115 delete_report( @ids );
119 elsif ( $op eq 'show'){
121 my $id = $input->param('id');
122 my $report = Koha::Reports->find($id);
125 'reportname' => $report->report_name,
126 'notes' => $report->notes,
127 'sql' => $report->savedsql,
129 'mana_success' => scalar $input->param('mana_success'),
130 'mana_id' => $report->{mana_id},
131 'mana_comments' => $report->{comments}
135 elsif ( $op eq 'edit_form'){
136 my $id = $input->param('id');
137 my $report = Koha::Reports->find($id);
138 my $group = $report->report_group;
139 my $subgroup = $report->report_subgroup;
140 my $tables = get_tables();
142 'sql' => $report->savedsql,
143 'reportname' => $report->report_name,
144 'groups_with_subgroups' => groups_with_subgroups($group, $subgroup),
145 'notes' => $report->notes,
147 'cache_expiry' => $report->cache_expiry,
148 'public' => $report->public,
149 'usecache' => $usecache,
151 'mana_id' => $report->{mana_id},
152 'mana_comments' => $report->{comments},
157 elsif ( $op eq 'cud-update_sql' || $op eq 'cud-update_and_run_sql' ){
158 my $id = $input->param('id');
159 my $sql = $input->param('sql');
160 my $reportname = $input->param('reportname');
161 my $group = $input->param('group');
162 my $subgroup = $input->param('subgroup');
163 my $notes = $input->param('notes');
164 my $cache_expiry = $input->param('cache_expiry');
165 my $cache_expiry_units = $input->param('cache_expiry_units');
166 my $public = $input->param('public');
167 my $save_anyway = $input->param('save_anyway');
169 my $tables = get_tables();
171 # if we have the units, then we came from creating a report from SQL and thus need to handle converting units
172 if( $cache_expiry_units ){
173 if( $cache_expiry_units eq "minutes" ){
175 } elsif( $cache_expiry_units eq "hours" ){
176 $cache_expiry *= 3600; # 60 * 60
177 } elsif( $cache_expiry_units eq "days" ){
178 $cache_expiry *= 86400; # 60 * 60 * 24
181 # check $cache_expiry isn't too large, Memcached::set requires it to be less than 30 days or it will be treated as if it were an absolute time stamp
182 if( $cache_expiry >= 2592000 ){
183 push @errors, {cache_expiry => $cache_expiry};
186 create_non_existing_group_and_subgroup($input, $group, $subgroup);
188 my ( $is_sql_valid, $validation_errors ) = Koha::Report->new({ savedsql => $sql })->is_sql_valid;
189 push(@errors, @$validation_errors) unless $is_sql_valid;
193 'errors' => \@errors,
198 # Check defined SQL parameters for authorised value validity
199 my $problematic_authvals = ValidateSQLParameters($sql);
201 if ( scalar @$problematic_authvals > 0 && not $save_anyway ) {
202 # There's at least one problematic parameter, report to the
203 # GUI and provide all user input for further actions
207 'reportname' => $reportname,
209 'subgroup' => $subgroup,
212 'problematic_authvals' => $problematic_authvals,
213 'warn_authval_problem' => 1,
218 # No params problem found or asked to save anyway
223 subgroup => $subgroup,
226 cache_expiry => $cache_expiry,
229 'save_successful' => 1,
230 'reportname' => $reportname,
234 'groups_with_subgroups' => groups_with_subgroups($group, $subgroup),
236 'cache_expiry' => $cache_expiry,
238 'usecache' => $usecache,
241 logaction( "REPORTS", "MODIFY", $id, "$reportname | $sql" ) if C4::Context->preference("ReportsLog");
245 cache_expiry => $cache_expiry,
246 cache_expiry_units => $cache_expiry_units,
249 if ( $op eq 'cud-update_and_run_sql' ) {
255 elsif ($op eq 'retrieve_results') {
256 my $id = $input->param('id');
257 my $result = format_results( $id );
259 report_name => $result->{report_name},
260 notes => $result->{notes},
261 saved_results => $result->{results},
262 date_run => $result->{date_run},
266 elsif ( $op eq 'cud-report' ) {
267 my $cache_expiry_units = $input->param('cache_expiry_units'),
268 my $cache_expiry = $input->param('cache_expiry');
270 # we need to handle converting units
271 if( $cache_expiry_units eq "minutes" ){
273 } elsif( $cache_expiry_units eq "hours" ){
274 $cache_expiry *= 3600; # 60 * 60
275 } elsif( $cache_expiry_units eq "days" ){
276 $cache_expiry *= 86400; # 60 * 60 * 24
278 # check $cache_expiry isn't too large, Memcached::set requires it to be less than 30 days or it will be treated as if it were an absolute time stamp
279 if( $cache_expiry >= 2592000 ){ # oops, over the limit of 30 days
280 # report error to user
284 'areas' => get_report_areas(),
285 'cache_expiry' => $cache_expiry,
286 'usecache' => $usecache,
287 'public' => scalar $input->param('public'),
290 # they have chosen a new report and the area to report on
293 'area' => scalar $input->param('area'),
294 'types' => get_report_types(),
295 'cache_expiry' => $cache_expiry,
296 'public' => scalar $input->param('public'),
301 elsif ( $op eq 'cud-choose_type' ) {
302 # they have chosen type and area
303 # get area and type and pass them to the template
304 my $area = $input->param('area');
305 my $type = $input->param('types');
310 columns => get_columns($area,$input),
311 'cache_expiry' => scalar $input->param('cache_expiry'),
312 'public' => scalar $input->param('public'),
316 elsif ( $op eq 'cud-choose_columns' ) {
317 # we now know type, area, and columns
318 # next step is the constraints
319 my $area = $input->param('area');
320 my $type = $input->param('type');
321 my @columns = $input->multi_param('columns');
322 my $column = join( ',', @columns );
329 definitions => get_from_dictionary($area),
330 criteria => get_criteria($area,$input),
331 'public' => scalar $input->param('public'),
335 cache_expiry => scalar $input->param('cache_expiry'),
336 cache_expiry_units => scalar $input->param('cache_expiry_units'),
342 elsif ( $op eq 'cud-choose_criteria' ) {
343 my $area = $input->param('area');
344 my $type = $input->param('type');
345 my $column = $input->param('column');
346 my @definitions = $input->multi_param('definition');
347 my $definition = join (',',@definitions);
348 my @criteria = $input->multi_param('criteria_column');
350 foreach my $crit (@criteria) {
351 my $value = $input->param( $crit . "_value" );
353 # If value is not defined, then it may be range values
354 if (!defined $value) {
355 my $fromvalue = $input->param( "from_" . $crit . "_value" );
356 my $tovalue = $input->param( "to_" . $crit . "_value" );
358 if ($fromvalue && $tovalue) {
359 $query_criteria .= " AND $crit >= '$fromvalue' AND $crit <= '$tovalue'";
362 # don't escape runtime parameters, they'll be at runtime
363 if ($value =~ /<<.*>>/) {
364 $query_criteria .= " AND $crit=$value";
366 $query_criteria .= " AND $crit='$value'";
375 'definition' => $definition,
376 'criteriastring' => $query_criteria,
377 'public' => scalar $input->param('public'),
381 cache_expiry => scalar $input->param('cache_expiry'),
382 cache_expiry_units => scalar $input->param('cache_expiry_units'),
387 my @columns = split( ',', $column );
390 # build structue for use by tmpl_loop to choose columns to order by
391 # need to do something about the order of the order :)
392 # we also want to use the %columns hash to get the plain english names
393 foreach my $col (@columns) {
394 my %total = (name => $col);
395 my @selects = map {+{ value => $_ }} (qw(sum min max avg count));
396 $total{'select'} = \@selects;
397 push @total_by, \%total;
400 $template->param( 'total_by' => \@total_by );
403 elsif ( $op eq 'cud-choose_operations' ) {
404 my $area = $input->param('area');
405 my $type = $input->param('type');
406 my $column = $input->param('column');
407 my $criteria = $input->param('criteria');
408 my $definition = $input->param('definition');
409 my @total_by = $input->multi_param('total_by');
411 foreach my $total (@total_by) {
412 my $value = $input->param( $total . "_tvalue" );
413 $totals .= "$value($total),";
421 'criteriastring' => $criteria,
423 'definition' => $definition,
424 'cache_expiry' => scalar $input->param('cache_expiry'),
425 'public' => scalar $input->param('public'),
429 my @columns = split( ',', $column );
432 # build structue for use by tmpl_loop to choose columns to order by
433 # need to do something about the order of the order :)
434 foreach my $col (@columns) {
435 my %order = (name => $col);
436 my @selects = map {+{ value => $_ }} (qw(asc desc));
437 $order{'select'} = \@selects;
438 push @order_by, \%order;
441 $template->param( 'order_by' => \@order_by );
444 elsif ( $op eq 'cud-build_report' ) {
446 # now we have all the info we need and can build the sql
447 my $area = $input->param('area');
448 my $type = $input->param('type');
449 my $column = $input->param('column');
450 my $crit = $input->param('criteria');
451 my $totals = $input->param('totals');
452 my $definition = $input->param('definition');
453 my $query_criteria=$crit;
454 # split the columns up by ,
455 my @columns = split( ',', $column );
456 my @order_by = $input->multi_param('order_by');
459 foreach my $order (@order_by) {
460 my $value = $input->param( $order . "_ovalue" );
461 if ($query_orderby) {
462 $query_orderby .= ",$order $value";
465 $query_orderby = " ORDER BY $order $value";
471 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
477 'cache_expiry' => scalar $input->param('cache_expiry'),
478 'public' => scalar $input->param('public'),
482 elsif ( $op eq 'save' ) {
483 # Save the report that has just been built
484 my $area = $input->param('area');
485 my $sql = $input->param('sql');
486 my $type = $input->param('type');
492 'cache_expiry' => scalar $input->param('cache_expiry'),
493 'public' => scalar $input->param('public'),
494 'groups_with_subgroups' => groups_with_subgroups($area), # in case we have a report group that matches area
498 elsif ( $op eq 'cud-save' ) {
499 # save the sql pasted in by a user
500 my $area = $input->param('area');
501 my $group = $input->param('group');
502 my $subgroup = $input->param('subgroup');
503 my $sql = $input->param('sql');
504 my $name = $input->param('reportname');
505 my $type = $input->param('types');
506 my $notes = $input->param('notes');
507 my $cache_expiry = $input->param('cache_expiry');
508 my $cache_expiry_units = $input->param('cache_expiry_units');
509 my $public = $input->param('public');
510 my $save_anyway = $input->param('save_anyway');
511 my $tables = get_tables();
514 # if we have the units, then we came from creating a report from SQL and thus need to handle converting units
515 if( $cache_expiry_units ){
516 if( $cache_expiry_units eq "minutes" ){
518 } elsif( $cache_expiry_units eq "hours" ){
519 $cache_expiry *= 3600; # 60 * 60
520 } elsif( $cache_expiry_units eq "days" ){
521 $cache_expiry *= 86400; # 60 * 60 * 24
524 # check $cache_expiry isn't too large, Memcached::set requires it to be less than 30 days or it will be treated as if it were an absolute time stamp
525 if( $cache_expiry && $cache_expiry >= 2592000 ){
526 push @errors, {cache_expiry => $cache_expiry};
529 create_non_existing_group_and_subgroup($input, $group, $subgroup);
530 ## FIXME this is AFTER entering a name to save the report under
531 my ( $is_sql_valid, $validation_errors ) = Koha::Report->new({ savedsql => $sql })->is_sql_valid;
532 push(@errors, @$validation_errors) unless $is_sql_valid;
536 'errors' => \@errors,
538 'reportname'=> $name,
541 'cache_expiry' => $cache_expiry,
545 # Check defined SQL parameters for authorised value validity
546 my $problematic_authvals = ValidateSQLParameters($sql);
548 if ( scalar @$problematic_authvals > 0 && not $save_anyway ) {
549 # There's at least one problematic parameter, report to the
550 # GUI and provide all user input for further actions
554 'subgroup' => $subgroup,
556 'reportname' => $name,
560 'problematic_authvals' => $problematic_authvals,
561 'warn_authval_problem' => 1,
566 cache_expiry => $cache_expiry,
567 cache_expiry_units => $cache_expiry_units,
571 # No params problem found or asked to save anyway
572 my $id = save_report( {
573 borrowernumber => $borrowernumber,
578 subgroup => $subgroup,
581 cache_expiry => $cache_expiry,
584 logaction( "REPORTS", "ADD", $id, "$name | $sql" ) if C4::Context->preference("ReportsLog");
586 'save_successful' => 1,
587 'reportname' => $name,
591 'groups_with_subgroups' => groups_with_subgroups($group, $subgroup),
593 'cache_expiry' => $cache_expiry,
595 'usecache' => $usecache,
602 elsif ($op eq 'cud-share'){
603 my $lang = $input->param('mana_language') || '';
604 my $reportid = $input->param('reportid');
605 my $result = Koha::SharedContent::send_entity($lang, $borrowernumber, $reportid, 'report');
607 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?op=listmanamsg=".$result->{msg});
609 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?op=list&manamsg=noanswer");
613 elsif ($op eq 'export'){
615 # export results to tab separated text or CSV
616 my $report_id = $input->param('id');
617 my $report = Koha::Reports->find($report_id);
618 my $sql = $report->savedsql;
619 my @param_names = $input->multi_param('param_name');
620 my @sql_params = $input->multi_param('sql_params');
621 my $format = $input->param('format');
622 my $reportname = $input->param('reportname');
624 $reportname ? "$report_id-$reportname-reportresults.$format" : "$report_id-reportresults.$format";
625 my $scrubber = C4::Scrubber->new();
627 ($sql, undef) = $report->prep_report( \@param_names, \@sql_params );
628 my ( $sth, $q_errors ) = execute_query( { sql => $sql, report_id => $report_id } );
629 unless ($q_errors and @$q_errors) {
630 my ( $type, $content );
631 if ($format eq 'tab') {
632 $type = 'application/octet-stream';
633 $content .= join("\t", header_cell_values($sth)) . "\n";
634 $content = $scrubber->scrub( Encode::decode( 'UTF-8', $content ) );
635 while ( my $row = $sth->fetchrow_arrayref() ) {
636 $content .= join( "\t", $scrubber->scrub(@$row) ) . "\n";
639 if ( $format eq 'csv' ) {
640 my $delimiter = C4::Context->csv_delimiter;
641 $type = 'application/csv';
642 my $csv = Text::CSV::Encoded->new({ encoding_out => 'UTF-8', sep_char => $delimiter});
643 $csv or die "Text::CSV::Encoded->new({binary => 1}) FAILED: " . Text::CSV::Encoded->error_diag();
644 if ( $csv->combine( header_cell_values($sth) ) ) {
645 $content .= $scrubber->scrub( Encode::decode( 'UTF-8', $csv->string() ) ) . "\n";
648 push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() };
650 while ( my $row = $sth->fetchrow_arrayref() ) {
651 if ( $csv->combine(@$row) ) {
652 $content .= $scrubber->scrub( $csv->string() ) . "\n";
655 push @$q_errors, { combine => $csv->error_diag() };
659 elsif ( $format eq 'ods' ) {
660 $type = 'application/vnd.oasis.opendocument.spreadsheet';
661 my $ods_fh = File::Temp->new( UNLINK => 0 );
662 my $ods_filepath = $ods_fh->filename;
665 # First line is headers
666 my @headers = header_cell_values($sth);
667 push @$ods_content, \@headers;
669 # Other line in Unicode
670 my $sql_rows = $sth->fetchall_arrayref();
671 foreach my $sql_row (@$sql_rows) {
673 foreach my $sql_cell (@$sql_row) {
674 push @content_row, $scrubber->scrub( Encode::encode( 'UTF8', $sql_cell ) );
677 push @$ods_content, \@content_row;
681 generate_ods($ods_filepath, $ods_content);
685 open $ods_fh, '<', $ods_filepath;
686 $content .= $_ while <$ods_fh>;
687 unlink $ods_filepath;
689 elsif ( $format eq 'template' ) {
690 my $template_id = $input->param('template');
691 my $notice_template = Koha::Notice::Templates->find($template_id);
692 my $data = $sth->fetchall_arrayref( {} );
693 $content = process_tt(
694 $notice_template->content,
697 report_id => $report_id,
701 $reportfilename = process_tt(
702 $notice_template->title,
705 report_id => $report_id,
710 print $input->header(
712 -attachment=> $reportfilename
716 foreach my $err (@$q_errors, @errors) {
717 print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
718 } # here we print all the non-fatal errors at the end. Not super smooth, but better than nothing.
724 'name' => 'Error exporting report!',
726 'errors' => $q_errors,
730 elsif ( $op eq 'add_form_sql' || $op eq 'duplicate' ) {
732 my ($group, $subgroup, $sql, $reportname, $notes);
733 if ( $input->param('sql') ) {
734 $group = $input->param('report_group');
735 $subgroup = $input->param('report_subgroup');
736 $sql = $input->param('sql') // '';
737 $reportname = $input->param('reportname') // '';
738 $notes = $input->param('notes') // '';
740 elsif ( my $report_id = $input->param('id') ) {
741 my $report = Koha::Reports->find($report_id);
742 $group = $report->report_group;
743 $subgroup = $report->report_subgroup;
744 $sql = $report->savedsql // '';
745 $reportname = $report->report_name // '';
746 $notes = $report->notes // '';
749 my $tables = get_tables();
753 reportname => $reportname,
756 'groups_with_subgroups' => groups_with_subgroups($group, $subgroup),
758 'cache_expiry' => 300,
759 'usecache' => $usecache,
766 # execute a saved report
767 my $limit = $input->param('limit') || 20;
769 my $report_id = $input->param('id');
770 my @sql_params = $input->multi_param('sql_params');
771 my @param_names = $input->multi_param('param_name');
772 my $template_id = $input->param('template');
773 my $want_full_chart = $input->param('want_full_chart') || 0;
777 if ($input->param('page')) {
778 $offset = ($input->param('page') - 1) * $limit;
786 my ( $sql, $original_sql, $type, $name, $notes );
787 if (my $report = Koha::Reports->find($report_id)) {
788 $sql = $original_sql = $report->savedsql;
789 $name = $report->report_name;
790 $notes = $report->notes;
794 # if we have at least 1 parameter, and it's not filled, then don't execute but ask for parameters
795 if ($sql =~ /<</ && !@sql_params) {
796 # split on ??. Each odd (2,4,6,...) entry should be a parameter to fill
797 my @split = split /<<|>>/,$sql;
801 for(my $i=0;$i<($#split/2);$i++) {
802 my ($text,$authorised_value_all) = split /\|/,$split[$i*2+1];
803 my $sep = $authorised_value_all ? "|" : "";
804 if( defined $uniq_params{$text.$sep.$authorised_value_all} ){
806 } else { $uniq_params{$text.$sep.$authorised_value_all} = "$i"; }
807 my ($authorised_value, $param_options) = split /:/, $authorised_value_all;
810 if ( $param_options eq "all" ) {
812 } elsif ( $param_options eq "in" ) {
813 $multiple = "multiple";
817 if ( not defined $authorised_value ) {
818 # no authorised value input, provide a text box
820 } elsif ( $authorised_value eq "date" ) {
821 # require a date, provide a date picker
823 } elsif ( $authorised_value eq "list" ) {
824 # require a list, provide a textarea
827 # defined $authorised_value, and not 'date'
828 my $dbh=C4::Context->dbh;
829 my @authorised_values;
831 # builds list, depending on authorised value...
832 if ( $authorised_value eq "branches" ) {
833 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchname'] } );
834 while ( my $library = $libraries->next ) {
835 push @authorised_values, $library->branchcode;
836 $authorised_lib{$library->branchcode} = $library->branchname;
839 elsif ( $authorised_value eq "itemtypes" ) {
840 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
842 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
843 push @authorised_values, $itemtype;
844 $authorised_lib{$itemtype} = $description;
847 elsif ( $authorised_value eq "biblio_framework" ) {
848 my @frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] })->as_list;
849 my $default_source = '';
850 push @authorised_values,$default_source;
851 $authorised_lib{$default_source} = 'Default';
852 foreach my $framework (@frameworks) {
853 push @authorised_values, $framework->frameworkcode;
854 $authorised_lib{$framework->frameworkcode} = $framework->frameworktext;
857 elsif ( $authorised_value eq "cn_source" ) {
858 my $class_sources = GetClassSources();
859 my $default_source = C4::Context->preference("DefaultClassificationSource");
860 foreach my $class_source (sort keys %$class_sources) {
861 next unless $class_sources->{$class_source}->{'used'} or
862 ($class_source eq $default_source);
863 push @authorised_values, $class_source;
864 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
867 elsif ( $authorised_value eq "categorycode" ) {
868 my @patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description']})->as_list;
869 %authorised_lib = map { $_->categorycode => $_->description } @patron_categories;
870 push @authorised_values, $_->categorycode for @patron_categories;
872 elsif ( $authorised_value eq "cash_registers" ) {
873 my $sth = $dbh->prepare("SELECT id, name FROM cash_registers ORDER BY description");
875 while ( my ( $id, $name ) = $sth->fetchrow_array ) {
876 push @authorised_values, $id;
877 $authorised_lib{$id} = $name;
880 elsif ( $authorised_value eq "debit_types" ) {
881 my $sth = $dbh->prepare("SELECT code, description FROM account_debit_types ORDER BY code");
883 while ( my ( $code, $description ) = $sth->fetchrow_array ) {
884 push @authorised_values, $code;
885 $authorised_lib{$code} = $description;
888 elsif ( $authorised_value eq "credit_types" ) {
889 my $sth = $dbh->prepare("SELECT code, description FROM account_credit_types ORDER BY code");
891 while ( my ( $code, $description ) = $sth->fetchrow_array ) {
892 push @authorised_values, $code;
893 $authorised_lib{$code} = $description;
897 if ( Koha::AuthorisedValues->search({ category => $authorised_value })->count ) {
899 SELECT authorised_value,lib
900 FROM authorised_values
904 my $authorised_values_sth = $dbh->prepare($query);
905 $authorised_values_sth->execute( $authorised_value);
907 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
908 push @authorised_values, $value;
909 $authorised_lib{$value} = $lib;
910 # For item location, we show the code and the libelle
911 $authorised_lib{$value} = $lib;
914 # not exists $authorised_value_categories{$authorised_value})
915 push @authval_errors, {'entry' => $text,
916 'auth_val' => $authorised_value };
917 # tell the template there's an error
918 $template->param( auth_val_error => 1 );
919 # skip scrolling list creation and params push
926 name => "sql_params",
927 id => "sql_params_".$labelid,
928 values => \@authorised_values,
929 labels => \%authorised_lib,
933 push @tmpl_parameters, {'entry' => $text, 'input' => $input, 'labelid' => $labelid, 'name' => $text.$sep.$authorised_value_all, 'include_all' => $all, 'select_multiple' => $multiple };
935 $template->param('sql' => $sql,
938 'sql_params' => \@tmpl_parameters,
939 'auth_val_errors' => \@authval_errors,
944 my ($sql,$header_types) = $report->prep_report( \@param_names, \@sql_params );
945 $template->param(header_types => $header_types);
946 my ( $sth, $errors ) = execute_query(
951 report_id => $report_id,
956 die "execute_query failed to return sth for report $report_id: $sql";
957 } elsif ( !$errors ) {
958 $total = nb_rows($sql) || 0;
959 my $headers = header_cell_loop($sth);
960 $template->param(header_row => $headers);
961 while (my $row = $sth->fetchrow_arrayref()) {
962 my @cells = map { +{ cell => $_ } } @$row;
963 push @rows, { cells => \@cells };
965 if( $want_full_chart ){
966 my ( $sth2, $errors2 ) = execute_query( { sql => $sql, report_id => $report_id } );
967 while (my $row = $sth2->fetchrow_arrayref()) {
968 my @cells = map { +{ cell => $_ } } @$row;
969 push @allrows, { cells => \@cells };
973 my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0);
974 my $url = "/cgi-bin/koha/reports/guided_reports.pl?id=$report_id&op=run&limit=$limit&want_full_chart=$want_full_chart";
976 $url = join('&param_name=', $url, map { URI::Escape::uri_escape_utf8($_) } @param_names);
979 $url = join('&sql_params=', $url, map { URI::Escape::uri_escape_utf8($_) } @sql_params);
983 my $notice_template = Koha::Notice::Templates->find($template_id);
984 my ( $sth2, $errors2 ) = execute_query( { sql => $sql, report_id => $report_id } );
985 my $data = $sth2->fetchall_arrayref( {} );
986 my $notice_rendered =
987 process_tt( $notice_template->content, { data => $data, report_id => $report_id } );
989 process_tt( $notice_template->title, { data => $data, report_id => $report_id } );
991 template_id => $template_id,
992 processed_notice => $notice_rendered,
993 processed_notice_title => $title_rendered,
999 'allresults' => \@allrows,
1000 'pagination_bar' => pagination_bar($url, $totpages, scalar $input->param('page')),
1001 'unlimited_total' => $total,
1006 original_sql => $original_sql,
1011 'errors' => defined($errors) ? [$errors] : undef,
1012 'sql_params' => \@sql_params,
1013 'param_names' => \@param_names,
1018 push @errors, { no_sql_for_id => $report_id };
1022 if ( $op eq 'list' || $op eq 'convert') {
1024 if ( $op eq 'convert' ) {
1025 my $report_id = $input->param('id');
1026 my $report = Koha::Reports->find($report_id);
1028 my $updated_sql = C4::Reports::Guided::convert_sql( $report->savedsql );
1029 C4::Reports::Guided::update_sql(
1032 sql => $updated_sql,
1033 name => $report->report_name,
1034 group => $report->report_group,
1035 subgroup => $report->report_subgroup,
1036 notes => $report->notes,
1037 public => $report->public,
1038 cache_expiry => $report->cache_expiry,
1041 $template->param( report_converted => $report->report_name );
1045 # use a saved report
1046 # get list of reports and display them
1047 my $group = $input->param('group');
1048 my $subgroup = $input->param('subgroup');
1049 $filter->{group} = $group;
1050 $filter->{subgroup} = $subgroup;
1051 my $reports = get_saved_reports($filter);
1052 my $has_obsolete_reports;
1053 for my $report ( @$reports ) {
1054 $report->{results} = C4::Reports::Guided::get_results( $report->{id} );
1055 if ( $report->{savedsql} =~ m|biblioitems| and $report->{savedsql} =~ m|marcxml| ) {
1056 $report->{seems_obsolete} = 1;
1057 $has_obsolete_reports++;
1061 'manamsg' => $input->param('manamsg') || '',
1063 'savedreports' => $reports,
1064 'usecache' => $usecache,
1065 'groups_with_subgroups' => groups_with_subgroups( $group, $subgroup ),
1067 has_obsolete_reports => $has_obsolete_reports,
1070 # pass $sth, get back an array of names for the column headers
1071 sub header_cell_values {
1072 my $sth = shift or return ();
1073 return '' unless ($sth->{NAME});
1074 return @{$sth->{NAME}};
1077 # pass $sth, get back a TMPL_LOOP-able set of names for the column headers
1078 sub header_cell_loop {
1079 my @headers = map { +{ cell => decode('UTF-8',$_) } } header_cell_values (shift);
1083 #get a list of available tables for auto-complete
1086 my $cache = Koha::Caches->get_instance();
1087 my $tables = $cache->get_from_cache("Reports-SQL_tables-for-autocomplete");
1092 $tables = C4::Reports::Guided->get_all_tables();
1093 for my $table (@{$tables}) {
1094 my $sql = "SHOW COLUMNS FROM $table";
1095 my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
1096 for my $row (@{$rows}) {
1097 push @{$result->{$table}}, $row->{Field};
1100 $cache->set_in_cache("Reports-SQL_tables-for-autocomplete",$result);
1105 $template->{VARS}->{'build' . $_} and last;
1107 $template->param( 'referer' => $input->referer(),
1110 output_html_with_http_headers $input, $cookie, $template->output;
1112 sub groups_with_subgroups {
1113 my ($group, $subgroup) = @_;
1115 my $groups_with_subgroups = get_report_groups();
1117 my @sorted_keys = sort {
1118 $groups_with_subgroups->{$a}->{name} cmp $groups_with_subgroups->{$b}->{name}
1119 } keys %$groups_with_subgroups;
1120 foreach my $g_id (@sorted_keys) {
1121 my $v = $groups_with_subgroups->{$g_id};
1123 if (my $sg = $v->{subgroups}) {
1124 foreach my $sg_id (sort { $sg->{$a} cmp $sg->{$b} } keys %$sg) {
1127 name => $sg->{$sg_id},
1128 selected => ($group && $g_id eq $group && $subgroup && $sg_id eq $subgroup ),
1135 selected => ($group && $g_id eq $group),
1136 subgroups => \@subgroups,
1142 sub create_non_existing_group_and_subgroup {
1143 my ($input, $group, $subgroup) = @_;
1144 if (defined $group and $group ne '') {
1145 my $report_groups = C4::Reports::Guided::get_report_groups;
1146 if (not exists $report_groups->{$group}) {
1147 my $groupdesc = $input->param('groupdesc') // $group;
1148 Koha::AuthorisedValue->new({
1149 category => 'REPORT_GROUP',
1150 authorised_value => $group,
1153 my $cache_key = "AuthorisedValues-REPORT_GROUP-0-".C4::Context->userenv->{"branch"};
1154 my $cache = Koha::Caches->get_instance();
1155 my $result = $cache->clear_from_cache($cache_key);
1157 if (defined $subgroup and $subgroup ne '') {
1158 if (not exists $report_groups->{$group}->{subgroups}->{$subgroup}) {
1159 my $subgroupdesc = $input->param('subgroupdesc') // $subgroup;
1160 Koha::AuthorisedValue->new({
1161 category => 'REPORT_SUBGROUP',
1162 authorised_value => $subgroup,
1163 lib => $subgroupdesc,
1166 my $cache_key = "AuthorisedValues-REPORT_SUBGROUP-0-".C4::Context->userenv->{"branch"};
1167 my $cache = Koha::Caches->get_instance();
1168 my $result = $cache->clear_from_cache($cache_key);