From 1d920e3661c29398763d05c82bbe3a493a19fbae Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 20 Feb 2008 01:21:15 +0000 Subject: [PATCH] update the tax class editor to enable taxclass adding, RT#2929 --- FS/FS/Schema.pm | 10 +++ FS/FS/Upgrade.pm | 12 ++- FS/FS/part_pkg_taxclass.pm | 36 ++++++++- htetc/handler.pl | 1 + httemplate/browse/cust_main_county.cgi | 107 ++++++++++++++++++++----- httemplate/edit/part_pkg_taxclass.html | 32 ++++++++ httemplate/edit/process/part_pkg_taxclass.html | 53 ++++++++++++ httemplate/elements/tr-select-taxclass.html | 5 +- 8 files changed, 228 insertions(+), 28 deletions(-) create mode 100644 httemplate/edit/part_pkg_taxclass.html create mode 100644 httemplate/edit/process/part_pkg_taxclass.html diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index 7f8e6dace..9548aa760 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -943,6 +943,16 @@ sub tables_hashref { 'index' => [ [ 'promo_code' ], [ 'disabled' ], [ 'agentnum' ], ], }, + 'part_pkg_taxclass' => { + 'columns' => [ + 'taxclassnum', 'serial', '', '', '', '', + 'taxclass', 'varchar', '', $char_d, '', '', + ], + 'primary_key' => 'taxclassnum', + 'unique' => [ [ 'taxclass' ] ], + 'index' => [], + }, + # 'part_title' => { # 'columns' => [ # 'titlenum', 'int', '', '', diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index 90e66d87d..cb4823012 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -49,8 +49,12 @@ sub upgrade { eval "use $class;"; die $@ if $@; - $class->_upgrade_data(%opt) - if $class->can('_upgrade_data'); + if ( $class->can('_upgrade_data') ) { + $class->_upgrade_data(%opt); + } else { + warn "WARNING: asked for upgrade of $table,". + " but FS::$table has no _upgrade_data method\n"; + } # my @records = @{ $data->{$table} }; # @@ -88,6 +92,10 @@ sub upgrade_data { #populate cust_pay.otaker 'cust_pay' => [], + + #populate part_pkg_taxclass for starters + 'part_pkg_taxclass' => [], + ; \%hash; diff --git a/FS/FS/part_pkg_taxclass.pm b/FS/FS/part_pkg_taxclass.pm index 341be0ebd..fda200ee9 100644 --- a/FS/FS/part_pkg_taxclass.pm +++ b/FS/FS/part_pkg_taxclass.pm @@ -2,6 +2,7 @@ package FS::part_pkg_taxclass; use strict; use vars qw( @ISA ); +use FS::UID qw(dbh); use FS::Record qw( qsearch qsearchs ); @ISA = qw(FS::Record); @@ -100,8 +101,7 @@ sub check { my $self = shift; my $error = - $self->ut_numbern('serial') - || $self->ut_number('taxclassnum') + $self->ut_numbern('taxclassnum') || $self->ut_text('taxclass') ; return $error if $error; @@ -111,6 +111,38 @@ sub check { =back +=cut + +# _upgrade_data +# +# Used by FS::Upgrade to migrate to a new database. + +sub _upgrade_data { # class method + my ($class, %opts) = @_; + + my $sth = dbh->prepare(' + SELECT DISTINCT taxclass + FROM cust_main_county + LEFT JOIN part_pkg_taxclass USING ( taxclass ) + WHERE taxclassnum IS NULL + AND taxclass IS NOT NULL + ') or die dbh->errstr; + $sth->execute or die $sth->errstr; + my %taxclass = map { $_->[0] => 1 } @{$sth->fetchall_arrayref}; + my @taxclass = grep $_, keys %taxclass; + + foreach my $taxclass ( @taxclass ) { + + my $part_pkg_taxclass = new FS::part_pkg_taxclass ( { + 'taxclass' => $taxclass, + } ); + my $error = $part_pkg_taxclass->insert; + die $error if $error; + + } + +} + =head1 BUGS Other tables (cust_main_county, part_pkg, agent_payment_gateway) have a text diff --git a/htetc/handler.pl b/htetc/handler.pl index 09bada4fc..26cdaf49f 100644 --- a/htetc/handler.pl +++ b/htetc/handler.pl @@ -149,6 +149,7 @@ sub handler use FS::cust_credit_bill; use FS::cust_main qw(smart_search); use FS::cust_main_county; + use FS::part_pkg_taxclass; use FS::cust_pay; use FS::cust_pkg; use FS::cust_pkg_reason; diff --git a/httemplate/browse/cust_main_county.cgi b/httemplate/browse/cust_main_county.cgi index 3e67b1122..12bdeb333 100755 --- a/httemplate/browse/cust_main_county.cgi +++ b/httemplate/browse/cust_main_county.cgi @@ -1,6 +1,7 @@ <% include( 'elements/browse.html', - 'title' => 'Tax Rates', + 'title' => "Tax Rates $title", 'name_singular' => 'tax rate', + 'menubar' => \@menubar, 'html_init' => $html_init, 'html_posttotal' => $html_posttotal, 'query' => { @@ -89,11 +90,20 @@ my $edit_onclick = sub { }; sub expand_link { - my( $row, $desc, %opt ) = @_; + my( $row, $desc ) = @_; my $taxnum = $row->taxnum; - $taxnum = "taxclass$taxnum" if $opt{'taxclass'}; + my $url = "${p}edit/cust_main_county-expand.cgi?$taxnum"; my $color = '#333399'; - qq!!; + + qq!!; +} + +sub separate_taxclasses_link { + my( $row ) = @_; + my $taxnum = $row->taxnum; + my $url = "${p}edit/process/cust_main_county-expand.cgi?taxclass=1;taxnum=$taxnum"; + + qq!!; } @@ -106,10 +116,12 @@ die "access denied" #my $money_char = $conf->config('money_char') || '$'; my $enable_taxclasses = $conf->exists('enable_taxclasses'); +my @menubar; + my $html_init = "Click on add states to specify a country's tax rates by state or province.
Click on add counties to specify a state's tax rates by county."; -$html_init .= "
Click on add taxclasses to specify tax classes." +$html_init .= "
Click on separate taxclasses to specify taxes per taxclass." if $enable_taxclasses; $html_init .= '

