1 package FS::cust_refund;
5 use Business::CreditCard;
6 use FS::Record qw( 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), `BILL' (billing), or `COMP' (free)
52 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
54 =item paybatch - text field for tracking card processing
56 =item otaker - order taker (assigned automatically, see L<FS::UID>)
66 Creates a new refund. To add the refund to the database, see L<"insert">.
70 sub table { 'cust_refund'; }
74 Adds this refund to the database.
76 For backwards-compatibility and convenience, if the additional field crednum is
77 defined, an FS::cust_credit_refund record for the full amount of the refund
78 will be created. In this case, custnum is optional.
85 local $SIG{HUP} = 'IGNORE';
86 local $SIG{INT} = 'IGNORE';
87 local $SIG{QUIT} = 'IGNORE';
88 local $SIG{TERM} = 'IGNORE';
89 local $SIG{TSTP} = 'IGNORE';
90 local $SIG{PIPE} = 'IGNORE';
92 my $oldAutoCommit = $FS::UID::AutoCommit;
93 local $FS::UID::AutoCommit = 0;
96 if ( $self->crednum ) {
97 my $cust_credit = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
99 $dbh->rollback if $oldAutoCommit;
100 return "Unknown cust_credit.crednum: ". $self->crednum;
102 $self->custnum($cust_credit->custnum);
105 my $error = $self->check;
106 return $error if $error;
108 $error = $self->SUPER::insert;
110 $dbh->rollback if $oldAutoCommit;
114 if ( $self->crednum ) {
115 my $cust_credit_refund = new FS::cust_credit_refund {
116 'crednum' => $self->crednum,
117 'refundnum' => $self->refundnum,
118 'amount' => $self->refund,
119 '_date' => $self->_date,
121 $error = $cust_credit_refund->insert;
123 $dbh->rollback if $oldAutoCommit;
126 #$self->custnum($cust_credit_refund->cust_credit->custnum);
130 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
136 sub upgrade_replace { #1.3.x->1.4.x
139 local $SIG{HUP} = 'IGNORE';
140 local $SIG{INT} = 'IGNORE';
141 local $SIG{QUIT} = 'IGNORE';
142 local $SIG{TERM} = 'IGNORE';
143 local $SIG{TSTP} = 'IGNORE';
144 local $SIG{PIPE} = 'IGNORE';
146 my $oldAutoCommit = $FS::UID::AutoCommit;
147 local $FS::UID::AutoCommit = 0;
150 my $error = $self->check;
151 return $error if $error;
153 my %new = $self->hash;
154 my $new = FS::cust_refund->new(\%new);
156 if ( $self->crednum ) {
157 my $cust_credit_refund = new FS::cust_credit_refund {
158 'crednum' => $self->crednum,
159 'refundnum' => $self->refundnum,
160 'amount' => $self->refund,
161 '_date' => $self->_date,
163 $error = $cust_credit_refund->insert;
165 $dbh->rollback if $oldAutoCommit;
168 $new->custnum($cust_credit_refund->cust_credit->custnum);
173 $error = $new->SUPER::replace($self);
175 $dbh->rollback if $oldAutoCommit;
179 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
187 Currently unimplemented (accounting reasons).
192 return "Can't (yet?) delete cust_refund records!";
195 =item replace OLD_RECORD
197 Currently unimplemented (accounting reasons).
202 return "Can't (yet?) modify cust_refund records!";
207 Checks all fields to make sure this is a valid refund. If there is an error,
208 returns the error, otherwise returns false. Called by the insert method.
216 $self->ut_number('refundnum')
217 || $self->ut_numbern('custnum')
218 || $self->ut_money('refund')
219 || $self->ut_numbern('_date')
220 || $self->ut_textn('paybatch')
222 return $error if $error;
224 return "refund must be > 0 " if $self->refund == 0;
226 $self->_date(time) unless $self->_date;
228 return "unknown cust_main.custnum: ". $self->custnum
229 unless $self->crednum
230 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
232 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
235 #false laziness with cust_pay::check
236 if ( $self->payby eq 'CARD' ) {
237 my $payinfo = $self->payinfo;
239 $self->payinfo($payinfo);
240 if ( $self->payinfo ) {
241 $self->payinfo =~ /^(\d{13,16})$/
242 or return "Illegal (mistyped?) credit card number (payinfo)";
244 validate($self->payinfo) or return "Illegal credit card number";
245 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
247 $self->payinfo('N/A');
251 $error = $self->ut_textn('payinfo');
252 return $error if $error;
255 $self->otaker(getotaker);
264 $Id: cust_refund.pm,v 1.13 2002-01-24 11:52:02 ivan Exp $
268 Delete and replace methods.
272 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.