4 use vars qw( @ISA $conf $unsuspendauto );
6 use Business::CreditCard;
8 use FS::Record qw( dbh qsearch qsearchs );
9 use FS::Misc qw(send_email);
11 use FS::cust_bill_pay;
12 use FS::cust_pay_refund;
14 use FS::cust_pay_void;
16 @ISA = qw( FS::Record );
18 #ask FS::UID to run this stuff for us later
19 FS::UID->install_callback( sub {
21 $unsuspendauto = $conf->exists('unsuspendauto');
26 FS::cust_pay - Object methods for cust_pay objects
32 $record = new FS::cust_pay \%hash;
33 $record = new FS::cust_pay { 'column' => 'value' };
35 $error = $record->insert;
37 $error = $new_record->replace($old_record);
39 $error = $record->delete;
41 $error = $record->check;
45 An FS::cust_pay object represents a payment; the transfer of money from a
46 customer. FS::cust_pay inherits from FS::Record. The following fields are
51 =item paynum - primary key (assigned automatically for new payments)
53 =item custnum - customer (see L<FS::cust_main>)
55 =item paid - Amount of this payment
57 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
58 L<Time::Local> and L<Date::Parse> for conversion functions.
60 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
61 `LECB' (phone bill billing), `BILL' (billing), or `COMP' (free)
63 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
65 =item paybatch - text field for tracking card processing
67 =item closed - books closed flag, empty or `Y'
77 Creates a new payment. To add the payment to the databse, see L<"insert">.
81 sub table { 'cust_pay'; }
85 Adds this payment to the database.
87 For backwards-compatibility and convenience, if the additional field invnum
88 is defined, an FS::cust_bill_pay record for the full amount of the payment
89 will be created. In this case, custnum is optional.
96 local $SIG{HUP} = 'IGNORE';
97 local $SIG{INT} = 'IGNORE';
98 local $SIG{QUIT} = 'IGNORE';
99 local $SIG{TERM} = 'IGNORE';
100 local $SIG{TSTP} = 'IGNORE';
101 local $SIG{PIPE} = 'IGNORE';
103 my $oldAutoCommit = $FS::UID::AutoCommit;
104 local $FS::UID::AutoCommit = 0;
107 if ( $self->invnum ) {
108 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
110 $dbh->rollback if $oldAutoCommit;
111 return "Unknown cust_bill.invnum: ". $self->invnum;
113 $self->custnum($cust_bill->custnum );
116 my $cust_main = $self->cust_main;
117 my $old_balance = $cust_main->balance;
119 my $error = $self->check;
120 return $error if $error;
122 $error = $self->SUPER::insert;
124 $dbh->rollback if $oldAutoCommit;
125 return "error inserting $self: $error";
128 if ( $self->invnum ) {
129 my $cust_bill_pay = new FS::cust_bill_pay {
130 'invnum' => $self->invnum,
131 'paynum' => $self->paynum,
132 'amount' => $self->paid,
133 '_date' => $self->_date,
135 $error = $cust_bill_pay->insert;
137 $dbh->rollback if $oldAutoCommit;
138 return "error inserting $cust_bill_pay: $error";
142 if ( $self->paybatch =~ /^webui-/ ) {
143 my @cust_pay = qsearch('cust_pay', {
144 'custnum' => $self->custnum,
145 'paybatch' => $self->paybatch,
147 if ( scalar(@cust_pay) > 1 ) {
148 $dbh->rollback if $oldAutoCommit;
149 return "a payment with webui token ". $self->paybatch. " already exists";
153 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
155 #false laziness w/ cust_credit::insert
156 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
157 my @errors = $cust_main->unsuspend;
159 # side-fx with nested transactions? upstack rolls back?
160 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
166 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
168 #my $cust_main = $self->cust_main;
169 if ( $conf->exists('payment_receipt_email')
170 && grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list
173 my $receipt_template = new Text::Template (
175 SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
177 warn "can't create payment receipt template: $Text::Template::ERROR";
181 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list;
183 my $payby = $self->payby;
184 my $payinfo = $self->payinfo;
185 $payby =~ s/^BILL$/Check/ if $payinfo;
186 $payinfo = $self->payinfo_masked if $payby eq 'CARD' || $payby eq 'CHEK';
187 $payby =~ s/^CHEK$/Electronic check/;
189 my $error = send_email(
190 'from' => $conf->config('invoice_from'), #??? well as good as any
191 'to' => \@invoicing_list,
192 'subject' => 'Payment receipt',
193 'body' => [ $receipt_template->fill_in( HASH => {
194 'date' => time2str("%a %B %o, %Y", $self->_date),
195 'name' => $cust_main->name,
196 'paynum' => $self->paynum,
197 'paid' => sprintf("%.2f", $self->paid),
198 'payby' => ucfirst(lc($payby)),
199 'payinfo' => $payinfo,
200 'balance' => $cust_main->balance,
204 warn "can't send payment receipt: $error";
213 =item void [ REASON ]
215 Voids this payment: deletes the payment and all associated applications and
216 adds a record of the voided payment to the FS::cust_pay_void table.
223 local $SIG{HUP} = 'IGNORE';
224 local $SIG{INT} = 'IGNORE';
225 local $SIG{QUIT} = 'IGNORE';
226 local $SIG{TERM} = 'IGNORE';
227 local $SIG{TSTP} = 'IGNORE';
228 local $SIG{PIPE} = 'IGNORE';
230 my $oldAutoCommit = $FS::UID::AutoCommit;
231 local $FS::UID::AutoCommit = 0;
234 my $cust_pay_void = new FS::cust_pay_void ( {
235 map { $_ => $self->get($_) } $self->fields
237 $cust_pay_void->reason(shift) if scalar(@_);
238 my $error = $cust_pay_void->insert;
240 $dbh->rollback if $oldAutoCommit;
244 $error = $self->delete;
246 $dbh->rollback if $oldAutoCommit;
250 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
258 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
259 unless the closed flag is set. In most cases, you want to use the void
260 method instead to leave a record of the deleted payment.
266 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
268 local $SIG{HUP} = 'IGNORE';
269 local $SIG{INT} = 'IGNORE';
270 local $SIG{QUIT} = 'IGNORE';
271 local $SIG{TERM} = 'IGNORE';
272 local $SIG{TSTP} = 'IGNORE';
273 local $SIG{PIPE} = 'IGNORE';
275 my $oldAutoCommit = $FS::UID::AutoCommit;
276 local $FS::UID::AutoCommit = 0;
279 foreach my $app ( $self->cust_bill_pay, $self->cust_pay_refund ) {
280 my $error = $app->delete;
282 $dbh->rollback if $oldAutoCommit;
287 my $error = $self->SUPER::delete(@_);
289 $dbh->rollback if $oldAutoCommit;
293 if ( $conf->config('deletepayments') ne '' ) {
295 my $cust_main = $self->cust_main;
297 my $error = send_email(
298 'from' => $conf->config('invoice_from'), #??? well as good as any
299 'to' => $conf->config('deletepayments'),
300 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
302 "This is an automatic message from your Freeside installation\n",
303 "informing you that the following payment has been deleted:\n",
305 'paynum: '. $self->paynum. "\n",
306 'custnum: '. $self->custnum.
307 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
308 'paid: $'. sprintf("%.2f", $self->paid). "\n",
309 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
310 'payby: '. $self->payby. "\n",
311 'payinfo: '. $self->payinfo. "\n",
312 'paybatch: '. $self->paybatch. "\n",
317 $dbh->rollback if $oldAutoCommit;
318 return "can't send payment deletion notification: $error";
323 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
329 =item replace OLD_RECORD
331 You probably shouldn't modify payments...
335 Checks all fields to make sure this is a valid payment. If there is an error,
336 returns the error, otherwise returns false. Called by the insert method.
344 $self->ut_numbern('paynum')
345 || $self->ut_numbern('custnum')
346 || $self->ut_money('paid')
347 || $self->ut_numbern('_date')
348 || $self->ut_textn('paybatch')
349 || $self->ut_enum('closed', [ '', 'Y' ])
351 return $error if $error;
353 return "paid must be > 0 " if $self->paid <= 0;
355 return "unknown cust_main.custnum: ". $self->custnum
357 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
359 $self->_date(time) unless $self->_date;
361 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|PREP)$/ or return "Illegal payby";
364 #false laziness with cust_refund::check
365 if ( $self->payby eq 'CARD' ) {
366 my $payinfo = $self->payinfo;
368 $self->payinfo($payinfo);
369 if ( $self->payinfo ) {
370 $self->payinfo =~ /^(\d{13,16})$/
371 or return "Illegal (mistyped?) credit card number (payinfo)";
373 validate($self->payinfo) or return "Illegal credit card number";
374 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
376 $self->payinfo('N/A');
380 $error = $self->ut_textn('payinfo');
381 return $error if $error;
389 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
396 sort { $a->_date <=> $b->_date
397 || $a->invnum <=> $b->invnum }
398 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
402 =item cust_pay_refund
404 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
409 sub cust_pay_refund {
411 sort { $a->_date <=> $b->_date }
412 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
419 Returns the amount of this payment that is still unapplied; which is
420 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
421 applications (see L<FS::cust_pay_refund>).
427 my $amount = $self->paid;
428 $amount -= $_->amount foreach ( $self->cust_bill_pay );
429 $amount -= $_->amount foreach ( $self->cust_pay_refund );
430 sprintf("%.2f", $amount );
435 Returns the amount of this payment that has not been refuned; which is
436 paid minus all refund applications (see L<FS::cust_pay_refund>).
442 my $amount = $self->paid;
443 $amount -= $_->amount foreach ( $self->cust_pay_refund );
444 sprintf("%.2f", $amount );
450 Returns the parent customer object (see L<FS::cust_main>).
456 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
461 Returns a "masked" payinfo field with all but the last four characters replaced
462 by 'x'es. Useful for displaying credit cards.
468 #some false laziness w/cust_main::paymask
469 if ( $self->payby eq 'CARD' ) {
470 my $payinfo = $self->payinfo;
471 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
472 } elsif ( $self->payby eq 'CHEK' ) {
473 my( $account, $aba ) = split('@', $self->payinfo );
474 'x'x(length($account)-2). substr($account,(length($account)-2)). "@". $aba;
484 Delete and replace methods. payinfo_masked false laziness with cust_main.pm
489 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the