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 my $error = $self->check;
97 return $error if $error;
101 if ( $self->crednum ) {
102 my $cust_credit_refund = new FS::cust_credit_refund {
103 'crednum' => $self->crednum,
104 'refundnum' => $self->refundnum,
105 'amount' => $self->refund,
106 '_date' => $self->_date,
108 $error = $cust_credit_refund->insert;
110 $dbh->rollback if $oldAutoCommit;
113 $self->custnum($cust_credit_refund->cust_credit->custnum);
116 $error = $self->SUPER::insert;
118 $dbh->rollback if $oldAutoCommit;
122 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
128 sub upgrade_replace {
131 local $SIG{HUP} = 'IGNORE';
132 local $SIG{INT} = 'IGNORE';
133 local $SIG{QUIT} = 'IGNORE';
134 local $SIG{TERM} = 'IGNORE';
135 local $SIG{TSTP} = 'IGNORE';
136 local $SIG{PIPE} = 'IGNORE';
138 my $oldAutoCommit = $FS::UID::AutoCommit;
139 local $FS::UID::AutoCommit = 0;
142 my $error = $self->check;
143 return $error if $error;
145 my %new = $self->hash;
146 my $new = FS::cust_refund->new(\%new);
148 if ( $self->crednum ) {
149 my $cust_credit_refund = new FS::cust_credit_refund {
150 'crednum' => $self->crednum,
151 'refundnum' => $self->refundnum,
152 'amount' => $self->refund,
153 '_date' => $self->_date,
155 $error = $cust_credit_refund->insert;
157 $dbh->rollback if $oldAutoCommit;
160 $new->custnum($cust_credit_refund->cust_credit->custnum);
165 $error = $new->SUPER::insert($self);
167 $dbh->rollback if $oldAutoCommit;
171 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
179 Currently unimplemented (accounting reasons).
184 return "Can't (yet?) delete cust_refund records!";
187 =item replace OLD_RECORD
189 Currently unimplemented (accounting reasons).
194 return "Can't (yet?) modify cust_refund records!";
199 Checks all fields to make sure this is a valid refund. If there is an error,
200 returns the error, otherwise returns false. Called by the insert method.
208 $self->ut_number('refundnum')
209 || $self->ut_number('custnum')
210 || $self->ut_money('amount')
211 || $self->ut_numbern('_date')
212 || $self->ut_textn('paybatch')
214 return $error if $error;
216 $self->_date(time) unless $self->_date;
218 return "unknown cust_main.custnum: ". $self->custnum
220 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
222 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
225 if ( $self->payby eq 'CARD' ) {
226 my $payinfo = $self->payinfo;
227 $self->payinfo($payinfo =~ s/\D//g);
228 if ( $self->payinfo ) {
229 $self->payinfo =~ /^(\d{13,16})$/
230 or return "Illegal (mistyped?) credit card number (payinfo)";
232 validate($self->payinfo) or return "Illegal credit card number";
233 return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
235 $self->payinfo('N/A');
239 $error = $self->ut_textn('payinfo');
240 return $error if $error;
243 $self->otaker(getotaker);
252 $Id: cust_refund.pm,v 1.8 2001-09-02 05:38:13 ivan Exp $
256 Delete and replace methods.
260 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.