4 use vars qw( @ISA $conf $unsuspendauto $ignore_noapply );
6 use Business::CreditCard;
8 use FS::Misc qw(send_email);
9 use FS::Record qw( dbh qsearch qsearchs );
10 use FS::cust_main_Mixin;
12 use FS::cust_bill_pay;
13 use FS::cust_pay_refund;
15 use FS::cust_pay_void;
17 @ISA = qw( FS::cust_main_Mixin FS::Record );
21 #ask FS::UID to run this stuff for us later
22 FS::UID->install_callback( sub {
24 $unsuspendauto = $conf->exists('unsuspendauto');
29 FS::cust_pay - Object methods for cust_pay objects
35 $record = new FS::cust_pay \%hash;
36 $record = new FS::cust_pay { 'column' => 'value' };
38 $error = $record->insert;
40 $error = $new_record->replace($old_record);
42 $error = $record->delete;
44 $error = $record->check;
48 An FS::cust_pay object represents a payment; the transfer of money from a
49 customer. FS::cust_pay inherits from FS::Record. The following fields are
54 =item paynum - primary key (assigned automatically for new payments)
56 =item custnum - customer (see L<FS::cust_main>)
58 =item paid - Amount of this payment
60 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
61 L<Time::Local> and L<Date::Parse> for conversion functions.
63 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
64 `LECB' (phone bill billing), `BILL' (billing), `PREP` (prepaid card),
65 `CASH' (cash), `WEST' (Western Union), `MCRD' (Manual credit card), or
68 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
70 =item paybatch - text field for tracking card processing
72 =item closed - books closed flag, empty or `Y'
82 Creates a new payment. To add the payment to the databse, see L<"insert">.
86 sub table { 'cust_pay'; }
87 sub cust_linked { $_[0]->cust_main_custnum; }
88 sub cust_unlinked_msg {
90 "WARNING: can't find cust_main.custnum ". $self->custnum.
91 ' (cust_pay.paynum '. $self->paynum. ')';
96 Adds this payment to the database.
98 For backwards-compatibility and convenience, if the additional field invnum
99 is defined, an FS::cust_bill_pay record for the full amount of the payment
100 will be created. In this case, custnum is optional.
107 local $SIG{HUP} = 'IGNORE';
108 local $SIG{INT} = 'IGNORE';
109 local $SIG{QUIT} = 'IGNORE';
110 local $SIG{TERM} = 'IGNORE';
111 local $SIG{TSTP} = 'IGNORE';
112 local $SIG{PIPE} = 'IGNORE';
114 my $oldAutoCommit = $FS::UID::AutoCommit;
115 local $FS::UID::AutoCommit = 0;
118 if ( $self->invnum ) {
119 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
121 $dbh->rollback if $oldAutoCommit;
122 return "Unknown cust_bill.invnum: ". $self->invnum;
124 $self->custnum($cust_bill->custnum );
128 my $error = $self->check;
129 return $error if $error;
131 my $cust_main = $self->cust_main;
132 my $old_balance = $cust_main->balance;
134 $error = $self->SUPER::insert;
136 $dbh->rollback if $oldAutoCommit;
137 return "error inserting $self: $error";
140 if ( $self->invnum ) {
141 my $cust_bill_pay = new FS::cust_bill_pay {
142 'invnum' => $self->invnum,
143 'paynum' => $self->paynum,
144 'amount' => $self->paid,
145 '_date' => $self->_date,
147 $error = $cust_bill_pay->insert;
149 if ( $ignore_noapply ) {
150 warn "warning: error inserting $cust_bill_pay: $error ".
151 "(ignore_noapply flag set; inserting cust_pay record anyway)\n";
153 $dbh->rollback if $oldAutoCommit;
154 return "error inserting $cust_bill_pay: $error";
159 if ( $self->paybatch =~ /^webui-/ ) {
160 my @cust_pay = qsearch('cust_pay', {
161 'custnum' => $self->custnum,
162 'paybatch' => $self->paybatch,
164 if ( scalar(@cust_pay) > 1 ) {
165 $dbh->rollback if $oldAutoCommit;
166 return "a payment with webui token ". $self->paybatch. " already exists";
170 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
172 #false laziness w/ cust_credit::insert
173 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
174 my @errors = $cust_main->unsuspend;
176 # side-fx with nested transactions? upstack rolls back?
177 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
183 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
185 #my $cust_main = $self->cust_main;
186 if ( $conf->exists('payment_receipt_email')
187 && grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list
190 my $receipt_template = new Text::Template (
192 SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
194 warn "can't create payment receipt template: $Text::Template::ERROR";
198 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list;
200 my $payby = $self->payby;
201 my $payinfo = $self->payinfo;
202 $payby =~ s/^BILL$/Check/ if $payinfo;
203 $payinfo = $self->payinfo_masked if $payby eq 'CARD' || $payby eq 'CHEK';
204 $payby =~ s/^CHEK$/Electronic check/;
206 my $error = send_email(
207 'from' => $conf->config('invoice_from'), #??? well as good as any
208 'to' => \@invoicing_list,
209 'subject' => 'Payment receipt',
210 'body' => [ $receipt_template->fill_in( HASH => {
211 'date' => time2str("%a %B %o, %Y", $self->_date),
212 'name' => $cust_main->name,
213 'paynum' => $self->paynum,
214 'paid' => sprintf("%.2f", $self->paid),
215 'payby' => ucfirst(lc($payby)),
216 'payinfo' => $payinfo,
217 'balance' => $cust_main->balance,
221 warn "can't send payment receipt: $error";
230 =item void [ REASON ]
232 Voids this payment: deletes the payment and all associated applications and
233 adds a record of the voided payment to the FS::cust_pay_void table.
240 local $SIG{HUP} = 'IGNORE';
241 local $SIG{INT} = 'IGNORE';
242 local $SIG{QUIT} = 'IGNORE';
243 local $SIG{TERM} = 'IGNORE';
244 local $SIG{TSTP} = 'IGNORE';
245 local $SIG{PIPE} = 'IGNORE';
247 my $oldAutoCommit = $FS::UID::AutoCommit;
248 local $FS::UID::AutoCommit = 0;
251 my $cust_pay_void = new FS::cust_pay_void ( {
252 map { $_ => $self->get($_) } $self->fields
254 $cust_pay_void->reason(shift) if scalar(@_);
255 my $error = $cust_pay_void->insert;
257 $dbh->rollback if $oldAutoCommit;
261 $error = $self->delete;
263 $dbh->rollback if $oldAutoCommit;
267 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
275 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
276 unless the closed flag is set. In most cases, you want to use the void
277 method instead to leave a record of the deleted payment.
283 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
285 local $SIG{HUP} = 'IGNORE';
286 local $SIG{INT} = 'IGNORE';
287 local $SIG{QUIT} = 'IGNORE';
288 local $SIG{TERM} = 'IGNORE';
289 local $SIG{TSTP} = 'IGNORE';
290 local $SIG{PIPE} = 'IGNORE';
292 my $oldAutoCommit = $FS::UID::AutoCommit;
293 local $FS::UID::AutoCommit = 0;
296 foreach my $app ( $self->cust_bill_pay, $self->cust_pay_refund ) {
297 my $error = $app->delete;
299 $dbh->rollback if $oldAutoCommit;
304 my $error = $self->SUPER::delete(@_);
306 $dbh->rollback if $oldAutoCommit;
310 if ( $conf->config('deletepayments') ne '' ) {
312 my $cust_main = $self->cust_main;
314 my $error = send_email(
315 'from' => $conf->config('invoice_from'), #??? well as good as any
316 'to' => $conf->config('deletepayments'),
317 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
319 "This is an automatic message from your Freeside installation\n",
320 "informing you that the following payment has been deleted:\n",
322 'paynum: '. $self->paynum. "\n",
323 'custnum: '. $self->custnum.
324 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
325 'paid: $'. sprintf("%.2f", $self->paid). "\n",
326 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
327 'payby: '. $self->payby. "\n",
328 'payinfo: '. $self->payinfo. "\n",
329 'paybatch: '. $self->paybatch. "\n",
334 $dbh->rollback if $oldAutoCommit;
335 return "can't send payment deletion notification: $error";
340 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
346 =item replace OLD_RECORD
348 You probably shouldn't modify payments...
352 Checks all fields to make sure this is a valid payment. If there is an error,
353 returns the error, otherwise returns false. Called by the insert method.
361 $self->ut_numbern('paynum')
362 || $self->ut_numbern('custnum')
363 || $self->ut_money('paid')
364 || $self->ut_numbern('_date')
365 || $self->ut_textn('paybatch')
366 || $self->ut_enum('closed', [ '', 'Y' ])
368 return $error if $error;
370 return "paid must be > 0 " if $self->paid <= 0;
372 return "unknown cust_main.custnum: ". $self->custnum
374 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
376 $self->_date(time) unless $self->_date;
378 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|PREP|CASH|WEST|MCRD)$/
379 or return "Illegal payby";
382 #false laziness with cust_refund::check
383 if ( $self->payby eq 'CARD' ) {
384 my $payinfo = $self->payinfo;
386 $self->payinfo($payinfo);
387 if ( $self->payinfo ) {
388 $self->payinfo =~ /^(\d{13,16})$/
389 or return "Illegal (mistyped?) credit card number (payinfo)";
391 validate($self->payinfo) or return "Illegal credit card number";
392 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
394 $self->payinfo('N/A');
398 $error = $self->ut_textn('payinfo');
399 return $error if $error;
405 =item batch_insert CUST_PAY_OBJECT, ...
407 Class method which inserts multiple payments. Takes a list of FS::cust_pay
408 objects. Returns a list, each element representing the status of inserting the
409 corresponding payment - empty. If there is an error inserting any payment, the
410 entire transaction is rolled back, i.e. all payments are inserted or none are.
414 my @errors = FS::cust_pay->batch_insert(@cust_pay);
415 my $num_errors = scalar(grep $_, @errors);
416 if ( $num_errors == 0 ) {
417 #success; all payments were inserted
419 #failure; no payments were inserted.
425 my $self = shift; #class method
427 local $SIG{HUP} = 'IGNORE';
428 local $SIG{INT} = 'IGNORE';
429 local $SIG{QUIT} = 'IGNORE';
430 local $SIG{TERM} = 'IGNORE';
431 local $SIG{TSTP} = 'IGNORE';
432 local $SIG{PIPE} = 'IGNORE';
434 my $oldAutoCommit = $FS::UID::AutoCommit;
435 local $FS::UID::AutoCommit = 0;
441 my $error = $_->insert;
445 $_->cust_main->apply_payments;
451 $dbh->rollback if $oldAutoCommit;
453 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
462 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
469 sort { $a->_date <=> $b->_date
470 || $a->invnum <=> $b->invnum }
471 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
475 =item cust_pay_refund
477 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
482 sub cust_pay_refund {
484 sort { $a->_date <=> $b->_date }
485 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
492 Returns the amount of this payment that is still unapplied; which is
493 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
494 applications (see L<FS::cust_pay_refund>).
500 my $amount = $self->paid;
501 $amount -= $_->amount foreach ( $self->cust_bill_pay );
502 $amount -= $_->amount foreach ( $self->cust_pay_refund );
503 sprintf("%.2f", $amount );
508 Returns the amount of this payment that has not been refuned; which is
509 paid minus all refund applications (see L<FS::cust_pay_refund>).
515 my $amount = $self->paid;
516 $amount -= $_->amount foreach ( $self->cust_pay_refund );
517 sprintf("%.2f", $amount );
523 Returns the parent customer object (see L<FS::cust_main>).
529 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
534 Returns a "masked" payinfo field with all but the last four characters replaced
535 by 'x'es. Useful for displaying credit cards.
541 #some false laziness w/cust_main::paymask
542 if ( $self->payby eq 'CARD' ) {
543 my $payinfo = $self->payinfo;
544 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
545 } elsif ( $self->payby eq 'CHEK' ) {
546 my( $account, $aba ) = split('@', $self->payinfo );
547 'x'x(length($account)-2). substr($account,(length($account)-2)). "@". $aba;
557 Delete and replace methods. payinfo_masked false laziness with cust_main.pm
562 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the