1 package FS::Cron::notify;
4 use vars qw( @ISA @EXPORT_OK $DEBUG );
6 use FS::UID qw( dbh driver_name );
7 use FS::Record qw(qsearch qsearchs);
11 @ISA = qw( Exporter );
12 @EXPORT_OK = qw ( notify_flat_delay );
15 sub notify_flat_delay {
19 my $oldAutoCommit = $FS::UID::AutoCommit;
20 $DEBUG = 1 if $opt{'v'};
22 #we're at now now (and later).
24 my $conf = new FS::Conf;
27 my $integer = driver_name =~ /^mysql/ ? 'SIGNED' : 'INTEGER';
29 # select * from cust_pkg where
30 my $where_pkg = <<"END";
31 WHERE ( cancel IS NULL OR cancel = 0 )
34 SELECT 1 FROM part_pkg
35 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
36 AND part_pkg.plan = 'flat_delayed'
37 AND EXISTS ( SELECT 1 from part_pkg_option
38 WHERE part_pkg.pkgpart = part_pkg_option.pkgpart
39 AND part_pkg_option.optionname = 'recur_notify'
40 AND CAST( part_pkg_option.optionvalue AS $integer ) > 0
42 + CAST( part_pkg_option.optionvalue AS $integer )
46 AND ( cust_pkg.expire is null
47 OR cust_pkg.expire > ( $time
48 + CAST( part_pkg_option.optionvalue AS $integer )
53 #/* and ( cust_pkg.adjourn is null
54 # or cust_pkg.adjourn > $time
55 #-- Should notify suspended ones + cast(part_pkg_option.optionvalue as $integer)
59 $where_pkg .= <<"END";
64 SELECT 1 from cust_pkg_option
65 WHERE cust_pkg.pkgnum = cust_pkg_option.pkgnum
66 AND cust_pkg_option.optionname = 'impending_recur_notification_sent'
67 AND CAST( cust_pkg_option.optionvalue AS $integer ) = 1
73 AND EXISTS ( SELECT 1 from cust_main
74 WHERE cust_pkg.custnum = cust_main.custnum
75 AND cust_main.agentnum IN ( $opt{a} )
82 $where_pkg .= "and ( " . join( "OR ", map { "custnum = $_" } @ARGV) . " )";
85 my $orderby = "order by custnum, bill";
87 my $extra_sql = "$where_pkg $orderby";
89 @cust_pkg = qsearch('cust_pkg', {}, '', $extra_sql );
94 while ( scalar(@cust_pkg) ) {
95 my $cust_main = $cust_pkg[0]->cust_main;
96 my $custnum = $cust_pkg[0]->custnum;
97 warn "working on $custnum" if $DEBUG;
98 while (scalar(@cust_pkg)){
99 last if ($cust_pkg[0]->custnum != $custnum);
100 warn "storing information on " . $cust_pkg[0]->pkgnum if $DEBUG;
101 push @packages, $cust_pkg[0]->part_pkg->pkg;
102 push @recurdates, $cust_pkg[0]->bill;
103 push @cust_pkgs, $cust_pkg[0];
106 my $msgnum = $conf->config('impending_recur_msgnum',$cust_main->agentnum);
108 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
109 $cust_main->setfield('packages', \\@packages);
110 $cust_main->setfield('recurdates', \\@recurdates);
111 $error = $msg_template->send('cust_main' => $cust_main,
112 'object' => $cust_main);
115 $error = $cust_main->notify( 'impending_recur_template',
116 'extra_fields' => { 'packages' => \@packages,
117 'recurdates' => \@recurdates,
118 'package' => $packages[0],
119 'recurdate' => $recurdates[0],
123 warn "Error notifying, custnum ". $cust_main->custnum. ": $error" if $error;
126 local $SIG{HUP} = 'IGNORE';
127 local $SIG{INT} = 'IGNORE';
128 local $SIG{QUIT} = 'IGNORE';
129 local $SIG{TERM} = 'IGNORE';
130 local $SIG{TSTP} = 'IGNORE';
132 my $oldAutoCommit = $FS::UID::AutoCommit;
133 local $FS::UID::AutoCommit = 0;
137 my %options = ($_->options, 'impending_recur_notification_sent' => 1 );
138 $error = $_->replace( $_, options => \%options );
140 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
141 die "Error updating package options for customer". $cust_main->custnum.
142 ": $error" if $error;
146 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
156 dbh->commit or die dbh->errstr if $oldAutoCommit;