freeside/FS/FS cust_pay_refund.pm,NONE,1.1 cust_credit_bill.pm,1.11,1.12 cust_pay.pm,1.27,1.28 cust_refund.pm,1.22,1.23 cust_credit_refund.pm,1.10,1.11 cust_bill_pay.pm,1.14,1.15

ivan ivan at pouncequick.420.am
Mon Jun 28 21:02:49 PDT 2004


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory pouncequick:/tmp/cvs-serv2036/FS/FS

Modified Files:
	cust_credit_bill.pm cust_pay.pm cust_refund.pm 
	cust_credit_refund.pm cust_bill_pay.pm 
Added Files:
	cust_pay_refund.pm 
Log Message:
add cust_pay_refund table to refund payments

Index: cust_bill_pay.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_bill_pay.pm,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- cust_bill_pay.pm	29 Nov 2003 08:32:40 -0000	1.14
+++ cust_bill_pay.pm	29 Jun 2004 04:02:44 -0000	1.15
@@ -74,60 +74,11 @@
 
 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;
+  my $error = $self->SUPER::insert;
   return $error if $error;
 
-  $error = $self->SUPER::insert;
-
-  my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } ) or do {
-    $dbh->rollback if $oldAutoCommit;
-    return "unknown cust_pay.paynum: ". $self->paynum;
-  };
-
-  my $pay_total = 0;
-  $pay_total += $_ foreach map { $_->amount }
-    qsearch('cust_bill_pay', { 'paynum' => $self->paynum } );
-
-  if ( sprintf("%.2f", $pay_total) > sprintf("%.2f", $cust_pay->paid) ) {
-    $dbh->rollback if $oldAutoCommit;
-    return "total cust_bill_pay.amount $pay_total for paynum ". $self->paynum.
-           " greater than cust_pay.paid ". $cust_pay->paid;
-  }
-
-  my $cust_bill = $self->cust_bill;
-  unless ( $cust_bill ) {
-    $dbh->rollback if $oldAutoCommit;
-    return "unknown cust_bill.invnum: ". $self->invnum;
-  };
-
-  my $bill_total = 0;
-  $bill_total += $_ foreach map { $_->amount }
-    qsearch('cust_bill_pay', { 'invnum' => $self->invnum } );
-  $bill_total += $_ foreach map { $_->amount } 
-    qsearch('cust_credit_bill', { 'invnum' => $self->invnum } );
-  if ( sprintf("%.2f", $bill_total) > sprintf("%.2f", $cust_bill->charged) ) {
-    $dbh->rollback if $oldAutoCommit;
-    return "total cust_bill_pay.amount and cust_credit_bill.amount $bill_total".
-           " for invnum ". $self->invnum.
-           " greater than cust_bill.charged ". $cust_bill->charged;
-  }
-
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
-
-  if ( $conf->exists('invoice_send_receipts') ) {
-    my $send_error = $cust_bill->send;
+ if ( $conf->exists('invoice_send_receipts') ) {
+    my $send_error = $self->cust_bill->send;
     warn "Error sending receipt: $send_error\n" if $send_error;
   }
 
@@ -178,8 +129,22 @@
   return $error if $error;
 
   return "amount must be > 0" if $self->amount <= 0;
+  
+  return "Unknown invoice"
+    unless my $cust_bill =
+      qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
+
+  return "Unknown payment"
+    unless my $cust_pay = 
+      qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
 
   $self->_date(time) unless $self->_date;
+
+  return "Cannot apply more than remaining value of invoice"
+    unless $self->amount <= $cust_bill->owed;
+
+  return "Cannot apply more than remaining value of payment"
+    unless $self->amount <= $cust_pay->unapplied;
 
   $self->SUPER::check;
 }

--- NEW FILE: cust_pay_refund.pm ---
package FS::cust_pay_refund;

use strict;
use vars qw( @ISA ); #$conf );
use FS::UID qw( getotaker );
use FS::Record qw( qsearchs ); # qsearch );
use FS::cust_main;
use FS::cust_pay;
use FS::cust_refund;

@ISA = qw( FS::Record );

#ask FS::UID to run this stuff for us later
#FS::UID->install_callback( sub { 
#  $conf = new FS::Conf;
#} );

=head1 NAME

FS::cust_pay_refund - Object methods for cust_pay_refund records

=head1 SYNOPSIS

  use FS::cust_pay_refund;

  $record = new FS::cust_pay_refund \%hash;
  $record = new FS::cust_pay_refund { 'column' => 'value' };

  $error = $record->insert;

  $error = $new_record->replace($old_record);

  $error = $record->delete;

  $error = $record->check;

=head1 DESCRIPTION

An FS::cust_pay_refund object represents application of a refund (see
L<FS::cust_refund>) to an payment (see L<FS::cust_pay>).  FS::cust_pay_refund
inherits from FS::Record.  The following fields are currently supported:

=over 4

=item payrefundnum - primary key

