4 use vars qw( @ISA $conf $unsuspendauto );
6 use Business::CreditCard;
9 use FS::Record qw( dbh qsearch qsearchs dbh );
10 use FS::Misc qw(send_email);
12 use FS::cust_bill_pay;
13 use FS::cust_pay_refund;
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 #false laziness w/ cust_credit::insert
154 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
155 my @errors = $cust_main->unsuspend;
157 # side-fx with nested transactions? upstack rolls back?
158 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
164 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
166 #my $cust_main = $self->cust_main;
167 if ( $conf->exists('payment_receipt_email')
168 && grep { $_ ne 'POST' } $cust_main->invoicing_list
171 my $receipt_template = new Text::Template (
173 SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
175 warn "can't create payment receipt template: $Text::Template::ERROR";
179 my @invoicing_list = grep { $_ ne 'POST' } $cust_main->invoicing_list;
181 my $error = send_email(
182 'from' => $conf->config('invoice_from'), #??? well as good as any
183 'to' => \@invoicing_list,
184 'subject' => 'Payment receipt',
185 'body' => $receipt_template->fill_in( HASH => {
186 'date' => time2str("%a %B %o, %Y", $self->_date),
187 'paynum' => $self->paynum,
188 'paid' => $self->paid,
189 'payby' => ucfirst(lc($self->payby)),
190 'payinfo' => ( $self->payby eq 'CARD'
191 ? $self->payinfo_masked
193 'balance' => $cust_main->balance,
197 warn "can't send payment receipt: $error";
208 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
209 unless the closed flag is set.
215 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
217 local $SIG{HUP} = 'IGNORE';
218 local $SIG{INT} = 'IGNORE';
219 local $SIG{QUIT} = 'IGNORE';
220 local $SIG{TERM} = 'IGNORE';
221 local $SIG{TSTP} = 'IGNORE';
222 local $SIG{PIPE} = 'IGNORE';
224 my $oldAutoCommit = $FS::UID::AutoCommit;
225 local $FS::UID::AutoCommit = 0;
228 foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
229 my $error = $cust_bill_pay->delete;
231 $dbh->rollback if $oldAutoCommit;
236 my $error = $self->SUPER::delete(@_);
238 $dbh->rollback if $oldAutoCommit;
242 if ( $conf->config('deletepayments') ne '' ) {
244 my $cust_main = $self->cust_main;
246 my $error = send_email(
247 'from' => $conf->config('invoice_from'), #??? well as good as any
248 'to' => $conf->config('deletepayments'),
249 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
251 "This is an automatic message from your Freeside installation\n",
252 "informing you that the following payment has been deleted:\n",
254 'paynum: '. $self->paynum. "\n",
255 'custnum: '. $self->custnum.
256 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
257 'paid: $'. sprintf("%.2f", $self->paid). "\n",
258 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
259 'payby: '. $self->payby. "\n",
260 'payinfo: '. $self->payinfo. "\n",
261 'paybatch: '. $self->paybatch. "\n",
266 $dbh->rollback if $oldAutoCommit;
267 return "can't send payment deletion notification: $error";
272 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
278 =item replace OLD_RECORD
280 Currently unimplemented (accounting reasons).
285 return "Can't (yet?) modify cust_pay records!";
290 Checks all fields to make sure this is a valid payment. If there is an error,
291 returns the error, otherwise returns false. Called by the insert method.
299 $self->ut_numbern('paynum')
300 || $self->ut_numbern('custnum')
301 || $self->ut_money('paid')
302 || $self->ut_numbern('_date')
303 || $self->ut_textn('paybatch')
304 || $self->ut_enum('closed', [ '', 'Y' ])
306 return $error if $error;
308 return "paid must be > 0 " if $self->paid <= 0;
310 return "unknown cust_main.custnum: ". $self->custnum
312 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
314 $self->_date(time) unless $self->_date;
316 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
319 #false laziness with cust_refund::check
320 if ( $self->payby eq 'CARD' ) {
321 my $payinfo = $self->payinfo;
323 $self->payinfo($payinfo);
324 if ( $self->payinfo ) {
325 $self->payinfo =~ /^(\d{13,16})$/
326 or return "Illegal (mistyped?) credit card number (payinfo)";
328 validate($self->payinfo) or return "Illegal credit card number";
329 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
331 $self->payinfo('N/A');
335 $error = $self->ut_textn('payinfo');
336 return $error if $error;
344 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
351 sort { $a->_date <=> $b->_date }
352 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
356 =item cust_pay_refund
358 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
363 sub cust_pay_refund {
365 sort { $a->_date <=> $b->_date }
366 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
373 Returns the amount of this payment that is still unapplied; which is
374 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
375 applications (see L<FS::cust_pay_refund>).
381 my $amount = $self->paid;
382 $amount -= $_->amount foreach ( $self->cust_bill_pay );
383 $amount -= $_->amount foreach ( $self->cust_pay_refund );
384 sprintf("%.2f", $amount );
389 Returns the parent customer object (see L<FS::cust_main>).
395 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
400 Returns a "masked" payinfo field with all but the last four characters replaced
401 by 'x'es. Useful for displaying credit cards.
407 my $payinfo = $self->payinfo;
408 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
415 Delete and replace methods. payinfo_masked false laziness with cust_main.pm
420 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the