turn off debugging
[freeside.git] / FS / FS / cust_main.pm
index d131af9..7866369 100644 (file)
@@ -2,7 +2,7 @@ package FS::cust_main;
 
 use strict;
 use vars qw( @ISA @EXPORT_OK $DEBUG $me $conf @encrypted_fields
-             $import $skip_fuzzyfiles );
+             $import $skip_fuzzyfiles $ignore_expired_card );
 use vars qw( $realtime_bop_decline_quiet ); #ugh
 use Safe;
 use Carp;
@@ -54,11 +54,15 @@ use FS::banned_pay;
 
 $realtime_bop_decline_quiet = 0;
 
+# 1 is mostly method/subroutine entry and options
+# 2 traces progress of some operations
+# 3 is even more information including possibly sensitive data
 $DEBUG = 0;
 $me = '[FS::cust_main]';
 
 $import = 0;
 $skip_fuzzyfiles = 0;
+$ignore_expired_card = 0;
 
 @encrypted_fields = ('payinfo', 'paycvv');
 
@@ -343,7 +347,7 @@ sub insert {
   my $cust_pkgs = @_ ? shift : {};
   my $invoicing_list = @_ ? shift : '';
   my %options = @_;
-  warn "FS::cust_main::insert called with options ".
+  warn "$me insert called with options ".
        join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
     if $DEBUG;
 
@@ -367,6 +371,9 @@ sub insert {
     $prepay_identifier = $self->payinfo;
     $self->payinfo('');
 
+    warn "  looking up prepaid card $prepay_identifier\n"
+      if $DEBUG > 1;
+
     my $error = $self->get_prepay($prepay_identifier, \$amount, \$seconds);
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
@@ -376,7 +383,7 @@ sub insert {
 
     $payby = 'PREP' if $amount;
 
-  } elsif ( $self->payby =~ /^(CASH|WEST)$/ ) {
+  } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) {
 
     $payby = $1;
     $self->payby('BILL');
@@ -384,6 +391,9 @@ sub insert {
 
   }
 
+  warn "  inserting $self\n"
+    if $DEBUG > 1;
+
   my $error = $self->SUPER::insert;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
@@ -391,7 +401,9 @@ sub insert {
     return $error;
   }
 
-  # invoicing list
+  warn "  setting invoicing list\n"
+    if $DEBUG > 1;
+
   if ( $invoicing_list ) {
     $error = $self->check_invoicing_list( $invoicing_list );
     if ( $error ) {
@@ -401,7 +413,9 @@ sub insert {
     $self->invoicing_list( $invoicing_list );
   }
 
-  # packages
+  warn "  ordering packages\n"
+    if $DEBUG > 1;
+
   $error = $self->order_pkgs($cust_pkgs, \$seconds, %options);
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
@@ -414,6 +428,8 @@ sub insert {
   }
 
   if ( $amount ) {
+    warn "  inserting initial $payby payment of $amount\n"
+      if $DEBUG > 1;
     $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
@@ -421,9 +437,9 @@ sub insert {
     }
   }
 
-
-
   unless ( $import || $skip_fuzzyfiles ) {
+    warn "  queueing fuzzyfiles update\n"
+      if $DEBUG > 1;
     $error = $self->queue_fuzzyfiles_update;
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
@@ -431,6 +447,9 @@ sub insert {
     }
   }
 
+  warn "  insert complete; committing transaction\n"
+    if $DEBUG > 1;
+
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
   '';
 
@@ -477,7 +496,7 @@ sub order_pkgs {
   my %svc_options = ();
   $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
     if exists $options{'depend_jobnum'};
-  warn "FS::cust_main::order_pkgs called with options ".
+  warn "$me order_pkgs called with options ".
        join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
     if $DEBUG;
 
@@ -673,7 +692,7 @@ sub increment_seconds {
 
   my $cust_pkg = $cust_pkg[0];
   warn "  found package pkgnum ". $cust_pkg->pkgnum. "\n"
-    if $DEBUG;
+    if $DEBUG > 1;
 
   my @cust_svc =
     $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
@@ -687,7 +706,7 @@ sub increment_seconds {
   my $svc_acct = $cust_svc[0]->svc_x;
   warn "  found service svcnum ". $svc_acct->pkgnum.
        ' ('. $svc_acct->email. ")\n"
-    if $DEBUG;
+    if $DEBUG > 1;
 
   $svc_acct->increment_seconds($seconds);
 
@@ -717,7 +736,7 @@ sub insert_cust_pay_cash {
   shift->insert_cust_pay('CASH', @_);
 }
 
-=item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
+=item insert_cust_pay_west AMOUNT [ PAYINFO ]
 
 Inserts a Western Union payment in the specified amount for this customer.  An
 optional second argument can specify the prepayment identifier for tracking
@@ -758,7 +777,7 @@ otherwise returns false.
 sub reexport {
   my $self = shift;
 
-  carp "warning: FS::cust_main::reexport is deprectated; ".
+  carp "WARNING: FS::cust_main::reexport is deprectated; ".
        "use the depend_jobnum option to insert or order_pkgs to delay export";
 
   local $SIG{HUP} = 'IGNORE';
@@ -926,6 +945,11 @@ sub replace {
       unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
   }
 
+  local($ignore_expired_card) = 1
+    if $old->payby  =~ /^(CARD|DCRD)$/
+    && $self->payby =~ /^(CARD|DCRD)$/
+    && $old->payinfo eq $self->payinfo;
+
   my $oldAutoCommit = $FS::UID::AutoCommit;
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
@@ -1022,7 +1046,8 @@ and replace methods.
 sub check {
   my $self = shift;
 
-  #warn "BEFORE: \n". $self->_dump;
+  warn "$me check BEFORE: \n". $self->_dump
+    if $DEBUG > 2;
 
   my $error =
     $self->ut_numbern('custnum')
@@ -1144,7 +1169,7 @@ sub check {
     }
   }
 
-  $self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST)$/
+  $self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
     or return "Illegal payby: ". $self->payby;
 
   $error =    $self->ut_numbern('paystart_month')
@@ -1281,7 +1306,7 @@ sub check {
 
   if ( $self->paydate eq '' || $self->paydate eq '-' ) {
     return "Expriation date required"
-      unless $self->payby =~ /^(BILL|PREPAY|CHEK|LECB|CASH|WEST)$/;
+      unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
     $self->paydate('');
   } else {
     my( $m, $y );
@@ -1295,7 +1320,9 @@ sub check {
     $self->paydate("$y-$m-01");
     my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
     return gettext('expired_card')
-      if !$import && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
+      if !$import
+      && !$ignore_expired_card 
+      && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
   }
 
   if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
@@ -1314,7 +1341,8 @@ sub check {
 
   $self->otaker(getotaker) unless $self->otaker;
 
-  #warn "AFTER: \n". $self->_dump;
+  warn "$me check AFTER: \n". $self->_dump
+    if $DEBUG > 2;
 
   $self->SUPER::check;
 }
@@ -1565,7 +1593,8 @@ If there is an error, returns the error, otherwise returns false.
 sub bill {
   my( $self, %options ) = @_;
   return '' if $self->payby eq 'COMP';
-  warn "bill customer ". $self->custnum. "\n" if $DEBUG;
+  warn "$me bill customer ". $self->custnum. "\n"
+    if $DEBUG;
 
   my $time = $options{'time'} || time;
 
@@ -1604,7 +1633,7 @@ sub bill {
     #NO!! next if $cust_pkg->cancel;  
     next if $cust_pkg->getfield('cancel');  
 
-    warn "  bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG;
+    warn "  bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
 
     #? to avoid use of uninitialized value errors... ?
     $cust_pkg->setfield('bill', '')
@@ -1621,7 +1650,7 @@ sub bill {
     my $setup = 0;
     if ( !$cust_pkg->setup || $options{'resetup'} ) {
     
-      warn "    bill setup\n" if $DEBUG;
+      warn "    bill setup\n" if $DEBUG > 1;
 
       $setup = eval { $cust_pkg->calc_setup( $time ) };
       if ( $@ ) {
@@ -1640,7 +1669,7 @@ sub bill {
          ( $cust_pkg->getfield('bill') || 0 ) <= $time
     ) {
 
-      warn "    bill recur\n" if $DEBUG;
+      warn "    bill recur\n" if $DEBUG > 1;
 
       # XXX shared with $recur_prog
       $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
@@ -1689,7 +1718,8 @@ sub bill {
 
     if ( $cust_pkg->modified ) {
 
-      warn "  package ". $cust_pkg->pkgnum. " modified; updating\n" if $DEBUG;
+      warn "  package ". $cust_pkg->pkgnum. " modified; updating\n"
+        if $DEBUG >1;
 
       $error=$cust_pkg->replace($old_cust_pkg);
       if ( $error ) { #just in case
@@ -1709,7 +1739,7 @@ sub bill {
       }
       if ( $setup != 0 || $recur != 0 ) {
         warn "    charges (setup=$setup, recur=$recur); queueing line items\n"
-          if $DEBUG;
+          if $DEBUG > 1;
         my $cust_bill_pkg = new FS::cust_bill_pkg ({
           'pkgnum'  => $cust_pkg->pkgnum,
           'setup'   => $setup,
@@ -1976,7 +2006,8 @@ sub collect {
   $self->select_for_update; #mutex
 
   my $balance = $self->balance;
-  warn "collect customer ". $self->custnum. ": balance $balance\n" if $DEBUG;
+  warn "$me collect customer ". $self->custnum. ": balance $balance\n"
+    if $DEBUG;
   unless ( $balance > 0 ) { #redundant?????
     $dbh->rollback if $oldAutoCommit; #hmm
     return '';
@@ -2001,8 +2032,8 @@ sub collect {
 
     last if $self->balance <= 0;
 
-    warn "invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ")\n"
-      if $DEBUG;
+    warn "  invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ")\n"
+      if $DEBUG > 1;
 
     foreach my $part_bill_event (
       sort {    $a->seconds   <=> $b->seconds
@@ -2022,8 +2053,8 @@ sub collect {
       last if $cust_bill->owed <= 0  # don't run subsequent events if owed<=0
            || $self->balance   <= 0; # or if balance<=0
 
-      warn "calling invoice event (". $part_bill_event->eventcode. ")\n"
-        if $DEBUG;
+      warn "  calling invoice event (". $part_bill_event->eventcode. ")\n"
+        if $DEBUG > 1;
       my $cust_main = $self; #for callback
 
       my $error;
@@ -2163,7 +2194,7 @@ I<quiet> can be set true to surpress email decline notices.
 sub realtime_bop {
   my( $self, $method, $amount, %options ) = @_;
   if ( $DEBUG ) {
-    warn "$self $method $amount\n";
+    warn "$me realtime_bop: $method $amount\n";
     warn "  $_ => $options{$_}\n" foreach keys %options;
   }
 
@@ -2275,6 +2306,13 @@ sub realtime_bop {
               : $invoicing_list[0];
 
   my %content = ();
+
+  my $payip = exists($options{'payip'})
+                ? $options{'payip'}
+                : $self->payip;
+  $content{customer_ip} = $payip
+    if length($payip);
+
   if ( $method eq 'CC' ) { 
 
     $content{card_number} = $payinfo;
@@ -2306,12 +2344,6 @@ sub realtime_bop {
                            : $self->payissue;
     $content{issue_number} = $payissue if $payissue;
 
-    my $payip          = exists($options{'payip'})
-                           ? $options{'payip'}
-                           : $self->payip;
-    $content{customer_ip} = $payip
-      if length($payip);
-
     $content{recurring_billing} = 'YES'
       if qsearch('cust_pay', { 'custnum' => $self->custnum,
                                'payby'   => 'CARD',
@@ -2424,7 +2456,7 @@ sub realtime_bop {
   ) {
     my $error = $self->remove_cvv;
     if ( $error ) {
-      warn "error removing cvv: $error\n";
+      warn "WARNING: error removing cvv: $error\n";
     }
   }
 
@@ -2594,7 +2626,7 @@ gateway is attempted.
 sub realtime_refund_bop {
   my( $self, $method, %options ) = @_;
   if ( $DEBUG ) {
-    warn "$self $method refund\n";
+    warn "$me realtime_refund_bop: $method refund\n";
     warn "  $_ => $options{$_}\n" foreach keys %options;
   }
 
@@ -2613,7 +2645,7 @@ sub realtime_refund_bop {
 
   if ( $options{'paynum'} ) {
 
-    warn "FS::cust_main::realtime_bop: paynum: $options{paynum}\n" if $DEBUG;
+    warn "  paynum: $options{paynum}\n" if $DEBUG > 1;
     $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
       or return "Unknown paynum $options{'paynum'}";
     $amount ||= $cust_pay->paid;
@@ -2703,7 +2735,7 @@ sub realtime_refund_bop {
 
   #first try void if applicable
   if ( $cust_pay && $cust_pay->paid == $amount ) { #and check dates?
-    warn "FS::cust_main::realtime_bop: attempting void\n" if $DEBUG;
+    warn "  attempting void\n" if $DEBUG > 1;
     my $void = new Business::OnlinePayment( $processor, @bop_options );
     $void->content( 'action' => 'void', %content );
     $void->submit();
@@ -2716,13 +2748,13 @@ sub realtime_refund_bop {
         warn $e;
         return $e;
       }
-      warn "FS::cust_main::realtime_bop: void successful\n" if $DEBUG;
+      warn "  void successful\n" if $DEBUG > 1;
       return '';
     }
   }
 
-  warn "FS::cust_main::realtime_bop: void unsuccessful, trying refund\n"
-    if $DEBUG;
+  warn "  void unsuccessful, trying refund\n"
+    if $DEBUG > 1;
 
   #massage data
   my $address = $self->address1;