72225: OQH - Mandatory Fields on Customer Creation
[freeside.git] / FS / FS / cust_main.pm
index e4fc3db..90b93f2 100644 (file)
@@ -239,6 +239,10 @@ Name on card or billing name
 
 IP address from which payment information was received
 
+=item paycardtype
+
+The credit card type (deduced from the card number).
+
 =item tax
 
 Tax exempt, empty or `Y'
@@ -1962,9 +1966,12 @@ sub check {
     validate($payinfo)
       or return gettext('invalid_card'); # . ": ". $self->payinfo;
 
-    return gettext('unknown_card_type')
-      if $self->payinfo !~ /^99\d{14}$/ #token
-      && cardtype($self->payinfo) eq "Unknown";
+    my $cardtype = cardtype($payinfo);
+    $cardtype = 'Tokenized' if $self->payinfo =~ /^99\d{14}$/; # token
+
+    return gettext('unknown_card_type') if $cardtype eq 'Unknown';
+
+    $self->set('paycardtype', $cardtype);
 
     unless ( $ignore_banned_card ) {
       my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
@@ -1986,7 +1993,7 @@ sub check {
     }
 
     if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
-      if ( cardtype($self->payinfo) eq 'American Express card' ) {
+      if ( $cardtype eq 'American Express card' ) {
         $self->paycvv =~ /^(\d{4})$/
           or return "CVV2 (CID) for American Express cards is four digits.";
         $self->paycvv($1);
@@ -1999,7 +2006,6 @@ sub check {
       $self->paycvv('');
     }
 
-    my $cardtype = cardtype($payinfo);
     if ( $cardtype =~ /^(Switch|Solo)$/i ) {
 
       return "Start date or issue number is required for $cardtype cards"
@@ -2096,6 +2102,11 @@ sub check {
       unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
     $self->paycvv('');
 
+  } elsif ( $self->payby =~ /^CARD|DCRD$/ and $self->paymask ) {
+    # either ignoring invalid cards, or we can't decrypt the payinfo, but
+    # try to detect the card type anyway. this never returns failure, so
+    # the contract of $ignore_invalid_cards is maintained.
+    $self->set('paycardtype', cardtype($self->paymask));
   }
 
   if ( $self->paydate eq '' || $self->paydate eq '-' ) {
@@ -2147,6 +2158,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);
@@ -2168,10 +2183,14 @@ sub check_payinfo_cardtype {
   my $payinfo = $self->payinfo;
   $payinfo =~ s/\D//g;
 
-  return '' if $payinfo =~ /^99\d{14}$/; #token
+  if ( $payinfo =~ /^99\d{14}$/ ) {
+    $self->set('paycardtype', 'Tokenized');
+    return '';
+  }
 
   my %bop_card_types = map { $_=>1 } values %{ card_types() };
   my $cardtype = cardtype($payinfo);
+  $self->set('paycardtype', $cardtype);
 
   return "$cardtype not accepted" unless $bop_card_types{$cardtype};
 
@@ -2501,15 +2520,7 @@ sub cancel_pkgs {
         $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;
+        $lopt{'reason'} = '';
       }
     }
     my $error = $_->cancel(%lopt);