X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=httemplate%2Fmisc%2Fxmlhttp-cust_main-search.cgi;h=40697f134820fb474e5f8c8c84e4b36e0b09f494;hp=37462303950f6a8c67c812adbc2c0e5a34b42b10;hb=7d80f005462758e0271215240cdf99a9336f03dd;hpb=9dc88f6c738f30ce1eb6339ba4b739b45555dea4 diff --git a/httemplate/misc/xmlhttp-cust_main-search.cgi b/httemplate/misc/xmlhttp-cust_main-search.cgi index 374623039..40697f134 100644 --- a/httemplate/misc/xmlhttp-cust_main-search.cgi +++ b/httemplate/misc/xmlhttp-cust_main-search.cgi @@ -1,24 +1,109 @@ -% if ( $sub eq 'custnum_search' ) { -% +% if ( $sub eq 'custnum_search' ) { % my $custnum = $cgi->param('arg'); -% my $cust_main = qsearchs({ -% 'table' => 'cust_main', -% 'hashref' => { 'custnum' => $custnum }, -% 'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql, -% }); -% -"<% $cust_main ? $cust_main->name : '' %>" -% +% my $return = []; +% if ( $custnum =~ /^(\d+)$/ ) { #should also handle +% # cust_main-agent_custid-format') eq 'ww?d+' +% $return = findbycustnum_or_agent_custid($1); +% } +<% encode_json($return) %>\ % } elsif ( $sub eq 'smart_search' ) { % % my $string = $cgi->param('arg'); % my @cust_main = smart_search( 'search' => $string ); -% my $return = [ map [ $_->custnum, $_->name ], @cust_main ]; +% my $return = [ map [ $_->custnum, +% $_->name, +% $_->balance, +% $_->status_label, +% $_->statuscolor, +% scalar($_->open_cust_bill), +% $_->display_custnum, +% ], +% @cust_main +% ]; % -<% to_json($return) %> +<% encode_json($return) %>\ +% } elsif ( $sub eq 'invnum_search' ) { +% +% my $string = $cgi->param('arg'); +% if ( $string =~ /^(\d+)$/ ) { +% my $inv = qsearchs('cust_bill', { 'invnum' => $1 }); +% my $return = $inv ? findbycustnum($inv->custnum) : []; +<% encode_json($return) %>\ +% } else { #return nothing +[] +% } % } +% elsif ( $sub eq 'exact_search' ) { +% # XXX possibly should query each element separately +% my $hashref = decode_json($cgi->param('arg')); +% my @cust_main = qsearch('cust_main', $hashref); +% my $return = []; +% foreach (@cust_main) { +% push @$return, { +% custnum => $_->custnum, +% name => $_->name_short, +% address1 => $_->address1, +% city => $_->city, +% }; +% } +<% encode_json($return) %>\ +% } <%init> my $sub = $cgi->param('sub'); +sub findbycustnum { + + my $c = qsearchs({ + 'table' => 'cust_main', + 'hashref' => { 'custnum' => shift }, + 'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql, + }) or return []; + + [ $c->custnum, + $c->name, + $c->balance, + $c->status_label, + $c->statuscolor, + scalar($c->open_cust_bill), + $c->display_custnum, + ]; +} + +sub findbycustnum_or_agent_custid { + my $num = shift; + + my @or = ( 'agent_custid = ?' ); + my @param = ( $num ); + + if ( $num =~ /^\d+$/ && $num <= 2147483647 ) { #need a bigint custnum? wow + my $conf = new FS::Conf; + if ( $conf->exists('cust_main-default_agent_custid') ) { + push @or, "( agent_custid IS NULL AND custnum = $num )"; + } else { + push @or, "custnum = $num"; + } + } + + my $extra_sql = ' WHERE '. $FS::CurrentUser::CurrentUser->agentnums_sql. + ' AND ( '. join(' OR ', @or). ' )'; + + [ map [ $_->custnum, + $_->name, + $_->balance, + $_->status_label, + $_->statuscolor, + scalar($_->open_cust_bill), + $_->display_custnum, + ], + + qsearch({ + 'table' => 'cust_main', + 'hashref' => {}, + 'extra_sql' => $extra_sql, + 'extra_param' => \@param, + }) + ]; +} +