if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
<% $opt{prefix} %>addRow();
}
- var customer = document.getElementById('customer'+searchrow);
- customer.value = 'searching...';
- customer.disabled = true;
- customer.style.color = '#000000';
- customer.style.backgroundColor = '#dddddd';
+
+ var customer_obj = document.getElementById('customer'+searchrow);
+ customer_obj.value = 'searching...';
+ customer_obj.disabled = true;
+ customer_obj.style.color = '#000000';
+ customer_obj.style.backgroundColor = '#dddddd';
var customer_select = document.getElementById('cust_select'+searchrow);
- customer.style.display = '';
+ customer_obj.style.display = '';
customer_select.style.display = 'none';
var invnum = document.getElementById('invnum'+searchrow);
function search_custnum_update(customers) {
- var customerArray = eval('(' + customers + ')') || [];
- update_customer(searchrow, customerArray);
+ var customerArrayArray = eval('(' + customers + ')') || [];
+ if ( customerArrayArray.length == 1 ) {
+
+ update_customer(searchrow, customerArrayArray[0]);
% if ( $opt{custnum_update_callback} ) {
- <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
+ <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
% }
+
+ } else {
+
+ custnum_obj.value = 'Multiple'; // or something
+ custnum_obj.style.color = '#ff0000';
+
+ //blank the current list
+ customer_select.options.length = 0;
+
+ opt(customer_select, '', 'Multiple customers match "' + custnum + '" - select one', '#ff0000');
+ //add the multiple customers
+ for ( var s = 0; s < customerArrayArray.length; s++ ) {
+ opt(customer_select,
+ JSON.stringify(customerArrayArray[s]),
+ customerArrayArray[s][1],
+ '#000000');
+ }
+
+ opt(customer_select, 'cancel', '(Edit search string)', '#000000');
+
+ customer_obj.style.display = 'none';
+
+ customer_select.style.display = '';
+
+ }
+
}
custnum_search(custnum, search_custnum_update );
% if ( $sub eq 'custnum_search' ) {
% my $custnum = $cgi->param('arg');
% my $return = [];
-% if ( $custnum =~ /^(\d+)$/ ) {
-% $return = findbycustnum($1,0);
-% $return = findbycustnum($1,1) if(!scalar(@$return));
+% if ( $custnum =~ /^(\d+)$/ ) { #should also handle
+% # cust_main-agent_custid-format') eq 'ww?d+'
+% $return = findbycustnum_or_agent_custid($1);
% }
<% objToJson($return) %>
% } elsif ( $sub eq 'smart_search' ) {
% my @cust_main = smart_search( 'search' => $string,
% 'no_fuzzy_on_exact' => 1, #pref?
% );
-% my $return = [ map [ $_->custnum, $_->name, $_->balance, $_->ucfirst_status, $_->statuscolor, scalar($_->open_cust_bill) ], @cust_main ];
+% my $return = [ map [ $_->custnum,
+% $_->name,
+% $_->balance,
+% $_->ucfirst_status,
+% $_->statuscolor,
+% scalar($_->open_cust_bill)
+% ],
+% @cust_main
+% ];
%
<% objToJson($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,0) : [];
+% my $return = $inv ? findbycustnum($inv->custnum) : [];
<% objToJson($return) %>
% } else { #return nothing
[]
% }
<%init>
-my $conf = new FS::Conf;
-
my $sub = $cgi->param('sub');
-sub findbycustnum{
- my $custnum = shift;
- my $agent = shift;
- my $hashref = { 'custnum' => $custnum };
- $hashref = { 'agent_custid' => $custnum } if $agent;
- my $c = qsearchs({
- 'table' => 'cust_main',
- 'hashref' => $hashref,
- 'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
- });
- return [ $c->custnum, $c->name, $c->balance, $c->ucfirst_status, $c->statuscolor, scalar($c->open_cust_bill) ]
- if $c;
- [];
+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->ucfirst_status,
+ $c->statuscolor,
+ scalar($c->open_cust_bill)
+ ];
+}
+
+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,
+ $_->ucfirst_status,
+ $_->statuscolor,
+ scalar($_->open_cust_bill),
+ ],
+
+ qsearch({
+ 'table' => 'cust_main',
+ 'hashref' => {},
+ 'extra_sql' => $extra_sql,
+ 'extra_param' => \@param,
+ })
+ ];
}
+
</%init>