From 6626dc2a13c809092aa539c5a72bc72a0c56afdc Mon Sep 17 00:00:00 2001 From: jeff Date: Sun, 6 Apr 2008 16:12:46 +0000 Subject: [PATCH] new tax rating engine --- FS/FS/cust_bill.pm | 11 +- FS/FS/cust_main.pm | 239 ++++++++++++------------ FS/FS/cust_main_county.pm | 141 +++++++++++++- FS/FS/part_pkg.pm | 31 ++++ FS/FS/part_pkg_taxrate.pm | 7 +- FS/FS/tax_rate.pm | 188 +++++++------------ httemplate/browse/part_pkg_taxproduct.cgi | 246 +++++++++++++++++++++++++ httemplate/browse/tax_rate.cgi | 252 +++++++++++++++++++++----- httemplate/edit/elements/edit.html | 8 +- httemplate/edit/part_pkg.cgi | 15 +- httemplate/edit/process/elements/process.html | 6 +- httemplate/edit/process/tax_rate.html | 9 + httemplate/edit/tax_rate.html | 28 +-- 13 files changed, 863 insertions(+), 318 deletions(-) create mode 100755 httemplate/browse/part_pkg_taxproduct.cgi diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index eafe9307c..f536c972c 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -2486,9 +2486,18 @@ sub _items_pkg { $self->_items_cust_bill_pkg(\@cust_bill_pkg, %options); } +sub _taxsort { + return 0 unless $a cmp $b; + return -1 if $b eq 'Tax'; + return 1 if $a eq 'Tax'; + return -1 if $b eq 'Other surcharges'; + return 1 if $a eq 'Other surcharges'; + $a cmp $b; +} + sub _items_tax { my $self = shift; - my @cust_bill_pkg = grep { ! $_->pkgnum } $self->cust_bill_pkg; + my @cust_bill_pkg = sort _taxsort grep { ! $_->pkgnum } $self->cust_bill_pkg; $self->_items_cust_bill_pkg(\@cust_bill_pkg, @_); } diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index 37f98c69c..ceefeaf69 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -45,8 +45,6 @@ use FS::part_pkg; use FS::part_event; use FS::part_event_condition; #use FS::cust_event; -use FS::cust_tax_exempt; -use FS::cust_tax_exempt_pkg; use FS::type_pkgs; use FS::payment_gateway; use FS::agent_payment_gateway; @@ -2078,6 +2076,7 @@ sub bill { my( $total_setup, $total_recur ) = ( 0, 0 ); my %tax; + my %taxlisthash; my @precommit_hooks = (); foreach my $cust_pkg ( @@ -2247,140 +2246,94 @@ sub bill { unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) { + my @taxes = (); + my @taxoverrides = $part_pkg->part_pkg_taxoverride; + my $prefix = ( $conf->exists('tax-ship_address') && length($self->ship_last) ) ? 'ship_' : ''; - my %taxhash = map { $_ => $self->get("$prefix$_") } - qw( state county country ); - $taxhash{'taxclass'} = $part_pkg->taxclass; + if ( $conf->exists('enable_taxproducts') + && (scalar(@taxoverrides) || $part_pkg->taxproductnum ) + ) + { - my @taxes = qsearch( 'cust_main_county', \%taxhash ); + my @taxclassnums = (); + my $geocode = $self->geocode('cch'); - unless ( @taxes ) { - $taxhash{'taxclass'} = ''; - @taxes = qsearch( 'cust_main_county', \%taxhash ); - } + if ( scalar( @taxoverrides ) ) { + @taxclassnums = map { $_->taxclassnum } @taxoverrides; + }elsif ( $part_pkg->taxproductnum ) { + @taxclassnums = map { $_->taxclassnum } + $part_pkg->part_pkg_taxrate('cch', $geocode); + } - #one more try at a whole-country tax rate - unless ( @taxes ) { - $taxhash{$_} = '' foreach qw( state county ); - @taxes = qsearch( 'cust_main_county', \%taxhash ); - } + my $extra_sql = + "AND (". + join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")"; + + @taxes = qsearch({ 'table' => 'tax_rate', + 'hashref' => { 'geocode' => $geocode, }, + 'extra_sql' => $extra_sql, + }) + if scalar(@taxclassnums); + + + }else{ + + my %taxhash = map { $_ => $self->get("$prefix$_") } + qw( state county country ); + + $taxhash{'taxclass'} = $part_pkg->taxclass; + + @taxes = qsearch( 'cust_main_county', \%taxhash ); + + unless ( @taxes ) { + $taxhash{'taxclass'} = ''; + @taxes = qsearch( 'cust_main_county', \%taxhash ); + } + + #one more try at a whole-country tax rate + unless ( @taxes ) { + $taxhash{$_} = '' foreach qw( state county ); + @taxes = qsearch( 'cust_main_county', \%taxhash ); + } + + } #if $conf->exists('enable_taxproducts') # maybe eliminate this entirely, along with all the 0% records unless ( @taxes ) { $dbh->rollback if $oldAutoCommit; - return - "fatal: can't find tax rate for state/county/country/taxclass ". - join('/', ( map $self->get("$prefix$_"), - qw(state county country) - ), - $part_pkg->taxclass ). "\n"; + my $error; + if ( $conf->exists('enable_taxproducts') ) { + $error = + "fatal: can't find tax rate for zip/taxproduct/pkgpart ". + join('/', ( map $self->get("$prefix$_"), + qw(zip) + ), + $part_pkg->taxproduct_description, + $part_pkg->pkgpart ). "\n"; + }else{ + $error = + "fatal: can't find tax rate for state/county/country/taxclass ". + join('/', ( map $self->get("$prefix$_"), + qw(state county country) + ), + $part_pkg->taxclass ). "\n"; + } + return $error; } foreach my $tax ( @taxes ) { + my $taxname = ref( $tax ). ' '. $tax->taxnum; + if ( exists( $taxlisthash{ $taxname } ) ) { + push @{ $taxlisthash{ $taxname } }, $cust_bill_pkg; + }else{ + $taxlisthash{ $taxname } = [ $tax, $cust_bill_pkg ]; + } + } - my $taxable_charged = 0; - $taxable_charged += $setup - unless $part_pkg->setuptax =~ /^Y$/i - || $tax->setuptax =~ /^Y$/i; - $taxable_charged += $recur - unless $part_pkg->recurtax =~ /^Y$/i - || $tax->recurtax =~ /^Y$/i; - next unless $taxable_charged; - - if ( $tax->exempt_amount && $tax->exempt_amount > 0 ) { - #my ($mon,$year) = (localtime($sdate) )[4,5]; - my ($mon,$year) = (localtime( $sdate || $cust_bill->_date ) )[4,5]; - $mon++; - my $freq = $part_pkg->freq || 1; - if ( $freq !~ /(\d+)$/ ) { - $dbh->rollback if $oldAutoCommit; - return "daily/weekly package definitions not (yet?)". - " compatible with monthly tax exemptions"; - } - my $taxable_per_month = - sprintf("%.2f", $taxable_charged / $freq ); - - #call the whole thing off if this customer has any old - #exemption records... - my @cust_tax_exempt = - qsearch( 'cust_tax_exempt' => { custnum=> $self->custnum } ); - if ( @cust_tax_exempt ) { - $dbh->rollback if $oldAutoCommit; - return - 'this customer still has old-style tax exemption records; '. - 'run bin/fs-migrate-cust_tax_exempt?'; - } - - foreach my $which_month ( 1 .. $freq ) { - - #maintain the new exemption table now - my $sql = " - SELECT SUM(amount) - FROM cust_tax_exempt_pkg - LEFT JOIN cust_bill_pkg USING ( billpkgnum ) - LEFT JOIN cust_bill USING ( invnum ) - WHERE custnum = ? - AND taxnum = ? - AND year = ? - AND month = ? - "; - my $sth = dbh->prepare($sql) or do { - $dbh->rollback if $oldAutoCommit; - return "fatal: can't lookup exising exemption: ". dbh->errstr; - }; - $sth->execute( - $self->custnum, - $tax->taxnum, - 1900+$year, - $mon, - ) or do { - $dbh->rollback if $oldAutoCommit; - return "fatal: can't lookup exising exemption: ". dbh->errstr; - }; - my $existing_exemption = $sth->fetchrow_arrayref->[0] || 0; - - my $remaining_exemption = - $tax->exempt_amount - $existing_exemption; - if ( $remaining_exemption > 0 ) { - my $addl = $remaining_exemption > $taxable_per_month - ? $taxable_per_month - : $remaining_exemption; - $taxable_charged -= $addl; - - my $cust_tax_exempt_pkg = new FS::cust_tax_exempt_pkg ( { - 'billpkgnum' => $cust_bill_pkg->billpkgnum, - 'taxnum' => $tax->taxnum, - 'year' => 1900+$year, - 'month' => $mon, - 'amount' => sprintf("%.2f", $addl ), - } ); - $error = $cust_tax_exempt_pkg->insert; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return "fatal: can't insert cust_tax_exempt_pkg: $error"; - } - } # if $remaining_exemption > 0 - - #++ - $mon++; - #until ( $mon < 12 ) { $mon -= 12; $year++; } - until ( $mon < 13 ) { $mon -= 12; $year++; } - - } #foreach $which_month - - } #if $tax->exempt_amount - - $taxable_charged = sprintf( "%.2f", $taxable_charged); - - #$tax += $taxable_charged * $cust_main_county->tax / 100 - $tax{ $tax->taxname || 'Tax' } += - $taxable_charged * $tax->tax / 100 - - } #foreach my $tax ( @taxes ) } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP' @@ -2410,6 +2363,18 @@ sub bill { my $charged = sprintf( "%.2f", $total_setup + $total_recur ); + foreach my $tax ( keys %taxlisthash ) { + my $tax_object = shift @{ $taxlisthash{$tax} }; + my $listref_or_error = $tax_object->taxline( @{ $taxlisthash{$tax} } ); + unless (ref($listref_or_error)) { + $dbh->rollback if $oldAutoCommit; + return $listref_or_error; + } + + $tax{ $listref_or_error->[0] } += $listref_or_error->[1]; + + } + foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) { my $tax = sprintf("%.2f", $tax{$taxname} ); $charged = sprintf( "%.2f", $charged+$tax ); @@ -4862,6 +4827,40 @@ sub country_full { code2country($self->country); } +=item geocode DATA_PROVIDER + +Returns a value for the customer location as encoded by DATA_PROVIDER. +Currently this only makes sense for "CCH" as DATA_PROVIDER. + +=cut + +sub geocode { + my ($self, $data_provider) = (shift, shift); #always cch for now + + my $prefix = ( $conf->exists('tax-ship_address') && length($self->ship_last) ) + ? 'ship_' + : ''; + + my ($zip,$plus4) = split /-/, $self->get("${prefix}zip") + if $self->country eq 'US'; + + #CCH specific location stuff + my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'"; + + my $geocode = ''; + my $cust_tax_location = + qsearchs( { + 'table' => 'cust_tax_location', + 'hashref' => { 'zip' => $zip, 'data_provider' => $data_provider }, + 'extra_sql' => $extra_sql, + } + ); + $geocode = $cust_tax_location->geocode + if $cust_tax_location; + + $geocode; +} + =item cust_status =item status diff --git a/FS/FS/cust_main_county.pm b/FS/FS/cust_main_county.pm index 17f346071..3a0304b2c 100644 --- a/FS/FS/cust_main_county.pm +++ b/FS/FS/cust_main_county.pm @@ -4,7 +4,13 @@ use strict; use vars qw( @ISA @EXPORT_OK $conf @cust_main_county %cust_main_county $countyflag ); use Exporter; -use FS::Record qw( qsearch ); +use FS::Record qw( qsearch dbh ); +use FS::cust_bill_pkg; +use FS::cust_bill; +use FS::cust_pkg; +use FS::part_pkg; +use FS::cust_tax_exempt; +use FS::cust_tax_exempt_pkg; @ISA = qw( FS::Record ); @EXPORT_OK = qw( regionselector ); @@ -151,6 +157,139 @@ sub recurtax { return ''; } +=item taxline CUST_BILL_PKG, ... + +Returns a listref of a name and an amount of tax calculated for the list of +packages. Returns a scalar error message on error. + +=cut + +sub taxline { + my $self = shift; + + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $name = $self->taxname || 'Tax'; + my $amount = 0; + + foreach my $cust_bill_pkg (@_) { + + my $cust_bill = $cust_bill_pkg->cust_pkg->cust_bill; + my $part_pkg = $cust_bill_pkg->cust_pkg->part_pkg; + + my $taxable_charged = 0; + $taxable_charged += $cust_bill_pkg->setup + unless $part_pkg->setuptax =~ /^Y$/i + || $self->setuptax =~ /^Y$/i; + $taxable_charged += $cust_bill_pkg->recur + unless $part_pkg->recurtax =~ /^Y$/i + || $self->recurtax =~ /^Y$/i; + + return [ $name, 0 ] + unless $taxable_charged; + + if ( $self->exempt_amount && $self->exempt_amount > 0 ) { + #my ($mon,$year) = (localtime($cust_bill_pkg->sdate) )[4,5]; + my ($mon,$year) = + (localtime( $cust_bill_pkg->sdate || $cust_bill->_date ) )[4,5]; + $mon++; + my $freq = $part_pkg->freq || 1; + if ( $freq !~ /(\d+)$/ ) { + $dbh->rollback if $oldAutoCommit; + return "daily/weekly package definitions not (yet?)". + " compatible with monthly tax exemptions"; + } + my $taxable_per_month = + sprintf("%.2f", $taxable_charged / $freq ); + + #call the whole thing off if this customer has any old + #exemption records... + my @cust_tax_exempt = + qsearch( 'cust_tax_exempt' => { custnum=> $cust_bill->custnum } ); + if ( @cust_tax_exempt ) { + $dbh->rollback if $oldAutoCommit; + return + 'this customer still has old-style tax exemption records; '. + 'run bin/fs-migrate-cust_tax_exempt?'; + } + + foreach my $which_month ( 1 .. $freq ) { + + #maintain the new exemption table now + my $sql = " + SELECT SUM(amount) + FROM cust_tax_exempt_pkg + LEFT JOIN cust_bill_pkg USING ( billpkgnum ) + LEFT JOIN cust_bill USING ( invnum ) + WHERE custnum = ? + AND taxnum = ? + AND year = ? + AND month = ? + "; + my $sth = dbh->prepare($sql) or do { + $dbh->rollback if $oldAutoCommit; + return "fatal: can't lookup exising exemption: ". dbh->errstr; + }; + $sth->execute( + $cust_bill->custnum, + $self->taxnum, + 1900+$year, + $mon, + ) or do { + $dbh->rollback if $oldAutoCommit; + return "fatal: can't lookup exising exemption: ". dbh->errstr; + }; + my $existing_exemption = $sth->fetchrow_arrayref->[0] || 0; + + my $remaining_exemption = + $self->exempt_amount - $existing_exemption; + if ( $remaining_exemption > 0 ) { + my $addl = $remaining_exemption > $taxable_per_month + ? $taxable_per_month + : $remaining_exemption; + $taxable_charged -= $addl; + + my $cust_tax_exempt_pkg = new FS::cust_tax_exempt_pkg ( { + 'billpkgnum' => $cust_bill_pkg->billpkgnum, + 'taxnum' => $self->taxnum, + 'year' => 1900+$year, + 'month' => $mon, + 'amount' => sprintf("%.2f", $addl ), + } ); + my $error = $cust_tax_exempt_pkg->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "fatal: can't insert cust_tax_exempt_pkg: $error"; + } + } # if $remaining_exemption > 0 + + #++ + $mon++; + #until ( $mon < 12 ) { $mon -= 12; $year++; } + until ( $mon < 13 ) { $mon -= 12; $year++; } + + } #foreach $which_month + + } #if $tax->exempt_amount + + $taxable_charged = sprintf( "%.2f", $taxable_charged); + + $amount += $taxable_charged * $self->tax / 100 + } + + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + return [ $name, $amount ] +} + =back =head1 SUBROUTINES diff --git a/FS/FS/part_pkg.pm b/FS/FS/part_pkg.pm index dc0a4d58a..1e16f29e7 100644 --- a/FS/FS/part_pkg.pm +++ b/FS/FS/part_pkg.pm @@ -756,6 +756,37 @@ sub taxproduct_description { $part_pkg_taxproduct ? $part_pkg_taxproduct->description : ''; } +=item part_pkg_taxrate DATA_PROVIDER, GEOCODE + +Returns the package to taxrate m2m records for this package in the location +specified by GEOCODE (see L and ). + +=cut + +sub part_pkg_taxrate { + my $self = shift; + my ($data_provider, $geocode) = @_; + + my $dbh = dbh; + # CCH oddness in m2m + my $extra_sql = 'AND ('. + join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) } + qw(10 5 2) + ). + ')'; + my $order_by = 'ORDER BY taxclassnum, length(geocode) desc'; + my $select = 'DISTINCT ON(taxclassnum) *'; + + qsearch( { 'table' => 'part_pkg_taxrate', + 'select' => 'distinct on(taxclassnum) *', + 'hashref' => { 'data_provider' => $data_provider, + 'taxproductnum' => $self->taxproductnum, + }, + 'extra_sql' => $extra_sql, + 'order_by' => $order_by, + } ); +} + =item _rebless Reblesses the object into the FS::part_pkg::PLAN class (if available), where diff --git a/FS/FS/part_pkg_taxrate.pm b/FS/FS/part_pkg_taxrate.pm index aa1c3df76..3e7e7bd5b 100644 --- a/FS/FS/part_pkg_taxrate.pm +++ b/FS/FS/part_pkg_taxrate.pm @@ -211,11 +211,12 @@ sub batch_import { my $part_pkg_taxproduct = qsearchs( 'part_pkg_taxproduct', { %part_pkg_taxproduct } ); + unless ($part_pkg_taxproduct) { $part_pkg_taxproduct{'description'} = - join(' : ', map{ $hash->{$_} } qw(groupdesc itemdesc), - $providers{$hash->{'provider'}} || 'Unknown', - $customers{$hash->{'customer'}} || 'Unknown', + join(' : ', (map{ $hash->{$_} } qw(groupdesc itemdesc)), + $providers{$hash->{'provider'}}, + $customers{$hash->{'customer'}}, ); $part_pkg_taxproduct = new FS::part_pkg_taxproduct \%part_pkg_taxproduct; my $error = $part_pkg_taxproduct->insert; diff --git a/FS/FS/tax_rate.pm b/FS/FS/tax_rate.pm index 38e53434e..3d56a0de1 100644 --- a/FS/FS/tax_rate.pm +++ b/FS/FS/tax_rate.pm @@ -1,30 +1,19 @@ package FS::tax_rate; use strict; -use vars qw( @ISA @EXPORT_OK $conf $DEBUG $me +use vars qw( @ISA $DEBUG $me %tax_unittypes %tax_maxtypes %tax_basetypes %tax_authorities - %tax_passtypes - @tax_rate %tax_rate $countyflag ); -use Exporter; + %tax_passtypes ); use Date::Parse; -use Tie::IxHash; -use FS::Record qw( qsearchs qsearch dbh ); +use FS::Record qw( qsearchs dbh ); use FS::tax_class; +use FS::cust_bill_pkg; @ISA = qw( FS::Record ); -@EXPORT_OK = qw( regionselector ); -$DEBUG = 1; +$DEBUG = 0; $me = '[FS::tax_rate]'; -@tax_rate = (); -$countyflag = ''; - -#ask FS::UID to run this stuff for us later -$FS::UID::callback{'FS::tax_rate'} = sub { - $conf = new FS::Conf; -}; - =head1 NAME FS::tax_rate - Object methods for tax_rate objects @@ -44,9 +33,6 @@ FS::tax_rate - Object methods for tax_rate objects $error = $record->check; - ($county_html, $state_html, $country_html) = - FS::tax_rate::regionselector( $county, $state, $country ); - =head1 DESCRIPTION An FS::tax_rate object represents a tax rate, defined by locale. @@ -75,8 +61,7 @@ a location code provided by a tax authority a foreign key into FS::tax_class - the type of tax referenced but FS::part_pkg_taxrate - -=item effective_date +eitem effective_date the time after which the tax applies @@ -349,128 +334,81 @@ sub passtype_name { $tax_passtypes{$self->passtype}; } -=back - -=head1 SUBROUTINES +=item taxline CUST_BILL_PKG, ... -=over 4 - -=item regionselector [ COUNTY STATE COUNTRY [ PREFIX [ ONCHANGE [ DISABLED ] ] ] ] +Returns a listref of a name and an amount of tax calculated for the list +of packages. If an error occurs, a message is returned as a scalar. =cut -sub regionselector { - my ( $selected_county, $selected_state, $selected_country, - $prefix, $onchange, $disabled ) = @_; - - $prefix = '' unless defined $prefix; - - $countyflag = 0; +sub taxline { + my $self = shift; + my @cust_bill_pkg = @_; -# unless ( @tax_rate ) { #cache - @tax_rate = qsearch('tax_rate', {} ); - foreach my $c ( @tax_rate ) { - $countyflag=1 if $c->county; - #push @{$tax_rate{$c->country}{$c->state}}, $c->county; - $tax_rate{$c->country}{$c->state}{$c->county} = 1; - } -# } - $countyflag=1 if $selected_county; - - my $script_html = < - function opt(what,value,text) { - var optionName = new Option(text, value, false, false); - var length = what.length; - what.options[length] = optionName; - } - function ${prefix}country_changed(what) { - country = what.options[what.selectedIndex].text; - for ( var i = what.form.${prefix}state.length; i >= 0; i-- ) - what.form.${prefix}state.options[i] = null; -END - #what.form.${prefix}state.options[0] = new Option('', '', false, true); - - foreach my $country ( sort keys %tax_rate ) { - $script_html .= "\nif ( country == \"$country\" ) {\n"; - foreach my $state ( sort keys %{$tax_rate{$country}} ) { - ( my $dstate = $state ) =~ s/[\n\r]//g; - my $text = $dstate || '(n/a)'; - $script_html .= qq!opt(what.form.${prefix}state, "$dstate", "$text");\n!; - } - $script_html .= "}\n"; + if ($self->passflag eq 'N') { + return "fatal: can't (yet) handle taxes not passed to the customer"; } - $script_html .= <= 0; i-- ) - what.form.${prefix}county.options[i] = null; -END - - foreach my $country ( sort keys %tax_rate ) { - $script_html .= "\nif ( country == \"$country\" ) {\n"; - foreach my $state ( sort keys %{$tax_rate{$country}} ) { - $script_html .= "\nif ( state == \"$state\" ) {\n"; - #foreach my $county ( sort @{$tax_rate{$country}{$state}} ) { - foreach my $county ( sort keys %{$tax_rate{$country}{$state}} ) { - my $text = $county || '(n/a)'; - $script_html .= - qq!opt(what.form.${prefix}county, "$county", "$text");\n!; - } - $script_html .= "}\n"; - } - $script_html .= "}\n"; - } + if ($self->maxtype != 0 && $self->maxtype != 9) { + return qq!fatal: can't (yet) handle tax with "!. $self->maxtype_name. + '" threshold'; } - $script_html .= < -END - - my $county_html = $script_html; - if ( $countyflag ) { - $county_html .= qq!'; - } else { - $county_html .= - qq!!; + if ($self->maxtype == 9) { + return qq!fatal: can't (yet) handle tax with "!. $self->maxtype_name. + '" threshold'; # "texas" tax } - my $state_html = qq!'; - $state_html .= ''; + my $name = $self->taxname; + $name = 'Other surcharges' + if ($self->passtype == 2); + my $amount = 0; + + my $taxable_charged = 0; + unless ($self->setuptax =~ /^Y$/i) { + $taxable_charged += $_->setup foreach @cust_bill_pkg; + } + unless ($self->recurtax =~ /^Y$/i) { + $taxable_charged += $_->recur foreach @cust_bill_pkg; + } - my $country_html = qq!'; - ($county_html, $state_html, $country_html); + # + # XXX insert exemption handling here + # + # the tax or fee is applied to taxbase or feebase and then + # the excessrate or excess fee is applied to taxmax or feemax + # + + $amount += $taxable_charged * $self->tax; + $amount += $taxable_units * $self->fee; + + return [$name, $amount]; } +=back + +=head1 SUBROUTINES + +=over 4 + +=item batch_import + +=cut + sub batch_import { my $param = shift; @@ -518,6 +456,8 @@ sub batch_import { if length($hash->{$_}) > 80; } + ''; + }; } elsif ( $format eq 'extended' ) { diff --git a/httemplate/browse/part_pkg_taxproduct.cgi b/httemplate/browse/part_pkg_taxproduct.cgi new file mode 100755 index 000000000..3df819715 --- /dev/null +++ b/httemplate/browse/part_pkg_taxproduct.cgi @@ -0,0 +1,246 @@ +<% include( 'elements/browse.html', + 'title' => "Tax Products $title", + 'name_singular' => 'tax product', + 'menubar' => \@menubar, + 'html_init' => $html_init, + 'query' => { + 'table' => 'part_pkg_taxproduct', + 'hashref' => $hashref, + 'order_by' => 'ORDER BY description', + 'extra_sql' => $extra_sql, + }, + 'count_query' => $count_query, + 'header' => \@header, + 'fields' => \@fields, + 'align' => $align, + 'links' => \@links, + 'link_onclicks' => \@link_onclicks, + ) +%> +<%once> + +my $conf = new FS::Conf; + +my $select_link = [ 'javascript:void(0);', sub { ''; } ]; + + +<%init> + +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); + +my @menubar; +my $title = ''; + +my $data_vendor = ''; +if ( $cgi->param('data_vendor') =~ /^(\w+)$/ ) { + $data_vendor = $1; + $title = "$data_vendor"; +} +$cgi->delete('data_vendor'); + +$title = " for $title" if $title; + +my $taxproductnum = $1 + if ( $cgi->param('taxproductnum') =~ /^(\d+)$/ ); +my $tax_group = $1 + if ( $cgi->param('tax_group') =~ /^([- \w\(\).\/]+)$/ ); +my $tax_item = $1 + if ( $cgi->param('tax_item') =~ /^([- \w\(\).\/&%]+)$/ ); +my $tax_provider = $1 + if ( $cgi->param('tax_provider') =~ /^([ \w]+)$/ ); +my $tax_customer = $1 + if ( $cgi->param('tax_customer') =~ /^([ \w]+)$/ ); +my $id = $1 + if ( $cgi->param('id') =~ /^([ \w]+)$/ ); + +my $select_onclick = sub { + my $row = shift; + my $taxnum = $row->taxproductnum; + my $desc = $row->description; + "parent.document.getElementById('$id').value = $taxnum;". + "parent.document.getElementById('${id}_description').value = '$desc';". + "parent.cClick();"; +} + if $id; + +my $selected_part_pkg_taxproduct; +if ($taxproductnum) { + $selected_part_pkg_taxproduct = + qsearchs('part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum }); +} + +my $hashref = {}; +my $extra_sql = ''; +if ( $data_vendor ) { + $extra_sql .= ' WHERE data_vendor = '. dbh->quote($data_vendor); +} + +if ($tax_group || $tax_item || $tax_customer || $tax_provider) { + my $compare = "LIKE '". ( $tax_group || "%" ). " : ". ( $tax_item || "%" ). " : ". + ( $tax_provider || "%" ). " : ". ( $tax_customer || "%" ). "'"; + $compare = "= '$tax_group:$tax_item:$tax_provider:$tax_customer'" + if ($tax_group && $tax_item && $tax_provider && $tax_customer); + + $extra_sql .= ($extra_sql =~ /WHERE/ ? ' AND ' : ' WHERE '). + "description $compare"; + +} +$cgi->delete('tax_group'); +$cgi->delete('tax_item'); +$cgi->delete('tax_provider'); +$cgi->delete('tax_customer'); + + +if ( $tax_group || $tax_item || $tax_provider || $tax_customer ) { + push @menubar, 'View all tax products' => $p.'browse/part_pkg_taxproduct.cgi'; +} + +$cgi->param('dummy', 1); + +#restore this so pagination works +$cgi->param('data_vendor', $data_vendor) if $data_vendor; +$cgi->param('tax_group', $tax_group) if $tax_group; +$cgi->param('tax_item', $tax_item ) if $tax_item; +$cgi->param('tax_provider', $tax_provider ) if $tax_provider; +$cgi->param('tax_customer', $tax_customer ) if $tax_customer; + +my $count_query = "SELECT COUNT(*) FROM part_pkg_taxproduct $extra_sql"; + +my @header = ( 'Data Vendor', 'Group', 'Item', 'Provider', 'Customer' ); +my @links = ( $select_link, + $select_link, + $select_link, + $select_link, + $select_link, + ); +my @link_onclicks = ( $select_onclick, + $select_onclick, + $select_onclick, + $select_onclick, + $select_onclick, + ); +my $align = 'lllll'; + +my @fields = ( + 'data_vendor', + sub { shift->description =~ /^(.*):.*:.*:.*$/; $1;}, + sub { shift->description =~ /^.*:(.*):.*:.*$/; $1;}, + sub { shift->description =~ /^.*:.*:(.*):.*$/; $1;}, + sub { shift->description =~ /^.*:.*:.*:(.*)$/; $1;}, +); + +my $html_init = ''; + +$html_init = '
Current tax product: '. + $selected_part_pkg_taxproduct->description. + '


