s/referral/advertising source/
[freeside.git] / FS / FS / cust_main.pm
index 67b426b..b59b0d1 100644 (file)
@@ -99,7 +99,7 @@ FS::Record.  The following fields are currently supported:
 
 =item agentnum - agent (see L<FS::agent>)
 
-=item refnum - referral (see L<FS::part_referral>)
+=item refnum - Advertising source (see L<FS::part_referral>)
 
 =item first - name
 
@@ -512,6 +512,8 @@ and repalce methods.
 sub check {
   my $self = shift;
 
+  #warn "BEFORE: \n". $self->_dump;
+
   my $error =
     $self->ut_numbern('custnum')
     || $self->ut_number('agentnum')
@@ -529,14 +531,14 @@ sub check {
     || $self->ut_numbern('referral_custnum')
   ;
   #barf.  need message catalogs.  i18n.  etc.
-  $error .= "Please select a referral."
+  $error .= "Please select a advertising source."
     if $error =~ /^Illegal or empty \(numeric\) refnum: /;
   return $error if $error;
 
   return "Unknown agent"
     unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
 
-  return "Unknown referral"
+  return "Unknown refnum"
     unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
 
   return "Unknown referring custnum ". $self->referral_custnum
@@ -553,7 +555,9 @@ sub check {
     $self->ss("$1-$2-$3");
   }
 
-  unless ( $import ) {
+
+# bad idea to disable, causes billing to fail because of no tax rates later
+#  unless ( $import ) {
     unless ( qsearchs('cust_main_county', {
       'country' => $self->country,
       'state'   => '',
@@ -566,7 +570,7 @@ sub check {
           'country' => $self->country,
         } );
     }
-  }
+#  }
 
   $error =
     $self->ut_phonen('daytime', $self->country)
@@ -582,8 +586,9 @@ sub check {
   );
 
   if ( defined $self->dbdef_table->column('ship_last') ) {
-    if ( grep { $self->getfield($_) ne $self->getfield("ship_$_") } @addfields
-         && grep $self->getfield("ship_$_"), grep $_ ne 'state', @addfields
+    if ( scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
+                       @addfields )
+         && scalar ( grep { $self->getfield("ship_$_") ne '' } @addfields )
        )
     {
       my $error =
@@ -694,6 +699,8 @@ sub check {
 
   $self->otaker(getotaker);
 
+  #warn "AFTER: \n". $self->_dump;
+
   ''; #no error
 }
 
@@ -1000,7 +1007,8 @@ sub bill {
         'state'   => $self->state,
         'county'  => $self->county,
         'country' => $self->country,
-    } );
+    } ) or die "fatal: can't find tax rate for state/county/country ".
+               $self->state. "/". $self->county. "/". $self->country. "\n";
     my $tax = sprintf( "%.2f",
       $taxable_charged * ( $cust_main_county->getfield('tax') / 100 )
     );
@@ -1094,7 +1102,7 @@ sub collect {
   my $dbh = dbh;
 
   my $balance = $self->balance;
-  warn "collect: balance $balance" if $Debug;
+  warn "collect customer". $self->custnum. ": balance $balance" if $Debug;
   unless ( $balance > 0 ) { #redundant?????
     $dbh->rollback if $oldAutoCommit; #hmm
     return '';
@@ -1127,45 +1135,54 @@ sub collect {
         grep { $_->seconds <= ( $invoice_time - $cust_bill->_date )
                && ! qsearchs( 'cust_bill_event', {
                                 'invnum'    => $cust_bill->invnum,
-                                'eventpart' => $_->eventpart       } )
+                                'eventpart' => $_->eventpart,
+                                'status'    => 'done',
+                                                                   } )
              }
           qsearch('part_bill_event', { 'payby'    => $self->payby,
                                        'disabled' => '',           } )
     ) {
-      #run callback
+      warn "calling invoice event (". $part_bill_event->eventcode. ")\n"
+        if $Debug;
       my $cust_main = $self; #for callback
       my $error = eval $part_bill_event->eventcode;
 
-      if ( $error ) {
-
-        warn "Error running invoice event (". $part_bill_event->eventcode.
-             "): $error";
-
+      my $status = '';
+      my $statustext = '';
+      if ( $@ ) {
+        $status = 'failed';
+        $statustext = $@;
+      } elsif ( $error ) {
+        $status = 'done';
+        $statustext = $error;
       } else {
+        $status = 'done'
+      }
 
-        #add cust_bill_event
-        my $cust_bill_event = new FS::cust_bill_event {
-          'invnum'    => $cust_bill->invnum,
-          'eventpart' => $part_bill_event->eventpart,
-          '_date'     => $invoice_time,
-        };
-        $cust_bill_event->insert;
-        if ( $error ) {
-          #$dbh->rollback if $oldAutoCommit;
-          #return "error: $error";
-
-          # gah, even with transactions.
-          $dbh->commit if $oldAutoCommit; #well.
-          my $e = 'WARNING: Event run but database not updated - '.
-                  'error inserting cust_bill_event, invnum #'. $cust_bill->invnum.
-                  ', eventpart '. $part_bill_event->eventpart.
-                  ": $error";
-          warn $e;
-          return $e;
-        }
-
+      #add cust_bill_event
+      my $cust_bill_event = new FS::cust_bill_event {
+        'invnum'     => $cust_bill->invnum,
+        'eventpart'  => $part_bill_event->eventpart,
+        '_date'      => $invoice_time,
+        'status'     => $status,
+        'statustext' => $statustext,
+      };
+      $error = $cust_bill_event->insert;
+      if ( $error ) {
+        #$dbh->rollback if $oldAutoCommit;
+        #return "error: $error";
+
+        # gah, even with transactions.
+        $dbh->commit if $oldAutoCommit; #well.
+        my $e = 'WARNING: Event run but database not updated - '.
+                'error inserting cust_bill_event, invnum #'. $cust_bill->invnum.
+                ', eventpart '. $part_bill_event->eventpart.
+                ": $error";
+        warn $e;
+        return $e;
       }
 
+
     }
 
   }