new tax rating engine
authorjeff <jeff>
Sun, 6 Apr 2008 16:12:46 +0000 (16:12 +0000)
committerjeff <jeff>
Sun, 6 Apr 2008 16:12:46 +0000 (16:12 +0000)
13 files changed:
FS/FS/cust_bill.pm
FS/FS/cust_main.pm
FS/FS/cust_main_county.pm
FS/FS/part_pkg.pm
FS/FS/part_pkg_taxrate.pm
FS/FS/tax_rate.pm
httemplate/browse/part_pkg_taxproduct.cgi [new file with mode: 0755]
httemplate/browse/tax_rate.cgi
httemplate/edit/elements/edit.html
httemplate/edit/part_pkg.cgi
httemplate/edit/process/elements/process.html
httemplate/edit/process/tax_rate.html
httemplate/edit/tax_rate.html

index eafe930..f536c97 100644 (file)
@@ -2486,9 +2486,18 @@ sub _items_pkg {
   $self->_items_cust_bill_pkg(\@cust_bill_pkg, %options);
 }
 
   $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;
 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, @_);
 }
 
   $self->_items_cust_bill_pkg(\@cust_bill_pkg, @_);
 }
 
index 37f98c6..ceefeaf 100644 (file)
@@ -45,8 +45,6 @@ use FS::part_pkg;
 use FS::part_event;
 use FS::part_event_condition;
 #use FS::cust_event;
 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;
 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( $total_setup, $total_recur ) = ( 0, 0 );
   my %tax;
+  my %taxlisthash;
   my @precommit_hooks = ();
 
   foreach my $cust_pkg (
   my @precommit_hooks = ();
 
   foreach my $cust_pkg (
@@ -2247,140 +2246,94 @@ sub bill {
 
         unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
 
 
         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 $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;
 
           # 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 ) {
           }
   
           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'
 
 
         } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
 
@@ -2410,6 +2363,18 @@ sub bill {
 
   my $charged = sprintf( "%.2f", $total_setup + $total_recur );
 
 
   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 );
   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);
 }
 
   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
 =item cust_status
 
 =item status
index 17f3460..3a0304b 100644 (file)
@@ -4,7 +4,13 @@ use strict;
 use vars qw( @ISA @EXPORT_OK $conf
              @cust_main_county %cust_main_county $countyflag );
 use Exporter;
 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 );
 
 @ISA = qw( FS::Record );
 @EXPORT_OK = qw( regionselector );
@@ -151,6 +157,139 @@ sub recurtax {
   return '';
 }
 
   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
 =back
 
 =head1 SUBROUTINES
index dc0a4d5..1e16f29 100644 (file)
@@ -756,6 +756,37 @@ sub taxproduct_description {
   $part_pkg_taxproduct ? $part_pkg_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<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
 =item _rebless
 
 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
index aa1c3df..3e7e7bd 100644 (file)
@@ -211,11 +211,12 @@ sub batch_import {
       my $part_pkg_taxproduct = qsearchs( 'part_pkg_taxproduct', 
                                           { %part_pkg_taxproduct }
                                         );
       my $part_pkg_taxproduct = qsearchs( 'part_pkg_taxproduct', 
                                           { %part_pkg_taxproduct }
                                         );
+
       unless ($part_pkg_taxproduct) {
         $part_pkg_taxproduct{'description'} = 
       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;
               );
         $part_pkg_taxproduct = new FS::part_pkg_taxproduct \%part_pkg_taxproduct;
         my $error = $part_pkg_taxproduct->insert;
index 38e5343..3d56a0d 100644 (file)
@@ -1,30 +1,19 @@
 package FS::tax_rate;
 
 use strict;
 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_unittypes %tax_maxtypes %tax_basetypes %tax_authorities
-             %tax_passtypes
-             @tax_rate %tax_rate $countyflag );
-use Exporter;
+             %tax_passtypes );
 use Date::Parse;
 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::tax_class;
+use FS::cust_bill_pkg;
 
 @ISA = qw( FS::Record );
 
 @ISA = qw( FS::Record );
