fix up re-email these events
authorjeff <jeff>
Tue, 2 Oct 2007 15:31:35 +0000 (15:31 +0000)
committerjeff <jeff>
Tue, 2 Oct 2007 15:31:35 +0000 (15:31 +0000)
FS/FS/UI/Web.pm
FS/FS/cust_bill_event.pm
httemplate/search/cust_bill_event.cgi

index 7699a7d..29e06bc 100644 (file)
@@ -410,7 +410,7 @@ use Carp;
 use Storable qw(nfreeze);
 use MIME::Base64;
 use JSON;
-use FS::UID;
+use FS::UID qw(getotaker);
 use FS::Record qw(qsearchs);
 use FS::queue;
 
@@ -483,6 +483,7 @@ sub start_job {
       push @{$param{$field}}, $value;
     }
   }
+  $param{CurrentUser} = getotaker();
   warn "FS::UI::Web::start_job\n".
        join('', map {
                       if ( ref($param{$_}) ) {
index 82e22f7..ff383ff 100644 (file)
@@ -189,6 +189,62 @@ sub retriable {
   $self->replace($old);
 }
 
+=item search_sql HREF
+
+Class method which returns an SQL WHERE fragment to search for parameters
+specified in HREF.  Valid parameters are
+
+=over 4
+=item agentnum
+=item beginning - an epoch date setting a lower bound for _date values
+=item ending - an epoch date setting a upper bound for _date values
+=item failed - limits the search to failed events if true
+=item payby - requires that the search be JOIN'd to part_bill_event # Bug?
+=item invnum 
+=item currentuser - specifies the user for agent virtualization
+=back
+
+=cut
+
+sub search_sql {
+  my ($class, $params) = @_;
+  my @search = ();
+
+  push @search, "agentnum = ". $params->{agentnum} if $params->{agentnum};
+
+  push @search, "cust_bill_event._date >= ". $params->{beginning}
+    if $params->{beginning};
+  push @search, "cust_bill_event._date <= ". $params->{ending}
+    if $params->{ending};
+
+  push @search, "statustext != ''",
+                "statustext IS NOT NULL",
+                "statustext != 'N/A'"
+    if $params->{failed};
+
+  push @search, "part_bill_event.payby = '". $params->{payby}. "'"
+    if $params->{payby};
+
+  push @search, "cust_bill_event.invnum = '". $params->{invnum}. "'"
+    if $params->{invnum};
+
+  if ($params->{CurrentUser}) {
+    my $access_user = qsearchs('access_user',
+                              {username => $params->{CurrentUser} }
+                             );
+    if ($access_user) {
+      push @search, $access_user->agentnums_sql;
+    }else{
+      push @search, "1=0";
+    }
+  }else{
+    push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
+  }
+
+  join(' AND ', @search );
+
+}
+
 =back
 
 =head1 SUBROUTINES
@@ -230,24 +286,21 @@ sub process_re_X {
 
   re_X(
     $method,
-    $param->{'beginning'},
-    $param->{'ending'},
-    $param->{'failed'},
+    $param,
     $job,
   );
 
 }
 
 sub re_X {
-  my($method, $beginning, $ending, $failed, $job) = @_;
+  my($method, $param, $job) = @_;
 
-  my $where = " WHERE plan LIKE 'send%'".
-              "   AND cust_bill_event._date >= $beginning".
-              "   AND cust_bill_event._date <= $ending";
-  $where .= " AND statustext != '' AND statustext IS NOT NULL"
-    if $failed;
+  my $where = FS::cust_bill_event->search_sql($param);
+  $where = " WHERE plan LIKE 'send%'". ( $where ? " AND $where" : "" );
 
-  my $from = 'LEFT JOIN part_bill_event USING ( eventpart )';
+  my $from = 'LEFT JOIN part_bill_event USING ( eventpart )'.
+             'LEFT JOIN cust_bill       USING ( invnum )'.
+             'LEFT JOIN cust_main       USING ( custnum )';
 
   my @cust_bill_event = qsearch( 'cust_bill_event', {}, '', $where, '', $from );
 
index ffd95e9..51f9f88 100644 (file)
@@ -75,36 +75,28 @@ my $title = $cgi->param('failed')
               ? 'Failed invoice events'
               : 'Invoice events';
 
-my @search = ();
+my %search = ();
 
 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-  push @search, "agentnum = $1";
-  #my $agent = qsearchs('agent', { 'agentnum' => $1 } );
-  #die "unknown agentnum $1" unless $agent;
+  $search{agentnum} = $1;
 }
 
-my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-push @search, "cust_bill_event._date >= $beginning",
-              "cust_bill_event._date <= $ending";
+($search{beginning}, $search{ending})
+  = FS::UI::Web::parse_beginning_ending($cgi);
 
 if ( $cgi->param('failed') ) {
-  push @search, "statustext != ''",
-                "statustext IS NOT NULL",
-                "statustext != 'N/A'";
+  push $search{failed} = '1';
 }
 
 if ( $cgi->param('part_bill_event.payby') =~ /^(\w+)$/ ) {
-  push @search, "part_bill_event.payby = '$1'";
+  $search{payby} = $1;
 }
 
 if ( $cgi->param('invnum') =~ /^(\d+)$/ ) {
-  push @search, "cust_bill_event.invnum = '$1'";
+  $search{invnum} = $1;
 }
 
-#here is the agent virtualization
-push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
-
-my $where = 'WHERE '. join(' AND ', @search );
+my $where = 'WHERE '. FS::cust_bill_event->search_sql( \%search );
 
 my $join = 'LEFT JOIN part_bill_event USING ( eventpart ) '.
            'LEFT JOIN cust_bill       USING ( invnum    ) '.
@@ -139,16 +131,14 @@ $html_init .= join("\n", map {
   ( my $action = $_ ) =~ s/_$//;
   include('/elements/progress-init.html',
             $_.'form',
-            [ 'action', 'beginning', 'ending', 'failed' ],
+            [ keys(%search) ],
             "../misc/${_}invoice_events.cgi",
             { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
             $_, #key
          ),
   qq!<FORM NAME="${_}form">!,
   qq!<INPUT TYPE="hidden" NAME="action" VALUE="$_">!, #not used though
-  qq!<INPUT TYPE="hidden" NAME="beginning" VALUE="$beginning">!,
-  qq!<INPUT TYPE="hidden" NAME="ending"    VALUE="$ending">!,
-  qq!<INPUT TYPE="hidden" NAME="failed"    VALUE="$failed">!,
+  (map {qq!<INPUT TYPE="hidden" NAME="$_" VALUE="$search{$_}">!} keys(%search)),
   qq!</FORM>!
 } qw( print_ email_ fax_ ) );