RT# 81961 Repair broken links in POD documentation
[freeside.git] / FS / FS / cust_tax_exempt_pkg.pm
index bbabb5b..6c4c641 100644 (file)
@@ -6,6 +6,7 @@ use FS::Record qw( qsearch qsearchs );
 use FS::cust_main_Mixin;
 use FS::cust_bill_pkg;
 use FS::cust_main_county;
+use FS::tax_rate;
 use FS::cust_credit_bill_pkg;
 use FS::UID qw(dbh);
 use FS::upgrade_journal;
@@ -50,6 +51,9 @@ currently supported:
 =item billpkgnum - invoice line item (see L<FS::cust_bill_pkg>) that 
 was exempted from tax.
 
+=item taxtype - the object class of the tax record ('FS::cust_main_county'
+or 'FS::tax_rate').
+
 =item taxnum - tax rate (see L<FS::cust_main_county>)
 
 =item year - the year in which the exemption occurred.  NULL if this 
@@ -68,7 +72,7 @@ may be the setup fee, the recurring fee, or the sum of those.
 (cust_main.tax = 'Y').
 
 =item exempt_cust_taxname - flag indicating that the customer is exempt 
-from the tax with this name (see L<FS::cust_main_exemption).
+from the tax with this name (see L<FS::cust_main_exemption>).
 
 =item exempt_setup, exempt_recur: flag indicating that the package's setup
 or recurring fee is not taxable (part_pkg.setuptax and part_pkg.recurtax).
@@ -138,7 +142,7 @@ sub check {
 
   my $error = $self->ut_numbern('exemptnum')
     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
-    || $self->ut_foreign_key('taxnum', 'cust_main_county', 'taxnum')
+    || $self->ut_enum('taxtype', [ 'FS::cust_main_county', 'FS::tax_rate' ])
     || $self->ut_foreign_keyn('creditbillpkgnum',
                               'cust_credit_bill_pkg',
                               'creditbillpkgnum')
@@ -152,6 +156,10 @@ sub check {
     || $self->SUPER::check
   ;
 
+  $self->get('taxtype') =~ /^FS::(\w+)$/;
+  my $rate_table = $1;
+  $error ||= $self->ut_foreign_key('taxnum', $rate_table, 'taxnum');
+
   return $error if $error;
 
   if ( $self->get('exempt_cust') ) {
@@ -178,6 +186,8 @@ sub check {
 
 =item cust_main_county
 
+=item tax_rate
+
 Returns the associated tax definition if it still exists in the database.
 Otherwise returns false.
 
@@ -185,7 +195,14 @@ Otherwise returns false.
 
 sub cust_main_county {
   my $self = shift;
-  qsearchs( 'cust_main_county', { 'taxnum', $self->taxnum } );
+  my $class = $self->taxtype;
+  $class->by_key($self->taxnum);
+}
+
+sub tax_rate {
+  my $self = shift;
+  my $class = $self->taxtype;
+  $class->by_key($self->taxnum);
 }
 
 sub _upgrade_data {
@@ -198,6 +215,19 @@ sub _upgrade_data {
     dbh->do($sql) or die dbh->errstr;
     FS::upgrade_journal->set_done($journal);
   }
+
+  $journal = 'cust_tax_exempt_pkg_taxtype';
+  if ( !FS::upgrade_journal->is_done($journal) ) {
+    my $sql = "UPDATE cust_tax_exempt_pkg ".
+              "SET taxtype = 'FS::cust_main_county' WHERE taxtype IS NULL";
+    dbh->do($sql) or die dbh->errstr;
+    $sql =    "UPDATE cust_tax_exempt_pkg_void ".
+              "SET taxtype = 'FS::cust_main_county' WHERE taxtype IS NULL";
+    dbh->do($sql) or die dbh->errstr;
+    FS::upgrade_journal->set_done($journal);
+  }
+
+
 }
 
 =back