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;
@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
=item reason - text
+=item closed - books closed flag, empty or `Y'
+
=back
=head1 METHODS
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.
=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
|| $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 "amount must be > 0 " if $self->amount <= 0;
+
return "Unknown customer"
unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
$self->otaker(getotaker);
- ''; #no error
+ $self->SUPER::check;
}
=item cust_refund
=head1 VERSION
-$Id: cust_credit.pm,v 1.11 2001-09-02 07:49:52 ivan Exp $
+$Id: cust_credit.pm,v 1.17 2003-08-05 00:20:41 khoff Exp $
=head1 BUGS