-@EXPORT_OK = qw( regionselector );
 
 
-$DEBUG = 1;
+$DEBUG = 0;
 $me = '[FS::tax_rate]';
 
 $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
 =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;
 
 
   $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.
 =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
 
 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
 
 
 the time after which the tax applies
 
@@ -349,128 +334,81 @@ sub passtype_name {
   $tax_passtypes{$self->passtype};
 }
 
   $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
 
 
 =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 = <<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);
-
-  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 .= <<END;
-    }
-    function ${prefix}state_changed(what) {
-END
-
-  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
-
-    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;
-    }
-    </SCRIPT>
-END
-
-  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">!;
+  if ($self->maxtype == 9) {
+    return qq!fatal: can't (yet) handle tax with "!. $self->maxtype_name. 
+      '" threshold';  # "texas" tax
   }
 
   }
 
-  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>);
+  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';
   }
   }
-  $state_html .= '</SELECT>';
 
 
-  $state_html .= '</SELECT>';
+  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!<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>");
+  my $taxable_units = 0;
+  unless ($self->recurtax =~ /^Y$/i) {
+    $taxable_units += $_->units foreach @cust_bill_pkg;
   }
   }
-  $country_html .= '</SELECT>';
 
 
-  ($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;
 
 sub batch_import {
   my $param = shift;
 
@@ -518,6 +456,8 @@ sub batch_import {
           if length($hash->{$_}) > 80;
       }
 
           if length($hash->{$_}) > 80;
       }
 
+      '';
+
     };
 
   } elsif ( $format eq 'extended' ) {
     };
 
   } elsif ( $format eq 'extended' ) {
diff --git a/httemplate/browse/part_pkg_taxproduct.cgi b/httemplate/browse/part_pkg_taxproduct.cgi
new file mode 100755 (executable)
index 0000000..3df8197
--- /dev/null
@@ -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 { ''; } ];
+
+</%once>
+<%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 = '<TABLE><TR><TD>Current tax product: </TD><TD>'.
+                $selected_part_pkg_taxproduct->description.
+                '</TD></TR></TABLE><BR><BR>'
+  if $selected_part_pkg_taxproduct;
+
+my $type = $cgi->param('_type');
+$html_init .= qq(
+  <FORM>
+    <INPUT NAME="_type" TYPE="hidden" VALUE="$type">
+    <INPUT NAME="taxproductnum" TYPE="hidden" VALUE="$taxproductnum">
+    <INPUT NAME="id" TYPE="hidden" VALUE="$id">
+    <TABLE>
+      <TR>
+        <TD><SELECT NAME="data_vendor" onChange="this.form.submit()">
+);
+
+my $sql = "SELECT DISTINCT data_vendor FROM part_pkg_taxproduct ORDER BY data_vendor";
+my $dbh = dbh;
+my $sth = $dbh->prepare($sql) or die $dbh->errstr;
+$sth->execute or die $sth->errstr;
+for (['(choose data vendor)'], @{$sth->fetchall_arrayref}) {
+  $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
+                ($_->[0] eq $data_vendor ? " SELECTED" : "").
+                '">'.  $_->[0];
+}
+$html_init .= qq(
+        </SELECT>
+
+<!-- cch specific -->
+        <TD><SELECT NAME="tax_group" onChange="this.form.submit()">
+);
+
+$sql = "SELECT DISTINCT ".
+       qq!substring(description from '#"%#" : % : % : %' for '#'),!.
+       qq!substring(description from '#"%#" : % : % : %' for '#')!.
+       "FROM part_pkg_taxproduct ORDER BY 1";
+
+$sth = $dbh->prepare($sql) or die $dbh->errstr;
+$sth->execute or die $sth->errstr;
+for (['', '(choose group)'], @{$sth->fetchall_arrayref}) {
+  $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
+                 ($_->[0] eq $tax_group ? " SELECTED" : "").
+                 '">'. $_->[1];
+}
+
+$html_init .= qq(
+        </SELECT>
+
+        <TD><SELECT NAME="tax_item" onChange="this.form.submit()">
+);
+
+$sql = "SELECT DISTINCT ".
+       qq!substring(description from '% : #"%#" : %: %' for '#'),!.
+       qq!substring(description from '% : #"%#" : %: %' for '#')!.
+       "FROM part_pkg_taxproduct ORDER BY 1";
+
+$sth = $dbh->prepare($sql) or die $dbh->errstr;
+$sth->execute or die $sth->errstr;
+for (@{$sth->fetchall_arrayref}) {
+  $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
+                 ($_->[0] eq $tax_item ? " SELECTED" : "").
+                 '">'.  ($_->[0] ? $_->[1] : '(choose item)');
+}
+
+$html_init .= qq(
+        </SELECT>
+
+        <TD><SELECT NAME="tax_provider" onChange="this.form.submit()">
+);
+
+$sql = "SELECT DISTINCT ".
+       qq!substring(description from '% : % : #"%#" : %' for '#'),!.
+       qq!substring(description from '% : % : #"%#" : %' for '#')!.
+       "FROM part_pkg_taxproduct ORDER BY 1";
+
+$sth = $dbh->prepare($sql) or die $dbh->errstr;
+$sth->execute or die $sth->errstr;
+for (@{$sth->fetchall_arrayref}) {
+  $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
+                 ($_->[0] eq $tax_provider ? " SELECTED" : "").
+                 '">'.  ($_->[0] ? $_->[1] : '(choose provider type)');
+}
+
+$html_init .= qq(
+        </SELECT>
+
+        <TD><SELECT NAME="tax_customer" onChange="this.form.submit()">
+);
+
+$sql = "SELECT DISTINCT ".
+       qq!substring(description from '% : % : % : #"%#"' for '#'),!.
+       qq!substring(description from '% : % : % : #"%#"' for '#')!.
+       "FROM part_pkg_taxproduct ORDER BY 1";
+
+$sth = $dbh->prepare($sql) or die $dbh->errstr;
+$sth->execute or die $sth->errstr;
+for (@{$sth->fetchall_arrayref}) {
+  $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
+                 ($_->[0] eq $tax_customer ? " SELECTED" : "").
+                 '">'.  ($_->[0] ? $_->[1] : '(choose customer type)');
+}
+
+$html_init .= qq(
+        </SELECT>
+
+      </TR>
+    </TABLE>
+  </FORM>
+
+);
+
+</%init>
index b401b37..5d43d59 100755 (executable)
@@ -4,9 +4,10 @@
      'menubar'        => \@menubar,
      'html_init'      => $html_init,
      'query'          => {
      '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,
                          },
      'count_query'    => $count_query,
      'header'         => \@header,
 my $conf = new FS::Conf;
 my $money_char = $conf->config('money_char') || '$';
 
 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 $tax_rate = shift;
 
