Merge branch 'master' of git.freeside.biz:/home/git/freeside
authorIvan Kohler <ivan@freeside.biz>
Wed, 1 Feb 2017 17:32:28 +0000 (09:32 -0800)
committerIvan Kohler <ivan@freeside.biz>
Wed, 1 Feb 2017 17:32:28 +0000 (09:32 -0800)
FS/FS/cust_main.pm
FS/FS/part_event/Condition/cust_bill_hasnt_noauto.pm
FS/FS/payinfo_Mixin.pm

index 6eee505..e0855d8 100644 (file)
@@ -5389,10 +5389,11 @@ sub queueable_upgrade {
     FS::upgrade_journal->set_done('clear_payinfo_history');
   }
 
-  # encrypt old records
-  if ( $conf->exists('encryption')
-         && ! FS::upgrade_journal->is_done('encryption_check')
-  ) {
+  # fix Tokenized paycardtype and encrypt old records
+  if (    ! FS::upgrade_journal->is_done('paycardtype_Tokenized')
+       || ! FS::upgrade_journal->is_done('encryption_check')
+     )
+  {
 
     # allow replacement of closed cust_pay/cust_refund records
     local $FS::payinfo_Mixin::allow_closed_replace = 1;
@@ -5422,6 +5423,7 @@ sub queueable_upgrade {
         if (!$record->custnum && $table eq 'cust_pay_pending') {
           $record->set('custnum_pending',1);
         }
+        $record->paycardtype('') if $record->paycardtype eq 'Tokenized';
 
         local($ignore_expired_card) = 1;
         local($ignore_banned_card) = 1;
@@ -5433,7 +5435,8 @@ sub queueable_upgrade {
       }
     }
 
-    FS::upgrade_journal->set_done('encryption_check');
+    FS::upgrade_journal->set_done('paycardtype_Tokenized');
+    FS::upgrade_journal->set_done('encryption_check') if $conf->exists('encryption');
   }
 
   # now that everything's encrypted, tokenize...
@@ -5451,7 +5454,9 @@ sub _upgrade_next_recnum {
   my $sql = 'SELECT '.$tclass->primary_key.
             ' FROM '.$table.
             ' WHERE '.$tclass->primary_key.' > '.$$lastrecnum.
-            ' ORDER BY '.$tclass->primary_key.' LIMIT 500';;
+            "   AND payby IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
+            "   AND ( length(payinfo) < 80 OR paycardtype = 'Tokenized' ) ".
+            ' ORDER BY '.$tclass->primary_key.' LIMIT 500';
   my $sth = $dbh->prepare($sql) or die $dbh->errstr;
   $sth->execute() or die $sth->errstr;
   my @recnums;
index 0276255..d782c12 100644 (file)
@@ -26,32 +26,18 @@ sub condition {
 sub condition_sql {
   my( $class, $table, %opt ) = @_;
   
-  # XXX: can be made faster with optimizations?
-  # -remove some/all sub-selects?
-  # -remove the two main separate selects?
-
-  "0 = (select count(1) from cust_pkg 
-            where cust_pkg.no_auto = 'Y' and cust_pkg.pkgnum in
-                (select distinct cust_bill_pkg.pkgnum 
-                    from cust_bill_pkg, cust_pkg 
-                    where cust_bill_pkg.pkgnum = cust_pkg.pkgnum
-                        and cust_bill_pkg.invnum = cust_bill.invnum
-                        and cust_bill_pkg.pkgnum > 0
-                )
-        )
-   AND
-   0 = (select count(1) from part_pkg 
-            where part_pkg.no_auto = 'Y' and part_pkg.pkgpart in
-                (select cust_pkg.pkgpart from cust_pkg 
-                    where pkgnum in 
-                        (select distinct cust_bill_pkg.pkgnum 
-                            from cust_bill_pkg, cust_pkg 
-                            where cust_bill_pkg.pkgnum = cust_pkg.pkgnum 
-                                and cust_bill_pkg.invnum = cust_bill.invnum
-                                and cust_bill_pkg.pkgnum > 0
-                        ) 
-                )
-        )
+  # can be made still faster with optimizations?
+
+  "NOT EXISTS ( SELECT 1 FROM cust_pkg 
+                           LEFT JOIN part_pkg USING (pkgpart)
+                  WHERE ( cust_pkg.no_auto = 'Y' OR part_pkg.no_auto = 'Y' )
+                    AND cust_pkg.pkgnum IN
+                          ( SELECT DISTINCT cust_bill_pkg.pkgnum 
+                              FROM cust_bill_pkg
+                              WHERE cust_bill_pkg.invnum = cust_bill.invnum
+                                AND cust_bill_pkg.pkgnum > 0
+                          )
+              )
   ";
 }
 
index ac92623..7401eb9 100644 (file)
@@ -197,10 +197,20 @@ sub payinfo_check {
 
   if ( $self->payby eq 'CARD' && ! $self->is_encrypted($self->payinfo) ) {
 
-    if ( $self->tokenized && ! $self->paycardtype ) {
-      return "paycardtype required (cannot be derived from a token)";
-    } else {
-      $self->set('paycardtype', cardtype($self->payinfo));
+    unless ( $self->paycardtype ) {
+
+      if ( $self->tokenized ) {
+        if ( $self->paymask =~ /^\d+x/ ) {
+          $self->set('paycardtype', cardtype($self->paymask));
+        } else {
+          $self->set('paycardtype', '');
+         # return "paycardtype required ".
+         #        "(can't derive from a token and no paymask w/prefix provided)";
+        }
+      } else {
+        $self->set('paycardtype', cardtype($self->payinfo));
+      }
+
     }
 
     if ( $ignore_masked_payinfo and $self->mask_payinfo eq $self->payinfo ) {
@@ -219,13 +229,22 @@ sub payinfo_check {
         $self->payinfo('N/A'); #??? re-masks card
       }
     }
+
   } else {
-    if ( $self->payby eq 'CARD' and $self->paymask ) {
-      # if we can't decrypt the card, at least detect the cardtype
-      $self->set('paycardtype', cardtype($self->paymask));
-    } else {
-      $self->set('paycardtype', '');
+
+    unless ( $self->paycardtype ) {
+
+      if ( $self->payby eq 'CARD' && $self->paymask =~ /^\d+x/  ) {
+        # if we can't decrypt the card, at least detect the cardtype
+        $self->set('paycardtype', cardtype($self->paymask));
+      } else {
+        $self->set('paycardtype', '');
+        # return "paycardtype required ".
+        #        "(can't derive from a token and no paymask w/prefix provided)";
+      }
+
     }
+
     if ( $self->is_encrypted($self->payinfo) ) {
       #something better?  all it would cause is a decryption error anyway?
       my $error = $self->ut_anything('payinfo');