add cust_pay_refund table to refund payments
[freeside.git] / httemplate / view / cust_main.cgi
index 624fe37..125c51a 100755 (executable)
@@ -235,8 +235,7 @@ if ( $conf->config('payby-default') ne 'HIDE' ) {
   ;
 
   if ( $cust_main->payby eq 'CARD' || $cust_main->payby eq 'DCRD' ) {
-    my $payinfo = $cust_main->payinfo;
-    $payinfo = 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
+    my $payinfo = $cust_main->payinfo_masked;
     print 'Credit card ',
           ( $cust_main->payby eq 'CARD' ? '(automatic)' : '(on-demand)' ),
           '</TD></TR>',
@@ -601,11 +600,12 @@ function cust_credit_areyousure(href) {
   foreach my $cust_pay ($cust_main->cust_pay) {
 
     my $payby = $cust_pay->payby;
-    my $payinfo = $cust_pay->payinfo;
+    my $payinfo = $payby eq 'CARD'
+                    ? $cust_pay->payinfo_masked
+                    : $cust_pay->payinfo;
     my @cust_bill_pay = $cust_pay->cust_bill_pay;
+    my @cust_pay_refund = $cust_pay->cust_pay_refund;
 
-    $payinfo = 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4))
-      if $payby eq 'CARD';
     my $target = "$payby$payinfo";
     $payby =~ s/^BILL$/Check #/ if $payinfo;
     $payby =~ s/^BILL$//;
@@ -613,25 +613,42 @@ function cust_credit_areyousure(href) {
     my $info = $payby ? " ($payby$payinfo)" : '';
 
     my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
-    if ( scalar(@cust_bill_pay) == 0 ) {
+    if (    scalar(@cust_bill_pay)   == 0
+         && scalar(@cust_pay_refund) == 0 ) {
       #completely unapplied
       $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
       $post = '</FONT></B>';
       $apply = qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
                $cust_pay->paynum. '">apply</A>)';
-    } elsif ( scalar(@cust_bill_pay) == 1 && $cust_pay->unapplied == 0 ) {
-      #applied to one invoice
+    } elsif (    scalar(@cust_bill_pay)   == 1
+              && scalar(@cust_pay_refund) == 0
+              && $cust_pay->unapplied == 0     ) {
+      #applied to one invoice, the usual situation
       $desc = ' applied to Invoice #'. $cust_bill_pay[0]->invnum;
+    } elsif (    scalar(@cust_bill_pay)   == 0
+              && scalar(@cust_pay_refund) == 1
+              && $cust_pay->unapplied == 0     ) {
+      #applied to one refund
+      $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
     } else {
       #complicated
       $desc = '<BR>';
-      foreach my $cust_bill_pay (@cust_bill_pay) {
-        $desc .= '&nbsp;&nbsp;'.
-                 '$'. $cust_bill_pay->amount.
-                 ' applied to Invoice #'. $cust_bill_pay->invnum.
-                 '<BR>';
-                 #' on '. time2str("%D", $cust_bill_pay->_date).
-
+      foreach my $app ( sort { $a->_date <=> $b->_date }
+                             ( @cust_bill_pay, @cust_pay_refund ) ) {
+        if ( $app->isa('FS::cust_bill_pay') ) {
+          $desc .= '&nbsp;&nbsp;'.
+                   '$'. $app->amount.
+                   ' applied to Invoice #'. $app->invnum.
+                   '<BR>';
+                   #' on '. time2str("%D", $cust_bill_pay->_date).
+        } elsif ( $app->isa('FS::cust_pay_refund') ) {
+          $desc .= '&nbsp;&nbsp;'.
+                   '$'. $app->amount.
+                   ' refunded on'. time2str("%D", $app->_date).
+                   '<BR>';
+        } else {
+          die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
+        }
       }
       if ( $cust_pay->unapplied > 0 ) {
         $desc .= '&nbsp;&nbsp;'.
@@ -685,7 +702,7 @@ function cust_credit_areyousure(href) {
     } elsif (    scalar(@cust_credit_bill)   == 1
               && scalar(@cust_credit_refund) == 0
               && $cust_credit->credited == 0      ) {
-      #applied to one invoice
+      #applied to one invoice, the usual situation
       $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
     } elsif (    scalar(@cust_credit_bill)   == 0
               && scalar(@cust_credit_refund) == 1
@@ -751,10 +768,10 @@ function cust_credit_areyousure(href) {
   foreach my $cust_refund ($cust_main->cust_refund) {
 
     my $payby = $cust_refund->payby;
-    my $payinfo = $cust_refund->payinfo;
+    my $payinfo = $payby eq 'CARD'
+                    ? $cust_refund->payinfo_masked
+                    : $cust_refund->payinfo;
 
-    $payinfo = 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4))
-      if $payby eq 'CARD';
     $payby =~ s/^BILL$/Check #/ if $payinfo;
     $payby =~ s/^(CARD|COMP)$/$1 /;