1 package FS::Cron::notify;
4 use vars qw( @ISA @EXPORT_OK $DEBUG );
7 use FS::Record qw(qsearch);
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).
25 # select * from cust_pkg where
26 my $where_pkg = <<"END";
27 where ( cancel is null or cancel = 0 )
30 0 < ( select count(*) from part_pkg
31 where cust_pkg.pkgpart = part_pkg.pkgpart
32 and part_pkg.plan = 'flat_delayed'
33 and 0 < ( select count (*) from part_pkg_option
34 where part_pkg.pkgpart = part_pkg_option.pkgpart
35 and part_pkg_option.optionname = 'recur_notify'
36 and part_pkg_option.optionvalue > 0
38 + cast(part_pkg_option.optionvalue as integer)
41 and ( cust_pkg.expire is null
42 or cust_pkg.expire > $time
43 + cast(part_pkg_option.optionvalue as integer)
49 0 = ( select count(*) from cust_pkg_option
50 where cust_pkg.pkgnum = cust_pkg_option.pkgnum
51 and cust_pkg_option.optionname = 'impending_recur_notification_sent'
52 and cust_pkg_option.optionvalue = 1
58 and 0 < ( select count(*) from cust_main
59 where cust_pkg.custnum = cust_main.custnum
60 and cust_main.agentnum = $opt{a}
67 $where_pkg .= "and ( " . join( "OR ", map { "custnum = $_" } @ARGV) . " )";
70 my $orderby = "order by custnum, bill";
72 my $extra_sql = "$where_pkg $orderby";
74 @cust_pkg = qsearch('cust_pkg', {}, '', $extra_sql );
79 while ( scalar(@cust_pkg) ) {
80 my $cust_main = $cust_pkg[0]->cust_main;
81 my $custnum = $cust_pkg[0]->custnum;
82 warn "working on $custnum" if $DEBUG;
83 while (scalar(@cust_pkg)){
84 last if ($cust_pkg[0]->custnum != $custnum);
85 warn "storing information on " . $cust_pkg[0]->pkgnum if $DEBUG;
86 push @packages, $cust_pkg[0]->part_pkg->pkg;
87 push @recurdates, $cust_pkg[0]->bill;
88 push @cust_pkgs, $cust_pkg[0];
92 $cust_main->notify( 'impending_recur_template',
93 'extra_fields' => { 'packages' => \@packages,
94 'recurdates' => \@recurdates,
97 warn "Error notifying, custnum ". $cust_main->custnum. ": $error" if $error;
100 local $SIG{HUP} = 'IGNORE';
101 local $SIG{INT} = 'IGNORE';
102 local $SIG{QUIT} = 'IGNORE';
103 local $SIG{TERM} = 'IGNORE';
104 local $SIG{TSTP} = 'IGNORE';
106 my $oldAutoCommit = $FS::UID::AutoCommit;
107 local $FS::UID::AutoCommit = 0;
111 my %options = ($_->options, 'impending_recur_notification_sent' => 1 );
112 $error = $_->replace( $_, options => \%options );
114 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
115 die "Error updating package options for customer". $cust_main->custnum.
116 ": $error" if $error;
120 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
130 dbh->commit or die dbh->errstr if $oldAutoCommit;