search services by tower/sector, #15950
[freeside.git] / FS / FS / cust_event.pm
index 10fb0ac..30dea6e 100644 (file)
@@ -1,17 +1,16 @@
 package FS::cust_event;
 
 use strict;
+use base qw( FS::cust_main_Mixin FS::Record );
 use vars qw( @ISA $DEBUG $me );
 use Carp qw( croak confess );
 use FS::Record qw( qsearch qsearchs dbdef );
-use FS::cust_main_Mixin;
 use FS::part_event;
 #for cust_X
 use FS::cust_main;
 use FS::cust_pkg;
 use FS::cust_bill;
-
-@ISA = qw(FS::cust_main_Mixin FS::Record);
+use FS::svc_acct;
 
 $DEBUG = 0;
 $me = '[FS::cust_event]';
@@ -140,7 +139,7 @@ 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')
   ;
   return $error if $error;
@@ -192,6 +191,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;
@@ -214,6 +218,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;
 
@@ -224,13 +230,10 @@ sub do_event {
        " (". $part_event->action. ") $for\n"
     if $DEBUG;
 
-  my $oldAutoCommit = $FS::UID::AutoCommit;
-  local $FS::UID::AutoCommit = 0;
-
   my $error;
   {
     local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
-    $error = eval { $part_event->do_action($object); };
+    $error = eval { $part_event->do_action($object, $self); };
   }
 
   my $status = '';
@@ -247,7 +250,7 @@ sub do_event {
   }
 
   #replace or add myself
-  $self->_date(time);
+  $self->_date($time);
   $self->status($status);
   $self->statustext($statustext);
 
@@ -296,7 +299,7 @@ sub retriable {
   $self->replace($old);
 }
 
-=item join_cust_sql
+=item join_sql
 
 =cut
 
@@ -307,24 +310,44 @@ 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_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 = 'svc_acct'  AND cust_pkg_for_svc.custnum  = cust_main.custnum )
                          )
   ";
 
 }
 
-=item search_sql HASHREF
+=item search_sql_where HASHREF
 
 Class method which returns an SQL WHERE fragment to search for parameters
 specified in HASHREF.  Valid parameters are
 
 =over 4
 
-=item
+=item agentnum
+
+=item custnum
+
+=item invnum
+
+=item pkgnum
+
+=item svcnum
+
+=item failed
 
-=item
+=item beginning
+
+=item ending
+
+=item payby
+
+=item 
 
 =back
 
@@ -333,19 +356,22 @@ specified in HASHREF.  Valid parameters are
 #Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
 #sub 
 
-sub search_sql {
+sub search_sql_where {
   my($class, $param) = @_;
   if ( $DEBUG ) {
-    warn "$me search_sql called with params: \n".
+    warn "$me search_sql_where called with params: \n".
          join("\n", map { "  $_: ". $param->{$_} } keys %$param ). "\n";
   }
 
-  my @search = ();
+  my @search = $class->cust_search_sql($param);
 
-  if ( $param->{'agentnum'} && $param->{'agentnum'} =~ /^(\d+)$/ ) {
-    push @search, "cust_main.agentnum = $1";
-    #my $agent = qsearchs('agent', { 'agentnum' => $1 } );
-    #die "unknown agentnum $1" unless $agent;
+  #eventpart
+  my @eventpart = ref($param->{'eventpart'})
+                    ? @{ $param->{'eventpart'} }
+                    : split(',', $param->{'eventpart'});
+  @eventpart = grep /^(\d+)$/, @eventpart;
+  if ( @eventpart ) {
+    push @search, 'eventpart IN ('. join(',', @eventpart). ')';
   }
 
   if ( $param->{'beginning'} =~ /^(\d+)$/ ) {
@@ -361,10 +387,6 @@ sub search_sql {
                   "statustext != 'N/A'";
   }
 
-  #if ( $param->{'part_event.payby'} =~ /^(\w+)$/ ) {
-  #  push @search, "part_event.payby = '$1'";
-  #}
-
   if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
     push @search, "cust_main.custnum = '$1'";
   }
@@ -379,13 +401,13 @@ sub search_sql {
                   "tablenum = '$1'";
   }
 
-  #here is the agent virtualization
-  push @search,
-    $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
+  if ( $param->{'svcnum'} =~ /^(\d+)$/ ) {
+    push @search, "part_event.eventtable = 'svc_acct'",
+                  "tablenum = '$1'";
+  }
 
   my $where = 'WHERE '. join(' AND ', @search );
 
-
   join(' AND ', @search );
 
 }
@@ -440,7 +462,7 @@ sub process_re_X {
 sub re_X {
   my($method, $param, $job) = @_;
 
-  my $search_sql = FS::cust_event->search_sql($param);
+  my $search_sql = FS::cust_event->search_sql_where($param);
 
   #maybe not...?  we do want the "re-" action to match the search more closely
   #            # yuck!  hardcoded *AND* sequential scans!