add cust_pay_refund table to refund payments
[freeside.git] / httemplate / view / cust_main.cgi
index 6057017..125c51a 100755 (executable)
@@ -3,7 +3,6 @@
 
 my $conf = new FS::Conf;
 
-#false laziness with view/cust_pkg.cgi, but i'm trying to make that go away so
 my %uiview = ();
 my %uiadd = ();
 foreach my $part_svc ( qsearch('part_svc',{}) ) {
@@ -236,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>',
@@ -401,7 +399,7 @@ foreach my $pkg (sort pkgsort_pkgnum_cancel @$packages) {
 <!--pkgnum: <%=$pkg->{pkgnum}%>-->
 <TR>
   <TD ROWSPAN=<%=$rowspan%>>
-    <%=$pkg->{pkgnum}%>:
+    <A NAME="cust_pkg<%=$pkg->{pkgnum}%>"><%=$pkg->{pkgnum}%></A>:
     <%=$pkg->{pkg}%> - <%=$pkg->{comment}%><BR>
 <% unless ($pkg->{cancel}) { %>
     (&nbsp;<%=pkg_change_link($pkg)%>&nbsp;)
@@ -571,9 +569,12 @@ function cust_credit_areyousure(href) {
 
 <% if ( $conf->config('payby-default') ne 'HIDE' ) { %>
   
-  <BR><BR><A NAME="history">Payment History</A>
-  (<A HREF="<%= $p %>edit/cust_pay.cgi?custnum=<%= $custnum %>">Post payment</A>
-  | <A HREF="<%= $p %>edit/cust_credit.cgi?<%= $custnum %>">Post credit</A>)
+  <BR><BR><A NAME="history"><FONT SIZE="+2">Payment History</FONT></A><BR>
+  <A HREF="<%= $p %>edit/cust_pay.cgi?custnum=<%= $custnum %>">Post cash/check payment</A>
+  | <A HREF="<%= $p %>misc/payment.cgi?payby=CARD;custnum=<%= $custnum %>">Process credit card payment</A>
+  | <A HREF="<%= $p %>misc/payment.cgi?payby=CHEK;custnum=<%= $custnum %>">Process electronic check (ACH) payment</A>
+  <BR><A HREF="<%= $p %>edit/cust_credit.cgi?<%= $custnum %>">Post credit</A>
+  <BR>
 
   <%
   #get payment history
@@ -599,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$//;
@@ -611,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;'.
@@ -683,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
@@ -711,10 +730,10 @@ function cust_credit_areyousure(href) {
         }
       }
       if ( $cust_credit->credited > 0 ) {
-        $desc .= ' - <B><FONT COLOR="#FF0000">$'.
-                 $cust_credit->unapplied. ' unapplied</FONT></B>'.
+        $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
+                 $cust_credit->credited. ' unapplied</FONT></B>'.
                  qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
-                 $cust_credit->crednum. '">apply</A>'.
+                 $cust_credit->crednum. '">apply</A>)'.
                  '<BR>';
       }
     }
@@ -749,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 /;
 
@@ -889,15 +908,8 @@ sub get_packages {
 
     }
 
-    foreach my $cust_svc (
-      qsearch( 'cust_svc', {
-                             'pkgnum' => $cust_pkg->pkgnum,
-                             #'svcpart' => $part_svc->svcpart,
-                           }
-      )
-    ) {
-
-      warn "svcnum ". $cust_svc->svcnum. " / svcpart ". $cust_svc->svcpart. "\n";
+    foreach my $cust_svc ( $cust_pkg->cust_svc ) {
+      #warn "svcnum ". $cust_svc->svcnum. " / svcpart ". $cust_svc->svcpart. "\n";
       my $svc = {
         'svcnum' => $cust_svc->svcnum,
         'label'  => ($cust_svc->label)[1],
@@ -990,11 +1002,6 @@ sub pkg_datestr {
   $strip;
 }
 
-#sub pkg_details_link {
-#  my $pkg = shift or return '';
-#  return qq!<a href="${p}view/cust_pkg.cgi?$pkg->{pkgnum}">Details</a>!;
-#}
-
 sub pkg_change_link {
   my $pkg = shift or return '';
   return qq!<a href="${p}misc/change_pkg.cgi?$pkg->{pkgnum}">Change&nbsp;package</a>!;