DEL out voided payments to distinguish them visually better
[freeside.git] / httemplate / view / cust_main.cgi
index 6057017..4497713 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',{}) ) {
@@ -37,15 +36,15 @@ print qq!<A HREF="${p}edit/cust_main.cgi?$custnum">Edit this customer</A>!;
 %>
 
 <SCRIPT>
-function cancel_areyousure(href) {
-    if (confirm("Perminantly delete all services and cancel this customer?") == true)
+function areyousure(href, message) {
+    if (confirm(message) == true)
         window.location.href = href;
 }
 </SCRIPT>
 
 <%
 
-print qq! | <A HREF="javascript:cancel_areyousure('${p}misc/cust_main-cancel.cgi?$custnum')">!.
+print qq! | <A HREF="javascript:areyousure('${p}misc/cust_main-cancel.cgi?$custnum', 'Perminantly delete all services and cancel this customer?')">!.
       'Cancel this customer</A>'
   if $cust_main->ncancelled_pkgs;
 
@@ -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&nbsp;card&nbsp;',
           ( $cust_main->payby eq 'CARD' ? '(automatic)' : '(on-demand)' ),
           '</TD></TR>',
@@ -351,21 +349,6 @@ if ( $conf->config('payby-default') ne 'HIDE' ) {
 
 }
 
-%>
-
-<SCRIPT>
-function cust_pkg_areyousure(href) {
-    if (confirm("Permanently delete included services and cancel this package?") == true)
-        window.location.href = href;
-}
-function svc_areyousure(href) {
-    if (confirm("Permanently unprovision and delete this service?") == true)
-        window.location.href = href;
-}
-</SCRIPT>
-
-<%
-
 print qq!<A NAME="cust_pkg">Packages</A> !,
       qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> (preserves services) )!,
 ;
@@ -401,7 +384,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;)
@@ -546,34 +529,14 @@ print '</TABLE>';
 #end display packages
 %>
 
-<SCRIPT>
-function cust_pay_areyousure(href) {
-    if (confirm("Are you sure you want to delete this payment?")
- == true)
-        window.location.href = href;
-}
-function cust_pay_unapply_areyousure(href) {
-    if (confirm("Are you sure you want to unapply this payment?")
- == true)
-        window.location.href = href;
-}
-function cust_credit_unapply_areyousure(href) {
-    if (confirm("Are you sure you want to unapply this credit?")
- == true)
-        window.location.href = href;
-}
-function cust_credit_areyousure(href) {
-    if (confirm("Are you sure you want to delete this credit?")
- == true)
-        window.location.href = href;
-}
-</SCRIPT>
-
 <% 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 +562,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 +575,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;'.
@@ -641,31 +622,77 @@ function cust_credit_areyousure(href) {
       }
     }
 
