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;
12 use FS::cust_bill_pay;
13 use FS::cust_pay_refund;
15 use FS::cust_pay_void;
17 @ISA = qw(FS::Record FS::cust_main_Mixin FS::payinfo_Mixin );
21 #ask FS::UID to run this stuff for us later
22 FS::UID->install_callback( sub {
24 $unsuspendauto = $conf->exists('unsuspendauto');
27 @encrypted_fields = ('payinfo');
31 FS::cust_pay - Object methods for cust_pay objects
37 $record = new FS::cust_pay \%hash;
38 $record = new FS::cust_pay { 'column' => 'value' };
40 $error = $record->insert;
42 $error = $new_record->replace($old_record);
44 $error = $record->delete;
46 $error = $record->check;
50 An FS::cust_pay object represents a payment; the transfer of money from a
51 customer. FS::cust_pay inherits from FS::Record. The following fields are
56 =item paynum - primary key (assigned automatically for new payments)
58 =item custnum - customer (see L<FS::cust_main>)
60 =item paid - Amount of this payment
62 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
63 L<Time::Local> and L<Date::Parse> for conversion functions.
65 =item payby - Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
67 =item payinfo - Payment Information (See L<FS::payinfo_Mixin> for data format)
69 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
71 =item paybatch - text field for tracking card processing
73 =item closed - books closed flag, empty or `Y'
83 Creates a new payment. To add the payment to the databse, see L<"insert">.
87 sub table { 'cust_pay'; }
88 sub cust_linked { $_[0]->cust_main_custnum; }
89 sub cust_unlinked_msg {
91 "WARNING: can't find cust_main.custnum ". $self->custnum.
92 ' (cust_pay.paynum '. $self->paynum. ')';
97 Adds this payment to the database.
99 For backwards-compatibility and convenience, if the additional field invnum
100 is defined, an FS::cust_bill_pay record for the full amount of the payment
101 will be created. In this case, custnum is optional.
108 local $SIG{HUP} = 'IGNORE';
109 local $SIG{INT} = 'IGNORE';
110 local $SIG{QUIT} = 'IGNORE';
111 local $SIG{TERM} = 'IGNORE';
112 local $SIG{TSTP} = 'IGNORE';
113 local $SIG{PIPE} = 'IGNORE';
115 my $oldAutoCommit = $FS::UID::AutoCommit;
116 local $FS::UID::AutoCommit = 0;
119 if ( $self->invnum ) {
120 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
122 $dbh->rollback if $oldAutoCommit;
123 return "Unknown cust_bill.invnum: ". $self->invnum;
125 $self->custnum($cust_bill->custnum );
129 my $error = $self->check;
130 return $error if $error;
132 my $cust_main = $self->cust_main;
133 my $old_balance = $cust_main->balance;
135 $error = $self->SUPER::insert;
137 $dbh->rollback if $oldAutoCommit;
138 return "error inserting $self: $error";
141 if ( $self->invnum ) {
142 my $cust_bill_pay = new FS::cust_bill_pay {
143 'invnum' => $self->invnum,
144 'paynum' => $self->paynum,
145 'amount' => $self->paid,
146 '_date' => $self->_date,
148 $error = $cust_bill_pay->insert;
150 if ( $ignore_noapply ) {
151 warn "warning: error inserting $cust_bill_pay: $error ".
152 "(ignore_noapply flag set; inserting cust_pay record anyway)\n";
154 $dbh->rollback if $oldAutoCommit;
155 return "error inserting $cust_bill_pay: $error";
160 if ( $self->paybatch =~ /^webui-/ ) {
161 my @cust_pay = qsearch('cust_pay', {
162 'custnum' => $self->custnum,
163 'paybatch' => $self->paybatch,
165 if ( scalar(@cust_pay) > 1 ) {
166 $dbh->rollback if $oldAutoCommit;
167 return "a payment with webui token ". $self->paybatch. " already exists";
171 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
173 #false laziness w/ cust_credit::insert
174 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
175 my @errors = $cust_main->unsuspend;
177 # side-fx with nested transactions? upstack rolls back?
178 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
184 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
186 #my $cust_main = $self->cust_main;
187 if ( $conf->exists('payment_receipt_email')
188 && grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list
191 my $receipt_template = new Text::Template (
193 SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
195 warn "can't create payment receipt template: $Text::Template::ERROR";
199 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list;
201 my $payby = $self->payby;
202 my $payinfo = $self->payinfo;
203 $payby =~ s/^BILL$/Check/ if $payinfo;
204 $payinfo = $self->paymask if $payby eq 'CARD' || $payby eq 'CHEK';
205 $payby =~ s/^CHEK$/Electronic check/;
207 my $error = send_email(
208 'from' => $conf->config('invoice_from'), #??? well as good as any
209 'to' => \@invoicing_list,
210 'subject' => 'Payment receipt',
211 'body' => [ $receipt_template->fill_in( HASH => {
212 'date' => time2str("%a %B %o, %Y", $self->_date),
213 'name' => $cust_main->name,
214 'paynum' => $self->paynum,
215 'paid' => sprintf("%.2f", $self->paid),
216 'payby' => ucfirst(lc($payby)),
217 'payinfo' => $payinfo,
218 'balance' => $cust_main->balance,
222 warn "can't send payment receipt: $error";
231 =item void [ REASON ]
233 Voids this payment: deletes the payment and all associated applications and
234 adds a record of the voided payment to the FS::cust_pay_void table.
241 local $SIG{HUP} = 'IGNORE';
242 local $SIG{INT} = 'IGNORE';
243 local $SIG{QUIT} = 'IGNORE';
244 local $SIG{TERM} = 'IGNORE';
245 local $SIG{TSTP} = 'IGNORE';
246 local $SIG{PIPE} = 'IGNORE';
248 my $oldAutoCommit = $FS::UID::AutoCommit;
249 local $FS::UID::AutoCommit = 0;
252 my $cust_pay_void = new FS::cust_pay_void ( {
253 map { $_ => $self->get($_) } $self->fields
255 $cust_pay_void->reason(shift) if scalar(@_);
256 my $error = $cust_pay_void->insert;
258 $dbh->rollback if $oldAutoCommit;
262 $error = $self->delete;
264 $dbh->rollback if $oldAutoCommit;
268 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
276 Unless the closed flag is set, deletes this payment and all associated
277 applications (see L<FS::cust_bill_pay> and L<FS::cust_pay_refund>). In most
278 cases, you want to use the void method instead to leave a record of the
283 # very similar to FS::cust_credit::delete
286 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
288 local $SIG{HUP} = 'IGNORE';
289 local $SIG{INT} = 'IGNORE';
290 local $SIG{QUIT} = 'IGNORE';
291 local $SIG{TERM} = 'IGNORE';
292 local $SIG{TSTP} = 'IGNORE';
293 local $SIG{PIPE} = 'IGNORE';
295 my $oldAutoCommit = $FS::UID::AutoCommit;
296 local $FS::UID::AutoCommit = 0;
299 foreach my $app ( $self->cust_bill_pay, $self->cust_pay_refund ) {
300 my $error = $app->delete;
302 $dbh->rollback if $oldAutoCommit;
307 my $error = $self->SUPER::delete(@_);
309 $dbh->rollback if $oldAutoCommit;
313 if ( $conf->config('deletepayments') ne '' ) {
315 my $cust_main = $self->cust_main;
317 my $error = send_email(
318 'from' => $conf->config('invoice_from'), #??? well as good as any
319 'to' => $conf->config('deletepayments'),
320 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
322 "This is an automatic message from your Freeside installation\n",
323 "informing you that the following payment has been deleted:\n",
325 'paynum: '. $self->paynum. "\n",
326 'custnum: '. $self->custnum.
327 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
328 'paid: $'. sprintf("%.2f", $self->paid). "\n",
329 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
330 'payby: '. $self->payby. "\n",
331 'payinfo: '. $self->paymask. "\n",
332 'paybatch: '. $self->paybatch. "\n",
337 $dbh->rollback if $oldAutoCommit;
338 return "can't send payment deletion notification: $error";
343 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
349 =item replace OLD_RECORD
351 You can, but probably shouldn't modify payments...
356 #return "Can't modify payment!"
358 return "Can't modify closed payment" if $self->closed =~ /^Y/i;
359 $self->SUPER::replace(@_);
364 Checks all fields to make sure this is a valid payment. If there is an error,
365 returns the error, otherwise returns false. Called by the insert method.
373 $self->ut_numbern('paynum')
374 || $self->ut_numbern('custnum')
375 || $self->ut_money('paid')
376 || $self->ut_numbern('_date')
377 || $self->ut_textn('paybatch')
378 || $self->ut_enum('closed', [ '', 'Y' ])
379 || $self->payinfo_check()
381 return $error if $error;
383 return "paid must be > 0 " if $self->paid <= 0;
385 return "unknown cust_main.custnum: ". $self->custnum
387 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
389 $self->_date(time) unless $self->_date;
394 =item batch_insert CUST_PAY_OBJECT, ...
396 Class method which inserts multiple payments. Takes a list of FS::cust_pay
397 objects. Returns a list, each element representing the status of inserting the
398 corresponding payment - empty. If there is an error inserting any payment, the
399 entire transaction is rolled back, i.e. all payments are inserted or none are.
403 my @errors = FS::cust_pay->batch_insert(@cust_pay);
404 my $num_errors = scalar(grep $_, @errors);
405 if ( $num_errors == 0 ) {
406 #success; all payments were inserted
408 #failure; no payments were inserted.
414 my $self = shift; #class method
416 local $SIG{HUP} = 'IGNORE';
417 local $SIG{INT} = 'IGNORE';
418 local $SIG{QUIT} = 'IGNORE';
419 local $SIG{TERM} = 'IGNORE';
420 local $SIG{TSTP} = 'IGNORE';
421 local $SIG{PIPE} = 'IGNORE';
423 my $oldAutoCommit = $FS::UID::AutoCommit;
424 local $FS::UID::AutoCommit = 0;
430 my $error = $_->insert;
434 $_->cust_main->apply_payments;
440 $dbh->rollback if $oldAutoCommit;
442 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
451 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
458 sort { $a->_date <=> $b->_date
459 || $a->invnum <=> $b->invnum }
460 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
464 =item cust_pay_refund
466 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
471 sub cust_pay_refund {
473 sort { $a->_date <=> $b->_date }
474 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
481 Returns the amount of this payment that is still unapplied; which is
482 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
483 applications (see L<FS::cust_pay_refund>).
489 my $amount = $self->paid;
490 $amount -= $_->amount foreach ( $self->cust_bill_pay );
491 $amount -= $_->amount foreach ( $self->cust_pay_refund );
492 sprintf("%.2f", $amount );
497 Returns the amount of this payment that has not been refuned; which is
498 paid minus all refund applications (see L<FS::cust_pay_refund>).
504 my $amount = $self->paid;
505 $amount -= $_->amount foreach ( $self->cust_pay_refund );
506 sprintf("%.2f", $amount );
512 Returns the parent customer object (see L<FS::cust_main>).
518 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
525 Delete and replace methods.
529 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the