-  my @exempt = ();
-  push @exempt,
-       sprintf("$money_char%.2f&nbsp;per&nbsp;month", $tax_rate->exempt_amount )
-    if $tax_rate->exempt_amount > 0;
+  my $units = $tax_rate->unittype_name;
+  $units =~ s/ /&nbsp;/g;
+
+  my @rate = ();
+  push @rate,
+      ($tax_rate->tax * 100). '%&nbsp;<FONT SIZE="-1">(edit)</FONT>'
+    if $tax_rate->tax > 0 || $tax_rate->taxbase > 0;
+  push @rate,
+      ($tax_rate->excessrate * 100). '%&nbsp;<FONT SIZE="-1">(edit)</FONT>'
+    if $tax_rate->excessrate > 0;
+  push @rate,
+      $money_char. $tax_rate->fee.
+      qq!&nbsp;per&nbsp;$units<FONT SIZE="-1">(edit)</FONT>!
+    if $tax_rate->fee > 0 || $tax_rate->feebase > 0;
+  push @rate,
+      $money_char. $tax_rate->excessfee.
+      qq!&nbsp;per&nbsp;$units<FONT SIZE="-1">(edit)</FONT>!
+    if $tax_rate->excessfee > 0;
+
+
+  [ map [ {'data'=>$_} ], @rate ];
+};
+
+my $limit_sub = sub {
+  my $tax_rate = shift;
 
 
-  push @exempt, 'Setup&nbsp;fee'
+  my $maxtype = $tax_rate->maxtype_name;
+  $maxtype =~ s/ /&nbsp;/g;
+
+  my $units = $tax_rate->unittype_name;
+  $units =~ s/ /&nbsp;/g;
+
+  my @limit = ();
+  push @limit,
+       sprintf("$money_char%.2f&nbsp%s", $tax_rate->taxbase, $maxtype )
+    if $tax_rate->taxbase > 0;
+  push @limit,
+       sprintf("$money_char%.2f&nbsp;tax", $tax_rate->taxmax )
+    if $tax_rate->taxmax > 0;
+  push @limit,
+       $tax_rate->feebase. "&nbsp;$units". ($tax_rate->feebase == 1 ? '' : 's')
+    if $tax_rate->feebase > 0;
+  push @limit,
+       $tax_rate->feemax. "&nbsp;$units". ($tax_rate->feebase == 1 ? '' : 's')
+    if $tax_rate->feemax > 0;
+
+  push @limit, 'Excluding&nbsp;setup&nbsp;fee'
     if $tax_rate->setuptax =~ /^Y$/i;
 
     if $tax_rate->setuptax =~ /^Y$/i;
 
-  push @exempt, 'Recurring&nbsp;fee'
+  push @limit, 'Excluding&nbsp;recurring&nbsp;fee'
     if $tax_rate->recurtax =~ /^Y$/i;
 
     if $tax_rate->recurtax =~ /^Y$/i;
 
-  [ map [ {'data'=>$_} ], @exempt ];
+  [ map [ {'data'=>$_} ], @limit ];
 };
 
 my $oldrow;
 };
 
 my $oldrow;