=item paynum - credit being applied 

=item refundnum - invoice to which credit is applied (see L<FS::cust_bill>)

=item amount - amount of the credit applied

=item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
L<Time::Local> and L<Date::Parse> for conversion functions.

=back

=head1 METHODS

=over 4

=item new HASHREF

Creates a new cust_pay_refund.  To add the cust_pay_refund to the database,
see L<"insert">.

=cut

sub table { 'cust_pay_refund'; }

=item insert

Adds this cust_pay_refund to the database.  If there is an error, returns the
error, otherwise returns false.

=cut

sub insert {
  my $self = shift;
  my $error = $self->SUPER::insert(@_);
  return $error if $error;

  '';
}

=item delete

=cut

sub delete {
  my $self = shift;
  return "Can't apply refund to closed payment"
    if $self->cust_pay->closed =~ /^Y/i;
  return "Can't apply closed refund"
    if $self->cust_refund->closed =~ /^Y/i;
  $self->SUPER::delete(@_);
}

=item replace OLD_RECORD

Application of refunds to payments may not be modified.

=cut

sub replace {
  return "Can't modify application of a refund to payment!"
}

=item check

Checks all fields to make sure this is a valid refund application to a payment.
If there is an error, returns the error, otherwise returns false.  Called by
the insert and replace methods.

=cut

sub check {
  my $self = shift;

  my $error =
    $self->ut_numbern('payrefundnum')
    || $self->ut_number('paynum')
    || $self->ut_number('refundnum')
    || $self->ut_numbern('_date')
    || $self->ut_money('amount')
  ;
  return $error if $error;

  return "amount must be > 0" if $self->amount <= 0;

  return "Unknown payment"
    unless my $cust_pay = 
      qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );

  return "Unknown refund"
    unless my $cust_refund =
      qsearchs( 'cust_refund', { 'refundnum' => $self->refundnum } );

  $self->_date(time) unless $self->_date;

  return 'Cannot apply ($'. $self->amount. ') more than'.
         ' remaining value of refund ($'. $cust_refund->unapplied. ')'
    unless $self->amount <= $cust_refund->unapplied;

  return "Cannot apply more than remaining value of payment"
    unless $self->amount <= $cust_pay->unapplied;

  $self->SUPER::check;
}

=item sub cust_credit

Returns the credit (see L<FS::cust_credit>)

=cut

sub cust_credit {
  my $self = shift;
  qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
}

=item cust_bill 

Returns the invoice (see L<FS::cust_bill>)

=cut

sub cust_bill {
  my $self = shift;
  qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
}

=back

=head1 BUGS

The delete method.

=head1 SEE ALSO

L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, L<FS::cust_credit>,
schema.html from the base documentation.

=cut

1;


Index: cust_credit_bill.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_credit_bill.pm,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- cust_credit_bill.pm	9 Jan 2004 22:11:53 -0000	1.11
+++ cust_credit_bill.pm	29 Jun 2004 04:02:44 -0000	1.12
@@ -38,7 +38,7 @@
 =head1 DESCRIPTION
 
 An FS::cust_credit_bill object represents application of a credit (see
-L<FS::cust_credit>) to an invoice (see L<FS::cust_bill>).  FS::cust_credit
+L<FS::cust_credit>) to an invoice (see L<FS::cust_bill>).  FS::cust_credit_bill
 inherits from FS::Record.  The following fields are currently supported:
 
 =over 4

Index: cust_credit_refund.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_credit_refund.pm,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- cust_credit_refund.pm	5 Aug 2003 00:20:41 -0000	1.10
+++ cust_credit_refund.pm	29 Jun 2004 04:02:44 -0000	1.11
@@ -70,43 +70,9 @@
 
 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;
+  my $error = $self->SUPER::insert;
   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;
-
   '';
 }
 
@@ -132,8 +98,9 @@
 
 =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
 
@@ -151,10 +118,21 @@
 
   return "amount must be > 0" if $self->amount <= 0;
 
+  return "unknown cust_credit.crednum: ". $self->crednum
+    unless my $cust_credit =
+      qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
+
+  return "Unknown refund"
+    unless my $cust_refund =
+      qsearchs( 'cust_refund', { 'refundnum' => $self->refundnum } );
+
   $self->_date(time) unless $self->_date;
 
-  return "unknown cust_credit.crednum: ". $self->crednum
-    unless qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
+  return "Cannot apply more than remaining value of credit"
+    unless $self->amount <= $cust_credit->credited;
+
+  return "Cannot apply more than remaining value of refund"
+    unless $self->amount <= $cust_refund->unapplied;
 
   $self->SUPER::check;
 }

Index: cust_refund.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_refund.pm,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- cust_refund.pm	20 Apr 2004 00:58:04 -0000	1.22
+++ cust_refund.pm	29 Jun 2004 04:02:44 -0000	1.23
@@ -3,10 +3,11 @@
 use strict;
 use vars qw( @ISA );
 use Business::CreditCard;
