RT# 73964 - Added biling event action to send an email to phone nunber, and updated...
[freeside.git] / httemplate / misc / xmlhttp-cust_main-search.cgi
index 436501e..40697f1 100644 (file)
@@ -1,27 +1,34 @@
 % 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) %>
+<% encode_json($return) %>\
 % } elsif ( $sub eq 'smart_search' ) {
 %
 %   my $string = $cgi->param('arg');
-%   my @cust_main = smart_search( 'search' => $string,
-%                                 'no_fuzzy_on_exact' => 1, #pref?
-%                               );
-%   my $return = [ map [ $_->custnum, $_->name, $_->balance, $_->ucfirst_status, $_->statuscolor ], @cust_main ];
+%   my @cust_main = smart_search( 'search' => $string );
+%   my $return = [ map [ $_->custnum,
+%                        $_->name,
+%                        $_->balance,
+%                        $_->status_label,
+%                        $_->statuscolor,
+%                        scalar($_->open_cust_bill),
+%                        $_->display_custnum,
+%                      ],
+%                    @cust_main
+%                ];
 %     
-<% objToJson($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,0) : [];
-<% objToJson($return) %>
+%     my $return = $inv ? findbycustnum($inv->custnum) : [];
+<% encode_json($return) %>\
 %   } else { #return nothing
 []
 %   }
 %       city => $_->city,
 %     };
 %   }
-<% objToJson($return) %>
+<% encode_json($return) %>\
 % }
 <%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 ] 
-       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->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,
+      })
+  ];
 }
+
 </%init>