@@ -67,15 +109,7 @@ my $select_onclick = sub {
   my $row = shift;
   my $taxnum = $row->taxnum;
   my $color = '#333399';
   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!<FONT SIZE="-1"><A HREF="$url">!;
+  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;!;
 };
 
 </%once>
 };
 
 </%once>
@@ -85,26 +119,19 @@ die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
 
 my @menubar;
   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
 
 my @menubar;
-
-my $html_init =
-  "Click on <u>geocodes</u> to specify rates for a new area.";
-$html_init .= "<BR>Click on <u>separate taxclasses</u> to specify taxes per taxclass.";
-$html_init .= '<BR><BR>';
-
-$html_init .= qq(
-  <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws.js"></SCRIPT>
-  <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_iframe.js"></SCRIPT>
-  <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_draggable.js"></SCRIPT>
-  <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/iframecontentmws.js"></SCRIPT>
-);
-
 my $title = '';
 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;
 
 my $geocode = '';
 if ( $cgi->param('geocode') =~ /^(\w+)$/ ) {
   $geocode = $1;
-  $title = "$geocode";
+  $title = " geocode $geocode";
 }
 $cgi->delete('geocode');
 
 }
 $cgi->delete('geocode');
 
@@ -123,6 +150,36 @@ if ( $cgi->param('taxclassnum') =~ /^(\d+)$/ ) {
 }
 $cgi->delete('taxclassnum');
 
 }
 $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';
 }
 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('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('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 $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 ) {
 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 ) {
 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 = '';
 
 
 $cell_style = '';
 
@@ -164,18 +234,15 @@ my @color = (
 
 push @header, qq!Tax class (<A HREF="${p}edit/tax_class.html">add new</A>)!;
 push @header2, '(per-tax classification)';
 
 push @header, qq!Tax class (<A HREF="${p}edit/tax_class.html">add new</A>)!;
 push @header2, '(per-tax classification)';
-push @fields, sub { $_[0]->taxclass_description || '(all)&nbsp'.
-                     &{$separate_taxclasses_link}($_[0], 'Separate Taxclasses').
-                     'separate&nbsp;taxclasses</A></FONT>'
-                  };
-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',
 push @links, '';
 push @link_onclicks, '';
 $align .= 'l';
 
 push @header, 'Tax name',
               'Rate', #'Tax',
-              'Exemptions',
+              'Limits',
               ;
 
 push @header2, '(printed on invoices)',
               ;
 
 push @header2, '(printed on invoices)',
@@ -185,8 +252,8 @@ push @header2, '(printed on invoices)',
 
 push @fields, 
   sub { shift->taxname || 'Tax' },
 
 push @fields, 
   sub { shift->taxname || 'Tax' },
-  sub { shift->tax. '%&nbsp;<FONT SIZE="-1">('. $select_word. ')</FONT>' },
-  $exempt_sub,
+  $rate_sub,
+  $limit_sub,
 ;
 
 push @color,
 ;
 
 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, '';
 
 push @links,         '', $select_link,    '';
 push @link_onclicks, '', $select_onclick, '';
 
+my $html_init = '';
+
+$html_init .= qq(
+  <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws.js"></SCRIPT>
+  <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_iframe.js"></SCRIPT>
+  <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/overlibmws_draggable.js"></SCRIPT>
+  <SCRIPT TYPE="text/javascript" SRC="${fsurl}elements/iframecontentmws.js"></SCRIPT>
+
+);
+
+$html_init .= qq(
+  <FORM>
+    <TABLE>
+      <TR>
+        <TD><SELECT NAME="data_vendor" onChange="this.form.submit()">
+);
+
+my $sql = "SELECT DISTINCT data_vendor FROM tax_rate ORDER BY data_vendor";
+my $dbh = dbh;
+my $sth = $dbh->prepare($sql) or die $dbh->errstr;
+$sth->execute or die $sth->errstr;
+for (['(choose data vendor)'], @{$sth->fetchall_arrayref}) {
+  $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
+                ($_->[0] eq $data_vendor ? " SELECTED" : "").
+                '">'.  $_->[0];
+}
+$html_init .= qq(
+        </SELECT>
+
+        <TD><INPUT NAME="geocode" TYPE="text" SIZE="12" VALUE="$geocode"></TD>
+
+<!-- generic
+        <TD><INPUT NAME="taxclassnum" TYPE="text" SIZE="12" VALUE="$taxclassnum"></TD>
+        <TD><INPUT TYPE="submit" VALUE="Filter by tax_class"></TD>
+-->
+
+<!-- cch specific -->
+        <TD><SELECT NAME="tax_type" onChange="this.form.submit()">
+);
+
+$sql = "SELECT DISTINCT ".
+       "substring(taxclass from 1 for position(':' in taxclass)-1),".
+       "substring(description from 1 for position(':' in description)-1) ".
+       "FROM tax_class WHERE data_vendor='cch' ORDER BY 2";
+
+$sth = $dbh->prepare($sql) or die $dbh->errstr;
+$sth->execute or die $sth->errstr;
+for (['', '(choose tax type)'], @{$sth->fetchall_arrayref}) {
+  $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
+                 ($_->[0] eq $tax_type ? " SELECTED" : "").
+                 '">'. $_->[1];
+}
+
+$html_init .= qq(
+        </SELECT>
+
+        <TD><SELECT NAME="tax_cat" onChange="this.form.submit()">
+);
+
+$sql = "SELECT DISTINCT ".
+       "substring(taxclass from position(':' in taxclass)+1),".
+       "substring(description from position(':' in description)+1) ".
+       "from tax_class WHERE data_vendor='cch' ORDER BY 2";
+
+$sth = $dbh->prepare($sql) or die $dbh->errstr;
+$sth->execute or die $sth->errstr;
+for (['', '(choose tax category)'], @{$sth->fetchall_arrayref}) {
+  $html_init .= '<OPTION VALUE="'. $_->[0]. '"'.
+                 ($_->[0] eq $tax_cat ? " SELECTED" : "").
+                 '">'.  $_->[1];
+}
+
+$html_init .= qq(
+        </SELECT>
+
+      </TR>
+      <TR>
+        <TD></TD>
+        <TD><INPUT TYPE="submit" VALUE="Filter by geocode"></TD>
+        <TD></TD>
+        <TD></TD>
+      </TR>
+    </TABLE>
+  </FORM>
+
+);
+
 </%init>
 </%init>
index ad52f7a..c80586a 100644 (file)
@@ -97,6 +97,9 @@ Example:
     #run when adding
     'new_callback' => sub { my( $cgi, $object, $fields_listref ) = @_; },
    
     #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 { },
 
     #XXX describe
     'field_callback' => sub { },
 
@@ -273,7 +276,10 @@ Example:
 %     #$field .= $fieldnum;
 %     $onchange .= "\nspawn_$field(what);";
 %   } else {
 %     #$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 }(
 %   }
 %
 %   my @include = &{ $include_sub }(
index 2e7c732..c00af19 100755 (executable)
@@ -94,7 +94,7 @@ Tax information
         <TD align="right">Tax product</TD>
         <TD>
           <INPUT name="part_pkg_taxproduct_taxproductnum" id="taxproductnum" type="hidden" value="<% $hashref->{'taxproductnum'}%>">
         <TD align="right">Tax product</TD>
         <TD>
           <INPUT name="part_pkg_taxproduct_taxproductnum" id="taxproductnum" type="hidden" value="<% $hashref->{'taxproductnum'}%>">
-          <INPUT name="part_pkg_taxproduct_description" id="taxproduct_description" type="text" value="<% $taxproduct_description %>" size="12" onclick="overlib( OLiframeContent('part_pkg_taxproduct.html?'+document.getElementById('taxproductnum').value, 1000, 400, 'tax_product_popup'), CAPTION, 'Select product', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK); return false;">
+          <INPUT name="part_pkg_taxproduct_description" id="taxproductnum_description" type="text" value="<% $taxproduct_description %>" size="12" onclick="overlib( OLiframeContent('<% $p %>/browse/part_pkg_taxproduct.cgi?_type=select&id=taxproductnum&taxproductnum='+document.getElementById('taxproductnum').value, 1000, 400, 'tax_product_popup'), CAPTION, 'Select product', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK); return false;">
         </TD>
       </TR>
       <TR>
         </TD>
       </TR>
       <TR>
@@ -111,6 +111,7 @@ Tax information
 % } else { 
 
   <INPUT TYPE="hidden" NAME="taxproductnum" VALUE="<% $hashref->{taxproductnum} %>">
 % } else { 
 
   <INPUT TYPE="hidden" NAME="taxproductnum" VALUE="<% $hashref->{taxproductnum} %>">
+  <INPUT TYPE="hidden" NAME="tax_override" VALUE="<% $tax_override %>">
 
 % } 
 
 
 % } 
 
