things are starting to work again, sorta.
[freeside.git] / httemplate / view / cust_main.cgi
index 8b6fb0c..6ece5c2 100755 (executable)
@@ -1,5 +1,5 @@
 <%
-#<!-- $Id: cust_main.cgi,v 1.3 2001-08-11 05:52:56 ivan Exp $ -->
+#<!-- $Id: cust_main.cgi,v 1.7 2001-09-02 07:49:52 ivan Exp $ -->
 
 use strict;
 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral 
@@ -20,6 +20,8 @@ use FS::part_referral;
 use FS::agent;
 use FS::cust_main;
 use FS::cust_refund;
+use FS::cust_bill_pay;
+use FS::cust_credit_bill;
 
 $cgi = new CGI;
 &cgisuidsetup($cgi);
@@ -43,8 +45,18 @@ print qq!<A HREF="!, popurl(2),
 print qq! | <A HREF="!, popurl(2), 
       qq!misc/delete-customer.cgi?$custnum"> Delete this customer</A>!
   if $conf->exists('deletecustomers');
-print '<BR><BR>';
 
+unless ( $conf->exists('disable_customer_referrals') ) {
+  print qq! | <A HREF="!, popurl(2),
+        qq!edit/cust_main.cgi?referral_custnum=$custnum">!,
+        qq!Refer a new customer</A>!;
+
+  print qq! | <A HREF="!, popurl(2),
+        qq!search/cust_main.cgi?referral_custnum=$custnum">!,
+        qq!View this customer's referrals<A>!;
+}
+
+print '<BR><BR>';
 print '<A NAME="cust_main"></A>';
 
 print &itable(), '<TR>';
@@ -158,6 +170,22 @@ print '<TD VALIGN="top">';
   print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
     $cust_main->otaker, '</TD></TR>';
 
+  print '<TR><TD ALIGN="right">Referring Customer</TD><TD BGCOLOR="#ffffff">';
+  my $referring_cust_main = '';
+  if ( $cust_main->referral_custnum
+       && ( $referring_cust_main =
+            qsearchs('cust_main', { custnum => $cust_main->referral_custnum } )
+          )
+     ) {
+    print '<A HREF="'. popurl(1). 'cust_main.cgi?'.
+          $cust_main->referral_custnum. '">'.
+          $cust_main->referral_custnum. ': '.
+          ( $referring_cust_main->company
+            || $referring_cust_main->last. ', '. $referring_cust_main->first ).
+          '</A>';
+  }
+  print '</TD></TR>';
+
   print '</TABLE></TD></TR></TABLE>';
 
 print '<BR>';
@@ -210,7 +238,9 @@ print '<BR>';
 
 print '</TD></TR></TABLE>';
 
-if ( defined $cust_main->dbdef_table->column('comments') ) {
+if ( defined $cust_main->dbdef_table->column('comments')
+     && $cust_main->comments )
+{
   print "<BR>Comments", &ntable("#cccccc"), "<TR><TD>",
         &ntable("#cccccc",2),
         '<TR><TD BGCOLOR="#ffffff"><PRE>', $cust_main->comments,
@@ -301,6 +331,8 @@ print qq!<BR><BR><A NAME="history">Payment History!,
 # major problem: this whole thing is way too sloppy.
 # minor problem: the description lines need better formatting.
 
+# SHOULD SHOW UNAPPLIED PAYMENTS (now show unapplied credits)
+
 @history = (); #needed for mod_perl :)
 
 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
@@ -312,40 +344,66 @@ foreach $bill (@bills) {
     qq! (Balance \$! . $bill->owed . qq!)</A>\t! .
     $bref->{charged} . qq!\t\t\t!;
 
-  my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
-  my($payment);
-  foreach $payment (@payments) {
-    my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
-                                             $payment->getfield('invnum'),
-                                             $payment->getfield('payby'),
-                                             $payment->getfield('payinfo'),
-                                             $payment->getfield('paid'),
+  my(@cust_bill_pay)=qsearch('cust_bill_pay',{'invnum'=> $bref->{invnum} } );
+#  my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
+#  my($payment);
+#  foreach $payment (@payments) {
+  foreach my $cust_bill_pay (@cust_bill_pay) {
+    my $payment = $cust_bill_pay->cust_pay;
+    my($date,$invnum,$payby,$payinfo,$paid)=($payment->_date,
+                                             $cust_bill_pay->invnum,
+                                             $payment->payby,
+                                             $payment->payinfo,
+                                             $cust_bill_pay->amount,
                       );
     push @history,
       "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
   }
+
+  my(@cust_credit_bill)=
+    qsearch('cust_credit_bill', { 'invnum'=> $bref->{invnum} } );
+  foreach my $cust_credit_bill (@cust_credit_bill) {
+    my $cust_credit = $cust_credit_bill->cust_credit;
+    my($date, $invnum, $crednum, $amount, $reason ) = (
+      $cust_credit->_date,
+      $cust_credit_bill->invnum,
+      $cust_credit_bill->crednum,
+      $cust_credit_bill->amount,
+      $cust_credit->reason,
+    );
+    push @history,
+      "$date\tCredit #$crednum, Invoice #$invnum $reason\t\t\t$amount\t";
+  }
 }
 
-@credits = qsearch('cust_credit',{'custnum'=>$custnum});
+@credits = grep $_->credited, qsearch('cust_credit',{'custnum'=>$custnum});
 foreach $credit (@credits) {
   my($cref)=$credit->hashref;
   push @history,
-    $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
-    $cref->{credited} . ") by " . $cref->{otaker} . " - " .
+    $cref->{_date} . "\t" .
+    qq!<A HREF="! . popurl(2). qq!edit/cust_credit_bill.cgi?!. $cref->{crednum} . qq!">!.
+    '<font color="#ff0000">Unapplied credit #' .
+    $cref->{crednum} . ", (Balance \$" .
+    $credit->credited . ")</font></A> ".
     $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
+}
 
-  my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
-  my($refund);
-  foreach $refund (@refunds) {
-    my($rref)=$refund->hashref;
-    push @history,
-      $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
-      $rref->{payby} . " " . $rref->{payinfo} . ") by " .
-      $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
-      $rref->{refund};
-  }
+my(@refunds)=qsearch('cust_refund',{'custnum'=> $custnum } );
+my($refund);
+foreach $refund (@refunds) {
+  my($rref)=$refund->hashref;
+  my($refundnum) = (
+    $refund->refundnum,
+  );
+
+  push @history,
+    $rref->{_date} . "\tRefund #$refundnum, (" .
+    $rref->{payby} . " " . $rref->{payinfo} . ") by " .
+    $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
+    $rref->{refund};
 }
 
+
         #formatting
         print &table(), <<END;
 <TR>