add cust_main.agent_custid (at least to schema and customer view, no manual editing...
[freeside.git] / httemplate / search / cust_bill.html
index 2108653..79c05dc 100755 (executable)
@@ -1,4 +1,9 @@
 <%
+
+   my $join_cust_main = 'LEFT JOIN cust_main USING ( custnum )';
+   #here is the agent virtualization
+   my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
+
    my( $count_query, $sql_query );
    my( $count_addl ) = ( '' );
    my( $distinct ) = ( '' );
    my($agentnum) = ( '' );
    my($open, $days) = ( '', '' );
    if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
-     $count_query = "SELECT COUNT(*) FROM cust_bill WHERE invnum = $2";
+     $count_query =
+       "SELECT COUNT(*) FROM cust_bill $join_cust_main".
+       "  WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
      $sql_query = {
        'table'     => 'cust_bill',
+       'addl_from' => $join_cust_main,
        'hashref'   => { 'invnum' => $2 },
        #'select'    => '*',
+       'extra_sql' => " AND $agentnums_sql", #agent virtualization
      };
    } else {
    #if ( $cgi->param('begin') || $cgi->param('end')
@@ -28,7 +37,7 @@
        $begin = str2time($1);
        push @where, "cust_bill._date >= $begin";
      }
-     if (  $cgi->param('ending')
+     if ( $cgi->param('ending')
            && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
        $end = str2time($1) + 86399;
        push @where, "cust_bill._date < $end";
        push @where, "cust_bill._date < $end";
      }
 
+     if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
+       push @where, "cust_bill.invnum >= $1";
+     }
+     if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
+       push @where, "cust_bill.invnum <= $1";
+     }
+
      if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
        $agentnum = $1;
        push @where, "cust_main.agentnum = $agentnum";
        push @where, "cust_bill._date < ". (time-86400*$days) if $days;
      }
 
+     #here is the agent virtualization
+     push @where, $agentnums_sql;
      my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
 
-     my $addl_from = 'left join cust_main using ( custnum )';
-
      if ( $cgi->param('newest_percust') ) {
        $distinct = 'DISTINCT ON ( cust_bill.custnum )';
        $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
                        '$%.2f total outstanding balance',
                      ];
      }
-     $count_query .=  " FROM cust_bill $addl_from $extra_sql";
+     $count_query .=  " FROM cust_bill $join_cust_main $extra_sql";
    
      $sql_query = {
        'table'     => 'cust_bill',
-       'addl_from' => $addl_from,
+       'addl_from' => $join_cust_main,
        'hashref'   => {},
        'select'    => "$distinct ". join(', ',
                         'cust_bill.*',