[freeside-commits] freeside/FS/FS cust_bill.pm, 1.193, 1.194 cust_main.pm, 1.336, 1.337 cust_main_county.pm, 1.17, 1.18 part_pkg.pm, 1.62, 1.63 part_pkg_taxrate.pm, 1.1, 1.2 tax_rate.pm, 1.1, 1.2

Jeff Finucane,420,, jeff at wavetail.420.am
Sun Apr 6 09:12:47 PDT 2008


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv8622/FS/FS

Modified Files:
	cust_bill.pm cust_main.pm cust_main_county.pm part_pkg.pm 
	part_pkg_taxrate.pm tax_rate.pm 
Log Message:
new tax rating engine

Index: cust_main.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main.pm,v
retrieving revision 1.336
retrieving revision 1.337
diff -u -d -r1.336 -r1.337
--- cust_main.pm	1 Apr 2008 08:43:33 -0000	1.336
+++ cust_main.pm	6 Apr 2008 16:12:45 -0000	1.337
@@ -45,8 +45,6 @@
 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 @@
 
   my( $total_setup, $total_recur ) = ( 0, 0 );
   my %tax;
+  my %taxlisthash;
   my @precommit_hooks = ();
 
   foreach my $cust_pkg (
@@ -2247,140 +2246,94 @@
 
         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 ). ")";
 
-          # 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";
-          }
-  
-          foreach my $tax ( @taxes ) {
+            @taxes = qsearch({ 'table' => 'tax_rate',
+                               'hashref' => { 'geocode' => $geocode, },
+                               'extra_sql' => $extra_sql,
+                            })
+              if scalar(@taxclassnums);
 
-            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 );
+          }else{
 
-              #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?';
-              }
+            my %taxhash = map { $_ => $self->get("$prefix$_") }
+                              qw( state county country );
 
-              foreach my $which_month ( 1 .. $freq ) {
+            $taxhash{'taxclass'} = $part_pkg->taxclass;
 
-                #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;
+            @taxes = qsearch( 'cust_main_county', \%taxhash );
 
-                  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
+            unless ( @taxes ) {
+              $taxhash{'taxclass'} = '';
+              @taxes =  qsearch( 'cust_main_county', \%taxhash );
+            }
 
-                #++
-                $mon++;
-                #until ( $mon < 12 ) { $mon -= 12; $year++; }
-                until ( $mon < 13 ) { $mon -= 12; $year++; }
-  
-              } #foreach $which_month
-  
-            } #if $tax->exempt_amount
+            #one more try at a whole-country tax rate
+            unless ( @taxes ) {
+              $taxhash{$_} = '' foreach qw( state county );
+              @taxes =  qsearch( 'cust_main_county', \%taxhash );
+            }
 
-            $taxable_charged = sprintf( "%.2f", $taxable_charged);
+          } #if $conf->exists('enable_taxproducts') 
 
-            #$tax += $taxable_charged * $cust_main_county->tax / 100
-            $tax{ $tax->taxname || 'Tax' } +=
-              $taxable_charged * $tax->tax / 100
+          # maybe eliminate this entirely, along with all the 0% records
+          unless ( @taxes ) {
+            $dbh->rollback if $oldAutoCommit;
+            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 ];
+            }
+          }
 
-          } #foreach my $tax ( @taxes )
 
         } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
 
@@ -2410,6 +2363,18 @@
 
   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 @@
   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

Index: cust_main_county.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main_county.pm,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- cust_main_county.pm	24 Aug 2005 13:22:27 -0000	1.17
+++ cust_main_county.pm	6 Apr 2008 16:12:45 -0000	1.18
@@ -4,7 +4,13 @@
 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 @@
   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

Index: tax_rate.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/tax_rate.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- tax_rate.pm	1 Apr 2008 00:54:41 -0000	1.1
+++ tax_rate.pm	6 Apr 2008 16:12:45 -0000	1.2
@@ -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 );
- at EXPORT_OK = qw( regionselector );
 
-$DEBUG = 1;
+$DEBUG = 0;
 $me = '[FS::tax_rate]';
 
- at 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 @@
 
   $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 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,127 +334,80 @@
   $tax_passtypes{$self->passtype};
 }
 
-=back
-
-=head1 SUBROUTINES
-
-=over 4
+=item taxline CUST_BILL_PKG, ...
 
-=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;
+  if ($self->passflag eq 'N') {
+    return "fatal: can't (yet) handle taxes not passed to the customer";
+  }
 
