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;
14 @ISA = qw( FS::Record );
16 #ask FS::UID to run this stuff for us later
17 FS::UID->install_callback( sub {
19 $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), `CHEK' (electronic check/ACH),
59 `LECB' (phone bill billing), `BILL' (billing), or `COMP' (free)
61 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
63 =item paybatch - text field for tracking card processing
65 =item closed - books closed flag, empty or `Y'
75 Creates a new payment. To add the payment to the databse, see L<"insert">.
79 sub table { 'cust_pay'; }
83 Adds this payment to the database.
85 For backwards-compatibility and convenience, if the additional field invnum
86 is defined, an FS::cust_bill_pay record for the full amount of the payment
87 will be created. In this case, custnum is optional.
94 local $SIG{HUP} = 'IGNORE';
95 local $SIG{INT} = 'IGNORE';
96 local $SIG{QUIT} = 'IGNORE';
97 local $SIG{TERM} = 'IGNORE';
98 local $SIG{TSTP} = 'IGNORE';
99 local $SIG{PIPE} = 'IGNORE';
101 my $oldAutoCommit = $FS::UID::AutoCommit;
102 local $FS::UID::AutoCommit = 0;
105 if ( $self->invnum ) {
106 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
108 $dbh->rollback if $oldAutoCommit;
109 return "Unknown cust_bill.invnum: ". $self->invnum;
111 $self->custnum($cust_bill->custnum );
114 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
115 my $old_balance = $cust_main->balance;
117 my $error = $self->check;
118 return $error if $error;
120 $error = $self->SUPER::insert;
122 $dbh->rollback if $oldAutoCommit;
123 return "error inserting $self: $error";
126 if ( $self->invnum ) {
127 my $cust_bill_pay = new FS::cust_bill_pay {
128 'invnum' => $self->invnum,
129 'paynum' => $self->paynum,
130 'amount' => $self->paid,
131 '_date' => $self->_date,
133 $error = $cust_bill_pay->insert;
135 $dbh->rollback if $oldAutoCommit;
136 return "error inserting $cust_bill_pay: $error";
140 if ( $self->paybatch =~ /^webui-/ ) {
141 my @cust_pay = qsearch('cust_pay', {
142 'custnum' => $self->custnum,
143 'paybatch' => $self->paybatch,
145 if ( scalar(@cust_pay) > 1 ) {
146 $dbh->rollback if $oldAutoCommit;
147 return "a payment with webui token ". $self->paybatch. " already exists";
151 #false laziness w/ cust_credit::insert
152 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
153 my @errors = $cust_main->unsuspend;
155 # side-fx with nested transactions? upstack rolls back?
156 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
162 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
168 sub upgrade_replace { #1.3.x->1.4.x
171 local $SIG{HUP} = 'IGNORE';
172 local $SIG{INT} = 'IGNORE';
173 local $SIG{QUIT} = 'IGNORE';
174 local $SIG{TERM} = 'IGNORE';
175 local $SIG{TSTP} = 'IGNORE';
176 local $SIG{PIPE} = 'IGNORE';
178 my $oldAutoCommit = $FS::UID::AutoCommit;
179 local $FS::UID::AutoCommit = 0;
182 my $error = $self->check;
183 return $error if $error;
185 my %new = $self->hash;
186 my $new = FS::cust_pay->new(\%new);
188 if ( $self->invnum ) {
189 my $cust_bill_pay = new FS::cust_bill_pay {
190 'invnum' => $self->invnum,
191 'paynum' => $self->paynum,
192 'amount' => $self->paid,
193 '_date' => $self->_date,
195 $error = $cust_bill_pay->insert;
197 /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
199 my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
200 $new->custnum($cust_bill->custnum);
202 $dbh->rollback if $oldAutoCommit;
205 $new->custnum($cust_bill_pay->cust_bill->custnum);
211 $error = $new->SUPER::replace($self);
213 $dbh->rollback if $oldAutoCommit;
217 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
226 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
227 unless the closed flag is set.
233 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
235 local $SIG{HUP} = 'IGNORE';
236 local $SIG{INT} = 'IGNORE';
237 local $SIG{QUIT} = 'IGNORE';
238 local $SIG{TERM} = 'IGNORE';
239 local $SIG{TSTP} = 'IGNORE';
240 local $SIG{PIPE} = 'IGNORE';
242 my $oldAutoCommit = $FS::UID::AutoCommit;
243 local $FS::UID::AutoCommit = 0;
246 foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
247 my $error = $cust_bill_pay->delete;
249 $dbh->rollback if $oldAutoCommit;
254 my $error = $self->SUPER::delete(@_);
256 $dbh->rollback if $oldAutoCommit;
260 if ( $conf->config('deletepayments') ne '' ) {
262 my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
264 my $error = send_email(
265 'from' => $conf->config('invoice_from'), #??? well as good as any
266 'to' => $conf->config('deletepayments'),
267 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
269 "This is an automatic message from your Freeside installation\n",
270 "informing you that the following payment has been deleted:\n",
272 'paynum: '. $self->paynum. "\n",
273 'custnum: '. $self->custnum.
274 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
275 'paid: $'. sprintf("%.2f", $self->paid). "\n",
276 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
277 'payby: '. $self->payby. "\n",
278 'payinfo: '. $self->payinfo. "\n",
279 'paybatch: '. $self->paybatch. "\n",
284 $dbh->rollback if $oldAutoCommit;
285 return "can't send payment deletion notification: $error";
290 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
296 =item replace OLD_RECORD
298 Currently unimplemented (accounting reasons).
303 return "Can't (yet?) modify cust_pay records!";
308 Checks all fields to make sure this is a valid payment. If there is an error,
309 returns the error, otherwise returns false. Called by the insert method.
317 $self->ut_numbern('paynum')
318 || $self->ut_numbern('custnum')
319 || $self->ut_money('paid')
320 || $self->ut_numbern('_date')
321 || $self->ut_textn('paybatch')
322 || $self->ut_enum('closed', [ '', 'Y' ])
324 return $error if $error;
326 return "paid must be > 0 " if $self->paid <= 0;
328 return "unknown cust_main.custnum: ". $self->custnum
330 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
332 $self->_date(time) unless $self->_date;
334 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
337 #false laziness with cust_refund::check
338 if ( $self->payby eq 'CARD' ) {
339 my $payinfo = $self->payinfo;
341 $self->payinfo($payinfo);
342 if ( $self->payinfo ) {
343 $self->payinfo =~ /^(\d{13,16})$/
344 or return "Illegal (mistyped?) credit card number (payinfo)";
346 validate($self->payinfo) or return "Illegal credit card number";
347 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
349 $self->payinfo('N/A');
353 $error = $self->ut_textn('payinfo');
354 return $error if $error;
362 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
369 sort { $a->_date <=> $b->_date }
370 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
376 Returns the amount of this payment that is still unapplied; which is
377 paid minus all payment applications (see L<FS::cust_bill_pay>).
383 my $amount = $self->paid;
384 $amount -= $_->amount foreach ( $self->cust_bill_pay );
385 sprintf("%.2f", $amount );
390 Returns the parent customer object (see L<FS::cust_main>).
396 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
401 Returns a "masked" payinfo field with all but the last four characters replaced
402 by 'x'es. Useful for displaying credit cards.
408 my $payinfo = $self->payinfo;
409 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
416 Delete and replace methods. payinfo_masked false laziness with cust_main.pm
421 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the