From 392d4b113894af4ff051e3363d8b3c78c2d223dc Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Tue, 18 Oct 2016 23:06:03 -0700 Subject: [PATCH] during upgrade, fix broken tax links on old voided invoices, #72777 --- FS/FS/Upgrade.pm | 3 +++ FS/FS/cust_bill_pkg_void.pm | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index 8b7d7337f..d06b7d8a7 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -348,6 +348,9 @@ sub upgrade_data { #customer credits 'cust_credit' => [], + # fix some tax allocation records + 'cust_bill_pkg_void' => [], + #duplicate history records 'h_cust_svc' => [], diff --git a/FS/FS/cust_bill_pkg_void.pm b/FS/FS/cust_bill_pkg_void.pm index 080452e19..8bd80887e 100644 --- a/FS/FS/cust_bill_pkg_void.pm +++ b/FS/FS/cust_bill_pkg_void.pm @@ -12,6 +12,7 @@ use FS::cust_bill_pkg_fee; use FS::cust_bill_pkg_tax_location; use FS::cust_bill_pkg_tax_rate_location; use FS::cust_tax_exempt_pkg; +use FS::Cursor; =head1 NAME @@ -266,6 +267,41 @@ sub cust_bill_pkg_fee { qsearch( 'cust_bill_pkg_fee_void', { 'billpkgnum' => $self->billpkgnum } ); } +# _upgrade_data +# +# Used by FS::Upgrade to migrate to a new database. +sub _upgrade_data { # class method + my ($class, %opts) = @_; + + my $error; + # fix voids with tax from before July 2013, when the taxable_billpkgnum + # field was added to the void table + my $search = FS::Cursor->new({ + 'table' => 'cust_bill_pkg_tax_location_void', + 'hashref' => { 'taxable_billpkgnum' => '' } + }); + while (my $void = $search->fetch) { + # the history for the unvoided record should have the correct + # taxable_billpkgnum + my $num = $void->billpkgtaxlocationnum; + my $unvoid = qsearchs({ + 'table' => 'h_cust_bill_pkg_tax_location', + 'hashref' => { 'billpkgtaxlocationnum' => $num }, + 'order_by' => ' ORDER BY history_date DESC LIMIT 1' + }); + if (!$unvoid) { + # should never happen + die "billpkgtaxlocationnum $num: could not find pre-void history record to restore taxable_billpkgnum."; + } + if ($unvoid) { + $void->set('taxable_billpkgnum', $unvoid->taxable_billpkgnum); + $error = $void->replace; + die "billpkgtaxlocationnum $num: $error\n" if $error; + } + } + +} + =back =head1 BUGS -- 2.11.0