add active option to has_referral_custnum condition, RT#6150
authorivan <ivan>
Fri, 18 Sep 2009 22:41:25 +0000 (22:41 +0000)
committerivan <ivan>
Fri, 18 Sep 2009 22:41:25 +0000 (22:41 +0000)
FS/FS/cust_main.pm
FS/FS/part_event/Condition/has_referral_custnum.pm

index cdfe938..bf95fa9 100644 (file)
@@ -6951,6 +6951,24 @@ sub invoicing_list_emailonly_scalar {
   join(', ', $self->invoicing_list_emailonly);
 }
 
+=item referral_custnum_cust_main
+
+Returns the customer who referred this customer (or the empty string, if
+this customer was not referred).
+
+Note the difference with referral_cust_main method: This method,
+referral_custnum_cust_main returns the single customer (if any) who referred
+this customer, while referral_cust_main returns an array of customers referred
+BY this customer.
+
+=cut
+
+sub referral_custnum_cust_main {
+  my $self = shift;
+  return '' unless $self->referral_custnum;
+  qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
+}
+
 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
 
 Returns an array of customers referred by this customer (referral_custnum set
@@ -6958,6 +6976,11 @@ to this custnum).  If DEPTH is given, recurses up to the given depth, returning
 customers referred by customers referred by this customer and so on, inclusive.
 The default behavior is DEPTH 1 (no recursion).
 
+Note the difference with referral_custnum_cust_main method: This method,
+referral_cust_main, returns an array of customers referred BY this customer,
+while referral_custnum_cust_main returns the single customer (if any) who
+referred this customer.
+
 =cut
 
 sub referral_cust_main {
index d43d6c0..61a8155 100644 (file)
@@ -7,18 +7,42 @@ 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);
 
-  $cust_main->referral_custnum;
+  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 ) = @_;
 
   "cust_main.referral_custnum IS NOT NULL";
+
+  #XXX a bit harder to check active status here
 }
 
 1;