1 package FS::cust_refund;
5 use Business::CreditCard;
6 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::UID qw(getotaker);
9 use FS::cust_credit_refund;
10 use FS::cust_pay_refund;
13 @ISA = qw( FS::Record );
17 FS::cust_refund - Object method for cust_refund objects
23 $record = new FS::cust_refund \%hash;
24 $record = new FS::cust_refund { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::cust_refund represents a refund: the transfer of money to a customer;
37 equivalent to a negative payment (see L<FS::cust_pay>). FS::cust_refund
38 inherits from FS::Record. The following fields are currently supported:
42 =item refundnum - primary key (assigned automatically for new refunds)
44 =item custnum - customer (see L<FS::cust_main>)
46 =item refund - Amount of the refund
48 =item reason - Reason for the refund
50 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
51 L<Time::Local> and L<Date::Parse> for conversion functions.
53 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
54 `LECB' (Phone bill billing), `BILL' (billing), `CASH' (cash),
55 `WEST' (Western Union), `MCRD' (Manual credit card), or `COMP' (free)
57 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
59 =item paybatch - text field for tracking card processing
61 =item otaker - order taker (assigned automatically, see L<FS::UID>)
63 =item closed - books closed flag, empty or `Y'
73 Creates a new refund. To add the refund to the database, see L<"insert">.
77 sub table { 'cust_refund'; }
81 Adds this refund to the database.
83 For backwards-compatibility and convenience, if the additional field crednum is
84 defined, an FS::cust_credit_refund record for the full amount of the refund
85 will be created. Or (this time for convenience and consistancy), if the
86 additional field paynum is defined, an FS::cust_pay_refund record for the full
87 amount of the refund will be created. In both cases, 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->crednum ) {
106 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
108 $dbh->rollback if $oldAutoCommit;
109 return "Unknown cust_credit.crednum: ". $self->crednum;
111 $self->custnum($cust_credit->custnum);
112 } elsif ( $self->paynum ) {
113 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
115 $dbh->rollback if $oldAutoCommit;
116 return "Unknown cust_pay.paynum: ". $self->paynum;
118 $self->custnum($cust_pay->custnum);
121 my $error = $self->check;
122 return $error if $error;
124 $error = $self->SUPER::insert;
126 $dbh->rollback if $oldAutoCommit;
130 if ( $self->crednum ) {
131 my $cust_credit_refund = new FS::cust_credit_refund {
132 'crednum' => $self->crednum,
133 'refundnum' => $self->refundnum,
134 'amount' => $self->refund,
135 '_date' => $self->_date,
137 $error = $cust_credit_refund->insert;
139 $dbh->rollback if $oldAutoCommit;
142 #$self->custnum($cust_credit_refund->cust_credit->custnum);
143 } elsif ( $self->paynum ) {
144 my $cust_pay_refund = new FS::cust_pay_refund {
145 'paynum' => $self->paynum,
146 'refundnum' => $self->refundnum,
147 'amount' => $self->refund,
148 '_date' => $self->_date,
150 $error = $cust_pay_refund->insert;
152 $dbh->rollback if $oldAutoCommit;
158 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
166 Currently unimplemented (accounting reasons).
172 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
173 $self->SUPER::delete(@_);
176 =item replace OLD_RECORD
178 Currently unimplemented (accounting reasons).
183 return "Can't (yet?) modify cust_refund records!";
188 Checks all fields to make sure this is a valid refund. If there is an error,
189 returns the error, otherwise returns false. Called by the insert method.
197 $self->ut_numbern('refundnum')
198 || $self->ut_numbern('custnum')
199 || $self->ut_money('refund')
200 || $self->ut_text('reason')
201 || $self->ut_numbern('_date')
202 || $self->ut_textn('paybatch')
203 || $self->ut_enum('closed', [ '', 'Y' ])
205 return $error if $error;
207 return "refund must be > 0 " if $self->refund <= 0;
209 $self->_date(time) unless $self->_date;
211 return "unknown cust_main.custnum: ". $self->custnum
212 unless $self->crednum
213 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
215 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|CASH|WEST|MCRD)$/
216 or return "Illegal payby";
219 #false laziness with cust_pay::check
220 if ( $self->payby eq 'CARD' ) {
221 my $payinfo = $self->payinfo;
223 $self->payinfo($payinfo);
224 if ( $self->payinfo ) {
225 $self->payinfo =~ /^(\d{13,16})$/
226 or return "Illegal (mistyped?) credit card number (payinfo)";
228 validate($self->payinfo) or return "Illegal credit card number";
229 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
231 $self->payinfo('N/A');
235 $error = $self->ut_textn('payinfo');
236 return $error if $error;
239 $self->otaker(getotaker);
244 =item cust_credit_refund
246 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
251 sub cust_credit_refund {
253 sort { $a->_date <=> $b->_date }
254 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
258 =item cust_pay_refund
260 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
265 sub cust_pay_refund {
267 sort { $a->_date <=> $b->_date }
268 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
274 Returns the amount of this refund that is still unapplied; which is
275 amount minus all credit applications (see L<FS::cust_credit_refund>) and
276 payment applications (see L<FS::cust_pay_refund>).
282 my $amount = $self->refund;
283 $amount -= $_->amount foreach ( $self->cust_credit_refund );
284 $amount -= $_->amount foreach ( $self->cust_pay_refund );
285 sprintf("%.2f", $amount );
292 Returns a "masked" payinfo field with all but the last four characters replaced
293 by 'x'es. Useful for displaying credit cards.
300 my $payinfo = $self->payinfo;
301 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
309 Delete and replace methods. payinfo_masked false laziness with cust_main.pm
314 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.