4 use vars qw( @ISA $conf $unsuspendauto );
5 use Business::CreditCard;
7 use FS::Record qw( dbh qsearch qsearchs dbh );
12 @ISA = qw( FS::Record );
14 #ask FS::UID to run this stuff for us later
15 $FS::UID::callback{'FS::cust_pay'} = sub {
18 $unsuspendauto = $conf->exists('unsuspendauto');
24 FS::cust_pay - Object methods for cust_pay objects
30 $record = new FS::cust_pay \%hash;
31 $record = new FS::cust_pay { 'column' => 'value' };
33 $error = $record->insert;
35 $error = $new_record->replace($old_record);
37 $error = $record->delete;
39 $error = $record->check;
43 An FS::cust_pay object represents a payment; the transfer of money from a
44 customer. FS::cust_pay inherits from FS::Record. The following fields are
49 =item paynum - primary key (assigned automatically for new payments)
51 =item custnum - customer (see L<FS::cust_main>)
53 =item paid - Amount of this payment
55 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
56 L<Time::Local> and L<Date::Parse> for conversion functions.
58 =item payby - `CARD' (credit cards), `BILL' (billing), or `COMP' (free)
60 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
62 =item paybatch - text field for tracking card processing
72 Creates a new payment. To add the payment to the databse, see L<"insert">.
76 sub table { 'cust_pay'; }
80 Adds this payment to the database.
82 For backwards-compatibility and convenience, if the additional field invnum
83 is defined, an FS::cust_bill_pay record for the full amount of the payment
84 will be created. In this case, custnum is optional.
91 local $SIG{HUP} = 'IGNORE';
92 local $SIG{INT} = 'IGNORE';
93 local $SIG{QUIT} = 'IGNORE';
94 local $SIG{TERM} = 'IGNORE';
95 local $SIG{TSTP} = 'IGNORE';
96 local $SIG{PIPE} = 'IGNORE';
98 my $oldAutoCommit = $FS::UID::AutoCommit;
99 local $FS::UID::AutoCommit = 0;
102 if ( $self->invnum ) {
103 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
105 $dbh->rollback if $oldAutoCommit;
106 return "Unknown cust_bill.invnum: ". $self->invnum;
108 $self->custnum($cust_bill->custnum );
111 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
112 my $old_balance = $cust_main->balance;
114 my $error = $self->check;
115 return $error if $error;
117 $error = $self->SUPER::insert;
119 $dbh->rollback if $oldAutoCommit;
120 return "error inserting $self: $error";
123 if ( $self->invnum ) {
124 my $cust_bill_pay = new FS::cust_bill_pay {
125 'invnum' => $self->invnum,
126 'paynum' => $self->paynum,
127 'amount' => $self->paid,
128 '_date' => $self->_date,
130 $error = $cust_bill_pay->insert;
132 $dbh->rollback if $oldAutoCommit;
133 return "error inserting $cust_bill_pay: $error";
137 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
139 #false laziness w/ cust_credit::insert
140 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
141 my @errors = $cust_main->unsuspend;
143 # side-fx with nested transactions? upstack rolls back?
144 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
154 sub upgrade_replace { #1.3.x->1.4.x
157 local $SIG{HUP} = 'IGNORE';
158 local $SIG{INT} = 'IGNORE';
159 local $SIG{QUIT} = 'IGNORE';
160 local $SIG{TERM} = 'IGNORE';
161 local $SIG{TSTP} = 'IGNORE';
162 local $SIG{PIPE} = 'IGNORE';
164 my $oldAutoCommit = $FS::UID::AutoCommit;
165 local $FS::UID::AutoCommit = 0;
168 my $error = $self->check;
169 return $error if $error;
171 my %new = $self->hash;
172 my $new = FS::cust_pay->new(\%new);
174 if ( $self->invnum ) {
175 my $cust_bill_pay = new FS::cust_bill_pay {
176 'invnum' => $self->invnum,
177 'paynum' => $self->paynum,
178 'amount' => $self->paid,
179 '_date' => $self->_date,
181 $error = $cust_bill_pay->insert;
183 $dbh->rollback if $oldAutoCommit;
186 $new->custnum($cust_bill_pay->cust_bill->custnum);
191 $error = $new->SUPER::replace($self);
193 $dbh->rollback if $oldAutoCommit;
197 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
206 Currently unimplemented (accounting reasons).
211 return "Can't (yet?) delete cust_pay records!";
214 =item replace OLD_RECORD
216 Currently unimplemented (accounting reasons).
221 return "Can't (yet?) modify cust_pay records!";
226 Checks all fields to make sure this is a valid payment. If there is an error,
227 returns the error, otherwise returns false. Called by the insert method.
235 $self->ut_numbern('paynum')
236 || $self->ut_numbern('custnum')
237 || $self->ut_money('paid')
238 || $self->ut_numbern('_date')
239 || $self->ut_textn('paybatch')
241 return $error if $error;
243 return "paid must be > 0 " if $self->paid <= 0;
245 return "unknown cust_main.custnum: ". $self->custnum
247 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
249 $self->_date(time) unless $self->_date;
251 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
254 #false laziness with cust_refund::check
255 if ( $self->payby eq 'CARD' ) {
256 my $payinfo = $self->payinfo;
258 $self->payinfo($payinfo);
259 if ( $self->payinfo ) {
260 $self->payinfo =~ /^(\d{13,16})$/
261 or return "Illegal (mistyped?) credit card number (payinfo)";
263 validate($self->payinfo) or return "Illegal credit card number";
264 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
266 $self->payinfo('N/A');
270 $error = $self->ut_textn('payinfo');
271 return $error if $error;
280 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
287 sort { $a->_date <=> $b->_date }
288 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
294 Returns the amount of this payment that is still unapplied; which is
295 paid minus all payment applications (see L<FS::cust_bill_pay>).
301 my $amount = $self->paid;
302 $amount -= $_->amount foreach ( $self->cust_bill_pay );
303 sprintf("%.2f", $amount );
310 $Id: cust_pay.pm,v 1.13 2002-01-24 16:58:47 ivan Exp $
314 Delete and replace methods.
318 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the