Tables added to the list with data upgrade because of reason and void_reason
authorIrina Todeva <itodeva@hostgator.com>
Thu, 8 Oct 2015 18:03:08 +0000 (12:03 -0600)
committerMark Wells <mark@freeside.biz>
Tue, 3 Nov 2015 20:28:13 +0000 (12:28 -0800)
legacy fields:
  - cust_credit_void
  - cust_bill_void
  - cust_bill_pkg_void

Made some changes to reason_Mixin to be able to upgrade reason an void_reason
fields

FS/FS/Upgrade.pm
FS/FS/cust_bill_pkg_void.pm
FS/FS/cust_bill_void.pm
FS/FS/cust_credit_void.pm
FS/FS/reason_Mixin.pm

index 263230b..bffda56 100644 (file)
@@ -350,6 +350,11 @@ sub upgrade_data {
     #customer credits
     'cust_credit' => [],
 
+    # reason / void_reason migration to reasonnum / void_reasonnum
+    'cust_credit_void' => [],
+    'cust_bill_void' => [],
+    'cust_bill_pkg_void' => [],
+
     #duplicate history records
     'h_cust_svc'  => [],
 
index 4b9cffd..991dd37 100644 (file)
@@ -2,6 +2,7 @@ package FS::cust_bill_pkg_void;
 use base qw( FS::TemplateItem_Mixin FS::reason_Mixin FS::Record );
 
 use strict;
+use vars qw( $me $DEBUG );
 use FS::Record qw( qsearch qsearchs dbh fields );
 use FS::cust_bill_void;
 use FS::cust_bill_pkg_detail;
@@ -13,6 +14,9 @@ use FS::cust_bill_pkg_tax_location;
 use FS::cust_bill_pkg_tax_rate_location;
 use FS::cust_tax_exempt_pkg;
 
+$me = '[ FS::cust_bill_pkg_void ]';
+$DEBUG = 0;
+
 =head1 NAME
 
 FS::cust_bill_pkg_void - Object methods for cust_bill_pkg_void records
@@ -279,6 +283,18 @@ 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) = @_;
+
+  warn "$me upgrading $class\n" if $DEBUG;
+
+  $class->_upgrade_reasonnum(%opts);
+}
+
 =back
 
 =head1 BUGS
index 04c69d4..50f69c9 100644 (file)
@@ -3,12 +3,16 @@ use base qw( FS::Template_Mixin FS::cust_main_Mixin FS::otaker_Mixin
              FS::reason_Mixin FS::Record );
 
 use strict;
+use vars qw( $me $DEBUG );
 use FS::Record qw( qsearch qsearchs dbh fields );
 use FS::cust_statement;
 use FS::access_user;
 use FS::cust_bill_pkg_void;
 use FS::cust_bill;
 
+$me = '[ FS::cust_bill_void ]';
+$DEBUG = 0;
+
 =head1 NAME
 
 FS::cust_bill_void - Object methods for cust_bill_void records
@@ -349,6 +353,17 @@ sub search_sql_where {
 
 sub enable_previous { 0 }
 
+# _upgrade_data
+#
+# Used by FS::Upgrade to migrate to a new database.
+sub _upgrade_data {  # class method
+  my ($class, %opts) = @_;
+
+  warn "$me upgrading $class\n" if $DEBUG;
+
+  $class->_upgrade_reasonnum(%opts);
+}
+
 =back
 
 =head1 BUGS
index 9c92068..60beaa6 100644 (file)
@@ -2,12 +2,16 @@ package FS::cust_credit_void;
 use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::reason_Mixin FS::Record );
 
 use strict;
+use vars qw( $me $DEBUG );
 use FS::Record qw(qsearchs); # qsearch qsearchs);
 use FS::CurrentUser;
 use FS::access_user;
 use FS::cust_credit;
 use FS::UID qw( dbh );
 
+$me = '[ FS::cust_credit_void ]';
+$DEBUG = 0;
+
 =head1 NAME
 
 FS::cust_credit_void - Object methods for cust_credit_void objects
@@ -190,6 +194,17 @@ sub void_reason {
   return $reason_text;
 }
 
+# _upgrade_data
+#
+# Used by FS::Upgrade to migrate to a new database.
+sub _upgrade_data {    # class method
+    my ( $class, %opts ) = @_;
+
+    warn "$me upgrading $class\n" if $DEBUG;
+
+    $class->_upgrade_reasonnum(%opts);
+}
+
 =back
 
 =head1 BUGS
