4 use vars qw( @ISA $conf $unsuspendauto );
6 use Business::CreditCard;
8 use FS::Record qw( dbh qsearch qsearchs dbh );
9 use FS::Misc qw(send_email);
11 use FS::cust_bill_pay;
12 use FS::cust_pay_refund;
15 @ISA = qw( FS::Record );
17 #ask FS::UID to run this stuff for us later
18 FS::UID->install_callback( sub {
20 $unsuspendauto = $conf->exists('unsuspendauto');
25 FS::cust_pay - Object methods for cust_pay objects
31 $record = new FS::cust_pay \%hash;
32 $record = new FS::cust_pay { 'column' => 'value' };
34 $error = $record->insert;
36 $error = $new_record->replace($old_record);
38 $error = $record->delete;
40 $error = $record->check;
44 An FS::cust_pay object represents a payment; the transfer of money from a
45 customer. FS::cust_pay inherits from FS::Record. The following fields are
50 =item paynum - primary key (assigned automatically for new payments)
52 =item custnum - customer (see L<FS::cust_main>)
54 =item paid - Amount of this payment
56 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
57 L<Time::Local> and L<Date::Parse> for conversion functions.
59 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
60 `LECB' (phone bill billing), `BILL' (billing), or `COMP' (free)
62 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
64 =item paybatch - text field for tracking card processing
66 =item closed - books closed flag, empty or `Y'
76 Creates a new payment. To add the payment to the databse, see L<"insert">.
80 sub table { 'cust_pay'; }
84 Adds this payment to the database.
86 For backwards-compatibility and convenience, if the additional field invnum
87 is defined, an FS::cust_bill_pay record for the full amount of the payment
88 will be created. In this case, custnum is optional.
95 local $SIG{HUP} = 'IGNORE';
96 local $SIG{INT} = 'IGNORE';
97 local $SIG{QUIT} = 'IGNORE';
98 local $SIG{TERM} = 'IGNORE';
99 local $SIG{TSTP} = 'IGNORE';
100 local $SIG{PIPE} = 'IGNORE';
102 my $oldAutoCommit = $FS::UID::AutoCommit;
103 local $FS::UID::AutoCommit = 0;
106 if ( $self->invnum ) {
107 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
109 $dbh->rollback if $oldAutoCommit;
110 return "Unknown cust_bill.invnum: ". $self->invnum;
112 $self->custnum($cust_bill->custnum );
115 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
116 my $old_balance = $cust_main->balance;
118 my $error = $self->check;
119 return $error if $error;
121 $error = $self->SUPER::insert;
123 $dbh->rollback if $oldAutoCommit;
124 return "error inserting $self: $error";
127 if ( $self->invnum ) {
128 my $cust_bill_pay = new FS::cust_bill_pay {
129 'invnum' => $self->invnum,
130 'paynum' => $self->paynum,
131 'amount' => $self->paid,
132 '_date' => $self->_date,
134 $error = $cust_bill_pay->insert;
136 $dbh->rollback if $oldAutoCommit;
137 return "error inserting $cust_bill_pay: $error";
141 if ( $self->paybatch =~ /^webui-/ ) {
142 my @cust_pay = qsearch('cust_pay', {
143 'custnum' => $self->custnum,
144 'paybatch' => $self->paybatch,
146 if ( scalar(@cust_pay) > 1 ) {
147 $dbh->rollback if $oldAutoCommit;
148 return "a payment with webui token ". $self->paybatch. " already exists";
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. ": ".
163 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
169 sub upgrade_replace { #1.3.x->1.4.x
172 local $SIG{HUP} = 'IGNORE';
173 local $SIG{INT} = 'IGNORE';
174 local $SIG{QUIT} = 'IGNORE';
175 local $SIG{TERM} = 'IGNORE';
176 local $SIG{TSTP} = 'IGNORE';
177 local $SIG{PIPE} = 'IGNORE';
179 my $oldAutoCommit = $FS::UID::AutoCommit;
180 local $FS::UID::AutoCommit = 0;
183 my $error = $self->check;
184 return $error if $error;
186 my %new = $self->hash;
187 my $new = FS::cust_pay->new(\%new);
189 if ( $self->invnum ) {
190 my $cust_bill_pay = new FS::cust_bill_pay {
191 'invnum' => $self->invnum,
192 'paynum' => $self->paynum,
193 'amount' => $self->paid,
194 '_date' => $self->_date,
196 $error = $cust_bill_pay->insert;
198 /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
200 my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
201 $new->custnum($cust_bill->custnum);
203 $dbh->rollback if $oldAutoCommit;
206 $new->custnum($cust_bill_pay->cust_bill->custnum);
212 $error = $new->SUPER::replace($self);
214 $dbh->rollback if $oldAutoCommit;
218 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
227 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
228 unless the closed flag is set.
234 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
236 local $SIG{HUP} = 'IGNORE';
237 local $SIG{INT} = 'IGNORE';
238 local $SIG{QUIT} = 'IGNORE';
239 local $SIG{TERM} = 'IGNORE';
240 local $SIG{TSTP} = 'IGNORE';
241 local $SIG{PIPE} = 'IGNORE';
243 my $oldAutoCommit = $FS::UID::AutoCommit;
244 local $FS::UID::AutoCommit = 0;
247 foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
248 my $error = $cust_bill_pay->delete;
250 $dbh->rollback if $oldAutoCommit;
255 my $error = $self->SUPER::delete(@_);
257 $dbh->rollback if $oldAutoCommit;
261 if ( $conf->config('deletepayments') ne '' ) {
263 my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
265 my $error = send_email(
266 'from' => $conf->config('invoice_from'), #??? well as good as any
267 'to' => $conf->config('deletepayments'),
268 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
270 "This is an automatic message from your Freeside installation\n",
271 "informing you that the following payment has been deleted:\n",
273 'paynum: '. $self->paynum. "\n",
274 'custnum: '. $self->custnum.
275 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
276 'paid: $'. sprintf("%.2f", $self->paid). "\n",
277 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
278 'payby: '. $self->payby. "\n",
279 'payinfo: '. $self->payinfo. "\n",
280 'paybatch: '. $self->paybatch. "\n",
285 $dbh->rollback if $oldAutoCommit;
286 return "can't send payment deletion notification: $error";
291 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
297 =item replace OLD_RECORD
299 Currently unimplemented (accounting reasons).
304 return "Can't (yet?) modify cust_pay records!";
309 Checks all fields to make sure this is a valid payment. If there is an error,
310 returns the error, otherwise returns false. Called by the insert method.
318 $self->ut_numbern('paynum')
319 || $self->ut_numbern('custnum')
320 || $self->ut_money('paid')
321 || $self->ut_numbern('_date')
322 || $self->ut_textn('paybatch')
323 || $self->ut_enum('closed', [ '', 'Y' ])
325 return $error if $error;
327 return "paid must be > 0 " if $self->paid <= 0;
329 return "unknown cust_main.custnum: ". $self->custnum
331 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
333 $self->_date(time) unless $self->_date;
335 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
338 #false laziness with cust_refund::check
339 if ( $self->payby eq 'CARD' ) {
340 my $payinfo = $self->payinfo;
342 $self->payinfo($payinfo);
343 if ( $self->payinfo ) {
344 $self->payinfo =~ /^(\d{13,16})$/
345 or return "Illegal (mistyped?) credit card number (payinfo)";
347 validate($self->payinfo) or return "Illegal credit card number";
348 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
350 $self->payinfo('N/A');
354 $error = $self->ut_textn('payinfo');
355 return $error if $error;
363 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
370 sort { $a->_date <=> $b->_date }
371 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
375 =item cust_pay_refund
377 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
382 sub cust_pay_refund {
384 sort { $a->_date <=> $b->_date }
385 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
392 Returns the amount of this payment that is still unapplied; which is
393 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
394 applications (see L<FS::cust_pay_refund>).
400 my $amount = $self->paid;
401 $amount -= $_->amount foreach ( $self->cust_bill_pay );
402 $amount -= $_->amount foreach ( $self->cust_pay_refund );
403 sprintf("%.2f", $amount );
408 Returns the parent customer object (see L<FS::cust_main>).
414 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
419 Returns a "masked" payinfo field with all but the last four characters replaced
420 by 'x'es. Useful for displaying credit cards.
426 my $payinfo = $self->payinfo;
427 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
434 Delete and replace methods. payinfo_masked false laziness with cust_main.pm
439 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the