4 use vars qw( @ISA $conf $unsuspendauto $ignore_noapply @encrypted_fields );
6 use Business::CreditCard;
8 use FS::Misc qw(send_email);
9 use FS::Record qw( dbh qsearch qsearchs );
10 use FS::cust_main_Mixin;
11 use FS::payinfo_Mixin;
13 use FS::cust_bill_pay;
14 use FS::cust_pay_refund;
16 use FS::cust_pay_void;
18 @ISA = qw(FS::Record FS::cust_main_Mixin FS::payinfo_Mixin );
22 #ask FS::UID to run this stuff for us later
23 FS::UID->install_callback( sub {
25 $unsuspendauto = $conf->exists('unsuspendauto');
28 @encrypted_fields = ('payinfo');
32 FS::cust_pay - Object methods for cust_pay objects
38 $record = new FS::cust_pay \%hash;
39 $record = new FS::cust_pay { 'column' => 'value' };
41 $error = $record->insert;
43 $error = $new_record->replace($old_record);
45 $error = $record->delete;
47 $error = $record->check;
51 An FS::cust_pay object represents a payment; the transfer of money from a
52 customer. FS::cust_pay inherits from FS::Record. The following fields are
57 =item paynum - primary key (assigned automatically for new payments)
59 =item custnum - customer (see L<FS::cust_main>)
61 =item paid - Amount of this payment
63 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
64 L<Time::Local> and L<Date::Parse> for conversion functions.
66 =item payby - Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
68 =item payinfo - Payment Information (See L<FS::payinfo_Mixin> for data format)
70 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
72 =item paybatch - text field for tracking card processing
74 =item closed - books closed flag, empty or `Y'
84 Creates a new payment. To add the payment to the databse, see L<"insert">.
88 sub table { 'cust_pay'; }
89 sub cust_linked { $_[0]->cust_main_custnum; }
90 sub cust_unlinked_msg {
92 "WARNING: can't find cust_main.custnum ". $self->custnum.
93 ' (cust_pay.paynum '. $self->paynum. ')';
98 Adds this payment to the database.
100 For backwards-compatibility and convenience, if the additional field invnum
101 is defined, an FS::cust_bill_pay record for the full amount of the payment
102 will be created. In this case, custnum is optional.
109 local $SIG{HUP} = 'IGNORE';
110 local $SIG{INT} = 'IGNORE';
111 local $SIG{QUIT} = 'IGNORE';
112 local $SIG{TERM} = 'IGNORE';
113 local $SIG{TSTP} = 'IGNORE';
114 local $SIG{PIPE} = 'IGNORE';
116 my $oldAutoCommit = $FS::UID::AutoCommit;
117 local $FS::UID::AutoCommit = 0;
120 if ( $self->invnum ) {
121 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
123 $dbh->rollback if $oldAutoCommit;
124 return "Unknown cust_bill.invnum: ". $self->invnum;
126 $self->custnum($cust_bill->custnum );
130 my $error = $self->check;
131 return $error if $error;
133 my $cust_main = $self->cust_main;
134 my $old_balance = $cust_main->balance;
136 $error = $self->SUPER::insert;
138 $dbh->rollback if $oldAutoCommit;
139 return "error inserting $self: $error";
142 if ( $self->invnum ) {
143 my $cust_bill_pay = new FS::cust_bill_pay {
144 'invnum' => $self->invnum,
145 'paynum' => $self->paynum,
146 'amount' => $self->paid,
147 '_date' => $self->_date,
149 $error = $cust_bill_pay->insert;
151 if ( $ignore_noapply ) {
152 warn "warning: error inserting $cust_bill_pay: $error ".
153 "(ignore_noapply flag set; inserting cust_pay record anyway)\n";
155 $dbh->rollback if $oldAutoCommit;
156 return "error inserting $cust_bill_pay: $error";
161 if ( $self->paybatch =~ /^webui-/ ) {
162 my @cust_pay = qsearch('cust_pay', {
163 'custnum' => $self->custnum,
164 'paybatch' => $self->paybatch,
166 if ( scalar(@cust_pay) > 1 ) {
167 $dbh->rollback if $oldAutoCommit;
168 return "a payment with webui token ". $self->paybatch. " already exists";
172 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
174 #false laziness w/ cust_credit::insert
175 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
176 my @errors = $cust_main->unsuspend;
178 # side-fx with nested transactions? upstack rolls back?
179 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
185 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
187 #my $cust_main = $self->cust_main;
188 if ( $conf->exists('payment_receipt_email')
189 && grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list
192 my $receipt_template = new Text::Template (
194 SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
196 warn "can't create payment receipt template: $Text::Template::ERROR";
200 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list;
202 my $payby = $self->payby;
203 my $payinfo = $self->payinfo;
204 $payby =~ s/^BILL$/Check/ if $payinfo;
205 $payinfo = $self->paymask if $payby eq 'CARD' || $payby eq 'CHEK';
206 $payby =~ s/^CHEK$/Electronic check/;
208 my $error = send_email(
209 'from' => $conf->config('invoice_from'), #??? well as good as any
210 'to' => \@invoicing_list,
211 'subject' => 'Payment receipt',
212 'body' => [ $receipt_template->fill_in( HASH => {
213 'date' => time2str("%a %B %o, %Y", $self->_date),
214 'name' => $cust_main->name,
215 'paynum' => $self->paynum,
216 'paid' => sprintf("%.2f", $self->paid),
217 'payby' => ucfirst(lc($payby)),
218 'payinfo' => $payinfo,
219 'balance' => $cust_main->balance,
223 warn "can't send payment receipt: $error";
232 =item void [ REASON ]
234 Voids this payment: deletes the payment and all associated applications and
235 adds a record of the voided payment to the FS::cust_pay_void table.
242 local $SIG{HUP} = 'IGNORE';
243 local $SIG{INT} = 'IGNORE';
244 local $SIG{QUIT} = 'IGNORE';
245 local $SIG{TERM} = 'IGNORE';
246 local $SIG{TSTP} = 'IGNORE';
247 local $SIG{PIPE} = 'IGNORE';
249 my $oldAutoCommit = $FS::UID::AutoCommit;
250 local $FS::UID::AutoCommit = 0;
253 my $cust_pay_void = new FS::cust_pay_void ( {
254 map { $_ => $self->get($_) } $self->fields
256 $cust_pay_void->reason(shift) if scalar(@_);
257 my $error = $cust_pay_void->insert;
259 $dbh->rollback if $oldAutoCommit;
263 $error = $self->delete;
265 $dbh->rollback if $oldAutoCommit;
269 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
277 Unless the closed flag is set, deletes this payment and all associated
278 applications (see L<FS::cust_bill_pay> and L<FS::cust_pay_refund>). In most
279 cases, you want to use the void method instead to leave a record of the
284 # very similar to FS::cust_credit::delete
287 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
289 local $SIG{HUP} = 'IGNORE';
290 local $SIG{INT} = 'IGNORE';
291 local $SIG{QUIT} = 'IGNORE';
292 local $SIG{TERM} = 'IGNORE';
293 local $SIG{TSTP} = 'IGNORE';
294 local $SIG{PIPE} = 'IGNORE';
296 my $oldAutoCommit = $FS::UID::AutoCommit;
297 local $FS::UID::AutoCommit = 0;
300 foreach my $app ( $self->cust_bill_pay, $self->cust_pay_refund ) {
301 my $error = $app->delete;
303 $dbh->rollback if $oldAutoCommit;
308 my $error = $self->SUPER::delete(@_);
310 $dbh->rollback if $oldAutoCommit;
314 if ( $conf->config('deletepayments') ne '' ) {
316 my $cust_main = $self->cust_main;
318 my $error = send_email(
319 'from' => $conf->config('invoice_from'), #??? well as good as any
320 'to' => $conf->config('deletepayments'),
321 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
323 "This is an automatic message from your Freeside installation\n",
324 "informing you that the following payment has been deleted:\n",
326 'paynum: '. $self->paynum. "\n",
327 'custnum: '. $self->custnum.
328 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
329 'paid: $'. sprintf("%.2f", $self->paid). "\n",
330 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
331 'payby: '. $self->payby. "\n",
332 'payinfo: '. $self->paymask. "\n",
333 'paybatch: '. $self->paybatch. "\n",
338 $dbh->rollback if $oldAutoCommit;
339 return "can't send payment deletion notification: $error";
344 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
350 =item replace OLD_RECORD
352 You can, but probably shouldn't modify payments...
357 #return "Can't modify payment!"
359 return "Can't modify closed payment" if $self->closed =~ /^Y/i;
360 $self->SUPER::replace(@_);
365 Checks all fields to make sure this is a valid payment. If there is an error,
366 returns the error, otherwise returns false. Called by the insert method.
374 $self->ut_numbern('paynum')
375 || $self->ut_numbern('custnum')
376 || $self->ut_money('paid')
377 || $self->ut_numbern('_date')
378 || $self->ut_textn('paybatch')
379 || $self->ut_enum('closed', [ '', 'Y' ])
380 || $self->payinfo_check()
382 return $error if $error;
384 return "paid must be > 0 " if $self->paid <= 0;
386 return "unknown cust_main.custnum: ". $self->custnum
388 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
390 $self->_date(time) unless $self->_date;
395 =item batch_insert CUST_PAY_OBJECT, ...
397 Class method which inserts multiple payments. Takes a list of FS::cust_pay
398 objects. Returns a list, each element representing the status of inserting the
399 corresponding payment - empty. If there is an error inserting any payment, the
400 entire transaction is rolled back, i.e. all payments are inserted or none are.
404 my @errors = FS::cust_pay->batch_insert(@cust_pay);
405 my $num_errors = scalar(grep $_, @errors);
406 if ( $num_errors == 0 ) {
407 #success; all payments were inserted
409 #failure; no payments were inserted.
415 my $self = shift; #class method
417 local $SIG{HUP} = 'IGNORE';
418 local $SIG{INT} = 'IGNORE';
419 local $SIG{QUIT} = 'IGNORE';
420 local $SIG{TERM} = 'IGNORE';
421 local $SIG{TSTP} = 'IGNORE';
422 local $SIG{PIPE} = 'IGNORE';
424 my $oldAutoCommit = $FS::UID::AutoCommit;
425 local $FS::UID::AutoCommit = 0;
431 my $error = $_->insert;
435 $_->cust_main->apply_payments;
441 $dbh->rollback if $oldAutoCommit;
443 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
452 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
459 sort { $a->_date <=> $b->_date
460 || $a->invnum <=> $b->invnum }
461 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
465 =item cust_pay_refund
467 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
472 sub cust_pay_refund {
474 sort { $a->_date <=> $b->_date }
475 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
482 Returns the amount of this payment that is still unapplied; which is
483 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
484 applications (see L<FS::cust_pay_refund>).
490 my $amount = $self->paid;
491 $amount -= $_->amount foreach ( $self->cust_bill_pay );
492 $amount -= $_->amount foreach ( $self->cust_pay_refund );
493 sprintf("%.2f", $amount );
498 Returns the amount of this payment that has not been refuned; which is
499 paid minus all refund applications (see L<FS::cust_pay_refund>).
505 my $amount = $self->paid;
506 $amount -= $_->amount foreach ( $self->cust_pay_refund );
507 sprintf("%.2f", $amount );
513 Returns the parent customer object (see L<FS::cust_main>).
519 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
526 Delete and replace methods.
530 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the