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.
77 my $error = $self->SUPER::insert;
78 return $error if $error;
80 if ( $conf->exists('invoice_send_receipts') ) {
81 my $send_error = $self->cust_bill->send;
82 warn "Error sending receipt: $send_error\n" if $send_error;
90 Deletes this payment application, unless the closed flag for the parent payment
91 (see L<FS::cust_pay>) is set.
97 return "Can't delete application for closed payment"
98 if $self->cust_pay->closed =~ /^Y/i;
99 $self->SUPER::delete(@_);
102 =item replace OLD_RECORD
104 Currently unimplemented (accounting reasons).
109 return "Can't (yet?) modify cust_bill_pay records!";
114 Checks all fields to make sure this is a valid payment. If there is an error,
115 returns the error, otherwise returns false. Called by the insert method.
123 $self->ut_numbern('billpaynum')
124 || $self->ut_number('invnum')
125 || $self->ut_number('paynum')
126 || $self->ut_money('amount')
127 || $self->ut_numbern('_date')
129 return $error if $error;
131 return "amount must be > 0" if $self->amount <= 0;
133 return "Unknown invoice"
134 unless my $cust_bill =
135 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
137 return "Unknown payment"
138 unless my $cust_pay =
139 qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
141 $self->_date(time) unless $self->_date;
143 return "Cannot apply more than remaining value of invoice"
144 unless $self->amount <= $cust_bill->owed;
146 return "Cannot apply more than remaining value of payment"
147 unless $self->amount <= $cust_pay->unapplied;
154 Returns the payment (see L<FS::cust_pay>)
160 qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
165 Returns the invoice (see L<FS::cust_bill>)
171 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
178 Delete and replace methods.
180 the checks for over-applied payments could be better done like the ones in
185 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the