optimize customer view: avoid looking up config values inside loops, RT#4728
[freeside.git] / httemplate / view / cust_main / payment_history / payment.html
index 96fca7f..2e24b17 100644 (file)
@@ -4,7 +4,6 @@
 
 my( $cust_pay, %opt ) = @_;
 
-my $conf = new FS::Conf;
 my $curuser = $FS::CurrentUser::CurrentUser;
 
 my $payby = $cust_pay->payby;
@@ -40,15 +39,30 @@ if (    scalar(@cust_bill_pay)   == 0
   $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
   $post = '</FONT></B>';
   if ( $curuser->access_right('Apply payment') ) {
-    $apply = ' ('. include( '/elements/popup_link.html',
-                              'label'       => 'apply',
-                              'action'      => "${p}edit/cust_bill_pay.cgi?".
-                                               $cust_pay->paynum,
-                              'actionlabel' => 'Apply payment',
-                              'width'       => 392,
-                              #default# 'height' => 336,
-                          ).
-              ')';
+    if ( $cust_pay->cust_main->total_owed > 0 ) {
+      $apply = ' ('.
+               include( '/elements/popup_link.html',
+                          'label'       => 'apply',
+                          'action'      => "${p}edit/cust_bill_pay.cgi?".
+                                           $cust_pay->paynum,
+                          'actionlabel' => 'Apply payment',
+                          'width'       => 392,
+                          #default# 'height' => 336,
+                      ).
+                ')';
+    }
+    if ( $cust_pay->cust_main->total_unapplied_refunds > 0 ) {
+      $apply.= ' ('.
+               include( '/elements/popup_link.html',
+                          'label'       => 'apply to refund',
+                          'action'      => "${p}edit/cust_pay_refund.cgi?".
+                                           $cust_pay->paynum,
+                          'actionlabel' => 'Apply payment to refund',
+                          'width'       => 392,
+                          #default# 'height' => 336,
+                      ).
+               ')';
+    }
   }
 } elsif (    scalar(@cust_bill_pay)   == 1
           && scalar(@cust_pay_refund) == 0
@@ -85,15 +99,30 @@ if (    scalar(@cust_bill_pay)   == 0
              '<B><FONT COLOR="#FF0000">$'.
              $cust_pay->unapplied. ' unapplied</FONT></B>';
     if ( $curuser->access_right('Apply payment') ) {
-      $desc = ' ('. include( '/elements/popup_link.html',
-                               'label'      => 'apply',
-                               'action'     => "${p}edit/cust_bill_pay.cgi?".
-                                               $cust_pay->paynum,
-                               'actionlabel' => 'Apply payment',
-                               'width'      => 392,
-                               #default# 'height' => 336,
-                          ).
-              ')';
+      if ( $cust_pay->cust_main->total_owed > 0 ) {
+        $apply = ' ('.
+                 include( '/elements/popup_link.html',
+                            'label'      => 'apply',
+                            'action'     => "${p}edit/cust_bill_pay.cgi?".
+                                            $cust_pay->paynum,
+                            'actionlabel' => 'Apply payment',
+                            'width'      => 392,
+                            #default# 'height' => 336,
+                        ).
+                 ')';
+      }
+      if ( $cust_pay->cust_main->total_unapplied_refunds > 0 ) {
+        $apply.= ' ('.
+                 include( '/elements/popup_link.html',
+                            'label'      => 'apply to refund',
+                            'action'     => "${p}edit/cust_pay_refund.cgi?".
+                                            $cust_pay->paynum,
+                            'actionlabel' => 'Apply payment to refund',
+                            'width'      => 392,
+                            #default# 'height' => 336,
+                        ).
+                 ')';
+      }
     }
     $desc .= '<BR>';
   }
@@ -109,7 +138,7 @@ my $view =
    ')';
 
 my $refund = '';
-my $refund_days = $conf->config('card_refund-days') || 120;
+my $refund_days = $opt{'card_refund-days'} || 120;
 if (    $cust_pay->closed !~ /^Y/i
      && $cust_pay->payby =~ /^(CARD|CHEK)$/
      && time-$cust_pay->_date < $refund_days*86400
@@ -149,7 +178,7 @@ if (    $cust_pay->closed !~ /^Y/i
 
 my $delete = '';
 if ( $cust_pay->closed !~ /^Y/i
-     && $conf->exists('deletepayments')
+     && $opt{'deletepayments'}
      && $curuser->access_right('Delete payment')
    )
 {