' + if $selected_part_pkg_taxproduct; + +my $type = $cgi->param('_type'); +$html_init .= qq( +
+ + + + + + +
+ + + + + + + + + + +
+
+ +); + + diff --git a/httemplate/browse/tax_rate.cgi b/httemplate/browse/tax_rate.cgi index b401b3786..5d43d5939 100755 --- a/httemplate/browse/tax_rate.cgi +++ b/httemplate/browse/tax_rate.cgi @@ -4,9 +4,10 @@ 'menubar' => \@menubar, 'html_init' => $html_init, 'query' => { - 'table' => 'tax_rate', - 'hashref' => $hashref, - 'order_by' => 'ORDER BY geocode, taxclassnum', + 'table' => 'tax_rate', + 'hashref' => $hashref, + 'order_by' => 'ORDER BY geocode, taxclassnum', + 'extra_sql' => $extra_sql, }, 'count_query' => $count_query, 'header' => \@header, @@ -24,21 +25,62 @@ my $conf = new FS::Conf; my $money_char = $conf->config('money_char') || '$'; -my $exempt_sub = sub { +my $rate_sub = sub { my $tax_rate = shift; - my @exempt = (); - push @exempt, - sprintf("$money_char%.2f per month", $tax_rate->exempt_amount ) - if $tax_rate->exempt_amount > 0; + my $units = $tax_rate->unittype_name; + $units =~ s/ / /g; + + my @rate = (); + push @rate, + ($tax_rate->tax * 100). '% (edit)' + if $tax_rate->tax > 0 || $tax_rate->taxbase > 0; + push @rate, + ($tax_rate->excessrate * 100). '% (edit)' + if $tax_rate->excessrate > 0; + push @rate, + $money_char. $tax_rate->fee. + qq! per $units(edit)! + if $tax_rate->fee > 0 || $tax_rate->feebase > 0; + push @rate, + $money_char. $tax_rate->excessfee. + qq! per $units(edit)! + if $tax_rate->excessfee > 0; + + + [ map [ {'data'=>$_} ], @rate ]; +}; + +my $limit_sub = sub { + my $tax_rate = shift; - push @exempt, 'Setup fee' + my $maxtype = $tax_rate->maxtype_name; + $maxtype =~ s/ / /g; + + my $units = $tax_rate->unittype_name; + $units =~ s/ / /g; + + my @limit = (); + push @limit, + sprintf("$money_char%.2f %s", $tax_rate->taxbase, $maxtype ) + if $tax_rate->taxbase > 0; + push @limit, + sprintf("$money_char%.2f tax", $tax_rate->taxmax ) + if $tax_rate->taxmax > 0; + push @limit, + $tax_rate->feebase. " $units". ($tax_rate->feebase == 1 ? '' : 's') + if $tax_rate->feebase > 0; + push @limit, + $tax_rate->feemax. " $units". ($tax_rate->feebase == 1 ? '' : 's') + if $tax_rate->feemax > 0; + + push @limit, 'Excluding setup fee' if $tax_rate->setuptax =~ /^Y$/i; - push @exempt, 'Recurring fee' + push @limit, 'Excluding recurring fee' if $tax_rate->recurtax =~ /^Y$/i; - [ map [ {'data'=>$_} ], @exempt ]; + [ map [ {'data'=>$_} ], @limit ]; }; my $oldrow; @@ -67,15 +109,7 @@ my $select_onclick = sub { my $row = shift; my $taxnum = $row->taxnum; my $color = '#333399'; - qq!overlib( OLiframeContent('${p}edit/tax_rate.html?$taxnum', 540, 420, 'edit_tax_rate_popup' ), CAPTION, 'Edit tax rate', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '$color', CGCOLOR, '$color' ); return false;!; -}; - -my $separate_taxclasses_link = sub { - my( $row ) = @_; - my $taxnum = $row->taxnum; - my $url = "${p}edit/process/tax_rate-expand.cgi?taxclassnum=1;taxnum=$taxnum"; - - qq!!; + qq!overlib( OLiframeContent('${p}edit/tax_rate.html?$taxnum', 540, 620, 'edit_tax_rate_popup' ), CAPTION, 'Edit tax rate', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK, BGCOLOR, '$color', CGCOLOR, '$color' ); return false;!; }; @@ -85,26 +119,19 @@ die "access denied" unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); my @menubar; - -my $html_init = - "Click on geocodes to specify rates for a new area."; -$html_init .= "
Click on separate taxclasses to specify taxes per taxclass."; -$html_init .= '

