show display_custnum on quick payment entry, RT#28616
[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 %                        $_->display_custnum,
22 %                      ],
23 %                    @cust_main
24 %                ];
25 %     
26 <% encode_json($return) %>\
27 % } elsif ( $sub eq 'invnum_search' ) {
28 %
29 %   my $string = $cgi->param('arg');
30 %   if ( $string =~ /^(\d+)$/ ) {
31 %     my $inv = qsearchs('cust_bill', { 'invnum' => $1 });
32 %     my $return = $inv ? findbycustnum($inv->custnum) : [];
33 <% encode_json($return) %>\
34 %   } else { #return nothing
35 []
36 %   }
37 % } 
38 % elsif ( $sub eq 'exact_search' ) {
39 %   # XXX possibly should query each element separately
40 %   my $hashref = decode_json($cgi->param('arg'));
41 %   my @cust_main = qsearch('cust_main', $hashref);
42 %   my $return = [];
43 %   foreach (@cust_main) {
44 %     push @$return, {
45 %       custnum => $_->custnum,
46 %       name => $_->name_short,
47 %       address1 => $_->address1,
48 %       city => $_->city,
49 %     };
50 %   }
51 <% encode_json($return) %>\
52 % }
53 <%init>
54
55 my $sub = $cgi->param('sub');
56
57 sub findbycustnum {
58
59   my $c = qsearchs({
60     'table'     => 'cust_main',
61     'hashref'   => { 'custnum' => shift },
62     'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
63   }) or return [];
64
65   [ $c->custnum,
66     $c->name,
67     $c->balance,
68     $c->ucfirst_status,
69     $c->statuscolor,
70     scalar($c->open_cust_bill),
71     $c->display_custnum,
72   ];
73 }
74
75 sub findbycustnum_or_agent_custid {
76   my $num = shift;
77
78   my @or = ( 'agent_custid = ?' );
79   my @param = ( $num );
80
81   if ( $num =~ /^\d+$/ && $num <= 2147483647 ) { #need a bigint custnum? wow
82     my $conf = new FS::Conf;
83     if ( $conf->exists('cust_main-default_agent_custid') ) {
84       push @or, "( agent_custid IS NULL AND custnum = $num )";
85     } else {
86       push @or, "custnum = $num";
87     }
88   }
89
90   my $extra_sql = ' WHERE '. $FS::CurrentUser::CurrentUser->agentnums_sql.
91                   ' AND ( '. join(' OR ', @or). ' )';
92                       
93   [ map [ $_->custnum,
94           $_->name,
95           $_->balance,
96           $_->ucfirst_status,
97           $_->statuscolor,
98           scalar($_->open_cust_bill),
99           $_->display_custnum,
100         ],
101
102       qsearch({
103         'table'       => 'cust_main',
104         'hashref'     => {},
105         'extra_sql'   => $extra_sql,
106         'extra_param' => \@param,
107       })
108   ];
109 }
110
111 </%init>