Merge branch 'master' of ssh://git.freeside.biz/home/git/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 %   my $return = [ map [ $_->custnum,
14 %                        $_->name,
15 %                        $_->balance,
16 %                        $_->status_label,
17 %                        $_->statuscolor,
18 %                        scalar($_->open_cust_bill),
19 %                        $_->display_custnum,
20 %                      ],
21 %                    @cust_main
22 %                ];
23 %     
24 <% encode_json($return) %>\
25 % } elsif ( $sub eq 'invnum_search' ) {
26 %
27 %   my $string = $cgi->param('arg');
28 %   if ( $string =~ /^(\d+)$/ ) {
29 %     my $inv = qsearchs('cust_bill', { 'invnum' => $1 });
30 %     my $return = $inv ? findbycustnum($inv->custnum) : [];
31 <% encode_json($return) %>\
32 %   } else { #return nothing
33 []
34 %   }
35 % } 
36 % elsif ( $sub eq 'exact_search' ) {
37 %   # XXX possibly should query each element separately
38 %   my $hashref = decode_json($cgi->param('arg'));
39 %   my @cust_main = qsearch('cust_main', $hashref);
40 %   my $return = [];
41 %   foreach (@cust_main) {
42 %     push @$return, {
43 %       custnum => $_->custnum,
44 %       name => $_->name_short,
45 %       address1 => $_->address1,
46 %       city => $_->city,
47 %     };
48 %   }
49 <% encode_json($return) %>\
50 % }
51 <%init>
52
53 my $sub = $cgi->param('sub');
54
55 sub findbycustnum {
56
57   my $c = qsearchs({
58     'table'     => 'cust_main',
59     'hashref'   => { 'custnum' => shift },
60     'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
61   }) or return [];
62
63   [ $c->custnum,
64     $c->name,
65     $c->balance,
66     $c->status_label,
67     $c->statuscolor,
68     scalar($c->open_cust_bill),
69     $c->display_custnum,
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           $_->status_label,
95           $_->statuscolor,
96           scalar($_->open_cust_bill),
97           $_->display_custnum,
98         ],
99
100       qsearch({
101         'table'       => 'cust_main',
102         'hashref'     => {},
103         'extra_sql'   => $extra_sql,
104         'extra_param' => \@param,
105       })
106   ];
107 }
108
109 </%init>