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)
45 /* and ( cust_pkg.adjourn is null
46 or cust_pkg.adjourn > $time
47 -- Should notify suspended ones + cast(part_pkg_option.optionvalue as integer)
54 0 = ( select count(*) from cust_pkg_option
55 where cust_pkg.pkgnum = cust_pkg_option.pkgnum
56 and cust_pkg_option.optionname = 'impending_recur_notification_sent'
57 and cust_pkg_option.optionvalue = 1
63 and 0 < ( select count(*) from cust_main
64 where cust_pkg.custnum = cust_main.custnum
65 and cust_main.agentnum = $opt{a}
72 $where_pkg .= "and ( " . join( "OR ", map { "custnum = $_" } @ARGV) . " )";
75 my $orderby = "order by custnum, bill";
77 my $extra_sql = "$where_pkg $orderby";
79 @cust_pkg = qsearch('cust_pkg', {}, '', $extra_sql );
84 while ( scalar(@cust_pkg) ) {
85 my $cust_main = $cust_pkg[0]->cust_main;
86 my $custnum = $cust_pkg[0]->custnum;
87 warn "working on $custnum" if $DEBUG;
88 while (scalar(@cust_pkg)){
89 last if ($cust_pkg[0]->custnum != $custnum);
90 warn "storing information on " . $cust_pkg[0]->pkgnum if $DEBUG;
91 push @packages, $cust_pkg[0]->part_pkg->pkg;
92 push @recurdates, $cust_pkg[0]->bill;
93 push @cust_pkgs, $cust_pkg[0];
97 $cust_main->notify( 'impending_recur_template',
98 'extra_fields' => { 'packages' => \@packages,
99 'recurdates' => \@recurdates,
100 'package' => $packages[0],
101 'recurdate' => $recurdates[0],
104 warn "Error notifying, custnum ". $cust_main->custnum. ": $error" if $error;
107 local $SIG{HUP} = 'IGNORE';
108 local $SIG{INT} = 'IGNORE';
109 local $SIG{QUIT} = 'IGNORE';
110 local $SIG{TERM} = 'IGNORE';
111 local $SIG{TSTP} = 'IGNORE';
113 my $oldAutoCommit = $FS::UID::AutoCommit;
114 local $FS::UID::AutoCommit = 0;
118 my %options = ($_->options, 'impending_recur_notification_sent' => 1 );
119 $error = $_->replace( $_, options => \%options );
121 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
122 die "Error updating package options for customer". $cust_main->custnum.
123 ": $error" if $error;
127 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
137 dbh->commit or die dbh->errstr if $oldAutoCommit;