From 962cd26e8e33bf3653cca439dbd9bd52f46bad29 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Fri, 28 Sep 2012 13:38:03 -0700 Subject: [PATCH] auto-upgrade tax data from start of 2012, #940 --- FS/FS/Upgrade.pm | 3 +++ FS/FS/cust_bill_pkg.pm | 34 +++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index b7971ff48..7dd889f2c 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -290,6 +290,9 @@ sub upgrade_data { #flag monthly tax exemptions 'cust_tax_exempt_pkg' => [], + + #kick off tax location history upgrade + 'cust_bill_pkg' => [], ; \%hash; diff --git a/FS/FS/cust_bill_pkg.pm b/FS/FS/cust_bill_pkg.pm index b8ae81d86..20c8e5a55 100644 --- a/FS/FS/cust_bill_pkg.pm +++ b/FS/FS/cust_bill_pkg.pm @@ -940,15 +940,14 @@ sub upgrade_tax_location { # they were calculated on a package-location basis. Create them here, # along with any necessary cust_location records and any tax exemption # records. - # - # This probably shouldn't run from freeside-upgrade. my ($class, %opt) = @_; # %opt may include 's' and 'e': start and end date ranges # and 'X': abort on any error, instead of just rolling back changes to # that invoice my $dbh = dbh; - $FS::UID::AutoCommit = 0; + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; eval { use FS::h_cust_main; @@ -1108,7 +1107,8 @@ sub upgrade_tax_location { push @{ $nontax_items{$taxclass} }, $item; } } - printf("%d tax items: \$%.2f\n", scalar(@tax_items), map {$_->setup} @tax_items); + printf("%d tax items: \$%.2f\n", scalar(@tax_items), map {$_->setup} @tax_items) + if @tax_items; # Use a variation on the procedure in # FS::cust_main::Billing::_handle_taxes to identify taxes that apply @@ -1378,21 +1378,41 @@ sub upgrade_tax_location { } #foreach (@tax_links) } #foreach $tax_item - $dbh->commit if $commit_each_invoice; + $dbh->commit if $commit_each_invoice and $oldAutoCommit; $committed = 1; } #foreach $invnum continue { if (!$committed) { - $dbh->rollback; + $dbh->rollback if $oldAutoCommit; die "Upgrade halted.\n" unless $commit_each_invoice; } } - $dbh->commit unless $commit_each_invoice; + $dbh->commit if $oldAutoCommit and !$commit_each_invoice; ''; } +sub _upgrade_data { + # Create a queue job to run upgrade_tax_location from January 1, 2012 to + # the present date. + eval { + use FS::queue; + use Date::Parse 'str2time'; + }; + my $class = shift; + my $upgrade = 'tax_location_2012'; + return if FS::upgrade_journal->is_done($upgrade); + my $job = FS::queue->new({ + 'job' => 'FS::cust_bill_pkg::upgrade_tax_location' + }); + # call it kind of like a class method, not that it matters much + $job->insert($class, 's' => str2time('2012-01-01')); + # Then mark the upgrade as done, so that we don't queue the job twice + # and somehow run two of them concurrently. + FS::upgrade_journal->set_done($upgrade); +} + =back =head1 BUGS -- 2.11.0