+    my $refund = '';
+    my $refund_days = $conf->config('card_refund-days') || 120;
+    if (    $cust_pay->closed !~ /^Y/i
+         && $cust_pay->payby eq 'CARD' 
+         && time-$cust_pay->_date < $refund_days*86400
+         && $cust_pay->unrefunded > 0
+    ) {
+      $refund = qq! (<A HREF="!. qq!${p}edit/cust_refund.cgi?payby=CARD;!.
+                qq!paynum=!. $cust_pay->paynum. qq!">refund</A>)!;
+    }
+
+    my $void = '';
+    if (    $cust_pay->closed !~ /^Y/i
+         && $cust_pay->payby ne 'CARD'
+       ) {
+      $void = qq! (<A HREF="javascript:areyousure('!.
+              qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
+              qq!', 'Are you sure you want to void this payment?')">!.
+              qq!void</A>)!;
+    }
+
     my $delete = '';
     if ( $cust_pay->closed !~ /^Y/i && $conf->exists('deletepayments') ) {
-      $delete = qq! (<A HREF="javascript:cust_pay_areyousure('!.
+      $delete = qq! (<A HREF="javascript:areyousure('!.
                 qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
-                qq!')">delete</A>)!;
+                qq!', 'Are you sure you want to delete this payment?')">!.
+                qq!delete</A>)!;
     }
 
     my $unapply = '';
     if (    $cust_pay->closed !~ /^Y/i
          && $conf->exists('unapplypayments')
          && scalar(@cust_bill_pay)           ) {
-      $unapply = qq! (<A HREF="javascript:cust_pay_unapply_areyousure('!.
+      $unapply = qq! (<A HREF="javascript:areyousure('!.
                  qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
-                 qq!')">unapply</A>)!;
+                 qq!', 'Are you sure you want to unapply this payment?')">!.
+                 qq!unapply</A>)!;
     }
 
     push @history, {
       'date'    => $cust_pay->_date,
       'desc'    => $pre. "Payment$post$info$desc".
-                   "$apply$delete$unapply",
+                   "$apply$refund$void$delete$unapply",
       'payment' => $cust_pay->paid,
       'target'  => $target,
     };
   }
 
+  #voided payments
+  foreach my $cust_pay_void ($cust_main->cust_pay_void) {
+
+    my $payby = $cust_pay_void->payby;
+    my $payinfo = $payby eq 'CARD'
+                    ? $cust_pay_void->payinfo_masked
+                    : $cust_pay_void->payinfo;
+
+    $payby =~ s/^BILL$/Check #/ if $payinfo;
+    $payby =~ s/^BILL$//;
+    $payby =~ s/^(CARD|COMP)$/$1 /;
+    my $info = $payby ? " ($payby$payinfo)" : '';
+
+    push @history, {
+      'date'   => $cust_pay_void->_date,
+      'desc'   => "<DEL>Payment $info</DEL> <I>voided ".
+                  time2str("%D", $cust_pay_void->void_date).
+                  " by ". $cust_pay_void->otaker. '</i>',
+      'void_payment' => $cust_pay_void->paid,
+    };
+  
+  }
+
   #credits (some false laziness w/payments)
   foreach my $cust_credit ($cust_main->cust_credit) {
 
@@ -683,7 +710,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,28 +738,30 @@ 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>';
       }
     }
 #
     my $delete = '';
     if ( $cust_credit->closed !~ /^Y/i && $conf->exists('deletecredits') ) {
-      $delete = qq! (<A HREF="javascript:cust_credit_areyousure('!.
+      $delete = qq! (<A HREF="javascript:areyousure('!.
                 qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
-                qq!')">delete</A>)!;
+                qq!', 'Are you sure you want to delete this credit?')">!.
+                qq!delete</A>)!;
     }
     
     my $unapply = '';
     if (    $cust_credit->closed !~ /^Y/i
          && $conf->exists('unapplycredits')
          && scalar(@cust_credit_bill)       ) {
-      $unapply = qq! (<A HREF="javascript:cust_credit_unapply_areyousure('!.
+      $unapply = qq! (<A HREF="javascript:areyousure('!.
                  qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
-                 qq!')">unapply</A>)!;
+                 qq!', 'Are you sure you want to unapply this credit?')">!.
+                 qq!unapply</A>)!;
     }
     
     push @history, {
@@ -749,10 +778,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 /;
 
@@ -790,6 +819,8 @@ function cust_credit_areyousure(href) {
     my $payment = exists($item->{'payment'})
                     ? sprintf('-&nbsp;$%.2f', $item->{'payment'})
                     : '';
+    $payment ||= sprintf('<DEL>-&nbsp;$%.2f</DEL>', $item->{'void_payment'})
+      if exists($item->{'void_payment'});
     my $credit  = exists($item->{'credit'})
                     ? sprintf('-&nbsp;$%.2f', $item->{'credit'})
                     : '';
@@ -889,15 +920,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],
@@ -964,7 +988,8 @@ sub svc_provision_link {
 
 sub svc_unprovision_link {
   my $svc = shift or return '';
-  return qq!<A HREF="javascript:svc_areyousure('${p}misc/unprovision.cgi?$svc->{svcnum}')">Unprovision</A>!;
+  qq!<A HREF="javascript:areyousure('${p}misc/unprovision.cgi?$svc->{svcnum}',!.
+  qq!'Permanently unprovision and delete this service?')">Unprovision</A>!;
 }
 
 # This should be generalized to use config options to determine order.
@@ -990,14 +1015,10 @@ 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>!;
+  return qq!<a href="${p}misc/change_pkg.cgi?$pkg->{pkgnum}">!.
+         qq!Change&nbsp;package</a>!;
 }
 
 sub pkg_suspend_link {
@@ -1012,19 +1033,22 @@ sub pkg_unsuspend_link {
 
 sub pkg_cancel_link {
   my $pkg = shift or return '';
-  qq!<A HREF="javascript:cust_pkg_areyousure('${p}misc/cancel_pkg.cgi?$pkg->{pkgnum}')">Cancel now</A> | !.
+  qq!<A HREF="javascript:areyousure('${p}misc/cancel_pkg.cgi?$pkg->{pkgnum}', !.
+  qq!'Permanently delete included services and cancel this package?')">!.
+  qq!Cancel now</A> | !.
   qq!<A HREF="${p}misc/expire_pkg.cgi?$pkg->{pkgnum}">Cancel later</A>!;
 }
 
 sub pkg_dates_link {
   my $pkg = shift or return '';
-  return qq!<A HREF="${p}edit/REAL_cust_pkg.cgi?$pkg->{pkgnum}">Edit&nbsp;dates</A>!;
+  qq!<A HREF="${p}edit/REAL_cust_pkg.cgi?$pkg->{pkgnum}">Edit&nbsp;dates</A>!;
 }
 
 sub pkg_customize_link {
   my $pkg = shift or return '';
   my $custnum = shift;
-  return qq!<A HREF="${p}edit/part_pkg.cgi?keywords=$custnum;clone=$pkg->{pkgpart};pkgnum=$pkg->{pkgnum}">Customize</A>!;
+  qq!<A HREF="${p}edit/part_pkg.cgi?keywords=$custnum;clone=$pkg->{pkgpart};!.
+  qq!pkgnum=$pkg->{pkgnum}">Customize</A>!;
 }
 
 %>