'; @@ -120,23 +132,60 @@ $html_init .= qq( ); -my $filter_country = ''; -if ( $cgi->param('filter_country') =~ /^(\w\w)$/ ) { - $filter_country = $1; +my $title = ''; + +my $country = ''; +if ( $cgi->param('country') =~ /^(\w\w)$/ ) { + $country = $1; + $title = $country; +} +$cgi->delete('country'); + +my $state = ''; +if ( $cgi->param('state') =~ /^([\w \-\'\[\]]+)$/ ) { + $state = $1; + $title = "$state, $title"; +} +$cgi->delete('state'); + +my $county = ''; +if ( $cgi->param('county') =~ /^([\w \-\'\[\]]+)$/ ) { + $county = $1; + $title = "$county county, $title"; } -$cgi->delete('filter_country'); +$cgi->delete('county'); + +$title = " for $title" if $title; + +my $taxclass = ''; +if ( $cgi->param('taxclass') =~ /^([\w \-]+)$/ ) { + $taxclass = $1; + $title .= " for $taxclass tax class"; +} +$cgi->delete('taxclass'); + +if ( $country || $taxclass ) { + push @menubar, 'View all tax rates' => $p.'browse/cust_main_county.cgi'; +} + $cgi->param('dummy', 1); my $country_filter_change = "window.location = '". - $cgi->self_url. ";filter_country=' + this.options[this.selectedIndex].value;"; + $cgi->self_url. ";country=' + this.options[this.selectedIndex].value;"; + +#restore this so pagination works +$cgi->param('country', $country) if $country; +$cgi->param('state', $state ) if $state; +$cgi->param('county', $county ) if $county; +$cgi->param('taxclass', $county ) if $taxclass; my $html_posttotal = '(show country: '. - qq(). qq(
add new)!; push @header2, '(per-package classification)'; - push @fields, sub { $_[0]->taxclass || '(all) '. - expand_link($_[0], 'Add Taxclasses', 'taxclass'=>1). - 'add taxclasses
' - }; - push @color, sub { shift->taxclass ? '000000' : '999999' }; - push @links, ''; + push @fields, sub { $_[0]->taxclass || '(all) '. + separate_taxclasses_link($_[0], 'Separate Taxclasses'). + 'separate taxclasses
' + }; + push @color, sub { shift->taxclass ? '000000' : '999999' }; + push @links, ''; push @link_onclicks, ''; $align .= 'l'; } diff --git a/httemplate/edit/part_pkg_taxclass.html b/httemplate/edit/part_pkg_taxclass.html new file mode 100644 index 000000000..e76705722 --- /dev/null +++ b/httemplate/edit/part_pkg_taxclass.html @@ -0,0 +1,32 @@ +<% include('/elements/header.html', "$action taxclass") %> + +<% include('/elements/error.html') %> + +
+ + + +Tax class + +

+ + +
+ +<% include('/elements/footer.html') %> + +<%init> + +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); + +my $taxclass = ''; +if ( $cgi->param('error') ) { + $taxclass = $cgi->param('taxclass'); +} + +my $action = 'Add'; + +my $p1 = popurl(1); + + diff --git a/httemplate/edit/process/part_pkg_taxclass.html b/httemplate/edit/process/part_pkg_taxclass.html new file mode 100644 index 000000000..8f149bb94 --- /dev/null +++ b/httemplate/edit/process/part_pkg_taxclass.html @@ -0,0 +1,53 @@ +% if ( $error ) { +% $cgi->param('error', $error); +<% $cgi->redirect(popurl(2). "part_pkg_taxclass.html?". $cgi->query_string ) %> +%} else { +<% $cgi->redirect(popurl(3). "browse/cust_main_county.cgi?taxclass=". uri_escape($part_pkg_taxclass->taxclass) ) %> +%} +<%init> + +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); + +my $part_pkg_taxclass = new FS::part_pkg_taxclass { + 'taxclass' => $cgi->param('taxclass'), +}; + +#maybe this whole thing should be in a transaction. at some point, no biggie +#none of the follow-up stuff will fail unless there's a more serious problem +#than a hanging record in part_pkg_taxclass... + +my $error = $part_pkg_taxclass->insert; + +unless ( $error ) { + #auto-add the new taxclass to any regions that have taxclasses already + + my $sth = dbh->prepare(" + SELECT country, state, county FROM cust_main_county + WHERE taxclass IS NOT NULL AND taxclass != '' + GROUP BY country, state, county + ") or die dbh->errstr; + $sth->execute or die $sth->errstr; + + while ( my $row = $sth->fetchrow_hashref ) { + warn "inserting for $row"; + my $cust_main_county = new FS::cust_main_county { + 'country' => $row->{country}, + 'state' => $row->{state}, + 'county' => $row->{county}, + 'tax' => 0, + 'taxclass' => $part_pkg_taxclass->taxclass, + #exempt_amount + #taxname + #setuptax + #recurtax + }; + $error = $cust_main_county->insert; + #last if $error; + die $error if $error; + } + + +} + + diff --git a/httemplate/elements/tr-select-taxclass.html b/httemplate/elements/tr-select-taxclass.html index a20dd17b5..3bba683f8 100644 --- a/httemplate/elements/tr-select-taxclass.html +++ b/httemplate/elements/tr-select-taxclass.html @@ -21,8 +21,9 @@ my $conf = new FS::Conf; unless ( $opt{'taxclasses'} ) { - my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county') - or die dbh->errstr; + #my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county') + my $sth = dbh->prepare('SELECT taxclass FROM part_pkg_taxclass') + or die dbh->errstr; $sth->execute or die $sth->errstr; my %taxclasses = map { $_->[0] => 1 } @{$sth->fetchall_arrayref}; @{ $opt{'taxclasses'} } = grep $_, keys %taxclasses; -- 2.11.0