4 use vars qw( @ISA $conf $unsuspendauto );
5 use Business::CreditCard;
7 use FS::Record qw( dbh qsearch qsearchs dbh );
12 @ISA = qw( FS::Record );
14 #ask FS::UID to run this stuff for us later
15 $FS::UID::callback{'FS::cust_pay'} = sub {
18 $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), `BILL' (billing), or `COMP' (free)
60 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
62 =item paybatch - text field for tracking card processing
64 =item closed - books closed flag, empty or `Y'
74 Creates a new payment. To add the payment to the databse, see L<"insert">.
78 sub table { 'cust_pay'; }
82 Adds this payment to the database.
84 For backwards-compatibility and convenience, if the additional field invnum
85 is defined, an FS::cust_bill_pay record for the full amount of the payment
86 will be created. In this case, custnum is optional.
93 local $SIG{HUP} = 'IGNORE';
94 local $SIG{INT} = 'IGNORE';
95 local $SIG{QUIT} = 'IGNORE';
96 local $SIG{TERM} = 'IGNORE';
97 local $SIG{TSTP} = 'IGNORE';
98 local $SIG{PIPE} = 'IGNORE';
100 my $oldAutoCommit = $FS::UID::AutoCommit;
101 local $FS::UID::AutoCommit = 0;
104 if ( $self->invnum ) {
105 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
107 $dbh->rollback if $oldAutoCommit;
108 return "Unknown cust_bill.invnum: ". $self->invnum;
110 $self->custnum($cust_bill->custnum );
113 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
114 my $old_balance = $cust_main->balance;
116 my $error = $self->check;
117 return $error if $error;
119 $error = $self->SUPER::insert;
121 $dbh->rollback if $oldAutoCommit;
122 return "error inserting $self: $error";
125 if ( $self->invnum ) {
126 my $cust_bill_pay = new FS::cust_bill_pay {
127 'invnum' => $self->invnum,
128 'paynum' => $self->paynum,
129 'amount' => $self->paid,
130 '_date' => $self->_date,
132 $error = $cust_bill_pay->insert;
134 $dbh->rollback if $oldAutoCommit;
135 return "error inserting $cust_bill_pay: $error";
139 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
141 #false laziness w/ cust_credit::insert
142 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
143 my @errors = $cust_main->unsuspend;
145 # side-fx with nested transactions? upstack rolls back?
146 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
156 sub upgrade_replace { #1.3.x->1.4.x
159 local $SIG{HUP} = 'IGNORE';
160 local $SIG{INT} = 'IGNORE';
161 local $SIG{QUIT} = 'IGNORE';
162 local $SIG{TERM} = 'IGNORE';
163 local $SIG{TSTP} = 'IGNORE';
164 local $SIG{PIPE} = 'IGNORE';
166 my $oldAutoCommit = $FS::UID::AutoCommit;
167 local $FS::UID::AutoCommit = 0;
170 my $error = $self->check;
171 return $error if $error;
173 my %new = $self->hash;
174 my $new = FS::cust_pay->new(\%new);
176 if ( $self->invnum ) {
177 my $cust_bill_pay = new FS::cust_bill_pay {
178 'invnum' => $self->invnum,
179 'paynum' => $self->paynum,
180 'amount' => $self->paid,
181 '_date' => $self->_date,
183 $error = $cust_bill_pay->insert;
185 /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
187 my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
188 $new->custnum($cust_bill->custnum);
190 $dbh->rollback if $oldAutoCommit;
193 $new->custnum($cust_bill_pay->cust_bill->custnum);
199 $error = $new->SUPER::replace($self);
201 $dbh->rollback if $oldAutoCommit;
205 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
214 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
215 unless the closed flag is set.
221 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
223 local $SIG{HUP} = 'IGNORE';
224 local $SIG{INT} = 'IGNORE';
225 local $SIG{QUIT} = 'IGNORE';
226 local $SIG{TERM} = 'IGNORE';
227 local $SIG{TSTP} = 'IGNORE';
228 local $SIG{PIPE} = 'IGNORE';
230 my $oldAutoCommit = $FS::UID::AutoCommit;
231 local $FS::UID::AutoCommit = 0;
234 foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
235 my $error = $cust_bill_pay->delete;
237 $dbh->rollback if $oldAutoCommit;
242 my $error = $self->SUPER::delete(@_);
244 $dbh->rollback if $oldAutoCommit;
248 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
254 =item replace OLD_RECORD
256 Currently unimplemented (accounting reasons).
261 return "Can't (yet?) modify cust_pay records!";
266 Checks all fields to make sure this is a valid payment. If there is an error,
267 returns the error, otherwise returns false. Called by the insert method.
275 $self->ut_numbern('paynum')
276 || $self->ut_numbern('custnum')
277 || $self->ut_money('paid')
278 || $self->ut_numbern('_date')
279 || $self->ut_textn('paybatch')
280 || $self->ut_enum('closed', [ '', 'Y' ])
282 return $error if $error;
284 return "paid must be > 0 " if $self->paid <= 0;
286 return "unknown cust_main.custnum: ". $self->custnum
288 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
290 $self->_date(time) unless $self->_date;
292 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
295 #false laziness with cust_refund::check
296 if ( $self->payby eq 'CARD' ) {
297 my $payinfo = $self->payinfo;
299 $self->payinfo($payinfo);
300 if ( $self->payinfo ) {
301 $self->payinfo =~ /^(\d{13,16})$/
302 or return "Illegal (mistyped?) credit card number (payinfo)";
304 validate($self->payinfo) or return "Illegal credit card number";
305 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
307 $self->payinfo('N/A');
311 $error = $self->ut_textn('payinfo');
312 return $error if $error;
321 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
328 sort { $a->_date <=> $b->_date }
329 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
335 Returns the amount of this payment that is still unapplied; which is
336 paid minus all payment applications (see L<FS::cust_bill_pay>).
342 my $amount = $self->paid;
343 $amount -= $_->amount foreach ( $self->cust_bill_pay );
344 sprintf("%.2f", $amount );
351 $Id: cust_pay.pm,v 1.16 2002-02-07 22:29:34 ivan Exp $
355 Delete and replace methods.
359 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the