1533e8810583d700df3b0bd8bfe0859703265fe2
[freeside.git] / httemplate / misc / taxproduct.cgi
1 <%once>
2 my $conf = FS::Conf->new;
3 my $vendor = $conf->config('tax_data_vendor');
4 </%once>
5 <%init>
6 my $term = $cgi->param('term');
7 warn "taxproduct.cgi?$term"; # XXX debug
8 my $search = {
9   table => 'part_pkg_taxproduct',
10   hashref => { 'data_vendor' => $vendor }
11 };
12 if ( $term =~ /^\d+$/ ) {
13   $search->{extra_sql} = " AND taxproduct LIKE '$term%'";
14   $search->{order_by} = " ORDER BY taxproduct ASC";
15 } elsif ( length($term) ) {
16   $term = dbh->quote( lc($term) ); # protect against bad strings
17   $search->{extra_sql} = " AND POSITION($term IN LOWER(description)) > 0";
18   # and sort by how close to the beginning of the string it is
19   $search->{order_by} = " ORDER BY POSITION($term IN LOWER(description)) ASC, LOWER(description) ASC, taxproduct ASC";
20 }
21 my @taxproducts = qsearch($search);
22 my @results = map {
23   { label => $_->taxproduct . ' ' . $_->description,
24     value => $_->taxproductnum } 
25 } @taxproducts;
26 </%init>
27 <% encode_json(\@results) %>\