1 package FS::cust_refund;
4 use vars qw( @ISA @encrypted_fields );
5 use Business::CreditCard;
6 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::UID qw(getotaker);
9 use FS::cust_credit_refund;
10 use FS::cust_pay_refund;
12 use FS::payinfo_Mixin;
14 @ISA = qw( FS::Record FS::payinfo_Mixin );
16 @encrypted_fields = ('payinfo');
20 FS::cust_refund - Object method for cust_refund objects
26 $record = new FS::cust_refund \%hash;
27 $record = new FS::cust_refund { 'column' => 'value' };
29 $error = $record->insert;
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
39 An FS::cust_refund represents a refund: the transfer of money to a customer;
40 equivalent to a negative payment (see L<FS::cust_pay>). FS::cust_refund
41 inherits from FS::Record. The following fields are currently supported:
45 =item refundnum - primary key (assigned automatically for new refunds)
47 =item custnum - customer (see L<FS::cust_main>)
49 =item refund - Amount of the refund
51 =item reason - Reason for the refund
53 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
54 L<Time::Local> and L<Date::Parse> for conversion functions.
56 =item payby - Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
58 =item payinfo - Payment Information (See L<FS::payinfo_Mixin> for data format)
60 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
62 =item paybatch - text field for tracking card processing
64 =item otaker - order taker (assigned automatically, see L<FS::UID>)
66 =item closed - books closed flag, empty or `Y'
76 Creates a new refund. To add the refund to the database, see L<"insert">.
80 sub table { 'cust_refund'; }
84 Adds this refund to the database.
86 For backwards-compatibility and convenience, if the additional field crednum is
87 defined, an FS::cust_credit_refund record for the full amount of the refund
88 will be created. Or (this time for convenience and consistancy), if the
89 additional field paynum is defined, an FS::cust_pay_refund record for the full
90 amount of the refund will be created. In both cases, custnum is optional.
97 local $SIG{HUP} = 'IGNORE';
98 local $SIG{INT} = 'IGNORE';
99 local $SIG{QUIT} = 'IGNORE';
100 local $SIG{TERM} = 'IGNORE';
101 local $SIG{TSTP} = 'IGNORE';
102 local $SIG{PIPE} = 'IGNORE';
104 my $oldAutoCommit = $FS::UID::AutoCommit;
105 local $FS::UID::AutoCommit = 0;
108 if ( $self->crednum ) {
109 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
111 $dbh->rollback if $oldAutoCommit;
112 return "Unknown cust_credit.crednum: ". $self->crednum;
114 $self->custnum($cust_credit->custnum);
115 } elsif ( $self->paynum ) {
116 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
118 $dbh->rollback if $oldAutoCommit;
119 return "Unknown cust_pay.paynum: ". $self->paynum;
121 $self->custnum($cust_pay->custnum);
124 my $error = $self->check;
125 return $error if $error;
127 $error = $self->SUPER::insert;
129 $dbh->rollback if $oldAutoCommit;
133 if ( $self->crednum ) {
134 my $cust_credit_refund = new FS::cust_credit_refund {
135 'crednum' => $self->crednum,
136 'refundnum' => $self->refundnum,
137 'amount' => $self->refund,
138 '_date' => $self->_date,
140 $error = $cust_credit_refund->insert;
142 $dbh->rollback if $oldAutoCommit;
145 #$self->custnum($cust_credit_refund->cust_credit->custnum);
146 } elsif ( $self->paynum ) {
147 my $cust_pay_refund = new FS::cust_pay_refund {
148 'paynum' => $self->paynum,
149 'refundnum' => $self->refundnum,
150 'amount' => $self->refund,
151 '_date' => $self->_date,
153 $error = $cust_pay_refund->insert;
155 $dbh->rollback if $oldAutoCommit;
161 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
169 Unless the closed flag is set, deletes this refund and all associated
170 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
176 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
178 local $SIG{HUP} = 'IGNORE';
179 local $SIG{INT} = 'IGNORE';
180 local $SIG{QUIT} = 'IGNORE';
181 local $SIG{TERM} = 'IGNORE';
182 local $SIG{TSTP} = 'IGNORE';
183 local $SIG{PIPE} = 'IGNORE';
185 my $oldAutoCommit = $FS::UID::AutoCommit;
186 local $FS::UID::AutoCommit = 0;
189 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
190 my $error = $cust_credit_refund->delete;
192 $dbh->rollback if $oldAutoCommit;
197 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
198 my $error = $cust_pay_refund->delete;
200 $dbh->rollback if $oldAutoCommit;
205 my $error = $self->SUPER::delete(@_);
207 $dbh->rollback if $oldAutoCommit;
211 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
217 =item replace OLD_RECORD
219 Currently unimplemented (accounting reasons).
225 $self->SUPER::replace(@_);
230 Checks all fields to make sure this is a valid refund. If there is an error,
231 returns the error, otherwise returns false. Called by the insert method.
238 $self->otaker(getotaker) unless ($self->otaker);
241 $self->ut_numbern('refundnum')
242 || $self->ut_numbern('custnum')
243 || $self->ut_money('refund')
244 || $self->ut_alpha('otaker')
245 || $self->ut_text('reason')
246 || $self->ut_numbern('_date')
247 || $self->ut_textn('paybatch')
248 || $self->ut_enum('closed', [ '', 'Y' ])
250 return $error if $error;
252 return "refund must be > 0 " if $self->refund <= 0;
254 $self->_date(time) unless $self->_date;
256 return "unknown cust_main.custnum: ". $self->custnum
257 unless $self->crednum
258 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
260 $error = $self->payinfo_check;
261 return $error if $error;
266 =item cust_credit_refund
268 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
273 sub cust_credit_refund {
275 sort { $a->_date <=> $b->_date }
276 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
280 =item cust_pay_refund
282 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
287 sub cust_pay_refund {
289 sort { $a->_date <=> $b->_date }
290 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
296 Returns the amount of this refund that is still unapplied; which is
297 amount minus all credit applications (see L<FS::cust_credit_refund>) and
298 payment applications (see L<FS::cust_pay_refund>).
304 my $amount = $self->refund;
305 $amount -= $_->amount foreach ( $self->cust_credit_refund );
306 $amount -= $_->amount foreach ( $self->cust_pay_refund );
307 sprintf("%.2f", $amount );
318 Returns an SQL fragment to retreive the unapplied amount.
327 ( SELECT SUM(amount) FROM cust_credit_refund
328 WHERE cust_refund.refundnum = cust_credit_refund.refundnum )
332 ( SELECT SUM(amount) FROM cust_pay_refund
333 WHERE cust_refund.refundnum = cust_pay_refund.refundnum )
344 Delete and replace methods.
348 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.