@@ -466,10 +467,16 @@ if ( $cgi->param('clone') ) {
 } elsif ( $query && $query =~ /^(\d+)$/ ) {
   (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1})
     unless $part_pkg;
 } 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}
     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 {
   $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1});
   $pkgpart = $part_pkg->pkgpart;
 } else {
index a671ca1..d29ffcf 100644 (file)
@@ -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 );
 
 
 my $new = $class->new( \%hash );
 
index 933bf07..431e542 100644 (file)
@@ -1,9 +1,18 @@
 <% include( 'elements/process.html',
               'table' => 'tax_rate',
 <% 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
           )
 %>
               '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
+                         };
+</%once>
 <%init>
 
 my $conf = new FS::Conf;
 <%init>
 
 my $conf = new FS::Conf;
index e1d8d4f..c48bdd1 100644 (file)
@@ -1,14 +1,21 @@
 <% include('elements/edit.html',
 <% 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;
    )
 %>
 <%once>
 
 my $conf = new FS::Conf;
+my $value_callback =
+  sub { my ( $field, $value ) = @_;
+        ( $field =~ /^(tax|excessrate|usetax|useexcessrate)$/ )
+          ? $value*100
+          : $value;
+      };
 
 </%once>
 
 
 </%once>
 
@@ -90,16 +97,9 @@ my @fields = (
     { field=>'passtype',             type=>'hidden' } ,
     { field=>'passtype_name',        type=>'fixed' } ,
     { field=>'passflag',             type=>'fixed' } ,
     { 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' } ,
 );
 
     { 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', },
-#;
-
 </%init>
 </%init>