summaryrefslogtreecommitdiff
path: root/httemplate/misc/xmlhttp-cust_main-search.cgi
blob: 73c9ff8ec37e4bc5105098046ac52a6b4933f4b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
% if ( $sub eq 'custnum_search' ) { 
%   my $custnum = $cgi->param('arg');
%   my $return = [];
%   if ( $custnum =~ /^(\d+)$/ ) { #should also handle
%                                  # cust_main-agent_custid-format') eq 'ww?d+'
%	$return = findbycustnum_or_agent_custid($1);
%   }
<% encode_json($return) %>\
% } elsif ( $sub eq 'smart_search' ) {
%
%   my $string = $cgi->param('arg');
%   my @cust_main = smart_search( 'search' => $string,
%                                 'no_fuzzy_on_exact' => ! $FS::CurrentUser::CurrentUser->option('enable_fuzzy_on_exact'),
%                               );
%   my $return = [ map [ $_->custnum,
%                        $_->name,
%                        $_->balance,
%                        $_->ucfirst_status,
%                        $_->statuscolor,
%                        scalar($_->open_cust_bill)
%                      ],
%                    @cust_main
%                ];
%     
<% 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) : [];
<% encode_json($return) %>\
%   } else { #return nothing
[]
%   }
% } 
% elsif ( $sub eq 'exact_search' ) {
%   # XXX possibly should query each element separately
%   my $hashref = decode_json($cgi->param('arg'));
%   my @cust_main = qsearch('cust_main', $hashref);
%   my $return = [];
%   foreach (@cust_main) {
%     push @$return, {
%       custnum => $_->custnum,
%       name => $_->name_short,
%       address1 => $_->address1,
%       city => $_->city,
%     };
%   }
<% encode_json($return) %>\
% }
<%init>

my $sub = $cgi->param('sub');

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>