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