i really hope this finally does it
[freeside.git] / FS / FS / cust_main.pm
index 6512888..a8a9ae4 100644 (file)
@@ -2433,6 +2433,8 @@ Currently available options are:
 
 =item payby - allows for one time override of normal customer billing method
 
+=back
+
 =cut
 
 sub collect {
@@ -2731,6 +2733,13 @@ sub retry_realtime {
 
   my $join = FS::part_event_condition->join_conditions_sql;
   my $order = FS::part_event_condition->order_conditions_sql;
+  my $mine = 
+  '( '
+   . join ( ' OR ' , map { 
+    "( part_event.eventtable = " . dbh->quote($_) 
+    . " AND tablenum IN( SELECT " . dbdef->table($_)->primary_key . " from $_ where custnum = " . dbh->quote( $self->custnum ) . "))" ;
+   } FS::part_event->eventtables)
+   . ') ';
 
   #here is the agent virtualization
   my $agent_virt = " (    part_event.agentnum IS NULL
@@ -2754,7 +2763,7 @@ sub retry_realtime {
     'addl_from' => "LEFT JOIN part_event USING ( eventpart ) $join",
     'hashref'   => { 'status' => 'done' },
     'extra_sql' => " AND statustext IS NOT NULL AND statustext != '' ".
-                   " AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
+                   " AND $mine AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
   });
 
   my %seen_invnum = ();
@@ -5780,13 +5789,14 @@ sub _agent_plandata {
 
   my $part_event_option =
     qsearchs({
+      'select'    => 'part_event_option.*',
       'table'     => 'part_event_option',
       'addl_from' => q{
         LEFT JOIN part_event USING ( eventpart )
         LEFT JOIN part_event_option AS peo_agentnum
           ON ( part_event.eventpart = peo_agentnum.eventpart
                AND peo_agentnum.optionname = 'agentnum'
-               AND peo_agentnum.optionvalue ~ '(^|,)agentnum(,|$)'
+               AND peo_agentnum.optionvalue ~ '(^|,)}. $agentnum. q{(,|$)'
              )
         LEFT JOIN part_event_option AS peo_cust_bill_age
           ON ( part_event.eventpart = peo_cust_bill_age.eventpart
@@ -5794,27 +5804,30 @@ sub _agent_plandata {
              )
       },
       #'hashref'   => { 'optionname' => $option },
-      'hashref'   => { 'part_event_option.optionname' => $option },
-      'extra_sql' => " AND event = 'cust_bill_send_agent' ".
-                     " AND peo_agentnum.optionname = 'agentnum' ".
-                     " AND agentnum IS NULL OR agentnum = $agentnum ".
-                     " ORDER BY
-                        CASE WHEN peo_cust_bill_age.optionname != 'cust_bill_age'
-                        THEN -1
-                        ELSE EXTRACT( EPOCH FROM
-                                        REPLACE( peo_cust_bill_age.optionvalue,
-                                                 'm',
-                                                 'mon'
-                                               )::interval
-                                    )
-                       END
-                       , part_event.weight".
-                     " LIMIT 1"
+      #'hashref'   => { 'part_event_option.optionname' => $option },
+      'extra_sql' =>
+        " WHERE part_event_option.optionname = ". dbh->quote($option).
+        " AND action = 'cust_bill_send_agent' ".
+        " AND ( disabled IS NULL OR disabled != 'Y' ) ".
+        " AND peo_agentnum.optionname = 'agentnum' ".
+        " AND agentnum IS NULL OR agentnum = $agentnum ".
+        " ORDER BY
+           CASE WHEN peo_cust_bill_age.optionname != 'cust_bill_age'
+           THEN -1
+           ELSE EXTRACT( EPOCH FROM
+                           REPLACE( peo_cust_bill_age.optionvalue,
+                                    'm',
+                                    'mon'
+                                  )::interval
+                       )
+          END
+          , part_event.weight".
+        " LIMIT 1"
     });
     
   unless ( $part_event_option ) {
     return $self->agent->invoice_template || ''
-      if $option eq '$agent_templatename';
+      if $option eq 'agent_templatename';
     return '';
   }
 
@@ -5822,6 +5835,16 @@ sub _agent_plandata {
 
 }
 
+sub queued_bill {
+  ## actual sub, not a method, designed to be called from the queue.
+  ## sets up the customer, and calls the bill_and_collect
+  my (%args) = @_; #, ($time, $invoice_time, $check_freq, $resetup) = @_;
+  my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
+      $cust_main->bill_and_collect(
+        %args,
+      );
+}
+
 =back
 
 =head1 BUGS