better times on failed billing events
[freeside.git] / FS / FS / cust_credit.pm
index 1f792da..284d59d 100644 (file)
@@ -1,8 +1,8 @@
 package FS::cust_credit;
 
 use strict;
 package FS::cust_credit;
 
 use strict;
-use vars qw( @ISA );
-use FS::UID qw( getotaker );
+use vars qw( @ISA $conf $unsuspendauto );
+use FS::UID qw( dbh getotaker );
 use FS::Record qw( qsearch qsearchs );
 use FS::cust_main;
 use FS::cust_refund;
 use FS::Record qw( qsearch qsearchs );
 use FS::cust_main;
 use FS::cust_refund;
@@ -10,6 +10,14 @@ use FS::cust_credit_bill;
 
 @ISA = qw( FS::Record );
 
 
 @ISA = qw( FS::Record );
 
+#ask FS::UID to run this stuff for us later
+$FS::UID::callback{'FS::cust_credit'} = sub { 
+
+  $conf = new FS::Conf;
+  $unsuspendauto = $conf->exists('unsuspendauto');
+
+};
+
 =head1 NAME
 
 FS::cust_credit - Object methods for cust_credit records
 =head1 NAME
 
 FS::cust_credit - Object methods for cust_credit records
@@ -50,6 +58,8 @@ L<Time::Local> and L<Date::Parse> for conversion functions.
 
 =item reason - text
 
 
 =item reason - text
 
+=item closed - books closed flag, empty or `Y'
+
 =back
 
 =head1 METHODS
 =back
 
 =head1 METHODS
@@ -69,6 +79,48 @@ sub table { 'cust_credit'; }
 Adds this credit to the database ("Posts" the credit).  If there is an error,
 returns the error, otherwise returns false.
 
 Adds this credit to the database ("Posts" the credit).  If there is an error,
 returns the error, otherwise returns false.
 
+=cut
+
+sub insert {
+  my $self = shift;
+
+  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_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
+  my $old_balance = $cust_main->balance;
+
+  my $error = $self->SUPER::insert;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return "error inserting $self: $error";
+  }
+
+  #false laziness w/ cust_credit::insert
+  if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
+    my @errors = $cust_main->unsuspend;
+    #return 
+    # side-fx with nested transactions?  upstack rolls back?
+    warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
+         join(' / ', @errors)
+      if @errors;
+  }
+  #eslaf
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  '';
+
+}
+
 =item delete
 
 Currently unimplemented.
 =item delete
 
 Currently unimplemented.
@@ -76,7 +128,9 @@ Currently unimplemented.
 =cut
 
 sub delete {
 =cut
 
 sub delete {
-  return "Can't remove credit!"
+  my $self = shift;
+  return "Can't delete closed credit" if $self->closed =~ /^Y/i;
+  $self->SUPER::delete(@_);
 }
 
 =item replace OLD_RECORD
 }
 
 =item replace OLD_RECORD
@@ -106,10 +160,13 @@ sub check {
     || $self->ut_number('custnum')
     || $self->ut_numbern('_date')
     || $self->ut_money('amount')
     || $self->ut_number('custnum')
     || $self->ut_numbern('_date')
     || $self->ut_money('amount')
-    || $self->ut_textn('reason');
+    || $self->ut_textn('reason')
+    || $self->ut_enum('closed', [ '', 'Y' ])
   ;
   return $error if $error;
 
   ;
   return $error if $error;
 
+  return "amount must be > 0 " if $self->amount <= 0;
+
   return "Unknown customer"
     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
 
   return "Unknown customer"
     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
 
@@ -185,7 +242,7 @@ sub credited {
 
 =head1 VERSION
 
 
 =head1 VERSION
 
-$Id: cust_credit.pm,v 1.11 2001-09-02 07:49:52 ivan Exp $
+$Id: cust_credit.pm,v 1.16 2002-06-04 14:35:52 ivan Exp $
 
 =head1 BUGS
 
 
 =head1 BUGS