'; - -$html_init .= qq( - - - - -); - my $title = ''; -my $select_word = 'edit'; + +my $data_vendor = ''; +if ( $cgi->param('data_vendor') =~ /^(\w+)$/ ) { + $data_vendor = $1; + $title = "$data_vendor"; +} +$cgi->delete('data_vendor'); my $geocode = ''; if ( $cgi->param('geocode') =~ /^(\w+)$/ ) { $geocode = $1; - $title = "$geocode"; + $title = " geocode $geocode"; } $cgi->delete('geocode'); @@ -123,6 +150,36 @@ if ( $cgi->param('taxclassnum') =~ /^(\d+)$/ ) { } $cgi->delete('taxclassnum'); +my $tax_type = $1 + if ( $cgi->param('tax_type') =~ /^(\d+)$/ ); +my $tax_cat = $1 + if ( $cgi->param('tax_cat') =~ /^(\d+)$/ ); + +my @taxclassnum = (); +if ($tax_type || $tax_cat ) { + my $compare = "LIKE '". ( $tax_type || "%" ). ":". ( $tax_cat || "%" ). "'"; + $compare = "= '$tax_type:$tax_cat'" if ($tax_type && $tax_cat); + my @tax_class = + qsearch({ 'table' => 'tax_class', + 'hashref' => {}, + 'extra_sql' => "WHERE taxclass $compare", + }); + if (@tax_class) { + @taxclassnum = map { $_->taxclassnum } @tax_class; + $tax_class[0]->description =~ /^(.*):(.*)/; + $title .= " for"; + $title .= " $tax_type ($1) tax type" if $tax_type; + $title .= " and" if ($tax_type && $tax_cat); + $title .= " $tax_cat ($2) tax category" if $tax_cat; + }else{ + $tax_type = ''; + $tax_cat = ''; + } +} +$cgi->delete('tax_type'); +$cgi->delete('tax_cat'); + + if ( $geocode || $taxclassnum ) { push @menubar, 'View all tax rates' => $p.'browse/tax_rate.cgi'; } @@ -130,21 +187,34 @@ if ( $geocode || $taxclassnum ) { $cgi->param('dummy', 1); #restore this so pagination works +$cgi->param('data_vendor', $data_vendor) if $data_vendor; $cgi->param('geocode', $geocode) if $geocode; $cgi->param('taxclassnum', $taxclassnum ) if $taxclassnum; +$cgi->param('tax_type', $tax_type ) if $tax_type; +$cgi->param('tax_cat', $tax_cat ) if $tax_cat; my $hashref = {}; -my $count_query = 'SELECT COUNT(*) FROM tax_rate'; +my $extra_sql = ''; +if ( $data_vendor ) { + $extra_sql .= ' WHERE data_vendor = '. dbh->quote($data_vendor); +} + if ( $geocode ) { - $hashref->{'geocode'} = $geocode; - $count_query .= ' WHERE geocode = '. dbh->quote($geocode); + $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ). + ' geocode LIKE '. dbh->quote($geocode.'%'); } + if ( $taxclassnum ) { - $hashref->{'taxclassnum'} = $taxclassnum; - $count_query .= ( $count_query =~ /WHERE/i ? ' AND ' : ' WHERE ' ). - ' taxclassnum = '. dbh->quote($taxclassnum); + $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ). + ' taxclassnum = '. dbh->quote($taxclassnum); } +if ( @taxclassnum ) { + $extra_sql .= ( $extra_sql =~ /WHERE/i ? ' AND ' : ' WHERE ' ). + join(' OR ', map { " taxclassnum = $_ " } @taxclassnum ); +} + +my $count_query = "SELECT COUNT(*) FROM tax_rate $extra_sql"; $cell_style = ''; @@ -164,18 +234,15 @@ my @color = ( push @header, qq!Tax class (
add new)!; push @header2, '(per-tax classification)'; -push @fields, sub { $_[0]->taxclass_description || '(all) '. - &{$separate_taxclasses_link}($_[0], 'Separate Taxclasses'). - 'separate taxclasses
' - }; -push @color, sub { shift->taxclass ? '000000' : '999999' }; +push @fields, 'taxclass_description'; +push @color, '000000'; push @links, ''; push @link_onclicks, ''; $align .= 'l'; push @header, 'Tax name', 'Rate', #'Tax', - 'Exemptions', + 'Limits', ; push @header2, '(printed on invoices)', @@ -185,8 +252,8 @@ push @header2, '(printed on invoices)', push @fields, sub { shift->taxname || 'Tax' }, - sub { shift->tax. '% ('. $select_word. ')' }, - $exempt_sub, + $rate_sub, + $limit_sub, ; push @color, @@ -202,4 +269,91 @@ my @cell_style = map $cell_style_sub, (1..scalar(@header)); push @links, '', $select_link, ''; push @link_onclicks, '', $select_onclick, ''; +my $html_init = ''; + +$html_init .= qq( + + + + + +); + +$html_init .= qq( +
+ + + + + + + + + + + + + + +
+ + + + + +
+
+ +); + diff --git a/httemplate/edit/elements/edit.html b/httemplate/edit/elements/edit.html index ad52f7a4c..c80586a4c 100644 --- a/httemplate/edit/elements/edit.html +++ b/httemplate/edit/elements/edit.html @@ -97,6 +97,9 @@ Example: #run when adding 'new_callback' => sub { my( $cgi, $object, $fields_listref ) = @_; }, + #run before display to return a different value + 'value_callback' => sub { my( $columname, $value } ) = @_; }, + #XXX describe 'field_callback' => sub { }, @@ -273,7 +276,10 @@ Example: % #$field .= $fieldnum; % $onchange .= "\nspawn_$field(what);"; % } else { -% $curr_value = $object->$field(); +% $curr_value = +% ($opt{'value_callback'} && $mode ne 'error') +% ? &{ $opt{'value_callback'} }( $f->{'field'}, $object->$field() ) +% : $object->$field(); % } % % my @include = &{ $include_sub }( diff --git a/httemplate/edit/part_pkg.cgi b/httemplate/edit/part_pkg.cgi index 2e7c732e6..c00af197e 100755 --- a/httemplate/edit/part_pkg.cgi +++ b/httemplate/edit/part_pkg.cgi @@ -94,7 +94,7 @@ Tax information Tax product - + @@ -111,6 +111,7 @@ Tax information % } else { + % } @@ -466,10 +467,16 @@ if ( $cgi->param('clone') ) { } elsif ( $query && $query =~ /^(\d+)$/ ) { (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1}) unless $part_pkg; - $tax_override = + unless ($part_pkg) { + $tax_override = join (",", map {$_->taxclassnum} - qsearch('part_pkg_taxoverride',{'pkgpart'=>$1})) - unless $part_pkg; + qsearch( 'part_pkg_taxoverride', {'pkgpart' => $1} ) + ); +# join (",", map {$_->taxclassnum} +# $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc') +# ); +# unless $tax_override; + } $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1}); $pkgpart = $part_pkg->pkgpart; } else { diff --git a/httemplate/edit/process/elements/process.html b/httemplate/edit/process/elements/process.html index a671ca118..d29ffcfe5 100644 --- a/httemplate/edit/process/elements/process.html +++ b/httemplate/edit/process/elements/process.html @@ -145,7 +145,11 @@ if ( $pkeyvalue ) { }); } -my %hash = map { $_ => scalar($cgi->param($_)) } @$fields; +my %hash = + map { my @entry = ( $_ => $cgi->param($_) ); + $opt{'value_callback'} ? ( $_ => &{ $opt{'value_callback'} }( @entry )) + : ( @entry ) + } @$fields; my $new = $class->new( \%hash ); diff --git a/httemplate/edit/process/tax_rate.html b/httemplate/edit/process/tax_rate.html index 933bf07d6..431e54264 100644 --- a/httemplate/edit/process/tax_rate.html +++ b/httemplate/edit/process/tax_rate.html @@ -1,9 +1,18 @@ <% include( 'elements/process.html', 'table' => 'tax_rate', + 'value_callback' => $value_callback, 'popup_reload' => 'Tax changed', #a popup "parent reload" for now #someday change the individual element and go away instead ) %> +<%once> + +my $value_callback = sub { my ($field, $value) = @_; + ($field =~ /^(tax|excessrate|usetax|useexcessrate)$/) + ? $value/100 + : $value + }; + <%init> my $conf = new FS::Conf; diff --git a/httemplate/edit/tax_rate.html b/httemplate/edit/tax_rate.html index e1d8d4f93..c48bdd1df 100644 --- a/httemplate/edit/tax_rate.html +++ b/httemplate/edit/tax_rate.html @@ -1,14 +1,21 @@ <% include('elements/edit.html', - 'popup' => 1, - 'name' => 'Tax rate', #Edit tax rate - 'table' => 'tax_rate', - 'labels' => $labels, - 'fields' => \@fields, + 'popup' => 1, + 'name' => 'Tax rate', #Edit tax rate + 'table' => 'tax_rate', + 'labels' => $labels, + 'fields' => \@fields, + 'value_callback' => $value_callback, ) %> <%once> my $conf = new FS::Conf; +my $value_callback = + sub { my ( $field, $value ) = @_; + ( $field =~ /^(tax|excessrate|usetax|useexcessrate)$/ ) + ? $value*100 + : $value; + }; @@ -90,16 +97,9 @@ my @fields = ( { field=>'passtype', type=>'hidden' } , { field=>'passtype_name', type=>'fixed' } , { field=>'passflag', type=>'fixed' } , - { field=>'setuptax', type=>'checkbox' } , - { field=>'recurtax', type=>'checkbox' } , + { field=>'setuptax', type=>'checkbox', value=>'Y' } , + { field=>'recurtax', type=>'checkbox', value=>'Y' } , { field=>'manual', type=>'hidden', value=>'Y' } , ); -#push @fields, -# { type=>'tablebreak-tr-title', value=>'Exemptions' }, -# { field=>'setuptax', type=>'checkbox', value=>'Y', }, -# { field=>'recurtax', type=>'checkbox', value=>'Y', }, -# { field=>'exempt_amount', type=>'money', }, -#; - -- 2.11.0