1 package FS::cust_bill_pay;
4 use vars qw( @ISA $conf );
5 use FS::Record qw( qsearch qsearchs dbh );
9 @ISA = qw( FS::Record );
11 #ask FS::UID to run this stuff for us later
12 FS::UID->install_callback( sub {
18 FS::cust_bill_pay - Object methods for cust_bill_pay records
22 use FS::cust_bill_pay;
24 $record = new FS::cust_bill_pay \%hash;
25 $record = new FS::cust_bill_pay { 'column' => 'value' };
27 $error = $record->insert;
29 $error = $new_record->replace($old_record);
31 $error = $record->delete;
33 $error = $record->check;
37 An FS::cust_bill_pay object represents the application of a payment to a
38 specific invoice. FS::cust_bill_pay inherits from FS::Record. The following
39 fields are currently supported:
43 =item billpaynum - primary key (assigned automatically)
45 =item invnum - Invoice (see L<FS::cust_bill>)
47 =item paynum - Payment (see L<FS::cust_pay>)
49 =item amount - Amount of the payment to apply to the specific invoice.
51 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
52 L<Time::Local> and L<Date::Parse> for conversion functions.
62 Creates a new record. To add the record to the database, see L<"insert">.
66 sub table { 'cust_bill_pay'; }
70 Adds this record to the database. If there is an error, returns the error,
71 otherwise returns false.
78 local $SIG{HUP} = 'IGNORE';
79 local $SIG{INT} = 'IGNORE';
80 local $SIG{QUIT} = 'IGNORE';
81 local $SIG{TERM} = 'IGNORE';
82 local $SIG{TSTP} = 'IGNORE';
83 local $SIG{PIPE} = 'IGNORE';
85 my $oldAutoCommit = $FS::UID::AutoCommit;
86 local $FS::UID::AutoCommit = 0;
89 my $error = $self->check;
90 return $error if $error;
92 $error = $self->SUPER::insert;
94 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } ) or do {
95 $dbh->rollback if $oldAutoCommit;
96 return "unknown cust_pay.paynum: ". $self->paynum;
100 $pay_total += $_ foreach map { $_->amount }
101 qsearch('cust_bill_pay', { 'paynum' => $self->paynum } );
103 if ( sprintf("%.2f", $pay_total) > sprintf("%.2f", $cust_pay->paid) ) {
104 $dbh->rollback if $oldAutoCommit;
105 return "total cust_bill_pay.amount $pay_total for paynum ". $self->paynum.
106 " greater than cust_pay.paid ". $cust_pay->paid;
109 my $cust_bill = $self->cust_bill;
110 unless ( $cust_bill ) {
111 $dbh->rollback if $oldAutoCommit;
112 return "unknown cust_bill.invnum: ". $self->invnum;
116 $bill_total += $_ foreach map { $_->amount }
117 qsearch('cust_bill_pay', { 'invnum' => $self->invnum } );
118 $bill_total += $_ foreach map { $_->amount }
119 qsearch('cust_credit_bill', { 'invnum' => $self->invnum } );
120 if ( sprintf("%.2f", $bill_total) > sprintf("%.2f", $cust_bill->charged) ) {
121 $dbh->rollback if $oldAutoCommit;
122 return "total cust_bill_pay.amount and cust_credit_bill.amount $bill_total".
123 " for invnum ". $self->invnum.
124 " greater than cust_bill.charged ". $cust_bill->charged;
127 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
129 if ( $conf->exists('invoice_send_receipts') ) {
130 my $send_error = $cust_bill->send;
131 warn "Error sending receipt: $send_error\n" if $send_error;
139 Deletes this payment application, unless the closed flag for the parent payment
140 (see L<FS::cust_pay>) is set.
146 return "Can't delete application for closed payment"
147 if $self->cust_pay->closed =~ /^Y/i;
148 $self->SUPER::delete(@_);
151 =item replace OLD_RECORD
153 Currently unimplemented (accounting reasons).
158 return "Can't (yet?) modify cust_bill_pay records!";
163 Checks all fields to make sure this is a valid payment. If there is an error,
164 returns the error, otherwise returns false. Called by the insert method.
172 $self->ut_numbern('billpaynum')
173 || $self->ut_number('invnum')
174 || $self->ut_number('paynum')
175 || $self->ut_money('amount')
176 || $self->ut_numbern('_date')
178 return $error if $error;
180 return "amount must be > 0" if $self->amount <= 0;
182 $self->_date(time) unless $self->_date;
189 Returns the payment (see L<FS::cust_pay>)
195 qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
200 Returns the invoice (see L<FS::cust_bill>)
206 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
213 Delete and replace methods.
215 the checks for over-applied payments could be better done like the ones in
220 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the