eliminate some false laziness in FS::Misc::send_email vs. msg_template/email.pm send_...
[freeside.git] / FS / FS / cust_credit_refund.pm
index f366eb1..a0aeca7 100644 (file)
@@ -1,12 +1,7 @@
-package cust_credit_refund;
+package FS::cust_credit_refund;
+use base qw( FS::cust_main_Mixin FS::Record );
 
 use strict;
-use vars qw( @ISA );
-use FS::Record qw( qsearch qsearchs dbh );
-#use FS::UID qw(getotaker);
-#use FS::cust_credit
-
-@ISA = qw( FS::Record );
 
 =head1 NAME
 
@@ -56,6 +51,8 @@ L<Time::Local> and L<Date::Parse> for conversion functions.
 
 Creates a new record.  To add the record to the database, see L<"insert">.
 
+=cut
+
 sub table { 'cust_credit_refund'; }
 
 =item insert
@@ -67,54 +64,27 @@ otherwise returns false.
 
 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 $error = $self->check;
-  return $error if $error;
-
-  $error = $self->SUPER::insert;
-
-  my $cust_refund =
-    qsearchs('cust_refund', { 'refundnum' => $self->refundnum } )
-  or do {
-    $dbh->rollback if $oldAutoCommit;
-    return "unknown cust_refund.refundnum: ". $self->refundnum
-  };
-
-  my $refund_total = 0;
-  $refund_total += $_ foreach map { $_->amount }
-    qsearch('cust_credit_refund', { 'refundnum' => $self->refundnum } );
-
-  if ( $refund_total > $cust_refund->refund ) {
-    $dbh->rollback if $oldAutoCommit;
-    return "total cust_credit_refund.amount $refund_total for refundnum ".
-           $self->refundnum.
-           " greater than cust_refund.refund ". $cust_refund->refund;
-  }
-
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
-
-  '';
+  return "Can't apply refund to closed credit"
+    if $self->cust_credit->closed =~ /^Y/i;
+  return "Can't apply credit to closed refund"
+    if $self->cust_refund->closed =~ /^Y/i;
+  $self->SUPER::insert(@_);
 }
 
 =item delete
 
-Currently unimplemented (accounting reasons).
+Remove this cust_credit_refund from the database.  If there is an error, 
+returns the error, otherwise returns false.
 
 =cut
 
 sub delete {
-  return "Can't (yet?) delete cust_credit_refund records!";
+  my $self = shift;
+  return "Can't remove refund from closed credit"
+    if $self->cust_credit->closed =~ /^Y/i;
+  return "Can't remove credit from closed refund"
+    if $self->cust_refund->closed =~ /^Y/i;
+  $self->SUPER::delete(@_);
 }
 
 =item replace OLD_RECORD
@@ -129,8 +99,9 @@ sub replace {
 
 =item check
 
-Checks all fields to make sure this is a valid payment.  If there is an error,
-returns the error, otherwise returns false.  Called by the insert method.
+Checks all fields to make sure this is a valid refund application.  If there is
+an error, returns the error, otherwise returns false.  Called by the insert
+method.
 
 =cut
 
@@ -140,30 +111,48 @@ sub check {
   my $error = 
     $self->ut_numbern('creditrefundnum')
     || $self->ut_number('crednum')
-    || $self->ut_numner('refundnum')
+    || $self->ut_number('refundnum')
     || $self->ut_money('amount')
     || $self->ut_numbern('_date')
   ;
   return $error if $error;
 
-  $self->_date(time) unless $self->_date;
+  return "amount must be > 0" if $self->amount <= 0;
 
   return "unknown cust_credit.crednum: ". $self->crednum
-    unless qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
+    unless my $cust_credit = $self->cust_credit;
+
+  return "Unknown refund"
+    unless my $cust_refund = $self->cust_refund;
+
+  $self->_date(time) unless $self->_date;
+
+  return "Cannot apply more than remaining value of credit"
+    unless $self->amount <= $cust_credit->credited;
 
-  ''; #no error
+  return "Cannot apply more than remaining value of refund"
+    unless $self->amount <= $cust_refund->unapplied;
+
+  $self->SUPER::check;
 }
 
-=back
+=item cust_refund
+
+Returns the refund (see L<FS::cust_refund>)
 
-=head1 VERSION
+=item cust_credit
 
-$Id: cust_credit_refund.pm,v 1.1 2001-09-01 20:11:07 ivan Exp $
+Returns the credit (see L<FS::cust_credit>)
+
+=back
 
 =head1 BUGS
 
 Delete and replace methods.
 
+the checks for over-applied refunds could be better done like the ones in
+cust_bill_credit
+
 =head1 SEE ALSO
 
 L<FS::cust_credit>, L<FS::cust_refund>, L<FS::Record>, schema.html from the