summaryrefslogtreecommitdiff
path: root/FS
diff options
context:
space:
mode:
authorIvan Kohler <ivan@freeside.biz>2018-09-13 13:02:25 -0700
committerIvan Kohler <ivan@freeside.biz>2018-09-13 13:10:14 -0700
commita0492f2b5d28bf1012d0b47c25d20891d58c4ca6 (patch)
tree2e84d0c8cf312cf2015bbb9d70cd69ff79a0de65 /FS
parent19bdea6731d4edb5fe419a9db657d0ce790376d4 (diff)
Optimize "Customer has a referring customer" condition, RT#74452
Diffstat (limited to 'FS')
-rw-r--r--FS/FS/part_event/Condition.pm16
-rw-r--r--FS/FS/part_event/Condition/has_referral_custnum.pm21
2 files changed, 27 insertions, 10 deletions
diff --git a/FS/FS/part_event/Condition.pm b/FS/FS/part_event/Condition.pm
index d1d519683..9900acaa9 100644
--- a/FS/FS/part_event/Condition.pm
+++ b/FS/FS/part_event/Condition.pm
@@ -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
diff --git a/FS/FS/part_event/Condition/has_referral_custnum.pm b/FS/FS/part_event/Condition/has_referral_custnum.pm
index 007ce4548..a56e3faec 100644
--- a/FS/FS/part_event/Condition/has_referral_custnum.pm
+++ b/FS/FS/part_event/Condition/has_referral_custnum.pm
@@ -60,19 +60,20 @@ sub condition {
sub condition_sql {
my( $class, $table, %opt ) = @_;
+ 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_sql = FS::cust_main->balance_sql( $age );
- my $balance_date_sql = FS::cust_main->balance_date_sql;
- my $active_sql = FS::cust_main->active_sql;
- $balance_sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
+ my $balance_date_sql = FS::cust_main->balance_date_sql($age);
$balance_date_sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
- $active_sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
-
- my $sql = "cust_main.referral_custnum IS NOT NULL".
- " AND (".$class->condition_sql_option('active')." IS NULL OR $active_sql)".
- " AND ($balance_date_sql <= $balance_sql)";
+ my $bal_sql = "$balance_date_sql <= $under";
- return $sql;
+ "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;