From f4bb9273f1ba174858e221fd37f6dd1dca4119e9 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Tue, 6 Sep 2016 12:42:34 -0700 Subject: [PATCH] UI improvements for selecting taxproducts, #71555 and #71556 --- httemplate/browse/part_pkg.cgi | 56 +++++++++++++- httemplate/browse/part_pkg_taxproduct/suretax.html | 3 +- httemplate/elements/select-taxproduct.html | 90 +++++++++++++++++++--- httemplate/elements/tr-part_pkg-taxproducts.html | 62 +++++++++++---- httemplate/misc/taxproduct.cgi | 24 ++++++ 5 files changed, 205 insertions(+), 30 deletions(-) create mode 100644 httemplate/misc/taxproduct.cgi diff --git a/httemplate/browse/part_pkg.cgi b/httemplate/browse/part_pkg.cgi index 1e8b51030..acc32113f 100755 --- a/httemplate/browse/part_pkg.cgi +++ b/httemplate/browse/part_pkg.cgi @@ -27,6 +27,19 @@ 'html_foot' => $html_foot, ) %> +<%def .style> + + + <%init> my $curuser = $FS::CurrentUser::CurrentUser; @@ -45,6 +58,7 @@ die "access denied" my $conf = new FS::Conf; my $taxclasses = $conf->exists('enable_taxclasses'); +my $taxvendor = $conf->config('tax_data_vendor'); my $money_char = $conf->config('money_char') || '$'; my $select = '*'; @@ -180,6 +194,7 @@ my $html_init = qq!

