bundled package presentation improvements
[freeside.git] / FS / FS / cust_refund.pm
index 3f17f9a..abc131e 100644 (file)
@@ -3,15 +3,16 @@ package FS::cust_refund;
 use strict;
 use vars qw( @ISA @encrypted_fields );
 use Business::CreditCard;
-use FS::Record qw( qsearch qsearchs dbh );
 use FS::UID qw(getotaker);
+use FS::Record qw( qsearch qsearchs dbh );
+use FS::cust_main_Mixin;
+use FS::payinfo_transaction_Mixin;
 use FS::cust_credit;
 use FS::cust_credit_refund;
 use FS::cust_pay_refund;
 use FS::cust_main;
-use FS::payinfo_Mixin;
 
-@ISA = qw( FS::Record FS::payinfo_Mixin );
+@ISA = qw( FS::payinfo_transaction_Mixin FS::cust_main_Mixin FS::Record );
 
 @encrypted_fields = ('payinfo');
 
@@ -166,24 +167,63 @@ sub insert {
 
 =item delete
 
-Currently unimplemented (accounting reasons).
+Unless the closed flag is set, deletes this refund and all associated
+applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
 
 =cut
 
 sub delete {
   my $self = shift;
   return "Can't delete closed refund" if $self->closed =~ /^Y/i;
-  $self->SUPER::delete(@_);
+
+  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;
+
+  foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
+    my $error = $cust_credit_refund->delete;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
+  foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
+    my $error = $cust_pay_refund->delete;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
+  my $error = $self->SUPER::delete(@_);
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  '';
+
 }
 
 =item replace OLD_RECORD
 
-Currently unimplemented (accounting reasons).
+Modifying a refund?  Well, don't say I didn't warn you.
 
 =cut
 
 sub replace {
-   return "Can't (yet?) modify cust_refund records!";
+  my $self = shift;
+  $self->SUPER::replace(@_);
 }
 
 =item check
@@ -196,10 +236,13 @@ returns the error, otherwise returns false.  Called by the insert method.
 sub check {
   my $self = shift;
 
+  $self->otaker(getotaker) unless ($self->otaker);
+
   my $error =
     $self->ut_numbern('refundnum')
     || $self->ut_numbern('custnum')
     || $self->ut_money('refund')
+    || $self->ut_alpha('otaker')
     || $self->ut_text('reason')
     || $self->ut_numbern('_date')
     || $self->ut_textn('paybatch')
@@ -218,8 +261,6 @@ sub check {
   $error = $self->payinfo_check;
   return $error if $error;
 
-  $self->otaker(getotaker);
-
   $self->SUPER::check;
 }
 
@@ -269,6 +310,36 @@ sub unapplied {
 
 =back
 
+=head1 CLASS METHODS
+
+=over 4
+
+=item unapplied_sql
+
+Returns an SQL fragment to retreive the unapplied amount.
+
+=cut 
+
+sub unapplied_sql {
+  #my $class = shift;
+
+  "refund
+    - COALESCE( 
+                ( SELECT SUM(amount) FROM cust_credit_refund
+                    WHERE cust_refund.refundnum = cust_credit_refund.refundnum )
+                ,0
+              )
+    - COALESCE(
+                ( SELECT SUM(amount) FROM cust_pay_refund
+                    WHERE cust_refund.refundnum = cust_pay_refund.refundnum )
+                ,0
+              )
+  ";
+
+}
+
+=back
+
 =head1 BUGS
 
 Delete and replace methods.