enable CardFortress in test database, #71513
[freeside.git] / FS / FS / Cron / notify.pm
1 package FS::Cron::notify;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $DEBUG );
5 use Exporter;
6 use FS::UID qw( dbh driver_name );
7 use FS::Record qw(qsearch qsearchs);
8 use FS::cust_main;
9 use FS::cust_pkg;
10
11 @ISA = qw( Exporter );
12 @EXPORT_OK = qw ( notify_flat_delay );
13 $DEBUG = 0;
14
15 sub notify_flat_delay {
16
17   my %opt = @_;
18
19   my $oldAutoCommit = $FS::UID::AutoCommit;
20   $DEBUG = 1 if $opt{'v'};
21   
22   #we're at now now (and later).
23   my($time) = $^T;
24   my $conf = new FS::Conf;
25   my $error = '';
26
27   my $integer = driver_name =~ /^mysql/ ? 'SIGNED' : 'INTEGER';
28
29   # select * from cust_pkg where
30   my $where_pkg = <<"END";
31     WHERE ( cancel IS NULL OR cancel = 0 )
32       AND ( bill > 0 )
33       AND EXISTS (
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
41                              AND 0 <= ( $time
42                                         + CAST( part_pkg_option.optionvalue AS $integer )
43                                           * 86400
44                                         - cust_pkg.bill
45                                       )
46                              AND (    cust_pkg.expire is null
47                                    OR cust_pkg.expire > ( $time
48                                                           + CAST( part_pkg_option.optionvalue AS $integer )
49                                                             * 86400
50                                                         )
51 END
52
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)
56 #                                          * 86400
57 #*/
58
59   $where_pkg .= <<"END";
60                                  )
61                        )
62       )
63       AND NOT EXISTS (
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
68       )
69 END
70   
71   if ($opt{a}) {
72     $where_pkg .= <<END;
73       AND EXISTS ( SELECT 1 from cust_main
74                      WHERE cust_pkg.custnum = cust_main.custnum
75                        AND cust_main.agentnum IN ( $opt{a} )
76                  )
77 END
78   }
79   
80   my @cust_pkg;
81   if ( @ARGV ) {
82     $where_pkg .= "and ( " . join( "OR ", map { "custnum = $_" } @ARGV) . " )";
83   } 
84
85   my $orderby = "order by custnum, bill";
86
87   my $extra_sql = "$where_pkg $orderby";
88
89   @cust_pkg = qsearch('cust_pkg', {}, '', $extra_sql );
90   
91   my @packages = ();
92   my @recurdates = ();
93   my @cust_pkgs = ();
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];
104       shift @cust_pkg;
105     }
106     my $msgnum = $conf->config('impending_recur_msgnum',$cust_main->agentnum);
107     if ( $msgnum ) {
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);
113     }
114     warn "Error notifying, custnum ". $cust_main->custnum. ": $error" if $error;
115
116     unless ($error) { 
117       local $SIG{HUP} = 'IGNORE';
118       local $SIG{INT} = 'IGNORE';
119       local $SIG{QUIT} = 'IGNORE';
120       local $SIG{TERM} = 'IGNORE';
121       local $SIG{TSTP} = 'IGNORE';
122
123       my $oldAutoCommit = $FS::UID::AutoCommit;
124       local $FS::UID::AutoCommit = 0;
125       my $dbh = dbh;
126
127       for (@cust_pkgs) {
128         my %options = ($_->options,  'impending_recur_notification_sent' => 1 );
129         $error = $_->replace( $_, options => \%options );
130         if ($error){
131           $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
132           die "Error updating package options for customer". $cust_main->custnum.
133                ": $error" if $error;
134         }
135       }
136
137       $dbh->commit or die $dbh->errstr if $oldAutoCommit;
138
139     }
140
141     @packages = ();
142     @recurdates = ();
143     @cust_pkgs = ();
144   
145   }
146
147   dbh->commit or die dbh->errstr if $oldAutoCommit;
148
149 }
150
151 1;