summaryrefslogtreecommitdiff
path: root/FS
diff options
context:
space:
mode:
authorivan <ivan>2001-09-02 02:46:55 +0000
committerivan <ivan>2001-09-02 02:46:55 +0000
commit8c1f9804d9a02c0c054eededeb500c72a640249a (patch)
tree20d0161443eb1366279937378f3aee16cc542eec /FS
parent51e148d543a59a3cdec24bfb5ffb839ee7b4ac72 (diff)
cust_refund and cust_pay get custnums
Diffstat (limited to 'FS')
-rw-r--r--FS/FS/cust_bill_pay.pm15
-rw-r--r--FS/FS/cust_main.pm64
-rw-r--r--FS/FS/cust_pay.pm53
-rw-r--r--FS/FS/cust_refund.pm31
4 files changed, 135 insertions, 28 deletions
diff --git a/FS/FS/cust_bill_pay.pm b/FS/FS/cust_bill_pay.pm
index 6d08b59c8..a7e2831ea 100644
--- a/FS/FS/cust_bill_pay.pm
+++ b/FS/FS/cust_bill_pay.pm
@@ -176,14 +176,25 @@ Returns the payment (see L<FS::cust_pay>)
sub cust_pay {
my $self = shift;
- qsearchs( 'cust_pay', { 'invnum' => $self->invnum } );
+ qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
+}
+
+=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 VERSION
-$Id: cust_bill_pay.pm,v 1.3 2001-09-02 01:27:11 ivan Exp $
+$Id: cust_bill_pay.pm,v 1.4 2001-09-02 02:46:55 ivan Exp $
=head1 BUGS
diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm
index 2158289d8..c44c89377 100644
--- a/FS/FS/cust_main.pm
+++ b/FS/FS/cust_main.pm
@@ -28,7 +28,8 @@ use FS::part_referral;
use FS::cust_main_county;
use FS::agent;
use FS::cust_main_invoice;
-#use FS::cust_credit_bill;
+use FS::cust_credit_bill;
+use FS::cust_bill_pay;
use FS::prepay_credit;
@ISA = qw( FS::Record );
@@ -1242,8 +1243,10 @@ sub total_owed {
=item apply_credits
-Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)to outstanding invoice balances in cronological order and returns the value
-of any remaining unapplied credits available for refund (see L<FS::cust_refund>).
+Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
+to outstanding invoice balances in chronological order and returns the value
+of any remaining unapplied credits available for refund
+(see L<FS::cust_refund>).
=cut
@@ -1264,8 +1267,7 @@ sub apply_credits {
my $amount;
if (!(defined $credit) || $credit->credited == 0) {
- $credit = pop @credits;
- last unless defined $credit;
+ $credit = pop @credits or last;
}
if ($cust_bill->owed >= $credit->credited) {
@@ -1278,7 +1280,6 @@ sub apply_credits {
'crednum' => $credit->crednum,
'invnum' => $cust_bill->invnum,
'amount' => $amount,
- '_date' => time,
} );
my $error = $cust_credit_bill->insert;
die $error if $error;
@@ -1290,6 +1291,55 @@ sub apply_credits {
return $self->total_credited;
}
+=item apply_payments
+
+Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
+to outstanding invoice balances in chronological order.
+
+ #and returns the value of any remaining unapplied payments.
+
+=cut
+
+sub apply_payments {
+ my $self = shift;
+
+ #return 0 unless
+
+ my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
+ qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
+
+ my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
+ qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
+
+ my $payment;
+
+ foreach my $cust_bill ( @invoices ) {
+ my $amount;
+
+ if ( !defined $payment || $payment->unapplied = 0 ) {
+ $payment = pop @payments or last;
+ }
+
+ if ( $cust_bill->owed >= $payment->unapplied ) {
+ $amount = $payment->unapplied;
+ } else {
+ $amount = $payment->owed;
+ }
+
+ my $cust_bill_pay = new FS::cust_bill_pay ( {
+ 'paynum' => $payment->paynum,
+ 'invnum' => $cust_bill->invnum,
+ 'amount' => $amount,
+ } );
+ my $error = $cust_bill_pay->insert;
+ die $error if $error;
+
+ redo if ( $cust_bill->owed > 0);
+
+ }
+
+ # return 0;
+}
=item total_credited
@@ -1451,7 +1501,7 @@ sub rebuild_fuzzyfiles {
=head1 VERSION
-$Id: cust_main.pm,v 1.26 2001-09-02 01:27:11 ivan Exp $
+$Id: cust_main.pm,v 1.27 2001-09-02 02:46:55 ivan Exp $
=head1 BUGS
diff --git a/FS/FS/cust_pay.pm b/FS/FS/cust_pay.pm
index b1c35d76f..a70b7cb37 100644
--- a/FS/FS/cust_pay.pm
+++ b/FS/FS/cust_pay.pm
@@ -6,6 +6,7 @@ use Business::CreditCard;
use FS::Record qw( dbh );
use FS::cust_bill;
use FS::cust_bill_pay;
+use FS::cust_main;
@ISA = qw( FS::Record );
@@ -38,6 +39,8 @@ currently supported:
=item paynum - primary key (assigned automatically for new payments)
+=item custnum - customer (see L<FS::cust_main>)
+
=item paid - Amount of this payment
=item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
@@ -69,7 +72,7 @@ Adds this payment to the database.
For backwards-compatibility and convenience, if the additional field invnum
is defined, an FS::cust_bill_pay record for the full amount of the payment
-will be created.
+will be created. In this case, custnum is optional.
=cut
@@ -90,12 +93,6 @@ sub insert {
my $error = $self->check;
return $error if $error;
- $error = $self->SUPER::insert;
- if ( $error ) {
- $dbh->rollback if $oldAutoCommit;
- return $error;
- }
-
if ( $self->invnum ) {
my $cust_bill_pay = new FS::cust_bill_pay {
'invnum' => $self->invnum,
@@ -108,6 +105,13 @@ sub insert {
$dbh->rollback if $oldAutoCommit;
return $error;
}
+ $self->custnum($cust_bill_pay->cust_bill->custnum);
+ }
+
+ $error = $self->SUPER::insert;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
}
$dbh->commit or die $dbh->errstr if $oldAutoCommit;
@@ -148,12 +152,17 @@ sub check {
my $error =
$self->ut_numbern('paynum')
+ || $self->ut_number('custnum')
|| $self->ut_money('paid')
|| $self->ut_numbern('_date')
|| $self->ut_textn('paybatch')
;
return $error if $error;
+ return "unknown cust_main.custnum: ". $self->custnum
+ unless $self->invnum
+ || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
+
$self->_date(time) unless $self->_date;
$self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
@@ -182,11 +191,39 @@ sub check {
}
+=item cust_bill_pay
+
+Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
+payment.
+
+=cut
+
+sub cust_bill_pay {
+ my $self = shift;
+ sort { $a->_date <=> $b->_date }
+ qsearch( 'cust_bill_pay', { '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>).
+
+=cut
+
+sub unapplied {
+ my $self = shift;
+ my $amount = $self->paid;
+ $amount -= $_->amount foreach ( $self->cust_bill_pay );
+ sprintf("%.2f", $amount );
+}
+
=back
=head1 VERSION
-$Id: cust_pay.pm,v 1.4 2001-09-01 20:11:07 ivan Exp $
+$Id: cust_pay.pm,v 1.5 2001-09-02 02:46:55 ivan Exp $
=head1 BUGS
diff --git a/FS/FS/cust_refund.pm b/FS/FS/cust_refund.pm
index 7eeacbfce..c216ec284 100644
--- a/FS/FS/cust_refund.pm
+++ b/FS/FS/cust_refund.pm
@@ -7,6 +7,7 @@ use FS::Record qw( dbh );
use FS::UID qw(getotaker);
use FS::cust_credit;
use FS::cust_credit_refund;
+use FS::cust_main;
@ISA = qw( FS::Record );
@@ -39,6 +40,8 @@ inherits from FS::Record. The following fields are currently supported:
=item refundnum - primary key (assigned automatically for new refunds)
+=item custnum - customer (see L<FS::cust_main>)
+
=item refund - Amount of the refund
=item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
@@ -72,7 +75,7 @@ Adds this refund to the database.
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.
+will be created. In this case, custnum is optional.
=cut
@@ -93,24 +96,25 @@ sub insert {
my $error = $self->check;
return $error if $error;
- $error = $self->SUPER::insert;
- if ( $error ) {
- $dbh->rollback if $oldAutoCommit;
- return $error;
- }
-
if ( $self->crednum ) {
my $cust_credit_refund = new FS::cust_credit_refund {
- 'cred' => $self->cred,
+ 'cred' => $self->cred,
'refundnum' => $self->refundnum,
- 'amount' => $self->refund,
- '_date' => $self->_date,
+ 'amount' => $self->refund,
+ '_date' => $self->_date,
};
$error = $cust_bill_pay->insert;
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
return $error;
}
+ $self->custnum($cust_credit_refund->cust_credit->custnum);
+ }
+
+ $error = $self->SUPER::insert;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
}
$dbh->commit or die $dbh->errstr if $oldAutoCommit;
@@ -151,6 +155,7 @@ sub check {
my $error =
$self->ut_number('refundnum')
+ || $self->ut_number('custnum')
|| $self->ut_money('amount')
|| $self->ut_numbern('_date')
|| $self->ut_textn('paybatch')
@@ -159,6 +164,10 @@ sub check {
$self->_date(time) unless $self->_date;
+ return "unknown cust_main.custnum: ". $self->custnum
+ unless $self->invnum
+ || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
+
$self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
$self->payby($1);
@@ -189,7 +198,7 @@ sub check {
=head1 VERSION
-$Id: cust_refund.pm,v 1.5 2001-09-02 01:27:11 ivan Exp $
+$Id: cust_refund.pm,v 1.6 2001-09-02 02:46:55 ivan Exp $
=head1 BUGS