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

(-)a/C4/Reports/Guided.pm (+103 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;
31
use Koha::Reports;
30
use C4::Output;
32
use C4::Output;
31
use C4::Debug;
33
use C4::Debug;
32
use C4::Log;
34
use C4::Log;
35
use Koha::Notice::Templates;
36
use C4::Letters;
33
37
34
use Koha::AuthorisedValues;
38
use Koha::AuthorisedValues;
35
use Koha::Patron::Categories;
39
use Koha::Patron::Categories;
Lines 928-933 sub ValidateSQLParameters { Link Here
928
    return \@problematic_parameters;
932
    return \@problematic_parameters;
929
}
933
}
930
934
935
=head2 EmailReport
936
937
    EmailReport($report_id, $letter_code, $module, $branch, $email)
938
939
Take a report and use it to process a Template Toolkit formatted notice
940
941
=cut
942
943
sub EmailReport {
944
945
    my $params  = shift;
946
    my $report  = $params->{report};
947
    my $from    = $params->{from};
948
    my $email   = $params->{email};
949
    my $module  = $params->{module};
950
    my $code    = $params->{code};
951
    my $branch  = $params->{branch} || "";
952
    my $verbose = $params->{verbose} || 0;
953
    my $commit  = $params->{commit} || 0;
954
955
    my @errors;
956
957
    return ( { FATAL => "MISSING_PARAMS" } ) unless ($report && $module && $code);
958
959
    return ( { FATAL => "NO_LETTER" } ) unless
960
    my $letter = Koha::Notice::Templates->find({
961
        module =>$module,
962
        code => $code,
963
        branchcode => $branch,
964
        message_transport_type => 'email',
965
    });
966
    $letter = $letter->unblessed;
967
    push ( @errors, { LETTER_FOUND => 1 } ) if $verbose;
968
969
    my $report = Koha::Reports->find( $report );
970
    my $sql = $report->savedsql;
971
    return ( { FATAL => "NO_REPORT" } ) unless $sql;
972
    push ( @errors, { REPORT_FOUND => 1 } ) if $verbose;
973
974
    my ( $sth, $errors ) = execute_query( $sql ); #don't pass offset or limit, hardcoded limit of 999,999 will be used
975
    return ( { FATAL => "REPORT_FAIL" } ) if $errors;
976
    push ( @errors, { REPORT_SUCCEED => 1 } ) if $verbose;
977
978
    my $counter = 1;
979
    my $template = $letter->{content};
980
981
    while ( my $row = $sth->fetchrow_hashref() ) {
982
        my $err_count = scalar @errors;
983
        push ( @errors, { NO_BOR_COL => $counter } ) unless defined $row->{borrowernumber};
984
        push ( @errors, { NO_EMAIL_COL => $counter } ) unless ( (defined $email && defined $row->{$email}) || defined $row->{email} );
985
        push ( @errors, { NO_FROM_COL => $counter } ) unless defined ( $from || $row->{from} );
986
        push ( @errors, { NO_BOR => $row->{borrowernumber} } ) unless Koha::Patrons->find({borrowernumber=>$row->{borrowernumber}});
987
988
        my $from_address = $from || $row->{from};
989
        my $to_address = $email ? $row->{$email} : $row->{email};
990
        push ( @errors, { NOT_PARSE => $counter } ) unless my $content = _process_row_TT( $row, $template );
991
        $letter->{content}=$content;
992
993
        $counter++;
994
        next if scalar @errors > $err_count;
995
        push ( @errors, { SENDING_SUCCEED => $counter-1 } ) if $verbose;
996
997
        C4::Letters::EnqueueLetter({
998
            letter => $letter,
999
            borrowernumber         => $row->{borrowernumber},
1000
            message_transport_type => 'email',
1001
            from_address           => $from_address,
1002
            to_address             => $to_address,
1003
        }) if $commit;
1004
1005
    }
1006
1007
    return @errors;
1008
1009
}
1010
1011
1012
1013
=head2 ProcessRowTT
1014
1015
   my $content = ProcessRowTT($row_hashref, $template);
1016
1017
Accepts a hashref containing values and processes them against Template Toolkit
1018
to produce content
1019
1020
=cut
1021
1022
sub _process_row_TT {
1023
1024
    my ($row, $template) = @_;
1025
1026
    return 0 unless ($row && $template);
1027
    my $content;
1028
    my $processor = Template->new();
1029
    $processor->process( \$template, $row, \$content);
1030
    return $content;
1031
1032
}
1033
931
sub _get_display_value {
1034
sub _get_display_value {
932
    my ( $original_value, $column ) = @_;
1035
    my ( $original_value, $column ) = @_;
933
    if ( $column eq 'periodicity' ) {
1036
    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