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
64 =item closed - books closed flag, empty or `Y'
74 Creates a new payment. To add the payment to the databse, see L<"insert">.
78 sub table { 'cust_pay'; }
82 Adds this payment to the database.
84 For backwards-compatibility and convenience, if the additional field invnum
85 is defined, an FS::cust_bill_pay record for the full amount of the payment
86 will be created. In this case, custnum is optional.
93 local $SIG{HUP} = 'IGNORE';
94 local $SIG{INT} = 'IGNORE';
95 local $SIG{QUIT} = 'IGNORE';
96 local $SIG{TERM} = 'IGNORE';
97 local $SIG{TSTP} = 'IGNORE';
98 local $SIG{PIPE} = 'IGNORE';
100 my $oldAutoCommit = $FS::UID::AutoCommit;
101 local $FS::UID::AutoCommit = 0;
104 if ( $self->invnum ) {
105 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
107 $dbh->rollback if $oldAutoCommit;
108 return "Unknown cust_bill.invnum: ". $self->invnum;
110 $self->custnum($cust_bill->custnum );
113 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
114 my $old_balance = $cust_main->balance;
116 my $error = $self->check;
117 return $error if $error;
119 $error = $self->SUPER::insert;
121 $dbh->rollback if $oldAutoCommit;
122 return "error inserting $self: $error";
125 if ( $self->invnum ) {
126 my $cust_bill_pay = new FS::cust_bill_pay {
127 'invnum' => $self->invnum,
128 'paynum' => $self->paynum,
129 'amount' => $self->paid,
130 '_date' => $self->_date,
132 $error = $cust_bill_pay->insert;
134 $dbh->rollback if $oldAutoCommit;
135 return "error inserting $cust_bill_pay: $error";
139 if ( $self->paybatch =~ /^webui-/ ) {
140 my @cust_pay = qsearch('cust_pay', {
141 'custnum' => $self->custnum,
142 'paybatch' => $self->paybatch,
144 if ( scalar(@cust_pay) > 1 ) {
145 $dbh->rollback if $oldAutoCommit;
146 return "a payment with webui token ". $self->paybatch. " already exists";
150 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
152 #false laziness w/ cust_credit::insert
153 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
154 my @errors = $cust_main->unsuspend;
156 # side-fx with nested transactions? upstack rolls back?
157 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
167 sub upgrade_replace { #1.3.x->1.4.x
170 local $SIG{HUP} = 'IGNORE';
171 local $SIG{INT} = 'IGNORE';
172 local $SIG{QUIT} = 'IGNORE';
173 local $SIG{TERM} = 'IGNORE';
174 local $SIG{TSTP} = 'IGNORE';
175 local $SIG{PIPE} = 'IGNORE';
177 my $oldAutoCommit = $FS::UID::AutoCommit;
178 local $FS::UID::AutoCommit = 0;
181 my $error = $self->check;
182 return $error if $error;
184 my %new = $self->hash;
185 my $new = FS::cust_pay->new(\%new);
187 if ( $self->invnum ) {
188 my $cust_bill_pay = new FS::cust_bill_pay {
189 'invnum' => $self->invnum,
190 'paynum' => $self->paynum,
191 'amount' => $self->paid,
192 '_date' => $self->_date,
194 $error = $cust_bill_pay->insert;
196 /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
198 my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
199 $new->custnum($cust_bill->custnum);
201 $dbh->rollback if $oldAutoCommit;
204 $new->custnum($cust_bill_pay->cust_bill->custnum);
210 $error = $new->SUPER::replace($self);
212 $dbh->rollback if $oldAutoCommit;
216 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
225 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
226 unless the closed flag is set.
232 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
234 local $SIG{HUP} = 'IGNORE';
235 local $SIG{INT} = 'IGNORE';
236 local $SIG{QUIT} = 'IGNORE';
237 local $SIG{TERM} = 'IGNORE';
238 local $SIG{TSTP} = 'IGNORE';
239 local $SIG{PIPE} = 'IGNORE';
241 my $oldAutoCommit = $FS::UID::AutoCommit;
242 local $FS::UID::AutoCommit = 0;
245 foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
246 my $error = $cust_bill_pay->delete;
248 $dbh->rollback if $oldAutoCommit;
253 my $error = $self->SUPER::delete(@_);
255 $dbh->rollback if $oldAutoCommit;
259 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
265 =item replace OLD_RECORD
267 Currently unimplemented (accounting reasons).
272 return "Can't (yet?) modify cust_pay records!";
277 Checks all fields to make sure this is a valid payment. If there is an error,
278 returns the error, otherwise returns false. Called by the insert method.
286 $self->ut_numbern('paynum')
287 || $self->ut_numbern('custnum')
288 || $self->ut_money('paid')
289 || $self->ut_numbern('_date')
290 || $self->ut_textn('paybatch')
291 || $self->ut_enum('closed', [ '', 'Y' ])
293 return $error if $error;
295 return "paid must be > 0 " if $self->paid <= 0;
297 return "unknown cust_main.custnum: ". $self->custnum
299 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
301 $self->_date(time) unless $self->_date;
303 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
306 #false laziness with cust_refund::check
307 if ( $self->payby eq 'CARD' ) {
308 my $payinfo = $self->payinfo;
310 $self->payinfo($payinfo);
311 if ( $self->payinfo ) {
312 $self->payinfo =~ /^(\d{13,16})$/
313 or return "Illegal (mistyped?) credit card number (payinfo)";
315 validate($self->payinfo) or return "Illegal credit card number";
316 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
318 $self->payinfo('N/A');
322 $error = $self->ut_textn('payinfo');
323 return $error if $error;
332 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
339 sort { $a->_date <=> $b->_date }
340 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
346 Returns the amount of this payment that is still unapplied; which is
347 paid minus all payment applications (see L<FS::cust_bill_pay>).
353 my $amount = $self->paid;
354 $amount -= $_->amount foreach ( $self->cust_bill_pay );
355 sprintf("%.2f", $amount );
362 $Id: cust_pay.pm,v 1.17 2002-02-10 18:56:49 ivan Exp $
366 Delete and replace methods.
370 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the