X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_event%2FCondition%2Fhas_referral_custnum.pm;h=007ce4548d09c67d4a7aea436023d93d58c59f96;hb=dfe18a92acfb091dca8fdd79e329616bfc1af323;hp=70c9c7f8b20ba3334decd2e2fca0c614618a2855;hpb=0fb307c305e4bc2c9c27dc25a3308beae3a4d33c;p=freeside.git diff --git a/FS/FS/part_event/Condition/has_referral_custnum.pm b/FS/FS/part_event/Condition/has_referral_custnum.pm index 70c9c7f8b..007ce4548 100644 --- a/FS/FS/part_event/Condition/has_referral_custnum.pm +++ b/FS/FS/part_event/Condition/has_referral_custnum.pm @@ -13,37 +13,65 @@ sub option_fields { 'type' => 'checkbox', 'value' => 'Y', }, + 'check_bal' => { 'label' => 'Check referring customer balance', + 'type' => 'checkbox', + 'value' => 'Y', + }, + 'balance' => { 'label' => 'Referring customer balance under (or equal to)', + 'type' => 'money', + 'value' => '0.00', #default + }, + 'age' => { 'label' => 'Referring customer balance age', + 'type' => 'freq', + }, ); } sub condition { - my($self, $object) = @_; + my($self, $object, %opt) = @_; my $cust_main = $self->cust_main($object); + return 0 unless $cust_main; #sanity check + return 0 unless $cust_main->referral_custnum; - if ( $self->option('active') ) { + my $referring_cust_main = $cust_main->referral_custnum_cust_main; + return 0 unless $referring_cust_main; #sanity check; - return 0 unless $cust_main->referral_custnum; + #referring customer must sign up before referred customer + return 0 unless $cust_main->signupdate > $referring_cust_main->signupdate; + if ( $self->option('active') ) { #check for no cust_main for referral_custnum? (deleted?) + return 0 unless $referring_cust_main->status eq 'active'; + } - $cust_main->referral_custnum_cust_main->status eq 'active'; + return 1 unless $self->option('check_bal'); - } else { + #false laziness w/ balance_age_under + my $under = $self->option('balance'); + $under = 0 unless length($under); - $cust_main->referral_custnum; # ? 1 : 0; + my $age = $self->option_age_from('age', $opt{'time'} ); - } + $referring_cust_main->balance_date($age) <= $under; } sub condition_sql { - my( $class, $table ) = @_; + my( $class, $table, %opt ) = @_; + + 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/; + $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 $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; }