View | Details | Raw Unified | Return to bug 16149
Collapse All | Expand All

(-)a/C4/Reports/Guided.pm (+100 lines)
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 975-980 sub ValidateSQLParameters { Link Here
975
    return \@problematic_parameters;
978
    return \@problematic_parameters;
976
}
979
}
977
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
978
sub _get_display_value {
1078
sub _get_display_value {
979
    my ( $original_value, $column ) = @_;
1079
    my ( $original_value, $column ) = @_;
980
    if ( $column eq 'periodicity' ) {
1080
    if ( $column eq 'periodicity' ) {
(-)a/misc/cronjobs/patron_emailer.pl (-1 / +149 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
#
4
# This file is part of Koha.
5
#
6
# Koha is free software; you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Koha is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19
use Modern::Perl;
20
21
BEGIN {
22
    # find Koha's Perl modules
23
    # test carefully before changing this
24
    use FindBin;
25
    eval { require "$FindBin::Bin/../kohalib.pl" };
26
}
27
28
use Getopt::Long;
29
use Pod::Usage;
30
31
use C4::Reserves;
32
use C4::Log;
33
use C4::Reports::Guided;
34
use Koha::Holds;
35
use Koha::Calendar;
36
use Koha::DateUtils;
37
use Koha::Libraries;
38
use Koha::Notice::Templates;
39
40
cronlogaction();
41
42
=head1 NAME
43
44
patron_emailer.pl
45
46
=head1 SYNOPSIS
47
48
patron_emailer.pl
49
    [-report][-notice][-module][-library][-from]
50
51
 Options:
52
    -help                       brief help
53
    -report                     report to use as data for email template
54
    -notice                     specific notice code to use
55
    -module                     which module to find the above notice in
56
    -library                    specified branch for selecting notice, will use all libraries by default
57
    -from                       specified email for 'from' address, report column 'from' used if not specified
58
    -email                      specified column to use as 'to' email address, report column 'email' used if not specified
59
    -verbose                    increased verbosity
60
    -commit                     send emails, without this script will only report
61
62
head1 OPTIONS
63
64
=over 8
65
66
=item B<-help>
67
68
Print brief help and exit.
69
70
=item B<-man>
71
72
Print full documentation and exit.
73
74
=item B<-report>
75
76
Specify a saved SQL report in the Koha system to user for the emails. All, and only,
77
    columns in the report will be available for notice template variables
78
79
=item B<-notice>
80
81
Specific notice (CODE) to select
82
83
=item B<-module>
84
85
Which module to find the specified notice in
86
87
=item B<-library>
88
89
Option to specify which branches notice should be used, 'All libraries' is used if not specified
90
91
=item B<-from>
92
93
Specify the sender address of the email, if not specified a 'from' column in the report will be used.
94
95
=item B<-email>
96
97
Specify the column to find recipient address of the email, if not specified an 'email' column in the report will be used.
98
99
=item B<-verbose>
100
101
Increased verbosity, reports sucesses and errors.
102
103
=item B<-commit>
104
105
Send emails, if ommited script will only report.
106
107
=back
108
109
=cut
110
111
my $help     = 0;
112
my $report;
113
my $notice;
114
my $module;  #this is only for selecting correct report - report itself defines available columns, not module
115
my $library; #as above, determines which notice to use, will use 'all libraries' if not specified
116
my $email;   #to specify which column should be used as email in report will use 'email' from borrwers table
117
my $from;    #to specify from address, will expect 'from' column in report if not specified
118
my $verbose  = 0;
119
my $commit   = 0;
120
121
GetOptions(
122
    'help|?'      => \$help,
123
    'report=i'    => \$report,
124
    'notice=s'    => \$notice,
125
    'module=s'    => \$module,
126
    'library=s'   => \$library,
127
    'email=s'     => \$email,
128
    'from=s'      => \$from,
129
    'verbose'     => \$verbose,
130
    'commit'      => \$commit
131
) or pod2usage(1);
132
pod2usage(1) if $help;
133
134
my @errors = C4::Reports::Guided::EmailReport({
135
    email      => $email,
136
    from       => $from,
137
    report     => $report,
138
    module     => $module,
139
    code       => $notice,
140
    branchcode => $library,
141
    verbose    => $verbose,
142
    commit     => $commit,
143
});
144
145
foreach my $err_hash ( @errors ){
146
    foreach ( keys %{$err_hash} ){
147
        print "$_ => ${$err_hash}{$_}\n";
148
    }
149
}

Return to bug 16149