X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_event%2FCondition%2Fhas_referral_custnum.pm;h=f8a2b82eeee13d57d4c48a98d5a95ed041371699;hb=18af4f3316291938fa8f0a74e083209f62eac4fa;hp=d43d6c0c731668dc0c263fdccbf4ee947e7e0f1c;hpb=58f99accce35aa76abe9ff852f6c6ee84e8ce712;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 d43d6c0c7..f8a2b82ee 100644 --- a/FS/FS/part_event/Condition/has_referral_custnum.pm +++ b/FS/FS/part_event/Condition/has_referral_custnum.pm @@ -7,18 +7,66 @@ use base qw( FS::part_event::Condition ); sub description { 'Customer has a referring customer'; } +sub option_fields { + ( + 'active' => { 'label' => 'Referring customer is active', + '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; + + my $referring_cust_main = $cust_main->referral_custnum_cust_main; + return 0 unless $referring_cust_main; #sanity check; + + #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'; + } + + return 1 unless $self->option('check_bal'); + + #false laziness w/ balance_age_under + my $under = $self->option('balance'); + $under = 0 unless length($under); + + my $age = $self->option_age_from('age', $opt{'time'} ); + + $referring_cust_main->balance_date($age) <= $under; - $cust_main->referral_custnum; } +#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 ) = @_; - "cust_main.referral_custnum IS NOT NULL"; + 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; } 1;