import a2billing username as charged_party, RT#32909
[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->invoice_from_full();
33
34   my $outbuf = '';;
35   my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
36
37   my $comp = '/search/cust_main.html';
38   my %args = (
39     'cust_fields' => 'Cust# | Cust. Status | Customer | Current Balance',
40     '_type'       => 'html-print',
41   );
42   my $query = join('&', map "$_=".uri_escape($args{$_}), keys %args );
43
44   my $extra_sql = $opt{a} ? " AND agentnum IN ( $opt{a} ) " : '';
45
46   foreach my $agent ( qsearch({
47                         'table'     => 'agent',
48                         'hashref'   => {
49                           'disabled'      => '',
50                           'agent_custnum' => { op=>'!=', value=>'' },
51                         },
52                         'extra_sql' => $extra_sql,
53                       })
54                     )
55   {
56
57     $FS::Mason::Request::QUERY_STRING = $query. '&agentnum='. $agent->agentnum;
58     $fs_interp->exec($comp);
59
60     my @email = $agent->agent_cust_main->invoicing_list or next;
61
62     warn "emailing ". join(',',@email). " for agent ". $agent->agent. "\n"
63       if $DEBUG;
64     send_email(
65       'from'         => $from,
66       'to'           => \@email,
67       'subject'      => 'Customer report',
68       'body'         => $outbuf,
69       'content-type' => 'text/html',
70       #'content-encoding'
71     ); 
72
73     $outbuf = '';
74
75   }
76
77 }
78
79 1;