index a4c2d3f..af9aa50 100644 (file)
@@ -39,52 +39,64 @@ sub reason {
 # FS::reason->new_or_existing
  
 # Used by FS::Upgrade to migrate reason text fields to reasonnum.
-sub _upgrade_reasonnum {  # class method
-  my $class = shift;
-  my $table = $class->table;
-
-  if (   defined dbdef->table($table)->column('reason')
-      && defined dbdef->table($table)->column('reasonnum') )
-  {
-
-    warn "$me Checking for unmigrated reasons\n" if $DEBUG;
+sub _upgrade_reasonnum {    # class method
+    my $class = shift;
+    my $table = $class->table;
 
-    my @legacy_reason_records = qsearch(
+    for my $fieldname (qw(reason void_reason)) {
+        if (   defined dbdef->table($table)->column($fieldname)
+            && defined dbdef->table($table)->column( $fieldname . 'num' ) )
         {
-            'table'     => $table,
-            'hashref'   => {},
-            'extra_sql' => 'WHERE reason IS NOT NULL',
+
+            warn "$me Checking for unmigrated reasons\n" if $DEBUG;
+
+            my @legacy_reason_records = qsearch(
+                {
+                    'table'     => $table,
+                    'hashref'   => {},
+                    'extra_sql' => 'WHERE ' . $fieldname . ' IS NOT NULL',
+                }
+            );
+
+            if (
+                scalar(
+                    grep { $_->getfield($fieldname) =~ /\S/ }
+                      @legacy_reason_records
+                )
+              )
+            {
+                warn "$me Found unmigrated reasons\n" if $DEBUG;
+
+                my $reason_type =
+                  _upgrade_get_legacy_reason_type( $class, $table );
+                my $noreason = _upgrade_get_no_reason( $class, $reason_type );
+
+                foreach my $record_to_upgrade (@legacy_reason_records) {
+                    my $reason = $record_to_upgrade->getfield($fieldname);
+                    warn "Contemplating reason $reason\n" if $DEBUG > 1;
+                    if ( $reason =~ /\S/ ) {
+                        my $reason =
+                          _upgrade_get_reason( $class, $reason, $reason_type );
+                        $record_to_upgrade->set( $fieldname . 'num',
+                            $reason->reasonnum );
+                    }
+                    else {
+                        $record_to_upgrade->set( $fieldname . 'num',
+                            $noreason->reasonnum );
+                    }
+
+                    $record_to_upgrade->setfield( $fieldname, '' );
+                    my $error = $record_to_upgrade->replace;
+
+                    my $primary_key = $record_to_upgrade->primary_key;
+                    warn "*** WARNING: error replacing $fieldname in $class "
+                      . $record_to_upgrade->get($primary_key)
+                      . ": $error ***\n"
+                      if $error;
+                }
+            }
         }
-    );
-
-    if (scalar(grep { $_->getfield('reason') =~ /\S/ } @legacy_reason_records)) {
-      warn "$me Found unmigrated reasons\n" if $DEBUG;
-
-      my $reason_type = _upgrade_get_legacy_reason_type($class, $table);
-      my $noreason = _upgrade_get_no_reason($class, $reason_type);
-
-      foreach my $record_to_upgrade (@legacy_reason_records) {
-          my $reason = $record_to_upgrade->getfield('reason');
-          warn "Contemplating reason $reason\n" if $DEBUG > 1;
-          if ( $reason =~ /\S/ ) {
-              my $reason = _upgrade_get_reason( $class, $reason, $reason_type );
-              $record_to_upgrade->reasonnum( $reason->reasonnum );
-          }
-          else {
-              $record_to_upgrade->reasonnum( $noreason->reasonnum );
-          }
-
-          $record_to_upgrade->setfield( 'reason', '' );
-          my $error = $record_to_upgrade->replace;
-
-          my $primary_key = $record_to_upgrade->primary_key;
-          warn "*** WARNING: error replacing reason in $class "
-            . $record_to_upgrade->get($primary_key)
-            . ": $error ***\n"
-            if $error;
-       }
     }
-  }
 }
 
 # _upgrade_get_legacy_reason_type is class method supposed to be used only