Merge branch 'FREESIDE_3_BRANCH' of ssh://git.freeside.biz/home/git/freeside into...
authorChristopher Burger <burgerc@freeside.biz>
Thu, 13 Sep 2018 21:56:51 +0000 (17:56 -0400)
committerChristopher Burger <burgerc@freeside.biz>
Thu, 13 Sep 2018 21:56:51 +0000 (17:56 -0400)
FS/FS/part_event/Condition.pm
FS/FS/part_event/Condition/has_referral_custnum.pm
FS/FS/part_event/Condition/pkg_pkgpart.pm

index d1d5196..9900aca 100644 (file)
@@ -533,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
index f8a2b82..a56e3fa 100644 (file)
@@ -57,16 +57,23 @@ sub condition {
 
 }
 
-#this is incomplete wrt checking referring customer balances, but that's okay.
-# false positives are acceptable here, its just an optimizaiton
 sub condition_sql {
-  my( $class, $table ) = @_;
+  my( $class, $table, %opt ) = @_;
 
-  my $sql = FS::cust_main->active_sql;
-  $sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
-  $sql = 'cust_main.referral_custnum IS NOT NULL AND ('.
-          $class->condition_sql_option('active') . ' IS NULL OR '.$sql.')';
-  return $sql;
+  my $active_sql = FS::cust_main->active_sql;
+  $active_sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
+
+  my $under = $class->condition_sql_option_money('balance');
+
+  my $age = $class->condition_sql_option_age_from('age', $opt{'time'});
+  my $balance_date_sql = FS::cust_main->balance_date_sql($age);
+  $balance_date_sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
+  my $bal_sql = "$balance_date_sql <= $under";
+
+  "cust_main.referral_custnum IS NOT NULL
+    AND (". $class->condition_sql_option('active').    " IS NULL OR $active_sql)
+    AND (". $class->condition_sql_option('check_bal'). " IS NULL OR $bal_sql   )
+  ";
 }
 
 1;
index 6adef8e..d5b5c8d 100644 (file)
@@ -25,15 +25,16 @@ sub option_fields {
 sub condition {
   my( $self, $cust_pkg) = @_;
 
-  #XXX test
   my $if_pkgpart = $self->option('if_pkgpart') || {};
   $if_pkgpart->{ $cust_pkg->pkgpart };
 
 }
 
-#XXX 
-#sub condition_sql {
-#
-#}
+sub condition_sql {
+  my( $self, $table ) = @_;
+  
+  'cust_pkg.pkgpart IN '.
+    $self->condition_sql_option_option_integer('if_pkgpart');
+}
 
 1;