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;
61 my( $class, $table, %opt ) = @_;
63 my $active_sql = FS::cust_main->active_sql;
64 $active_sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
66 my $under = $class->condition_sql_option_money('balance');
68 my $age = $class->condition_sql_option_age_from('age', $opt{'time'});
69 my $balance_date_sql = FS::cust_main->balance_date_sql($age);
70 $balance_date_sql =~ s/cust_main.custnum/cust_main.referral_custnum/;
71 my $bal_sql = "$balance_date_sql <= $under";
73 "cust_main.referral_custnum IS NOT NULL
74 AND (". $class->condition_sql_option('active'). " IS NULL OR $active_sql)
75 AND (". $class->condition_sql_option('check_bal'). " IS NULL OR $bal_sql )