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), `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>)
58 =item closed - books closed flag, empty or `Y'
68 Creates a new refund. To add the refund to the database, see L<"insert">.
72 sub table { 'cust_refund'; }
76 Adds this refund to the database.
78 For backwards-compatibility and convenience, if the additional field crednum is
79 defined, an FS::cust_credit_refund record for the full amount of the refund
80 will be created. In this case, custnum is optional.
87 local $SIG{HUP} = 'IGNORE';
88 local $SIG{INT} = 'IGNORE';
89 local $SIG{QUIT} = 'IGNORE';
90 local $SIG{TERM} = 'IGNORE';
91 local $SIG{TSTP} = 'IGNORE';
92 local $SIG{PIPE} = 'IGNORE';
94 my $oldAutoCommit = $FS::UID::AutoCommit;
95 local $FS::UID::AutoCommit = 0;
98 if ( $self->crednum ) {
99 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
101 $dbh->rollback if $oldAutoCommit;
102 return "Unknown cust_credit.crednum: ". $self->crednum;
104 $self->custnum($cust_credit->custnum);
107 my $error = $self->check;
108 return $error if $error;
110 $error = $self->SUPER::insert;
112 $dbh->rollback if $oldAutoCommit;
116 if ( $self->crednum ) {
117 my $cust_credit_refund = new FS::cust_credit_refund {
118 'crednum' => $self->crednum,
119 'refundnum' => $self->refundnum,
120 'amount' => $self->refund,
121 '_date' => $self->_date,
123 $error = $cust_credit_refund->insert;
125 $dbh->rollback if $oldAutoCommit;
128 #$self->custnum($cust_credit_refund->cust_credit->custnum);
132 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
138 sub upgrade_replace { #1.3.x->1.4.x
141 local $SIG{HUP} = 'IGNORE';
142 local $SIG{INT} = 'IGNORE';
143 local $SIG{QUIT} = 'IGNORE';
144 local $SIG{TERM} = 'IGNORE';
145 local $SIG{TSTP} = 'IGNORE';
146 local $SIG{PIPE} = 'IGNORE';
148 my $oldAutoCommit = $FS::UID::AutoCommit;
149 local $FS::UID::AutoCommit = 0;
152 my $error = $self->check;
153 return $error if $error;
155 my %new = $self->hash;
156 my $new = FS::cust_refund->new(\%new);
158 if ( $self->crednum ) {
159 my $cust_credit_refund = new FS::cust_credit_refund {
160 'crednum' => $self->crednum,
161 'refundnum' => $self->refundnum,
162 'amount' => $self->refund,
163 '_date' => $self->_date,
165 $error = $cust_credit_refund->insert;
167 $dbh->rollback if $oldAutoCommit;
170 $new->custnum($cust_credit_refund->cust_credit->custnum);
175 $error = $new->SUPER::replace($self);
177 $dbh->rollback if $oldAutoCommit;
181 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
189 Currently unimplemented (accounting reasons).
195 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
196 $self->SUPER::delete(@_);
199 =item replace OLD_RECORD
201 Currently unimplemented (accounting reasons).
206 return "Can't (yet?) modify cust_refund records!";
211 Checks all fields to make sure this is a valid refund. If there is an error,
212 returns the error, otherwise returns false. Called by the insert method.
220 $self->ut_numbern('refundnum')
221 || $self->ut_numbern('custnum')
222 || $self->ut_money('refund')
223 || $self->ut_numbern('_date')
224 || $self->ut_textn('paybatch')
225 || $self->ut_enum('closed', [ '', 'Y' ])
227 return $error if $error;
229 return "refund must be > 0 " if $self->refund <= 0;
231 $self->_date(time) unless $self->_date;
233 return "unknown cust_main.custnum: ". $self->custnum
234 unless $self->crednum
235 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
237 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
240 #false laziness with cust_pay::check
241 if ( $self->payby eq 'CARD' ) {
242 my $payinfo = $self->payinfo;
244 $self->payinfo($payinfo);
245 if ( $self->payinfo ) {
246 $self->payinfo =~ /^(\d{13,16})$/
247 or return "Illegal (mistyped?) credit card number (payinfo)";
249 validate($self->payinfo) or return "Illegal credit card number";
250 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
252 $self->payinfo('N/A');
256 $error = $self->ut_textn('payinfo');
257 return $error if $error;
260 $self->otaker(getotaker);
269 $Id: cust_refund.pm,v 1.18 2002-02-19 03:22:39 jeff Exp $
273 Delete and replace methods.
277 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.