summaryrefslogtreecommitdiff
path: root/FS/FS/part_event/Condition/has_referral_custnum.pm
blob: 70c9c7f8b20ba3334decd2e2fca0c614618a2855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package FS::part_event::Condition::has_referral_custnum;

use strict;
use FS::cust_main;

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',
                },
  );
}

sub condition {
  my($self, $object) = @_;

  my $cust_main = $self->cust_main($object);

  if ( $self->option('active') ) {

    return 0 unless $cust_main->referral_custnum;

    #check for no cust_main for referral_custnum? (deleted?)

    $cust_main->referral_custnum_cust_main->status eq 'active';

  } else {

    $cust_main->referral_custnum; # ? 1 : 0;

  }

}

sub condition_sql {
  my( $class, $table ) = @_;

  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;