1 package FS::cust_bill_pay;
5 use FS::Record qw( qsearch qsearchs dbh );
9 @ISA = qw( FS::Record );
13 FS::cust_bill_pay - Object methods for cust_bill_pay records
17 use FS::cust_bill_pay;
19 $record = new FS::cust_bill_pay \%hash;
20 $record = new FS::cust_bill_pay { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::cust_bill_pay object represents the application of a payment to a
33 specific invoice. FS::cust_bill_pay inherits from FS::Record. The following
34 fields are currently supported:
38 =item billpaynum - primary key (assigned automatically)
40 =item invnum - Invoice (see L<FS::cust_bill>)
42 =item paynum - Payment (see L<FS::cust_pay>)
44 =item amount - Amount of the payment to apply to the specific invoice.
46 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
47 L<Time::Local> and L<Date::Parse> for conversion functions.
57 Creates a new record. To add the record to the database, see L<"insert">.
61 sub table { 'cust_bill_pay'; }
65 Adds this record to the database. If there is an error, returns the error,
66 otherwise returns false.
73 local $SIG{HUP} = 'IGNORE';
74 local $SIG{INT} = 'IGNORE';
75 local $SIG{QUIT} = 'IGNORE';
76 local $SIG{TERM} = 'IGNORE';
77 local $SIG{TSTP} = 'IGNORE';
78 local $SIG{PIPE} = 'IGNORE';
80 my $oldAutoCommit = $FS::UID::AutoCommit;
81 local $FS::UID::AutoCommit = 0;
84 my $error = $self->check;
85 return $error if $error;
87 $error = $self->SUPER::insert;
89 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } ) or do {
90 $dbh->rollback if $oldAutoCommit;
91 return "unknown cust_pay.paynum: ". $self->paynum;
95 $pay_total += $_ foreach map { $_->amount }
96 qsearch('cust_bill_pay', { 'paynum' => $self->paynum } );
98 if ( sprintf("%.2f", $pay_total) > sprintf("%.2f", $cust_pay->paid) ) {
99 $dbh->rollback if $oldAutoCommit;
100 return "total cust_bill_pay.amount $pay_total for paynum ". $self->paynum.
101 " greater than cust_pay.paid ". $cust_pay->paid;
104 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } ) or do {
105 $dbh->rollback if $oldAutoCommit;
106 return "unknown cust_bill.invnum: ". $self->invnum;
110 $bill_total += $_ foreach map { $_->amount }
111 qsearch('cust_bill_pay', { 'invnum' => $self->invnum } );
112 $bill_total += $_ foreach map { $_->amount }
113 qsearch('cust_credit_bill', { 'invnum' => $self->invnum } );
114 if ( sprintf("%.2f", $bill_total) > sprintf("%.2f", $cust_bill->charged) ) {
115 $dbh->rollback if $oldAutoCommit;
116 return "total cust_bill_pay.amount and cust_credit_bill.amount $bill_total".
117 " for invnum ". $self->invnum.
118 " greater than cust_bill.charged ". $cust_bill->charged;
121 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
128 Deletes this payment application, unless the closed flag for the parent payment
129 (see L<FS::cust_pay>) is set.
135 return "Can't delete application for closed payment"
136 if $self->cust_pay->closed =~ /^Y/i;
137 $self->SUPER::delete(@_);
140 =item replace OLD_RECORD
142 Currently unimplemented (accounting reasons).
147 return "Can't (yet?) modify cust_bill_pay records!";
152 Checks all fields to make sure this is a valid payment. If there is an error,
153 returns the error, otherwise returns false. Called by the insert method.
161 $self->ut_numbern('billpaynum')
162 || $self->ut_number('invnum')
163 || $self->ut_number('paynum')
164 || $self->ut_money('amount')
165 || $self->ut_numbern('_date')
167 return $error if $error;
169 return "amount must be > 0" if $self->amount <= 0;
171 $self->_date(time) unless $self->_date;
178 Returns the payment (see L<FS::cust_pay>)
184 qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
189 Returns the invoice (see L<FS::cust_bill>)
195 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
202 $Id: cust_bill_pay.pm,v 1.12 2002-02-07 22:29:34 ivan Exp $
206 Delete and replace methods.
208 the checks for over-applied payments could be better done like the ones in
213 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the