!; +$html_init .= include('.style'); $cgi->param('dummy', 1); @@ -562,6 +577,43 @@ if ( $taxclasses ) { push @header, 'Taxclass'; push @fields, sub { shift->taxclass() || ' '; }; $align .= 'l'; +} elsif ( $taxvendor ) { + push @header, 'Tax product'; + my @classnums = ( 'setup', 'recur' ); + my @classnames = ( 'Setup', 'Recur' ); + foreach ( qsearch('usage_class', { disabled => '' }) ) { + push @classnums, $_->classnum; + push @classnames, $_->classname; + } + my $taxproduct_sub = sub { + my $ppt = shift; + '' . encode_entities($ppt->taxproduct) . '' + }; + my $taxproduct_list_sub = sub { + my $part_pkg = shift; + my $base_ppt = $part_pkg->taxproduct; + my $out = []; + if ( $base_ppt ) { + push @$out, [ + { 'data' => '', 'align' => 'left' }, + { 'data' => &$taxproduct_sub($base_ppt), 'align' => 'right' }, + ]; + } + for (my $i = 0; $i < scalar @classnums; $i++) { + my $num = $part_pkg->option('usage_taxproductnum_' . $classnums[$i]); + next if !$num; + my $ppt = FS::part_pkg_taxproduct->by_key($num); + push @$out, [ + { 'data' => $classnames[$i] . ': ', 'align' => 'left', }, + { 'data' => &$taxproduct_sub($ppt), 'align' => 'right' }, + ]; + } + $out; + }; + push @fields, $taxproduct_list_sub; + $align .= 'l'; } # make a table of report class optionnames => the actual @@ -602,7 +654,9 @@ push @fields, sort grep { $options{$_} =~ /\S/ } grep { $_ !~ /^(setup|recur)_fee$/ - and $_ !~ /^report_option_\d+$/ } + and $_ !~ /^report_option_\d+$/ + and $_ !~ /^usage_taxproductnum_/ + } keys %options ); if ( @report_options ) { diff --git a/httemplate/browse/part_pkg_taxproduct/suretax.html b/httemplate/browse/part_pkg_taxproduct/suretax.html index 9c00c5c69..8f237f360 100755 --- a/httemplate/browse/part_pkg_taxproduct/suretax.html +++ b/httemplate/browse/part_pkg_taxproduct/suretax.html @@ -138,8 +138,7 @@ my $id = $cgi->param('id'); my $select_onclick = sub { my $row = shift; my $taxnum = $row->taxproductnum; - my $code = $row->taxproduct; - my $desc = $row->description; + my $desc = $row->taxproduct . ' ' . $row->description; "select_taxproduct('$taxnum', '$desc')"; }; diff --git a/httemplate/elements/select-taxproduct.html b/httemplate/elements/select-taxproduct.html index 5feb71d80..d08ac22d1 100644 --- a/httemplate/elements/select-taxproduct.html +++ b/httemplate/elements/select-taxproduct.html @@ -1,14 +1,79 @@ -<% $opt{'prefix'} %> - <% $opt{'postfix'} %> +% if (!$init) { +% $init = 1; + + +% } +<% $opt{'prefix'} %> + + + +<% $opt{'postfix'} %> +<%shared> +my $init = 0; + <%init> my %opt = @_; @@ -19,7 +84,8 @@ my $description = $opt{'taxproduct_description'}; unless ( $description || ! $value ) { my $part_pkg_taxproduct = qsearchs( 'part_pkg_taxproduct', { 'taxproductnum'=> $value } ); - $description = $part_pkg_taxproduct->description + $description = $part_pkg_taxproduct->taxproduct . ' ' . + $part_pkg_taxproduct->description if $part_pkg_taxproduct; } diff --git a/httemplate/elements/tr-part_pkg-taxproducts.html b/httemplate/elements/tr-part_pkg-taxproducts.html index c57232c08..5dcea09f1 100644 --- a/httemplate/elements/tr-part_pkg-taxproducts.html +++ b/httemplate/elements/tr-part_pkg-taxproducts.html @@ -1,18 +1,39 @@ +%# by default, only show default - Tax products + <% emt('Tax product') %> + <& select-taxproduct.html, + %opt, + 'field' => $field.'_', # expected in edit/process/part_pkg + 'curr_value' => $curr_values{''}, + &> + + +% if ( !$separate ) { + + + + + + +% } % foreach my $usage_class (@classes) { % my $classnum = $usage_class->classnum; -% my $curr_value = -% $cgi->param("usage_taxproductnum_$classnum") -% || $pkg_options{"usage_taxproductnum_$classnum"} -% || ''; - - <% $usage_class->classname %> + + <% $usage_class->classname %> <& select-taxproduct.html, %opt, 'field' => $field.'_'.$classnum, - 'curr_value' => $curr_value + 'curr_value' => $curr_values{$classnum}, &> @@ -20,18 +41,29 @@ <%init> my %opt = @_; my $field = delete($opt{field}) || 'taxproductnum'; +my @classes = qsearch('usage_class', { 'disabled' => '' }); +unshift @classes, + FS::usage_class->new({ 'classnum' => 'setup', 'classname' => 'Setup', }), + FS::usage_class->new({ 'classnum' => 'recur', 'classname' => 'Recur', }), +; +my $separate = 0; # will change to 1 if any non-default classes have values + my $pkgpart = delete($opt{pkgpart}); +my %curr_values; my %pkg_options; if ($pkgpart) { my $part_pkg = FS::part_pkg->by_key($pkgpart); %pkg_options = $part_pkg->options; - $pkg_options{'usage_taxproductnum_'} = $part_pkg->taxproductnum; + $curr_values{''} = $part_pkg->taxproductnum; } -my @classes = qsearch('usage_class', { 'disabled' => '' }); -unshift @classes, - FS::usage_class->new({ 'classnum' => '', 'classname' => '(default)', }), - FS::usage_class->new({ 'classnum' => 'setup', 'classname' => 'Setup', }), - FS::usage_class->new({ 'classnum' => 'recur', 'classname' => 'Recur', }), -; +foreach my $usage_class (@classes) { + my $classnum = $usage_class->classnum; + my $curr_value = + $cgi->param("usage_taxproductnum_$classnum") + || $pkg_options{"usage_taxproductnum_$classnum"} + || ''; + $curr_values{$classnum} = $curr_value; + $separate = 1 if ( length($classnum) and length($curr_value) ); +} diff --git a/httemplate/misc/taxproduct.cgi b/httemplate/misc/taxproduct.cgi new file mode 100644 index 000000000..b22849343 --- /dev/null +++ b/httemplate/misc/taxproduct.cgi @@ -0,0 +1,24 @@ +<%once> +my $conf = FS::Conf->new; +my $vendor = $conf->config('tax_data_vendor'); + +<%init> +my $term = $cgi->param('term'); +warn "taxproduct.cgi?$term"; # XXX debug +my $search = { table => 'part_pkg_taxproduct' }; +if ( $term =~ /^\d+$/ ) { + $search->{extra_sql} = " WHERE taxproduct LIKE '$term%'"; + $search->{order_by} = " ORDER BY taxproduct ASC"; +} elsif ( length($term) ) { + $term = dbh->quote( lc($term) ); # protect against bad strings + $search->{extra_sql} = " WHERE POSITION($term IN LOWER(description)) > 0"; + # and sort by how close to the beginning of the string it is + $search->{order_by} = " ORDER BY POSITION($term IN LOWER(description)) ASC, LOWER(description) ASC, taxproduct ASC"; +} +my @taxproducts = qsearch($search); +my @results = map { + { label => $_->taxproduct . ' ' . $_->description, + value => $_->taxproductnum } +} @taxproducts; + +<% encode_json(\@results) %>\ -- 2.11.0