add cust_event.no_action flag for events that conditionally execute, #36741
[freeside.git] / FS / FS / cust_event.pm
index 1407f43..57fb52e 100644 (file)
@@ -10,6 +10,8 @@ use FS::part_event;
 use FS::cust_main;
 use FS::cust_pkg;
 use FS::cust_bill;
+use FS::cust_pay;
+use FS::svc_acct;
 
 $DEBUG = 0;
 $me = '[FS::cust_event]';
@@ -53,6 +55,13 @@ L<Time::Local> and L<Date::Parse> for conversion functions.
 
 =item statustext - additional status detail (i.e. error or progress message)
 
+=item no_action - 'Y' if the event action wasn't performed. Some actions
+contain an internal check to see if the action is going to be impossible (for
+example, emailing a notice to a customer who has no email address), and if so,
+won't attempt the action. It shouldn't be reported as a failure because
+there's no need to retry it. However, the action should set no_action = 'Y'
+so that there's a record.
+
 =back
 
 =head1 METHODS
@@ -138,8 +147,9 @@ sub check {
                               $dbdef_eventtable->primary_key
                             )
     || $self->ut_number('_date')
-    || $self->ut_enum('status', [qw( new locked done failed )])
+    || $self->ut_enum('status', [qw( new locked done failed initial)])
     || $self->ut_anything('statustext')
+    || $self->ut_flag('no_action')
   ;
   return $error if $error;
 
@@ -190,6 +200,11 @@ sub test_conditions {
   my $object = $self->cust_X;
   my @conditions = $part_event->part_event_condition;
   $opt{'cust_event'} = $self;
+  $opt{'time'} = $self->_date
+      or die "test_conditions called without cust_event._date\n";
+    # this MUST be set, or all hell breaks loose in event conditions.
+    # it MUST be in the same time as in the cust_event object, or
+    # future time-dependent events will trigger incorrectly.
 
   #no unsatisfied conditions
   #! grep ! $_->condition( $object, %opt ), @conditions;
@@ -212,6 +227,8 @@ Runs the event action.
 
 sub do_event {
   my $self = shift;
+  my %opt = @_; # currently only 'time'
+  my $time = $opt{'time'} || time;
 
   my $part_event = $self->part_event;
 
@@ -242,7 +259,7 @@ sub do_event {
   }
 
   #replace or add myself
-  $self->_date(time);
+  $self->_date($time);
   $self->status($status);
   $self->statustext($statustext);
 
@@ -291,7 +308,7 @@ sub retriable {
   $self->replace($old);
 }
 
-=item join_cust_sql
+=item join_sql
 
 =cut
 
@@ -302,10 +319,16 @@ sub join_sql {
        JOIN part_event USING ( eventpart )
   LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum  )
   LEFT JOIN cust_pkg  ON ( eventtable = 'cust_pkg'  AND tablenum = pkgnum  )
-  LEFT JOIN cust_main ON (    ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
-                           OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
-                           OR ( eventtable = 'cust_pkg'  AND cust_pkg.custnum  = cust_main.custnum )
-                         )
+  LEFT JOIN cust_pay  ON ( eventtable = 'cust_pay'  AND tablenum = paynum  )
+  LEFT JOIN cust_svc  ON ( eventtable = 'svc_acct'  AND tablenum = svcnum  )
+  LEFT JOIN cust_pkg AS cust_pkg_for_svc ON ( cust_svc.pkgnum = cust_pkg_for_svc.pkgnum )
+  LEFT JOIN cust_main ON (
+       ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
+    OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
+    OR ( eventtable = 'cust_pkg'  AND cust_pkg.custnum  = cust_main.custnum )
+    OR ( eventtable = 'cust_pay'  AND cust_pay.custnum  = cust_main.custnum )
+    OR ( eventtable = 'svc_acct'  AND cust_pkg_for_svc.custnum  = cust_main.custnum )
+  )
   ";
 
 }
@@ -325,6 +348,8 @@ specified in HASHREF.  Valid parameters are
 
 =item pkgnum
 
+=item svcnum
+
 =item failed
 
 =item beginning
@@ -367,11 +392,46 @@ sub search_sql_where {
     push @search, "cust_event._date <= $1";
   }
 
-  if ( $param->{'failed'} ) {
-    push @search, "statustext != ''",
-                  "statustext IS NOT NULL",
-                  "statustext != 'N/A'";
-  }
+  #if ( $param->{'failed'} ) {
+  #  push @search, "statustext != ''",
+  #                "statustext IS NOT NULL",
+  #                "statustext != 'N/A'";
+  #}
+  # huh?
+
+  if ( $param->{'event_status'} ) {
+
+    my @status;
+    my ($done_Y, $done_N);
+    foreach (@{ $param->{'event_status'} }) {
+      if ($_ eq 'done_Y') {
+        $done_Y = 1;
+      } elsif ( $_ eq 'done_N' ) {
+        $done_N = 1;
+      } else {
+        push @status, $_;
+      }
+    }
+    if ( $done_Y or $done_N ) {
+      push @status, 'done';
+    }
+    if ( @status ) {
+      push @search, "cust_event.status IN(" .
+                    join(',', map "'$_'", @status) .
+                    ')';
+    }
+
+    if ( $done_Y and not $done_N ) {
+      push @search, "cust_event.no_action IS NULL";
+    } elsif ( $done_N and not $done_Y ) {
+      push @search, "cust_event.no_action = 'Y'";
+    } # else they're both true, so don't add a constraint, or both false,
+      # and it doesn't matter.
+
+  } # event_status
+
+  # always hide initialization
+  push @search, 'cust_event.status != \'initial\'';
 
   if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
     push @search, "cust_main.custnum = '$1'";
@@ -387,6 +447,16 @@ sub search_sql_where {
                   "tablenum = '$1'";
   }
 
+  if ( $param->{'paynum'} =~ /^(\d+)$/ ) {
+    push @search, "part_event.eventtable = 'cust_pay'",
+                  "tablenum = '$1'";
+  }
+
+  if ( $param->{'svcnum'} =~ /^(\d+)$/ ) {
+    push @search, "part_event.eventtable = 'svc_acct'",
+                  "tablenum = '$1'";
+  }
+
   my $where = 'WHERE '. join(' AND ', @search );
 
   join(' AND ', @search );
@@ -468,9 +538,16 @@ sub re_X {
     my $cust_X = $cust_event->cust_X; # cust_bill
     next unless $cust_X->can($method);
 
-    $cust_X->$method( $cust_event->part_event->templatename
-                      || $cust_X->agent_template
-                    );
+    my $part_event = $cust_event->part_event;
+    my $template = $part_event->templatename
+                   || $cust_X->agent_template;
+    my $modenum = $part_event->option('modenum') || '';
+    my $invoice_from = $part_event->option('agent_invoice_from') || '';
+    $cust_X->set('mode' => $modenum);
+    $cust_X->$method( { template => $template,
+                        modenum  => $modenum,
+                        from     => $invoice_from,
+                    } );
 
     if ( $job ) { #progressbar foo
       $num++;