5 use Business::CreditCard;
6 use FS::Record qw( dbh );
11 @ISA = qw( FS::Record );
15 FS::cust_pay - Object methods for cust_pay objects
21 $record = new FS::cust_pay \%hash;
22 $record = new FS::cust_pay { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::cust_pay object represents a payment; the transfer of money from a
35 customer. FS::cust_pay inherits from FS::Record. The following fields are
40 =item paynum - primary key (assigned automatically for new payments)
42 =item custnum - customer (see L<FS::cust_main>)
44 =item paid - Amount of this payment
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.
49 =item payby - `CARD' (credit cards), `BILL' (billing), or `COMP' (free)
51 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
53 =item paybatch - text field for tracking card processing
63 Creates a new payment. To add the payment to the databse, see L<"insert">.
67 sub table { 'cust_pay'; }
71 Adds this payment to the database.
73 For backwards-compatibility and convenience, if the additional field invnum
74 is defined, an FS::cust_bill_pay record for the full amount of the payment
75 will be created. In this case, custnum is optional.
82 local $SIG{HUP} = 'IGNORE';
83 local $SIG{INT} = 'IGNORE';
84 local $SIG{QUIT} = 'IGNORE';
85 local $SIG{TERM} = 'IGNORE';
86 local $SIG{TSTP} = 'IGNORE';
87 local $SIG{PIPE} = 'IGNORE';
89 my $oldAutoCommit = $FS::UID::AutoCommit;
90 local $FS::UID::AutoCommit = 0;
93 my $error = $self->check;
94 return $error if $error;
96 if ( $self->invnum ) {
97 my $cust_bill_pay = new FS::cust_bill_pay {
98 'invnum' => $self->invnum,
99 'paynum' => $self->paynum,
100 'amount' => $self->paid,
101 '_date' => $self->_date,
103 $error = $cust_bill_pay->insert;
105 $dbh->rollback if $oldAutoCommit;
108 $self->custnum($cust_bill_pay->cust_bill->custnum);
111 $error = $self->SUPER::insert;
113 $dbh->rollback if $oldAutoCommit;
117 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
125 Currently unimplemented (accounting reasons).
130 return "Can't (yet?) delete cust_pay records!";
133 =item replace OLD_RECORD
135 Currently unimplemented (accounting reasons).
140 return "Can't (yet?) modify cust_pay records!";
145 Checks all fields to make sure this is a valid payment. If there is an error,
146 returns the error, otherwise returns false. Called by the insert method.
154 $self->ut_numbern('paynum')
155 || $self->ut_number('custnum')
156 || $self->ut_money('paid')
157 || $self->ut_numbern('_date')
158 || $self->ut_textn('paybatch')
160 return $error if $error;
162 return "unknown cust_main.custnum: ". $self->custnum
164 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
166 $self->_date(time) unless $self->_date;
168 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
171 if ( $self->payby eq 'CARD' ) {
172 my $payinfo = $self->payinfo;
174 $self->payinfo($payinfo);
175 if ( $self->payinfo ) {
176 $self->payinfo =~ /^(\d{13,16})$/
177 or return "Illegal (mistyped?) credit card number (payinfo)";
179 validate($self->payinfo) or return "Illegal credit card number";
180 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
182 $self->payinfo('N/A');
186 $error = $self->ut_textn('payinfo');
187 return $error if $error;
196 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
203 sort { $a->_date <=> $b->_date }
204 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
210 Returns the amount of this payment that is still unapplied; which is
211 paid minus all payment applications (see L<FS::cust_bill_pay>).
217 my $amount = $self->paid;
218 $amount -= $_->amount foreach ( $self->cust_bill_pay );
219 sprintf("%.2f", $amount );
226 $Id: cust_pay.pm,v 1.5 2001-09-02 02:46:55 ivan Exp $
230 Delete and replace methods.
234 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the