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 _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
48 L<Time::Local> and L<Date::Parse> for conversion functions.
50 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
51 `LECB' (Phone bill billing), `BILL' (billing), or `COMP' (free)
53 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
55 =item paybatch - text field for tracking card processing
57 =item otaker - order taker (assigned automatically, see L<FS::UID>)
59 =item closed - books closed flag, empty or `Y'
69 Creates a new refund. To add the refund to the database, see L<"insert">.
73 sub table { 'cust_refund'; }
77 Adds this refund to the database.
79 For backwards-compatibility and convenience, if the additional field crednum is
80 defined, an FS::cust_credit_refund record for the full amount of the refund
81 will be created. In this case, custnum is optional.
88 local $SIG{HUP} = 'IGNORE';
89 local $SIG{INT} = 'IGNORE';
90 local $SIG{QUIT} = 'IGNORE';
91 local $SIG{TERM} = 'IGNORE';
92 local $SIG{TSTP} = 'IGNORE';
93 local $SIG{PIPE} = 'IGNORE';
95 my $oldAutoCommit = $FS::UID::AutoCommit;
96 local $FS::UID::AutoCommit = 0;
99 if ( $self->crednum ) {
100 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
102 $dbh->rollback if $oldAutoCommit;
103 return "Unknown cust_credit.crednum: ". $self->crednum;
105 $self->custnum($cust_credit->custnum);
108 my $error = $self->check;
109 return $error if $error;
111 $error = $self->SUPER::insert;
113 $dbh->rollback if $oldAutoCommit;
117 if ( $self->crednum ) {
118 my $cust_credit_refund = new FS::cust_credit_refund {
119 'crednum' => $self->crednum,
120 'refundnum' => $self->refundnum,
121 'amount' => $self->refund,
122 '_date' => $self->_date,
124 $error = $cust_credit_refund->insert;
126 $dbh->rollback if $oldAutoCommit;
129 #$self->custnum($cust_credit_refund->cust_credit->custnum);
133 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
139 sub upgrade_replace { #1.3.x->1.4.x
142 local $SIG{HUP} = 'IGNORE';
143 local $SIG{INT} = 'IGNORE';
144 local $SIG{QUIT} = 'IGNORE';
145 local $SIG{TERM} = 'IGNORE';
146 local $SIG{TSTP} = 'IGNORE';
147 local $SIG{PIPE} = 'IGNORE';
149 my $oldAutoCommit = $FS::UID::AutoCommit;
150 local $FS::UID::AutoCommit = 0;
153 my $error = $self->check;
154 return $error if $error;
156 my %new = $self->hash;
157 my $new = FS::cust_refund->new(\%new);
159 if ( $self->crednum ) {
160 my $cust_credit_refund = new FS::cust_credit_refund {
161 'crednum' => $self->crednum,
162 'refundnum' => $self->refundnum,
163 'amount' => $self->refund,
164 '_date' => $self->_date,
166 $error = $cust_credit_refund->insert;
168 $dbh->rollback if $oldAutoCommit;
171 $new->custnum($cust_credit_refund->cust_credit->custnum);
176 $error = $new->SUPER::replace($self);
178 $dbh->rollback if $oldAutoCommit;
182 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
190 Currently unimplemented (accounting reasons).
196 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
197 $self->SUPER::delete(@_);
200 =item replace OLD_RECORD
202 Currently unimplemented (accounting reasons).
207 return "Can't (yet?) modify cust_refund records!";
212 Checks all fields to make sure this is a valid refund. If there is an error,
213 returns the error, otherwise returns false. Called by the insert method.
221 $self->ut_numbern('refundnum')
222 || $self->ut_numbern('custnum')
223 || $self->ut_money('refund')
224 || $self->ut_numbern('_date')
225 || $self->ut_textn('paybatch')
226 || $self->ut_enum('closed', [ '', 'Y' ])
228 return $error if $error;
230 return "refund must be > 0 " if $self->refund <= 0;
232 $self->_date(time) unless $self->_date;
234 return "unknown cust_main.custnum: ". $self->custnum
235 unless $self->crednum
236 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
238 $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
241 #false laziness with cust_pay::check
242 if ( $self->payby eq 'CARD' ) {
243 my $payinfo = $self->payinfo;
245 $self->payinfo($payinfo);
246 if ( $self->payinfo ) {
247 $self->payinfo =~ /^(\d{13,16})$/
248 or return "Illegal (mistyped?) credit card number (payinfo)";
250 validate($self->payinfo) or return "Illegal credit card number";
251 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
253 $self->payinfo('N/A');
257 $error = $self->ut_textn('payinfo');
258 return $error if $error;
261 $self->otaker(getotaker);
270 $Id: cust_refund.pm,v 1.18.4.2 2002-11-19 09:52:02 ivan Exp $
274 Delete and replace methods.
278 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.