1 package FS::part_event::Condition::has_referral_custnum;
6 use base qw( FS::part_event::Condition );
8 sub description { 'Customer has a referring customer'; }
12 'active' => { 'label' => 'Referring customer is active',
16 'check_bal' => { 'label' => 'Check referring customer balance',
20 'balance' => { 'label' => 'Referring customer balance under (or equal to)',
22 'value' => '0.00', #default
24 'age' => { 'label' => 'Referring customer balance age',
31 my($self, $object, %opt) = @_;
33 my $cust_main = $self->cust_main($object);
34 return 0 unless $cust_main; #sanity check
35 return 0 unless $cust_main->referral_custnum;
37 my $referring_cust_main = $cust_main->referral_custnum_cust_main;
38 return 0 unless $referring_cust_main; #sanity check;
40 #referring customer must sign up before referred customer
41 return 0 unless $cust_main->signupdate > $referring_cust_main->signupdate;
43 if ( $self->option('active') ) {
44 #check for no cust_main for referral_custnum? (deleted?)
45 return 0 unless $referring_cust_main->status eq 'active';
48 return 1 unless $self->option('check_bal');
50 #false laziness w/ balance_age_under
51 my $under = $self->option('balance');
52 $under = 0 unless length($under);
54 my $age = $self->option_age_from('age', $opt{'time'} );
56 $referring_cust_main->balance_date($age) <= $under;
60 #this is incomplete wrt checking referring customer balances, but that's okay.
61 # false positives are acceptable here, its just an optimizaiton
63 my( $class, $table ) = @_;
65 my $sql = FS::cust_main->active_sql;
66 $sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
67 $sql = 'cust_main.referral_custnum IS NOT NULL AND ('.
68 $class->condition_sql_option('active') . ' IS NULL OR '.$sql.')';