-use FS::Record qw( qsearchs dbh );
+use FS::Record qw( qsearch qsearchs dbh );
 use FS::UID qw(getotaker);
 use FS::cust_credit;
 use FS::cust_credit_refund;
+use FS::cust_pay_refund;
 use FS::cust_main;
 
 @ISA = qw( FS::Record );
@@ -78,7 +79,9 @@
 
 For backwards-compatibility and convenience, if the additional field crednum is
 defined, an FS::cust_credit_refund record for the full amount of the refund
-will be created.  In this case, custnum is optional.
+will be created.  Or (this time for convenience and consistancy), if the
+additional field paynum is defined, an FS::cust_pay_refund record for the full
+amount of the refund will be created.  In both cases, custnum is optional.
 
 =cut
 
@@ -103,6 +106,13 @@
         return "Unknown cust_credit.crednum: ". $self->crednum;
       };
     $self->custnum($cust_credit->custnum);
+  } elsif ( $self->paynum ) {
+    my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
+      or do {
+        $dbh->rollback if $oldAutoCommit;
+        return "Unknown cust_pay.paynum: ". $self->paynum;
+      };
+    $self->custnum($cust_pay->custnum);
   }
 
   my $error = $self->check;
@@ -127,57 +137,20 @@
       return $error;
     }
     #$self->custnum($cust_credit_refund->cust_credit->custnum);
-  }
-
-
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
-
-  '';
-
-}
-
-sub upgrade_replace { #1.3.x->1.4.x
-  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;
-
-  my %new = $self->hash;
-  my $new = FS::cust_refund->new(\%new);
-
-  if ( $self->crednum ) {
-    my $cust_credit_refund = new FS::cust_credit_refund {
-      'crednum'   => $self->crednum,
+  } elsif ( $self->paynum ) {
+    my $cust_pay_refund = new FS::cust_pay_refund {
+      'paynum'    => $self->paynum,
       'refundnum' => $self->refundnum,
       'amount'    => $self->refund,
       '_date'     => $self->_date,
     };
-    $error = $cust_credit_refund->insert;
+    $error = $cust_pay_refund->insert;
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return $error;
     }
-    $new->custnum($cust_credit_refund->cust_credit->custnum);
-  } else {
-    die;
   }
 
-  $error = $new->SUPER::replace($self);
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return $error;
-  }
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
@@ -262,6 +235,52 @@
 
   $self->SUPER::check;
 }
+
+=item cust_credit_refund
+
+Returns all applications to credits (see L<FS::cust_credit_refund>) for this
+refund.
+
+=cut
+
+sub cust_credit_refund {
+  my $self = shift;
+  sort { $a->_date <=> $b->_date }
+    qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
+  ;
+}
+
+=item cust_pay_refund
+
+Returns all applications to payments (see L<FS::cust_pay_refund>) for this
+refund.
+
+=cut
+
+sub cust_pay_refund {
+  my $self = shift;
+  sort { $a->_date <=> $b->_date }
+    qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
+  ;
+}
+
+=item unapplied
+
+Returns the amount of this refund that is still unapplied; which is
+amount minus all credit applications (see L<FS::cust_credit_refund>) and
+payment applications (see L<FS::cust_pay_refund>).
+
+=cut
+
+sub unapplied {
+  my $self = shift;
+  my $amount = $self->refund;
+  $amount -= $_->amount foreach ( $self->cust_credit_refund );
+  $amount -= $_->amount foreach ( $self->cust_pay_refund );
+  sprintf("%.2f", $amount );
+}
+
+
 
 =item payinfo_masked
 

Index: cust_pay.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_pay.pm,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- cust_pay.pm	20 Apr 2004 00:58:04 -0000	1.27
+++ cust_pay.pm	29 Jun 2004 04:02:44 -0000	1.28
@@ -9,6 +9,7 @@
 use FS::Misc qw(send_email);
 use FS::cust_bill;
 use FS::cust_bill_pay;
+use FS::cust_pay_refund;
 use FS::cust_main;
 
 @ISA = qw( FS::Record );
@@ -371,10 +372,26 @@
   ;
 }
 
+=item cust_pay_refund
+
+Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
+payment.
+
+=cut
+
+sub cust_pay_refund {
+  my $self = shift;
+  sort { $a->_date <=> $b->_date }
+    qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
+  ;
+}
+
+
 =item unapplied
 
 Returns the amount of this payment that is still unapplied; which is
-paid minus all payment applications (see L<FS::cust_bill_pay>).
+paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
+applications (see L<FS::cust_pay_refund>).
 
 =cut
 
@@ -382,6 +399,7 @@
   my $self = shift;
   my $amount = $self->paid;
   $amount -= $_->amount foreach ( $self->cust_bill_pay );
+  $amount -= $_->amount foreach ( $self->cust_pay_refund );
   sprintf("%.2f", $amount );
 }
 




More information about the freeside-commits mailing list