customer location columns in more reports, #940
[freeside.git] / httemplate / search / elements / cust_pay_or_refund.html
index 6f4aaf8..3dc54d9 100755 (executable)
@@ -39,8 +39,8 @@ Examples:
          )
 
 </%doc>
-<% include( 'search.html',
-                'title'          => $title,
+<& search.html,
+                'title'          => $title, # XXX: translate
                 'name_singular'  => $name_singular,
                 'query'          => $sql_query,
                 'count_query'    => $count_query,
@@ -48,12 +48,13 @@ Examples:
                 'redirect_empty' => $opt{'redirect_empty'},
                 'header'         => \@header,
                 'fields'         => \@fields,
+                'sort_fields'    => \@sort_fields,
                 'align'          => $align,
                 'links'          => \@links,
+                'link_onclicks'  => \@link_onclicks,
                 'color'          => \@color,
                 'style'          => \@style,
-          )
-%>
+&>
 <%init>
 
 my %opt = @_;
@@ -74,8 +75,9 @@ $title = 'Unapplied ' if $unapplied;
 $title .= "\u$name_singular Search Results";
 
 my $link = '';
-if (    ( $curuser->access_right('View invoices') #XXX for now
-          || $curuser->access_right('View customer payments')
+if (    ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
+          || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
+          || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
         )
      && ! $opt{'disable_link'}
    )
@@ -133,17 +135,30 @@ if ( $cgi->param('tax_names') ) {
   }
 }
 
-my @header = ();
-my @fields = ();
+my @header;
+my @fields;
+my @sort_fields;
 my $align = '';
-my @links = ();
+my @links;
+my @link_onclicks;
 if ( $opt{'pre_header'} ) {
   push @header, @{ $opt{'pre_header'} };
   $align .= 'c' x scalar(@{ $opt{'pre_header'} });
   push @links, map '', @{ $opt{'pre_header'} };
   push @fields, @{ $opt{'pre_fields'} };
+  push @sort_fields, @{ $opt{'pre_fields'} };
 }
 
+my $sub_receipt = sub {
+  my $obj = shift;
+  my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
+
+  include('/elements/popup_link_onclick.html',
+    'action'  => $p.'view/cust_pay.html?link=popup;'.$objnum,
+    'actionlabel' => emt('Payment Receipt'),
+  );
+};
+
 push @header, "\u$name_singular",
               'Amount',
 ;
@@ -152,21 +167,25 @@ push @links, '', '';
 push @fields, 'payby_payinfo_pretty',
               sub { sprintf('$%.2f', shift->$amount_field() ) },
 ;
+push @link_onclicks, $sub_receipt, '',
+push @sort_fields, '', $amount_field;
 
 if ( $unapplied ) {
-  push @header, 'Unapplied';
+  push @header, emt('Unapplied');
   $align .= 'r';
   push @links, '';
   push @fields, sub { sprintf('$%.2f', shift->unapplied_amount) };
+  push @sort_fields, '';
 }
 
-push @header, 'Date';
+push @header, emt('Date');
 $align .= 'r';
 push @links, '';
 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
+push @sort_fields, '_date';
 
 unless ( $opt{'disable_by'} ) {
-  push @header, 'By';
+  push @header, emt('By');
   $align .= 'c';
   push @links, '';
   push @fields, sub { my $o = shift->otaker;
@@ -177,7 +196,7 @@ unless ( $opt{'disable_by'} ) {
 }
 
 if ( $tax_names ) {
-  push @header, ('Tax names', 'Tax province');
+  push @header, (emt('Tax names'), emt('Tax province'));
   $align .= 'cc';
   push @links, ('','');
   push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
@@ -226,6 +245,19 @@ if ( $cgi->param('magic') ) {
       $title = $agent->agent. " $title";
     }
 
+    if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
+      push @search, "refnum = $1";
+      my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
+      die "unknown refnum $1" unless $part_referral;
+      $title = $part_referral->referral. " $title";
+    }
+
+    if ( $cgi->param('cust_classnum') ) {
+      my @classnums = grep /^\d+$/, $cgi->param('cust_classnum');
+      push @search, 'cust_main.classnum IN('.join(',',@classnums).')'
+        if @classnums;
+    }
+
     if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
       push @search, "custnum = $1";
     }
@@ -311,6 +343,16 @@ if ( $cgi->param('magic') ) {
       push @search, "$table.payinfo = '$1'";
     }
 
+    if ( $cgi->param('ccpay') =~ /^([\w-:]+)$/ ) {
+      # I think that's all the characters we need to allow.
+      # To avoid confusion, this parameter searches both auth and order_number.
+      push @search, "($table.auth LIKE '$1%') OR ($table.order_number LIKE '$1%')";
+      push @fields, 'auth', 'order_number';
+      push @header, 'Auth #', 'Transaction #';
+      $align .= 'rr';
+
+    }
+
     if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
       push @search, "$table.usernum = $1";
     }
@@ -344,6 +386,15 @@ if ( $cgi->param('magic') ) {
 
     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
 
+  } elsif ( $cgi->param('magic') eq 'batchnum' ) {
+
+    $cgi->param('batchnum') =~ /^(\d+)$/
+      or die "illegal batchnum: ".$cgi->param('batchnum');
+
+    push @search, "batchnum = $1";
+
+    $orderby = "LOWER(company || ' ' || last || ' ' || first )";
+
   } else {
     die "unknown search magic: ". $cgi->param('magic');
   }
@@ -373,7 +424,7 @@ if ( $cgi->param('magic') ) {
   #here is the agent virtualization
   push @search, $curuser->agentnums_sql;
 
-  my $addl_from = ' LEFT JOIN cust_main USING ( custnum ) ';
+  my $addl_from = FS::UI::Web::join_cust_main('cust_pay');
   my $group_by = '';
 
   if ( $cgi->param('tax_names') ) {
@@ -414,7 +465,8 @@ if ( $cgi->param('magic') ) {
     'table'     => $table,
     'select'    => join(', ', @select),
     'hashref'   => {},
-    'extra_sql' => "$search $group_by ORDER BY $orderby",
+    'extra_sql' => "$search $group_by",
+    'order_by'  => "ORDER BY $orderby",
     'addl_from' => $addl_from,
   };