1 package FS::cust_refund;
5 use Business::CreditCard;
6 use FS::Record qw( qsearchs dbh );
7 use FS::UID qw(getotaker);
9 use FS::cust_credit_refund;
12 @ISA = qw( FS::Record );
16 FS::cust_refund - Object method for cust_refund objects
22 $record = new FS::cust_refund \%hash;
23 $record = new FS::cust_refund { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::cust_refund represents a refund: the transfer of money to a customer;
36 equivalent to a negative payment (see L<FS::cust_pay>). FS::cust_refund
37 inherits from FS::Record. The following fields are currently supported:
41 =item refundnum - primary key (assigned automatically for new refunds)
43 =item custnum - customer (see L<FS::cust_main>)
45 =item refund - Amount of the refund
47 =item reason - Reason for the refund
49 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
50 L<Time::Local> and L<Date::Parse> for conversion functions.
52 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
53 `LECB' (Phone bill billing), `BILL' (billing), or `COMP' (free)
55 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
57 =item paybatch - text field for tracking card processing
59 =item otaker - order taker (assigned automatically, see L<FS::UID>)
61 =item closed - books closed flag, empty or `Y'
71 Creates a new refund. To add the refund to the database, see L<"insert">.
75 sub table { 'cust_refund'; }
79 Adds this refund to the database.
81 For backwards-compatibility and convenience, if the additional field crednum is
82 defined, an FS::cust_credit_refund record for the full amount of the refund
83 will be created. In this case, custnum is optional.
90 local $SIG{HUP} = 'IGNORE';
91 local $SIG{INT} = 'IGNORE';
92 local $SIG{QUIT} = 'IGNORE';
93 local $SIG{TERM} = 'IGNORE';
94 local $SIG{TSTP} = 'IGNORE';
95 local $SIG{PIPE} = 'IGNORE';
97 my $oldAutoCommit = $FS::UID::AutoCommit;
98 local $FS::UID::AutoCommit = 0;
101 if ( $self->crednum ) {
102 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
104 $dbh->rollback if $oldAutoCommit;
105 return "Unknown cust_credit.crednum: ". $self->crednum;
107 $self->custnum($cust_credit->custnum);
110 my $error = $self->check;
111 return $error if $error;
113 $error = $self->SUPER::insert;
115 $dbh->rollback if $oldAutoCommit;
119 if ( $self->crednum ) {
120 my $cust_credit_refund = new FS::cust_credit_refund {
121 'crednum' => $self->crednum,
122 'refundnum' => $self->refundnum,
123 'amount' => $self->refund,
124 '_date' => $self->_date,
126 $error = $cust_credit_refund->insert;
128 $dbh->rollback if $oldAutoCommit;
131 #$self->custnum($cust_credit_refund->cust_credit->custnum);
135 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
141 sub upgrade_replace { #1.3.x->1.4.x
144 local $SIG{HUP} = 'IGNORE';
145 local $SIG{INT} = 'IGNORE';
146 local $SIG{QUIT} = 'IGNORE';
147 local $SIG{TERM} = 'IGNORE';
148 local $SIG{TSTP} = 'IGNORE';
149 local $SIG{PIPE} = 'IGNORE';
151 my $oldAutoCommit = $FS::UID::AutoCommit;
152 local $FS::UID::AutoCommit = 0;
155 my $error = $self->check;
156 return $error if $error;
158 my %new = $self->hash;
159 my $new = FS::cust_refund->new(\%new);
161 if ( $self->crednum ) {
162 my $cust_credit_refund = new FS::cust_credit_refund {
163 'crednum' => $self->crednum,
164 'refundnum' => $self->refundnum,
165 'amount' => $self->refund,
166 '_date' => $self->_date,
168 $error = $cust_credit_refund->insert;
170 $dbh->rollback if $oldAutoCommit;
173 $new->custnum($cust_credit_refund->cust_credit->custnum);
178 $error = $new->SUPER::replace($self);
180 $dbh->rollback if $oldAutoCommit;
184 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
192 Currently unimplemented (accounting reasons).
198 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
199 $self->SUPER::delete(@_);
202 =item replace OLD_RECORD
204 Currently unimplemented (accounting reasons).
209 return "Can't (yet?) modify cust_refund records!";
214 Checks all fields to make sure this is a valid refund. If there is an error,
215 returns the error, otherwise returns false. Called by the insert method.
223 $self->ut_numbern('refundnum')
224 || $self->ut_numbern('custnum')
225 || $self->ut_money('refund')
226 || $self->ut_text('reason')
227 || $self->ut_numbern('_date')
228 || $self->ut_textn('paybatch')
229 || $self->ut_enum('closed', [ '', 'Y' ])
231 return $error if $error;
233 return "refund must be > 0 " if $self->refund <= 0;
235 $self->_date(time) unless $self->_date;
237 return "unknown cust_main.custnum: ". $self->custnum
238 unless $self->crednum
239 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
241 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
244 #false laziness with cust_pay::check
245 if ( $self->payby eq 'CARD' ) {
246 my $payinfo = $self->payinfo;
248 $self->payinfo($payinfo);
249 if ( $self->payinfo ) {
250 $self->payinfo =~ /^(\d{13,16})$/
251 or return "Illegal (mistyped?) credit card number (payinfo)";
253 validate($self->payinfo) or return "Illegal credit card number";
254 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
256 $self->payinfo('N/A');
260 $error = $self->ut_textn('payinfo');
261 return $error if $error;
264 $self->otaker(getotaker);
273 Delete and replace methods.
277 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.