|
Lines 27-35
use C4::Context;
Link Here
|
| 27 |
use C4::Templates qw/themelanguage/; |
27 |
use C4::Templates qw/themelanguage/; |
| 28 |
use C4::Koha; |
28 |
use C4::Koha; |
| 29 |
use Koha::DateUtils; |
29 |
use Koha::DateUtils; |
|
|
30 |
use Koha::Patrons; |
| 30 |
use C4::Output; |
31 |
use C4::Output; |
| 31 |
use C4::Debug; |
32 |
use C4::Debug; |
| 32 |
use C4::Log; |
33 |
use C4::Log; |
|
|
34 |
use Koha::Notice::Templates; |
| 35 |
use C4::Letters; |
| 33 |
|
36 |
|
| 34 |
use Koha::AuthorisedValues; |
37 |
use Koha::AuthorisedValues; |
| 35 |
use Koha::Patron::Categories; |
38 |
use Koha::Patron::Categories; |
|
Lines 854-860
sub get_sql {
Link Here
|
| 854 |
sub get_results { |
857 |
sub get_results { |
| 855 |
my ( $report_id ) = @_; |
858 |
my ( $report_id ) = @_; |
| 856 |
my $dbh = C4::Context->dbh; |
859 |
my $dbh = C4::Context->dbh; |
| 857 |
warn $report_id; |
|
|
| 858 |
return $dbh->selectall_arrayref(q| |
860 |
return $dbh->selectall_arrayref(q| |
| 859 |
SELECT id, report, date_run |
861 |
SELECT id, report, date_run |
| 860 |
FROM saved_reports |
862 |
FROM saved_reports |
|
Lines 976-981
sub ValidateSQLParameters {
Link Here
|
| 976 |
return \@problematic_parameters; |
978 |
return \@problematic_parameters; |
| 977 |
} |
979 |
} |
| 978 |
|
980 |
|
|
|
981 |
=head2 EmailReport |
| 982 |
|
| 983 |
EmailReport($report_id, $letter_code, $module, $branch, $email) |
| 984 |
|
| 985 |
Take a report and use it to process a Template Toolkit formatted notice |
| 986 |
|
| 987 |
=cut |
| 988 |
|
| 989 |
sub EmailReport { |
| 990 |
|
| 991 |
my $params = shift; |
| 992 |
my $report = $params->{report}; |
| 993 |
my $from = $params->{from}; |
| 994 |
my $email = $params->{email}; |
| 995 |
my $module = $params->{module}; |
| 996 |
my $code = $params->{code}; |
| 997 |
my $branch = $params->{branch} || ""; |
| 998 |
my $verbose = $params->{verbose} || 0; |
| 999 |
my $commit = $params->{commit} || 0; |
| 1000 |
|
| 1001 |
my @errors; |
| 1002 |
|
| 1003 |
return ( { FATAL => "MISSING_PARAMS" } ) unless ($report && $module && $code); |
| 1004 |
|
| 1005 |
return ( { FATAL => "NO_LETTER" } ) unless |
| 1006 |
my $letter = Koha::Notice::Templates->find({ |
| 1007 |
module =>$module, |
| 1008 |
code => $code, |
| 1009 |
branchcode => $branch, |
| 1010 |
message_transport_type => 'email', |
| 1011 |
}); |
| 1012 |
$letter = $letter->unblessed; |
| 1013 |
push ( @errors, { LETTER_FOUND => 1 } ) if $verbose; |
| 1014 |
|
| 1015 |
return ( { FATAL => "NO_REPORT" } ) unless my $sql = get_saved_report( $report )->{savedsql}; |
| 1016 |
push ( @errors, { REPORT_FOUND => 1 } ) if $verbose; |
| 1017 |
|
| 1018 |
my ( $sth, $errors ) = execute_query( $sql ); #don't pass offset or limit, hardcoded limit of 999,999 will be used |
| 1019 |
return ( { FATAL => "REPORT_FAIL" } ) if $errors; |
| 1020 |
push ( @errors, { REPORT_SUCCEED => 1 } ) if $verbose; |
| 1021 |
|
| 1022 |
my $counter = 0; |
| 1023 |
my $template = $letter->{content}; |
| 1024 |
|
| 1025 |
while ( my $row = $sth->fetchrow_hashref() ) { |
| 1026 |
my $err_count = scalar @errors; |
| 1027 |
push ( @errors, { NO_BOR_COL => $counter } ) unless defined $row->{borrowernumber}; |
| 1028 |
push ( @errors, { NO_EMAIL_COL => $counter } ) unless ( (defined $email && defined $row->{$email}) || defined $row->{email} ); |
| 1029 |
push ( @errors, { NO_FROM_COL => $counter } ) unless defined ( $from || $row->{from} ); |
| 1030 |
push ( @errors, { NO_BOR => $row->{borrowernumber} } ) unless Koha::Patrons->find({borrowernumber=>$row->{borrowernumber}}); |
| 1031 |
|
| 1032 |
my $from_address = $from || $row->{from}; |
| 1033 |
my $to_address = $email ? $row->{$email} : $row->{email}; |
| 1034 |
push ( @errors, { NOT_PARSE => $counter } ) unless my $content = _process_row_TT( $row, $template ); |
| 1035 |
$letter->{content}=$content; |
| 1036 |
|
| 1037 |
$counter++; |
| 1038 |
next if scalar @errors > $err_count; |
| 1039 |
push ( @errors, { SENDING_SUCCEED => $counter-1 } ) if $verbose; |
| 1040 |
|
| 1041 |
C4::Letters::EnqueueLetter({ |
| 1042 |
letter => $letter, |
| 1043 |
borrowernumber => $row->{borrowernumber}, |
| 1044 |
message_transport_type => 'email', |
| 1045 |
from_address => $from_address, |
| 1046 |
to_address => $to_address, |
| 1047 |
}) if $commit; |
| 1048 |
|
| 1049 |
} |
| 1050 |
|
| 1051 |
return @errors; |
| 1052 |
|
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
=head2 ProcessRowTT |
| 1058 |
|
| 1059 |
my $content = ProcessRowTT($row_hashref, $template); |
| 1060 |
|
| 1061 |
Accepts a hashref containing values and processes them against Template Toolkit |
| 1062 |
to produce content |
| 1063 |
|
| 1064 |
=cut |
| 1065 |
|
| 1066 |
sub _process_row_TT { |
| 1067 |
|
| 1068 |
my ($row, $template) = @_; |
| 1069 |
|
| 1070 |
return 0 unless ($row && $template); |
| 1071 |
my $content; |
| 1072 |
my $processor = Template->new(); |
| 1073 |
$processor->process( \$template, $row, \$content); |
| 1074 |
return $content; |
| 1075 |
|
| 1076 |
} |
| 1077 |
|
| 979 |
sub _get_display_value { |
1078 |
sub _get_display_value { |
| 980 |
my ( $original_value, $column ) = @_; |
1079 |
my ( $original_value, $column ) = @_; |
| 981 |
if ( $column eq 'periodicity' ) { |
1080 |
if ( $column eq 'periodicity' ) { |