-  my $script_html = <<END;
-    <SCRIPT>
-    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);
+  if ($self->maxtype != 0 && $self->maxtype != 9) {
+    return qq!fatal: can't (yet) handle tax with "!. $self->maxtype_name. 
+      '" threshold';
+  }
 
-  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->maxtype == 9) {
+    return qq!fatal: can't (yet) handle tax with "!. $self->maxtype_name. 
+      '" threshold';  # "texas" tax
   }
 
-  $script_html .= <<END;
-    }
-    function ${prefix}state_changed(what) {
-END
+  if ($self->basetype != 0 && $self->basetype != 1 &&
+      $self->basetype != 6 && $self->basetype != 7 &&
+      $self->basetype != 14
+  ) {
+    return qq!fatal: can't (yet) handle tax with "!. $self->basetype_name. 
+      '" basis';
+  }
 
-  if ( $countyflag ) {
-    $script_html .= <<END;
-      state = what.options[what.selectedIndex].text;
-      country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
-      for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
-          what.form.${prefix}county.options[i] = null;
-END
+  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;
+  }
 
-    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";
-    }
+  my $taxable_units = 0;
+  unless ($self->recurtax =~ /^Y$/i) {
+    $taxable_units += $_->units foreach @cust_bill_pkg;
   }
 
-  $script_html .= <<END;
-    }
-    </SCRIPT>
-END
+  #
+  # 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
+  #
 
-  my $county_html = $script_html;
-  if ( $countyflag ) {
-    $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange" $disabled>!;
-    $county_html .= '</SELECT>';
-  } else {
-    $county_html .=
-      qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
-  }
+  $amount += $taxable_charged * $self->tax;
+  $amount += $taxable_units * $self->fee;
+  
+  return [$name, $amount];
 
-  my $state_html = qq!<SELECT NAME="${prefix}state" !.
-                   qq!onChange="${prefix}state_changed(this); $onchange" $disabled>!;
-  foreach my $state ( sort keys %{ $tax_rate{$selected_country} } ) {
-    my $text = $state || '(n/a)';
-    my $selected = $state eq $selected_state ? 'SELECTED' : '';
-    $state_html .= qq(\n<OPTION $selected VALUE="$state">$text</OPTION>);
-  }
-  $state_html .= '</SELECT>';
+}
 
-  $state_html .= '</SELECT>';
+=back
 
-  my $country_html = qq!<SELECT NAME="${prefix}country" !.
-                     qq!onChange="${prefix}country_changed(this); $onchange" $disabled>!;
-  my $countrydefault = $conf->config('countrydefault') || 'US';
-  foreach my $country (
-    sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
-      keys %tax_rate
-  ) {
-    my $selected = $country eq $selected_country ? ' SELECTED' : '';
-    $country_html .= qq(\n<OPTION$selected VALUE="$country">$country</OPTION>");
-  }
-  $country_html .= '</SELECT>';
+=head1 SUBROUTINES
 
-  ($county_html, $state_html, $country_html);
+=over 4
 
-}
+=item batch_import
+
+=cut
 
 sub batch_import {
   my $param = shift;
@@ -518,6 +456,8 @@
           if length($hash->{$_}) > 80;
       }
 
+      '';
+
     };
 
   } elsif ( $format eq 'extended' ) {

Index: part_pkg.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_pkg.pm,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -d -r1.62 -r1.63
--- part_pkg.pm	1 Apr 2008 00:54:42 -0000	1.62
+++ part_pkg.pm	6 Apr 2008 16:12:45 -0000	1.63
@@ -756,6 +756,37 @@
   $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<FS::part_pkg_taxrate> 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

Index: cust_bill.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_bill.pm,v
retrieving revision 1.193
retrieving revision 1.194
diff -u -d -r1.193 -r1.194
--- cust_bill.pm	1 Apr 2008 09:19:33 -0000	1.193
+++ cust_bill.pm	6 Apr 2008 16:12:45 -0000	1.194
@@ -2486,9 +2486,18 @@
   $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, @_);
 }
 

Index: part_pkg_taxrate.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_pkg_taxrate.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- part_pkg_taxrate.pm	1 Apr 2008 00:54:40 -0000	1.1
+++ part_pkg_taxrate.pm	6 Apr 2008 16:12:45 -0000	1.2
@@ -211,11 +211,12 @@
       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;



More information about the freeside-commits mailing list