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);
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 my $integer = driver_name =~ /^mysql/ ? 'SIGNED' : 'INTEGER';
27 # select * from cust_pkg where
28 my $where_pkg = <<"END";
29 where ( cancel is null or cancel = 0 )
32 0 < ( select count(*) from part_pkg
33 where cust_pkg.pkgpart = part_pkg.pkgpart
34 and part_pkg.plan = 'flat_delayed'
35 and 0 < ( select count(*) from part_pkg_option
36 where part_pkg.pkgpart = part_pkg_option.pkgpart
37 and part_pkg_option.optionname = 'recur_notify'
38 and CAST( part_pkg_option.optionvalue AS INTEGER ) > 0
40 + CAST( part_pkg_option.optionvalue AS $integer )
44 and ( cust_pkg.expire is null
45 or cust_pkg.expire > ( $time
46 + CAST( part_pkg_option.optionvalue AS $integer )
51 #/* and ( cust_pkg.adjourn is null
52 # or cust_pkg.adjourn > $time
53 #-- Should notify suspended ones + cast(part_pkg_option.optionvalue as $integer)
57 $where_pkg .= <<"END";
62 0 = ( select count(*) from cust_pkg_option
63 where cust_pkg.pkgnum = cust_pkg_option.pkgnum
64 and cust_pkg_option.optionname = 'impending_recur_notification_sent'
65 and CAST( cust_pkg_option.optionvalue AS INTEGER ) = 1
71 and 0 < ( select count(*) from cust_main
72 where cust_pkg.custnum = cust_main.custnum
73 and cust_main.agentnum = $opt{a}
80 $where_pkg .= "and ( " . join( "OR ", map { "custnum = $_" } @ARGV) . " )";
83 my $orderby = "order by custnum, bill";
85 my $extra_sql = "$where_pkg $orderby";
87 @cust_pkg = qsearch('cust_pkg', {}, '', $extra_sql );
92 while ( scalar(@cust_pkg) ) {
93 my $cust_main = $cust_pkg[0]->cust_main;
94 my $custnum = $cust_pkg[0]->custnum;
95 warn "working on $custnum" if $DEBUG;
96 while (scalar(@cust_pkg)){
97 last if ($cust_pkg[0]->custnum != $custnum);
98 warn "storing information on " . $cust_pkg[0]->pkgnum if $DEBUG;
99 push @packages, $cust_pkg[0]->part_pkg->pkg;
100 push @recurdates, $cust_pkg[0]->bill;
101 push @cust_pkgs, $cust_pkg[0];
105 $cust_main->notify( 'impending_recur_template',
106 'extra_fields' => { 'packages' => \@packages,
107 'recurdates' => \@recurdates,
108 'package' => $packages[0],
109 'recurdate' => $recurdates[0],
112 warn "Error notifying, custnum ". $cust_main->custnum. ": $error" if $error;
115 local $SIG{HUP} = 'IGNORE';
116 local $SIG{INT} = 'IGNORE';
117 local $SIG{QUIT} = 'IGNORE';
118 local $SIG{TERM} = 'IGNORE';
119 local $SIG{TSTP} = 'IGNORE';
121 my $oldAutoCommit = $FS::UID::AutoCommit;
122 local $FS::UID::AutoCommit = 0;
126 my %options = ($_->options, 'impending_recur_notification_sent' => 1 );
127 $error = $_->replace( $_, options => \%options );
129 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
130 die "Error updating package options for customer". $cust_main->custnum.
131 ": $error" if $error;
135 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
145 dbh->commit or die dbh->errstr if $oldAutoCommit;