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
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 );
=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
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) {
'crednum' => $credit->crednum,
'invnum' => $cust_bill->invnum,
'amount' => $amount,
- '_date' => time,
} );
my $error = $cust_credit_bill->insert;
die $error if $error;
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
=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
use FS::Record qw( dbh );
use FS::cust_bill;
use FS::cust_bill_pay;
+use FS::cust_main;
@ISA = qw( FS::Record );
=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
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
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,
$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;
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";
}
+=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
use FS::UID qw(getotaker);
use FS::cust_credit;
use FS::cust_credit_refund;
+use FS::cust_main;
@ISA = qw( FS::Record );
=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
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
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;
my $error =
$self->ut_number('refundnum')
+ || $self->ut_number('custnum')
|| $self->ut_money('amount')
|| $self->ut_numbern('_date')
|| $self->ut_textn('paybatch')
$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);
=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
#!/usr/bin/perl -Tw
#
-# $Id: fs-setup,v 1.47 2001-09-01 22:18:38 ivan Exp $
+# $Id: fs-setup,v 1.48 2001-09-02 02:46:55 ivan Exp $
#to delay loading dbdef until we're ready
BEGIN { $FS::Record::setup_hack = 1; }
'columns' => [
'paynum', 'int', '', '',
#now cust_bill_pay #'invnum', 'int', '', '',
+ 'custnum', 'int', '', '',
'paid', @money_type,
'_date', @date_type,
'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
'columns' => [
'refundnum', 'int', '', '',
#now cust_credit_refund #'crednum', 'int', '', '',
+ 'custnum', 'int', '', '',
'_date', @date_type,
'refund', @money_type,
'otaker', 'varchar', '', 8,
<li><a name="cust_pay" href="man/FS/cust_pay.html">cust_pay</a> - Payments. Money being transferred from a customer.
<ul>
<li>paynum - primary key
+ <li>custnum - <a href="#cust_main">customer</a>
<li>paid - amount
<li>_date
<li>payby - CARD, BILL, or COMP
<li><a name="cust_refund" href="man/FS/cust_refund.html">cust_refund</a> - Refunds. The transfer of money to a customer; equivalent to a negative <a href="#cust_pay">cust_pay</a> record.
<ul>
<li>refundnum - primary key
+ <li>custnum - <a href="#cust_main">customer</a>
<li>refund - amount
<li>_date
<li>payby - CARD, BILL or COMP
ALTER TABLE part_svc ADD svc_forward__dst integer NULL;
ALTER TABLE part_svc ADD svc_forward__dst_flag char(1) NULL;
ALTER TABLE cust_main ADD referral_custnum integer NULL;
+ALTER TABLE cust_pay ADD custnum integer;
+ALTER TABLE cust_refund ADD custnum integer;
CREATE INDEX cust_main3 ON cust_main ( referral_custnum );
CREATE INDEX cust_credit_bill1 ON cust_credit_bill ( crednum );
CREATE INDEX cust_credit_bill2 ON cust_credit_bill ( invnum );