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), or `COMP' (free)
56 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
58 =item paybatch - text field for tracking card processing
60 =item otaker - order taker (assigned automatically, see L<FS::UID>)
62 =item closed - books closed flag, empty or `Y'
72 Creates a new refund. To add the refund to the database, see L<"insert">.
76 sub table { 'cust_refund'; }
80 Adds this refund to the database.
82 For backwards-compatibility and convenience, if the additional field crednum is
83 defined, an FS::cust_credit_refund record for the full amount of the refund
84 will be created. Or (this time for convenience and consistancy), if the
85 additional field paynum is defined, an FS::cust_pay_refund record for the full
86 amount of the refund will be created. In both cases, 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->crednum ) {
105 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
107 $dbh->rollback if $oldAutoCommit;
108 return "Unknown cust_credit.crednum: ". $self->crednum;
110 $self->custnum($cust_credit->custnum);
111 } elsif ( $self->paynum ) {
112 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
114 $dbh->rollback if $oldAutoCommit;
115 return "Unknown cust_pay.paynum: ". $self->paynum;
117 $self->custnum($cust_pay->custnum);
120 my $error = $self->check;
121 return $error if $error;
123 $error = $self->SUPER::insert;
125 $dbh->rollback if $oldAutoCommit;
129 if ( $self->crednum ) {
130 my $cust_credit_refund = new FS::cust_credit_refund {
131 'crednum' => $self->crednum,
132 'refundnum' => $self->refundnum,
133 'amount' => $self->refund,
134 '_date' => $self->_date,
136 $error = $cust_credit_refund->insert;
138 $dbh->rollback if $oldAutoCommit;
141 #$self->custnum($cust_credit_refund->cust_credit->custnum);
142 } elsif ( $self->paynum ) {
143 my $cust_pay_refund = new FS::cust_pay_refund {
144 'paynum' => $self->paynum,
145 'refundnum' => $self->refundnum,
146 'amount' => $self->refund,
147 '_date' => $self->_date,
149 $error = $cust_pay_refund->insert;
151 $dbh->rollback if $oldAutoCommit;
157 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
165 Currently unimplemented (accounting reasons).
171 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
172 $self->SUPER::delete(@_);
175 =item replace OLD_RECORD
177 Currently unimplemented (accounting reasons).
182 return "Can't (yet?) modify cust_refund records!";
187 Checks all fields to make sure this is a valid refund. If there is an error,
188 returns the error, otherwise returns false. Called by the insert method.
196 $self->ut_numbern('refundnum')
197 || $self->ut_numbern('custnum')
198 || $self->ut_money('refund')
199 || $self->ut_text('reason')
200 || $self->ut_numbern('_date')
201 || $self->ut_textn('paybatch')
202 || $self->ut_enum('closed', [ '', 'Y' ])
204 return $error if $error;
206 return "refund must be > 0 " if $self->refund <= 0;
208 $self->_date(time) unless $self->_date;
210 return "unknown cust_main.custnum: ". $self->custnum
211 unless $self->crednum
212 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
214 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
217 #false laziness with cust_pay::check
218 if ( $self->payby eq 'CARD' ) {
219 my $payinfo = $self->payinfo;
221 $self->payinfo($payinfo);
222 if ( $self->payinfo ) {
223 $self->payinfo =~ /^(\d{13,16})$/
224 or return "Illegal (mistyped?) credit card number (payinfo)";
226 validate($self->payinfo) or return "Illegal credit card number";
227 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
229 $self->payinfo('N/A');
233 $error = $self->ut_textn('payinfo');
234 return $error if $error;
237 $self->otaker(getotaker);
242 =item cust_credit_refund
244 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
249 sub cust_credit_refund {
251 sort { $a->_date <=> $b->_date }
252 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
256 =item cust_pay_refund
258 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
263 sub cust_pay_refund {
265 sort { $a->_date <=> $b->_date }
266 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
272 Returns the amount of this refund that is still unapplied; which is
273 amount minus all credit applications (see L<FS::cust_credit_refund>) and
274 payment applications (see L<FS::cust_pay_refund>).
280 my $amount = $self->refund;
281 $amount -= $_->amount foreach ( $self->cust_credit_refund );
282 $amount -= $_->amount foreach ( $self->cust_pay_refund );
283 sprintf("%.2f", $amount );
290 Returns a "masked" payinfo field with all but the last four characters replaced
291 by 'x'es. Useful for displaying credit cards.
298 my $payinfo = $self->payinfo;
299 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
307 Delete and replace methods. payinfo_masked false laziness with cust_main.pm
312 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.