4 use vars qw( @ISA $conf $unsuspendauto $smtpmachine $invoice_from );
7 use Mail::Internet 1.44;
8 use Business::CreditCard;
10 use FS::Record qw( dbh qsearch qsearchs dbh );
12 use FS::cust_bill_pay;
15 @ISA = qw( FS::Record );
17 #ask FS::UID to run this stuff for us later
18 $FS::UID::callback{'FS::cust_pay'} = sub {
21 $unsuspendauto = $conf->exists('unsuspendauto');
22 $smtpmachine = $conf->config('smtpmachine');
23 $invoice_from = $conf->config('invoice_from');
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), `BILL' (billing), or `COMP' (free)
65 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
67 =item paybatch - text field for tracking card processing
69 =item closed - books closed flag, empty or `Y'
79 Creates a new payment. To add the payment to the databse, see L<"insert">.
83 sub table { 'cust_pay'; }
87 Adds this payment to the database.
89 For backwards-compatibility and convenience, if the additional field invnum
90 is defined, an FS::cust_bill_pay record for the full amount of the payment
91 will be created. In this case, custnum is optional.
98 local $SIG{HUP} = 'IGNORE';
99 local $SIG{INT} = 'IGNORE';
100 local $SIG{QUIT} = 'IGNORE';
101 local $SIG{TERM} = 'IGNORE';
102 local $SIG{TSTP} = 'IGNORE';
103 local $SIG{PIPE} = 'IGNORE';
105 my $oldAutoCommit = $FS::UID::AutoCommit;
106 local $FS::UID::AutoCommit = 0;
109 if ( $self->invnum ) {
110 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
112 $dbh->rollback if $oldAutoCommit;
113 return "Unknown cust_bill.invnum: ". $self->invnum;
115 $self->custnum($cust_bill->custnum );
118 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
119 my $old_balance = $cust_main->balance;
121 my $error = $self->check;
122 return $error if $error;
124 $error = $self->SUPER::insert;
126 $dbh->rollback if $oldAutoCommit;
127 return "error inserting $self: $error";
130 if ( $self->invnum ) {
131 my $cust_bill_pay = new FS::cust_bill_pay {
132 'invnum' => $self->invnum,
133 'paynum' => $self->paynum,
134 'amount' => $self->paid,
135 '_date' => $self->_date,
137 $error = $cust_bill_pay->insert;
139 $dbh->rollback if $oldAutoCommit;
140 return "error inserting $cust_bill_pay: $error";
144 if ( $self->paybatch =~ /^webui-/ ) {
145 my @cust_pay = qsearch('cust_pay', {
146 'custnum' => $self->custnum,
147 'paybatch' => $self->paybatch,
149 if ( scalar(@cust_pay) > 1 ) {
150 $dbh->rollback if $oldAutoCommit;
151 return "a payment with webui token ". $self->paybatch. " already exists";
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;
172 sub upgrade_replace { #1.3.x->1.4.x
175 local $SIG{HUP} = 'IGNORE';
176 local $SIG{INT} = 'IGNORE';
177 local $SIG{QUIT} = 'IGNORE';
178 local $SIG{TERM} = 'IGNORE';
179 local $SIG{TSTP} = 'IGNORE';
180 local $SIG{PIPE} = 'IGNORE';
182 my $oldAutoCommit = $FS::UID::AutoCommit;
183 local $FS::UID::AutoCommit = 0;
186 my $error = $self->check;
187 return $error if $error;
189 my %new = $self->hash;
190 my $new = FS::cust_pay->new(\%new);
192 if ( $self->invnum ) {
193 my $cust_bill_pay = new FS::cust_bill_pay {
194 'invnum' => $self->invnum,
195 'paynum' => $self->paynum,
196 'amount' => $self->paid,
197 '_date' => $self->_date,
199 $error = $cust_bill_pay->insert;
201 /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
203 my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
204 $new->custnum($cust_bill->custnum);
206 $dbh->rollback if $oldAutoCommit;
209 $new->custnum($cust_bill_pay->cust_bill->custnum);
215 $error = $new->SUPER::replace($self);
217 $dbh->rollback if $oldAutoCommit;
221 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
230 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
231 unless the closed flag is set.
237 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
239 local $SIG{HUP} = 'IGNORE';
240 local $SIG{INT} = 'IGNORE';
241 local $SIG{QUIT} = 'IGNORE';
242 local $SIG{TERM} = 'IGNORE';
243 local $SIG{TSTP} = 'IGNORE';
244 local $SIG{PIPE} = 'IGNORE';
246 my $oldAutoCommit = $FS::UID::AutoCommit;
247 local $FS::UID::AutoCommit = 0;
250 foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
251 my $error = $cust_bill_pay->delete;
253 $dbh->rollback if $oldAutoCommit;
258 my $error = $self->SUPER::delete(@_);
260 $dbh->rollback if $oldAutoCommit;
264 if ( $conf->config('deletepayments') ne '' ) {
266 my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
267 #false laziness w/FS::cust_bill::send & fs_signup_server
268 $ENV{MAILADDRESS} = $invoice_from; #??? well as good as any
269 my $header = new Mail::Header ( [
270 "From: $invoice_from",
271 "To: ". $conf->config('deletepayments'),
272 "Sender: $invoice_from",
273 "Reply-To: $invoice_from",
274 "Date: ". time2str("%a, %d %b %Y %X %z", time),
275 "Subject: FREESIDE NOTIFICATION: Payment deleted",
277 my $message = new Mail::Internet (
280 "This is an automatic message from your Freeside installation\n",
281 "informing you that the following payment has been deleted:\n",
283 'paynum: '. $self->paynum. "\n",
284 'custnum: '. $self->custnum.
285 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
286 'paid: $'. sprintf("%.2f", $self->paid). "\n",
287 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
288 'payby: '. $self->payby. "\n",
289 'payinfo: '. $self->payinfo. "\n",
290 'paybatch: '. $self->paybatch. "\n",
294 $message->smtpsend( Host => $smtpmachine )
295 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
297 $dbh->rollback if $oldAutoCommit;
298 return "(customer # ". $self->custnum.
299 ") can't send payment deletion email to ".
300 $conf->config('deletepayments').
301 " via server $smtpmachine with SMTP: $!";
305 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
311 =item replace OLD_RECORD
313 Currently unimplemented (accounting reasons).
318 return "Can't (yet?) modify cust_pay records!";
323 Checks all fields to make sure this is a valid payment. If there is an error,
324 returns the error, otherwise returns false. Called by the insert method.
332 $self->ut_numbern('paynum')
333 || $self->ut_numbern('custnum')
334 || $self->ut_money('paid')
335 || $self->ut_numbern('_date')
336 || $self->ut_textn('paybatch')
337 || $self->ut_enum('closed', [ '', 'Y' ])
339 return $error if $error;
341 return "paid must be > 0 " if $self->paid <= 0;
343 return "unknown cust_main.custnum: ". $self->custnum
345 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
347 $self->_date(time) unless $self->_date;
349 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
352 #false laziness with cust_refund::check
353 if ( $self->payby eq 'CARD' ) {
354 my $payinfo = $self->payinfo;
356 $self->payinfo($payinfo);
357 if ( $self->payinfo ) {
358 $self->payinfo =~ /^(\d{13,16})$/
359 or return "Illegal (mistyped?) credit card number (payinfo)";
361 validate($self->payinfo) or return "Illegal credit card number";
362 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
364 $self->payinfo('N/A');
368 $error = $self->ut_textn('payinfo');
369 return $error if $error;
378 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
385 sort { $a->_date <=> $b->_date }
386 qsearch( 'cust_bill_pay', { '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>).
399 my $amount = $self->paid;
400 $amount -= $_->amount foreach ( $self->cust_bill_pay );
401 sprintf("%.2f", $amount );
408 $Id: cust_pay.pm,v 1.21 2002-06-04 14:35:52 ivan Exp $
412 Delete and replace methods.
416 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the