select quick payment entry batch in payment report, RT#26343
[freeside.git] / httemplate / search / elements / cust_pay_or_refund.html
index 4212daa..c272a16 100755 (executable)
@@ -252,9 +252,12 @@ if ( $cgi->param('magic') ) {
       $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).')'
+    # cust_classnum (false laziness w/ elements/cust_main_dayranges.html, prepaid_income.html, cust_bill_pay.html, cust_bill_pkg.html cust_bill_pkg_referral.html, unearned_detail.html, cust_credit.html, cust_credit_refund.html, cust_main::Search::search_sql)
+    if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
+      my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
+      push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
+                        join(',', map { $_ || '0' } @classnums ).
+                    ' )'
         if @classnums;
     }
 
@@ -268,7 +271,7 @@ if ( $cgi->param('magic') ) {
       foreach my $payby ( $cgi->param('payby') ) {
 
         $payby =~
-          /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
+          /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro|Tokenized))?$/
             or die "illegal payby $payby";
 
         my $payby_search = "$table.payby = '$1'";
@@ -279,6 +282,7 @@ if ( $cgi->param('magic') ) {
 
           my $search;
           if ( $cardtype eq 'VisaMC' ) {
+
             #avoid posix regexes for portability
             $search =
               " ( (     substring($table.payinfo from 1 for 1) = '4'     ".
@@ -302,11 +306,14 @@ if ( $cgi->param('magic') ) {
               "   OR substring($table.payinfo from 1 for 2) = '55' ".
 #              "   OR substring($table.payinfo from 1 for 2) = '36' ". #Diner's int'l was processed as Visa/MC inside US, now Discover
               " ) ";
+
           } elsif ( $cardtype eq 'Amex' ) {
+
             $search =
               " (    substring($table.payinfo from 1 for 2 ) = '34' ".
               "   OR substring($table.payinfo from 1 for 2 ) = '37' ".
               " ) ";
+
           } elsif ( $cardtype eq 'Discover' ) {
 
             my $conf = new FS::Conf;
@@ -344,7 +351,9 @@ if ( $cgi->param('magic') ) {
               ).
               "   OR substring($table.payinfo from 1 for 3 ) = '622'   ". #China Union Pay processed as Discover outside CN
               " ) ";
-          } elsif ( $cardtype eq 'Maestro' ) { 
+
+          } elsif ( $cardtype eq 'Maestro' ) {
+
             $search =
               " (    substring($table.payinfo from 1 for 2 ) = '63'     ".
               "   OR substring($table.payinfo from 1 for 2 ) = '67'     ".
@@ -361,6 +370,11 @@ if ( $cgi->param('magic') ) {
               "   OR substring($table.payinfo from 1 for 6 )            ".
               "      SIMILAR TO '49118[1-2]'                             ".
               " ) ";
+
+          } elsif ( $cardtype eq 'Tokenized' ) {
+
+            $search = " substring($table.payinfo from 1 for 2 ) = '99' ";
+
           } else {
             die "unknown card type $cardtype";
           }
@@ -376,7 +390,7 @@ if ( $cgi->param('magic') ) {
 
       }
 
-      push @search, join(' OR ', @all_payby_search) if @all_payby_search;
+      push @search, ' ( '. join(' OR ', @all_payby_search). ' ) ' if @all_payby_search;
 
     }
 
@@ -406,6 +420,7 @@ if ( $cgi->param('magic') ) {
     }
 
     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+
     push @search, "_date >= $beginning ",
                   "_date <= $ending";
 
@@ -425,8 +440,6 @@ if ( $cgi->param('magic') ) {
     $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
       or die "illegal paybatch: ". $cgi->param('paybatch');
 
-    push @search, "paybatch = '$1'";
-
     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
 
   } elsif ( $cgi->param('magic') eq 'batchnum' ) {
@@ -442,6 +455,10 @@ if ( $cgi->param('magic') ) {
     die "unknown search magic: ". $cgi->param('magic');
   }
 
+  if ( $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/ ) {
+    push @search, "paybatch = '$1'";
+  }
+
   #unapplied payment/refund
   if ( $unapplied ) {
     push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
@@ -467,7 +484,7 @@ if ( $cgi->param('magic') ) {
   #here is the agent virtualization
   push @search, $curuser->agentnums_sql;
 
-  my $addl_from = FS::UI::Web::join_cust_main('cust_pay');
+  my $addl_from = FS::UI::Web::join_cust_main($table);
   my $group_by = '';
 
   if ( $cgi->param('tax_names') ) {