Merge branch 'patch-18' of https://github.com/gjones2/Freeside
[freeside.git] / httemplate / misc / xmlhttp-cust_main-search.cgi
1 % if ( $sub eq 'custnum_search' ) { 
2 %   my $custnum = $cgi->param('arg');
3 %   my $return = [];
4 %   if ( $custnum =~ /^(\d+)$/ ) { #should also handle
5 %                                  # cust_main-agent_custid-format') eq 'ww?d+'
6 %       $return = findbycustnum_or_agent_custid($1);
7 %   }
8 <% encode_json($return) %>\
9 % } elsif ( $sub eq 'smart_search' ) {
10 %
11 %   my $string = $cgi->param('arg');
12 %   my @cust_main = smart_search( 'search' => $string,
13 %                                 'no_fuzzy_on_exact' => ! $FS::CurrentUser::CurrentUser->option('enable_fuzzy_on_exact'),
14 %                               );
15 %   my $return = [ map [ $_->custnum,
16 %                        $_->name,
17 %                        $_->balance,
18 %                        $_->ucfirst_status,
19 %                        $_->statuscolor,
20 %                        scalar($_->open_cust_bill)
21 %                      ],
22 %                    @cust_main
23 %                ];
24 %     
25 <% encode_json($return) %>\
26 % } elsif ( $sub eq 'invnum_search' ) {
27 %
28 %   my $string = $cgi->param('arg');
29 %   if ( $string =~ /^(\d+)$/ ) {
30 %     my $inv = qsearchs('cust_bill', { 'invnum' => $1 });
31 %     my $return = $inv ? findbycustnum($inv->custnum) : [];
32 <% encode_json($return) %>\
33 %   } else { #return nothing
34 []
35 %   }
36 % } 
37 % elsif ( $sub eq 'exact_search' ) {
38 %   # XXX possibly should query each element separately
39 %   my $hashref = decode_json($cgi->param('arg'));
40 %   my @cust_main = qsearch('cust_main', $hashref);
41 %   my $return = [];
42 %   foreach (@cust_main) {
43 %     push @$return, {
44 %       custnum => $_->custnum,
45 %       name => $_->name_short,
46 %       address1 => $_->address1,
47 %       city => $_->city,
48 %     };
49 %   }
50 <% encode_json($return) %>\
51 % }
52 <%init>
53
54 my $sub = $cgi->param('sub');
55
56 sub findbycustnum {
57
58   my $c = qsearchs({
59     'table'     => 'cust_main',
60     'hashref'   => { 'custnum' => shift },
61     'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
62   }) or return [];
63
64   [ $c->custnum,
65     $c->name,
66     $c->balance,
67     $c->ucfirst_status,
68     $c->statuscolor,
69     scalar($c->open_cust_bill)
70   ];
71 }
72
73 sub findbycustnum_or_agent_custid {
74   my $num = shift;
75
76   my @or = ( 'agent_custid = ?' );
77   my @param = ( $num );
78
79   if ( $num =~ /^\d+$/ && $num <= 2147483647 ) { #need a bigint custnum? wow
80     my $conf = new FS::Conf;
81     if ( $conf->exists('cust_main-default_agent_custid') ) {
82       push @or, "( agent_custid IS NULL AND custnum = $num )";
83     } else {
84       push @or, "custnum = $num";
85     }
86   }
87
88   my $extra_sql = ' WHERE '. $FS::CurrentUser::CurrentUser->agentnums_sql.
89                   ' AND ( '. join(' OR ', @or). ' )';
90                       
91   [ map [ $_->custnum,
92           $_->name,
93           $_->balance,
94           $_->ucfirst_status,
95           $_->statuscolor,
96           scalar($_->open_cust_bill),
97         ],
98
99       qsearch({
100         'table'       => 'cust_main',
101         'hashref'     => {},
102         'extra_sql'   => $extra_sql,
103         'extra_param' => \@param,
104       })
105   ];
106 }
107
108 </%init>