71513: Card tokenization in v4+
[freeside.git] / FS / FS / cust_main.pm
index 3fb0a87..11d7763 100644 (file)
@@ -29,6 +29,7 @@ use Date::Format;
 use File::Temp; #qw( tempfile );
 use Business::CreditCard 0.28;
 use List::Util qw(min);
+use Try::Tiny;
 use FS::UID qw( dbh driver_name );
 use FS::Record qw( qsearchs qsearch dbdef regexp_sql );
 use FS::Cursor;
@@ -76,6 +77,7 @@ use FS::upgrade_journal;
 use FS::sales;
 use FS::cust_payby;
 use FS::contact;
+use FS::reason;
 
 # 1 is mostly method/subroutine entry and options
 # 2 traces progress of some operations
@@ -596,17 +598,21 @@ sub insert {
       }
     }
 
-    my $contact = FS::contact->new({
-      'custnum'       => $self->get('custnum'),
-      'last'          => $self->get('last'),
-      'first'         => $self->get('first'),
-      'emailaddress'  => $email,
-      'invoice_dest'  => 'Y', # yes, you can set this via the contact
-    });
-    my $error = $contact->insert;
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $error;
+    if ( $email ) {
+
+      my $contact = FS::contact->new({
+        'custnum'       => $self->get('custnum'),
+        'last'          => $self->get('last'),
+        'first'         => $self->get('first'),
+        'emailaddress'  => $email,
+        'invoice_dest'  => 'Y', # yes, you can set this via the contact
+      });
+      my $error = $contact->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+
     }
 
   }
@@ -1863,6 +1869,10 @@ sub check {
     && ! $self->custnum
     && $conf->exists('cust_main-require_locale');
 
+  return "Please select a customer class"
+    if ! $self->classnum
+    && $conf->exists('cust_main-require_classnum');
+
   foreach my $flag (qw( tax spool_cdr squelch_cdr archived email_csv_cdr )) {
     $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
     $self->$flag($1);
@@ -2159,7 +2169,11 @@ FS::cust_pkg::cancel() methods.
 
 =item quiet - can be set true to supress email cancellation notices.
 
-=item reason - can be set to a cancellation reason (see L<FS:reason>), either a reasonnum of an existing reason, or passing a hashref will create a new reason.  The hashref should have the following keys: typenum - Reason type (see L<FS::reason_type>, reason - Text of the new reason.
+=item reason - can be set to a cancellation reason (see L<FS:reason>), either a
+reasonnum of an existing reason, or passing a hashref will create a new reason.
+The hashref should have the following keys:
+typenum - Reason type (see L<FS::reason_type>)
+reason - Text of the new reason.
 
 =item cust_pkg_reason - can be an arrayref of L<FS::cust_pkg_reason> objects
 for the individual packages, parallel to the C<cust_pkg> argument. The
@@ -2222,10 +2236,9 @@ sub cancel_pkgs {
   }
   dbh->commit;
 
-  $FS::UID::AutoCommit = 1;
   my @errors;
-  # now cancel all services, the same way we would for individual packages.
-  # if any of them fail, cancel the rest anyway.
+  # try to cancel each service, the same way we would for individual packages,
+  # but in cancel weight order.
   my @cust_svc = map { $_->cust_svc } @pkgs;
   my @sorted_cust_svc =
     map  { $_->[0] }
@@ -2238,8 +2251,15 @@ sub cancel_pkgs {
   foreach my $cust_svc (@sorted_cust_svc) {
     my $part_svc = $cust_svc->part_svc;
     next if ( defined($part_svc) and $part_svc->preserve );
-    my $error = $cust_svc->cancel; # immediate cancel, no date option
-    push @errors, $error if $error;
+    # immediate cancel, no date option
+    # transactionize individually
+    my $error = try { $cust_svc->cancel } catch { $_ };
+    if ( $error ) {
+      dbh->rollback;
+      push @errors, $error;
+    } else {
+      dbh->commit;
+    }
   }
   if (@errors) {
     return @errors;
@@ -2253,15 +2273,34 @@ sub cancel_pkgs {
   if ($opt{'cust_pkg_reason'}) {
     @cprs = @{ delete $opt{'cust_pkg_reason'} };
   }
+  my $null_reason;
   foreach (@pkgs) {
     my %lopt = %opt;
     if (@cprs) {
       my $cpr = shift @cprs;
-      $lopt{'reason'}        = $cpr->reasonnum;
-      $lopt{'reason_otaker'} = $cpr->otaker;
+      if ( $cpr ) {
+        $lopt{'reason'}        = $cpr->reasonnum;
+        $lopt{'reason_otaker'} = $cpr->otaker;
+      } else {
+        warn "no reason found when canceling package ".$_->pkgnum."\n";
+        # we're not actually required to pass a reason to cust_pkg::cancel,
+        # but if we're getting to this point, something has gone awry.
+        $null_reason ||= FS::reason->new_or_existing(
+          reason  => 'unknown reason',
+          type    => 'Cancel Reason',
+          class   => 'C',
+        );
+        $lopt{'reason'} = $null_reason->reasonnum;
+        $lopt{'reason_otaker'} = $FS::CurrentUser::CurrentUser->username;
+      }
     }
     my $error = $_->cancel(%lopt);
-    push @errors, 'pkgnum '.$_->pkgnum.': '.$error if $error;
+    if ( $error ) {
+      dbh->rollback;
+      push @errors, 'pkgnum '.$_->pkgnum.': '.$error;
+    } else {
+      dbh->commit;
+    }
   }
 
   return @errors;
@@ -4404,6 +4443,10 @@ CHEK only
 
 CHEK only
 
+=item saved_cust_payby
+
+scalar reference, for returning saved object
+
 =back
 
 =cut
@@ -4600,6 +4643,9 @@ PAYBYLOOP:
     return $error;
   }
 
+  ${$opt{'saved_cust_payby'}} = $new
+    if $opt{'saved_cust_payby'};
+
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
   '';