Optimize "Customer has a referring customer" condition, RT#74452
[freeside.git] / FS / FS / part_event / Condition.pm
index b394815..9900aca 100644 (file)
@@ -52,6 +52,7 @@ sub eventtable_hashref {
     { 'cust_main'      => 1,
       'cust_bill'      => 1,
       'cust_pkg'       => 1,
+      'cust_pay'       => 1,
       'cust_pay_batch' => 1,
       'cust_statement' => 1,
       'svc_acct'       => 1,
@@ -311,7 +312,7 @@ sub option_age_from {
   } elsif ( $age =~ /^(\d+)d$/i ) {
     $mday -= $1;
   } elsif ( $age =~ /^(\d+)h$/i ) {
-    $hour -= $hour;
+    $hour -= $1;
   } else {
     die "unparsable age: $age";
   }
@@ -363,7 +364,7 @@ sub condition_sql_option_option {
 #used for part_event/Condition/cust_bill_has_service.pm and has_cust_tag.pm
 #a little false laziness w/above and condition_sql_option_integer
 sub condition_sql_option_option_integer {
-  my( $class, $option, $driver_name ) = @_;
+  my( $class, $option ) = @_;
 
   ( my $condname = $class ) =~ s/^.*:://;
 
@@ -375,7 +376,7 @@ sub condition_sql_option_option_integer {
           AND part_event_condition_option.optionvalue = 'HASH'
      )";
 
-  my $integer = ($driver_name =~ /^mysql/) ? 'UNSIGNED INTEGER' : 'INTEGER';
+  my $integer = (driver_name =~ /^mysql/) ? 'UNSIGNED INTEGER' : 'INTEGER';
 
   my $optionname = "CAST(optionname AS $integer)";
 
@@ -524,7 +525,7 @@ comparison to other integers is type-correct.
 sub condition_sql_option_integer {
   my ($class, $option, $driver_name) = @_;
 
-  my $integer = ($driver_name =~ /^mysql/) ? 'UNSIGNED INTEGER' : 'INTEGER';
+  my $integer = (driver_name() =~ /^mysql/) ? 'UNSIGNED INTEGER' : 'INTEGER';
 
   'CAST(
          COALESCE('. $class->condition_sql_option($option).
@@ -532,6 +533,22 @@ sub condition_sql_option_integer {
        " AS $integer )";
 }
 
+=item condition_sql_option_money OPTION
+
+As I<condition_sql_option>, but cast the option value to DECIMAL so that
+comparison to other monetary values is type-correct.
+
+=cut
+
+sub condition_sql_option_money {
+  my ($class, $option ) = @_;
+
+  'CAST(
+         COALESCE('. $class->condition_sql_option($option).
+                " ,'0') ".
+       " AS DECIMAL(10,2) )";
+}
+
 =head1 NEW CONDITION CLASSES
 
 A module should be added in FS/FS/part_event/Condition/ which implements the