X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcust_main_county.pm;h=3c355e823aaaf76aab4da6e94e902906e4fbf7c6;hp=654e5674972366df3a1d52cf832aa2e11bad1993;hb=dbf521cf2f1f1a05f4722006f339b2595c8ee70f;hpb=439d00a59c67a7d9d53b5d89c14ab332be16e38b diff --git a/FS/FS/cust_main_county.pm b/FS/FS/cust_main_county.pm index 654e56749..3c355e823 100644 --- a/FS/FS/cust_main_county.pm +++ b/FS/FS/cust_main_county.pm @@ -12,6 +12,7 @@ use FS::cust_pkg; use FS::part_pkg; use FS::cust_tax_exempt; use FS::cust_tax_exempt_pkg; +use FS::upgrade_journal; @EXPORT_OK = qw( regionselector ); @@ -78,6 +79,9 @@ currently supported: =item recurtax - if 'Y', this tax does not apply to recurring fees +=item source - the tax lookup method that created this tax record. For records +created manually, this will be null. + =back =head1 METHODS @@ -132,6 +136,7 @@ sub check { || $self->ut_textn('taxname') || $self->ut_enum('setuptax', [ '', 'Y' ] ) || $self->ut_enum('recurtax', [ '', 'Y' ] ) + || $self->ut_textn('source') || $self->SUPER::check ; @@ -277,25 +282,29 @@ sub taxline { my $cust_bill = $taxables->[0]->cust_bill; my $custnum = $cust_bill ? $cust_bill->custnum : $opt{'custnum'}; my $invoice_time = $cust_bill ? $cust_bill->_date : $opt{'invoice_time'}; - my $cust_main = FS::cust_main->by_key($custnum) if $custnum > 0; - if (!$cust_main) { - # better way to handle this? should we just assume that it's taxable? - die "unable to calculate taxes for an unknown customer\n"; - } + my $cust_main = FS::cust_main->by_key($custnum) if $custnum; + # (to avoid complications with estimated tax on quotations, assume it's + # taxable if there is no customer) + #if (!$cust_main) { + #die "unable to calculate taxes for an unknown customer\n"; + #} # set a flag if the customer is tax-exempt - my $exempt_cust; + my ($exempt_cust, $exempt_cust_taxname); my $conf = FS::Conf->new; - if ( $conf->exists('cust_class-tax_exempt') ) { - my $cust_class = $cust_main->cust_class; - $exempt_cust = $cust_class->tax if $cust_class; - } else { - $exempt_cust = $cust_main->tax; - } + if ( $cust_main ) { + if ( $conf->exists('cust_class-tax_exempt') ) { + my $cust_class = $cust_main->cust_class; + $exempt_cust = $cust_class->tax if $cust_class; + } else { + $exempt_cust = $cust_main->tax; + } - # set a flag if the customer is exempt from this tax here - my $exempt_cust_taxname = $cust_main->tax_exemption($self->taxname) - if $self->taxname; + # set a flag if the customer is exempt from this tax here + if ( $self->taxname ) { + $exempt_cust_taxname = $cust_main->tax_exemption($self->taxname); + } + } # Gather any exemptions that are already attached to these cust_bill_pkgs # so that we can deduct them from the customer's monthly limit. @@ -313,14 +322,14 @@ sub taxline { my @tax_location; foreach my $cust_bill_pkg (@$taxables) { + # careful... may be a cust_bill_pkg or a quotation_pkg my $cust_pkg = $cust_bill_pkg->cust_pkg; my $part_pkg = $cust_bill_pkg->part_pkg; my $part_fee = $cust_bill_pkg->part_fee; - my $locationnum = $cust_pkg - ? $cust_pkg->locationnum - : $cust_main->bill_locationnum; + my $locationnum = $cust_bill_pkg->tax_locationnum + || $cust_main->ship_locationnum; my @new_exemptions; my $taxable_charged = $cust_bill_pkg->setup + $cust_bill_pkg->recur @@ -379,7 +388,11 @@ sub taxline { } if ( $self->exempt_amount && $self->exempt_amount > 0 - and $taxable_charged > 0 ) { + and $taxable_charged > 0 + and $cust_main ) { + + # XXX monthly exemptions currently don't work on quotations + # If the billing period extends across multiple calendar months, # there may be several months of exemption available. my $sdate = $cust_bill_pkg->sdate || $invoice_time; @@ -493,7 +506,7 @@ sub taxline { } } - } # if exempt_amount + } # if exempt_amount and $cust_main $_->taxnum($self->taxnum) foreach @new_exemptions; @@ -666,6 +679,31 @@ END } +sub _upgrade_data { + my $class = shift; + # assume taxes in Washington with district numbers, and null name, or + # named 'sales tax', are looked up via the wa_sales method. mark them. + my $journal = 'cust_main_county__source_wa_sales'; + if (!FS::upgrade_journal->is_done($journal)) { + my @taxes = qsearch({ + 'table' => 'cust_main_county', + 'extra_sql' => " WHERE tax > 0 AND country = 'US' AND state = 'WA'". + " AND district IS NOT NULL AND ( taxname IS NULL OR ". + " taxname ~* 'sales tax' )", + }); + if ( @taxes ) { + warn "Flagging Washington state sales taxes: ".scalar(@taxes)." records.\n"; + foreach (@taxes) { + $_->set('source', 'wa_sales'); + my $error = $_->replace; + die $error if $error; + } + } + FS::upgrade_journal->set_done($journal); + } + ''; +} + =back =head1 BUGS