623b9208e4e59430ee65bcc2471c966ec32061ab
[freeside.git] / FS / FS / Cron / agent_email.pm
1 package FS::Cron::agent_email;
2 use base qw( Exporter );
3
4 use strict;
5 use vars qw( @EXPORT_OK $DEBUG );
6 use Date::Simple qw(today);
7 use URI::Escape;
8 use FS::Mason qw( mason_interps );
9 use FS::Conf;
10 use FS::Misc qw(send_email);
11 use FS::Record qw(qsearch);# qsearchs);
12 use FS::agent;
13
14 @EXPORT_OK = qw ( agent_email );
15 $DEBUG = 0;
16
17 sub agent_email {
18   my %opt = @_;
19
20   my $conf = new FS::Conf;
21
22   my $day = $conf->config('agent-email_day') or return;
23   return unless $day == today->day;
24
25   if ( 1 ) { #XXX if ( %%%RT_ENABLED%%% ) {
26     require RT;
27     RT::LoadConfig();
28     RT::Init();
29     RT::ConnectToDatabase();
30   }
31
32   my $from = $conf->config('invoice_from_name') ?
33              $conf->config('invoice_from_name') . ' <' . 
34              $conf->config('invoice_from') . '>' :
35              $conf->config('invoice_from');
36
37   my $outbuf = '';;
38   my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
39
40   my $comp = '/search/cust_main.html';
41   my %args = (
42     'cust_fields' => 'Cust# | Cust. Status | Customer | Current Balance',
43     '_type'       => 'html-print',
44   );
45   my $query = join('&', map "$_=".uri_escape($args{$_}), keys %args );
46
47   my $extra_sql = $opt{a} ? " AND agentnum IN ( $opt{a} ) " : '';
48
49   foreach my $agent ( qsearch({
50                         'table'     => 'agent',
51                         'hashref'   => {
52                           'disabled'      => '',
53                           'agent_custnum' => { op=>'!=', value=>'' },
54                         },
55                         'extra_sql' => $extra_sql,
56                       })
57                     )
58   {
59
60     $FS::Mason::Request::QUERY_STRING = $query. '&agentnum='. $agent->agentnum;
61     $fs_interp->exec($comp);
62
63     my @email = $agent->agent_cust_main->invoicing_list or next;
64
65     warn "emailing ". join(',',@email). " for agent ". $agent->agent. "\n"
66       if $DEBUG;
67     send_email(
68       'from'         => $from,
69       'to'           => \@email,
70       'subject'      => 'Customer report',
71       'body'         => $outbuf,
72       'content-type' => 'text/html',
73       #'content-encoding'
74     ); 
75
76     $outbuf = '';
77
78   }
79
80 }
81
82 1;