backup the schema for tables we don't need the data from. RT#85959
[freeside.git] / FS / FS / cust_credit_void.pm
index ac47d95..60beaa6 100644 (file)
@@ -1,11 +1,16 @@
 package FS::cust_credit_void; 
+use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::reason_Mixin FS::Record );
 
 use strict;
-use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::Record );
-use FS::Record qw(qsearch qsearchs dbh fields);
+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
 
@@ -85,6 +90,7 @@ sub check {
     || $self->ut_numbern('void_date')
     || $self->ut_textn('void_reason')
     || $self->ut_foreign_keyn('void_usernum', 'access_user', 'usernum')
+    || $self->ut_foreign_keyn('void_reasonnum', 'reason', 'reasonnum')
   ;
   return $error if $error;
 
@@ -96,17 +102,53 @@ sub check {
   $self->SUPER::check;
 }
 
-=item cust_main
+=item unvoid 
 
-Returns the parent customer object (see L<FS::cust_main>).
+"Un-void"s this credit: Deletes the voided credit from the database and adds
+back (but does not re-apply) a normal credit.
 
 =cut
 
-sub cust_main {
+sub unvoid {
   my $self = shift;
-  qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
+
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE';
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my $cust_credit = new FS::cust_credit ( {
+    map { $_ => $self->get($_) } grep { $_ !~ /void/ } $self->fields
+  } );
+  my $error = $cust_credit->insert;
+
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  $error ||= $self->delete;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  '';
+
 }
 
+=item cust_main
+
+Returns the parent customer object (see L<FS::cust_main>).
+
 =item void_access_user
 
 Returns the voiding employee object (see L<FS::access_user>).
@@ -118,6 +160,51 @@ sub void_access_user {
   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
 }
 
+=item void_access_user_name
+
+Returns the voiding employee name.
+
+=cut
+
+sub void_access_user_name {
+  my $self = shift;
+  my $user = $self->void_access_user;
+  return unless $user;
+  return $user->name;
+}
+
+=item void_reason
+
+Returns the text of the associated void credit reason (see L<FS::reason>) for this voided credit.
+
+The reason for the original credit remains accessible through the reason method.
+
+=cut
+
+sub void_reason {
+  my ($self, $value, %options) = @_;
+  my $reason_text;
+  if ( $self->void_reasonnum ) {
+    my $reason = FS::reason->by_key($self->void_reasonnum);
+    $reason_text = $reason->reason;
+  } else { # in case one of these somehow still exists
+    $reason_text = $self->get('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