fix payinfo N/A upgrade, RT#8809
[freeside.git] / FS / FS / cust_pay.pm
index 69bcd87..f15c85b 100644 (file)
@@ -1,7 +1,9 @@
 package FS::cust_pay;
 
 use strict;
-use vars qw( @ISA $DEBUG $me $conf @encrypted_fields
+use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
+             FS::Record );
+use vars qw( $DEBUG $me $conf @encrypted_fields
              $unsuspendauto $ignore_noapply 
            );
 use Date::Format;
@@ -20,9 +22,7 @@ use FS::cust_main;
 use FS::cust_pkg;
 use FS::cust_pay_void;
 
-@ISA = qw( FS::payinfo_transaction_Mixin FS::cust_main_Mixin FS::Record );
-
-$DEBUG = 1;
+$DEBUG = 0;
 
 $me = '[FS::cust_pay]';
 
@@ -80,9 +80,9 @@ L<Time::Local> and L<Date::Parse> for conversion functions.
 
 Amount of this payment
 
-=item otaker
+=item usernum
 
-order taker (assigned automatically, see L<FS::UID>)
+order taker (see L<FS::access_user>)
 
 =item payby
 
@@ -179,7 +179,7 @@ sub insert {
   $error = $self->SUPER::insert;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
-    return "error inserting $self: $error";
+    return "error inserting cust_pay: $error";
   }
 
   if ( $self->invnum ) {
@@ -192,11 +192,11 @@ sub insert {
     $error = $cust_bill_pay->insert(%options);
     if ( $error ) {
       if ( $ignore_noapply ) {
-        warn "warning: error inserting $cust_bill_pay: $error ".
+        warn "warning: error inserting cust_bill_pay: $error ".
              "(ignore_noapply flag set; inserting cust_pay record anyway)\n";
       } else {
         $dbh->rollback if $oldAutoCommit;
-        return "error inserting $cust_bill_pay: $error";
+        return "error inserting cust_bill_pay: $error";
       }
     }
   }
@@ -381,7 +381,7 @@ sub check {
     || $self->ut_numbern('custnum')
     || $self->ut_numbern('_date')
     || $self->ut_money('paid')
-    || $self->ut_alpha('otaker')
+    || $self->ut_alphan('otaker')
     || $self->ut_textn('paybatch')
     || $self->ut_textn('payunique')
     || $self->ut_enum('closed', [ '', 'Y' ])
@@ -527,6 +527,7 @@ payment.
 
 sub cust_bill_pay {
   my $self = shift;
+  map { $_ } #return $self->num_cust_bill_pay unless wantarray;
   sort {    $a->_date  <=> $b->_date
          || $a->invnum <=> $b->invnum }
     qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
@@ -542,6 +543,7 @@ payment.
 
 sub cust_pay_refund {
   my $self = shift;
+  map { $_ } #return $self->num_cust_pay_refund unless wantarray;
   sort { $a->_date <=> $b->_date }
     qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
   ;
@@ -657,17 +659,23 @@ Returns an SQL fragment to retreive the unapplied amount.
 =cut 
 
 sub unapplied_sql {
-  #my $class = shift;
+  my ($class, $start, $end) = @_;
+  my $bill_start   = $start ? "AND cust_bill_pay._date <= $start"   : '';
+  my $bill_end     = $end   ? "AND cust_bill_pay._date > $end"     : '';
+  my $refund_start = $start ? "AND cust_pay_refund._date <= $start" : '';
+  my $refund_end   = $end   ? "AND cust_pay_refund._date > $end"   : '';
 
   "paid
         - COALESCE( 
                     ( SELECT SUM(amount) FROM cust_bill_pay
-                        WHERE cust_pay.paynum = cust_bill_pay.paynum )
+                        WHERE cust_pay.paynum = cust_bill_pay.paynum
+                        $bill_start $bill_end )
                     ,0
                   )
         - COALESCE(
                     ( SELECT SUM(amount) FROM cust_pay_refund
-                        WHERE cust_pay.paynum = cust_pay_refund.paynum )
+                        WHERE cust_pay.paynum = cust_pay_refund.paynum
+                        $refund_start $refund_end )
                     ,0
                   )
   ";
@@ -685,9 +693,14 @@ sub _upgrade_data {  #class method
 
   warn "$me upgrading $class\n" if $DEBUG;
 
+  ##
+  # otaker/ivan upgrade
+  ##
+
   #not the most efficient, but hey, it only has to run once
 
   my $where = "WHERE ( otaker IS NULL OR otaker = '' OR otaker = 'ivan' ) ".
+              "  AND usernum IS NULL ".
               "  AND 0 < ( SELECT COUNT(*) FROM cust_main                 ".
               "              WHERE cust_main.custnum = cust_pay.custnum ) ";
 
@@ -739,6 +752,40 @@ sub _upgrade_data {  #class method
 
   }
 
+  ###
+  # payinfo N/A upgrade
+  ###
+
+  #XXX remove the 'N/A (tokenized)' part (or just this entire thing)
+
+  my @na_cust_pay = qsearch( {
+    'table'     => 'cust_pay',
+    'hashref'   => {}, #could be encrypted# { 'payinfo' => 'N/A' },
+    'extra_sql' => "WHERE ( payinfo = 'N/A' OR paymask = 'N/AA' OR paymask = 'N/A (tokenized)' ) AND payby IN ( 'CARD', 'CHEK' )",
+  } );
+
+  foreach my $na ( @na_cust_pay ) {
+
+    next unless $na->payinfo eq 'N/A';
+
+    my $cust_pay_pending =
+      qsearchs('cust_pay_pending', { 'paynum' => $na->paynum } );
+    $na->$_($cust_pay_pending->$_) for qw( payinfo paymask );
+    my $error = $na->replace;
+    if ( $error ) {
+      warn " *** WARNING: Error updating payinfo for payment paynum ".
+           $na->paynun. ": $error\n";
+      next;
+    }
+
+  }
+
+  ###
+  # otaker->usernum upgrade
+  ###
+
+  $class->_upgrade_otaker(%opts);
+
 }
 
 =back