1 package FS::cust_refund;
4 use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
5 FS::reason_Mixin FS::Record );
6 use vars qw( @encrypted_fields $me $DEBUG $ignore_empty_reasonnum );
7 use Business::CreditCard;
8 use FS::Record qw( qsearch qsearchs dbh dbdef );
11 use FS::cust_credit_refund;
12 use FS::cust_pay_refund;
17 $me = '[ FS::cust_refund ]';
20 $ignore_empty_reasonnum = 0;
22 @encrypted_fields = ('payinfo');
23 sub nohistory_fields { ('payinfo'); }
27 FS::cust_refund - Object method for cust_refund objects
33 $record = new FS::cust_refund \%hash;
34 $record = new FS::cust_refund { 'column' => 'value' };
36 $error = $record->insert;
38 $error = $new_record->replace($old_record);
40 $error = $record->delete;
42 $error = $record->check;
46 An FS::cust_refund represents a refund: the transfer of money to a customer;
47 equivalent to a negative payment (see L<FS::cust_pay>). FS::cust_refund
48 inherits from FS::Record. The following fields are currently supported:
54 primary key (assigned automatically for new refunds)
58 customer (see L<FS::cust_main>)
66 Text stating the reason for the refund ( deprecated )
70 Reason (see L<FS::reason>)
74 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
75 L<Time::Local> and L<Date::Parse> for conversion functions.
79 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
83 Payment Information (See L<FS::payinfo_Mixin> for data format)
87 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
91 text field for tracking card processing
95 order taker (see L<FS::access_user>
99 books closed flag, empty or `Y'
101 =item gatewaynum, processor, auth, order_number
103 Same as for L<FS::cust_pay>, but specifically the result of realtime
104 authorization of the refund.
114 Creates a new refund. To add the refund to the database, see L<"insert">.
118 sub table { 'cust_refund'; }
122 Adds this refund to the database.
124 For backwards-compatibility and convenience, if the additional field crednum is
125 defined, an FS::cust_credit_refund record for the full amount of the refund
126 will be created. Or (this time for convenience and consistancy), if the
127 additional field paynum is defined, an FS::cust_pay_refund record for the full
128 amount of the refund will be created. In both cases, custnum is optional.
133 my ($self, %options) = @_;
135 local $SIG{HUP} = 'IGNORE';
136 local $SIG{INT} = 'IGNORE';
137 local $SIG{QUIT} = 'IGNORE';
138 local $SIG{TERM} = 'IGNORE';
139 local $SIG{TSTP} = 'IGNORE';
140 local $SIG{PIPE} = 'IGNORE';
142 my $oldAutoCommit = $FS::UID::AutoCommit;
143 local $FS::UID::AutoCommit = 0;
146 unless ($self->reasonnum) {
147 my $result = $self->reason( $self->getfield('reason'),
148 exists($options{ 'reason_type' })
149 ? ('reason_type' => $options{ 'reason_type' })
153 $dbh->rollback if $oldAutoCommit;
154 return "failed to set reason for $me"; #: ". $dbh->errstr;
158 $self->setfield('reason', '');
160 if ( $self->crednum ) {
161 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
163 $dbh->rollback if $oldAutoCommit;
164 return "Unknown cust_credit.crednum: ". $self->crednum;
166 $self->custnum($cust_credit->custnum);
167 } elsif ( $self->paynum ) {
168 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
170 $dbh->rollback if $oldAutoCommit;
171 return "Unknown cust_pay.paynum: ". $self->paynum;
173 $self->custnum($cust_pay->custnum);
176 my $error = $self->check;
177 return $error if $error;
179 $error = $self->SUPER::insert;
181 $dbh->rollback if $oldAutoCommit;
185 if ( $self->crednum ) {
186 my $cust_credit_refund = new FS::cust_credit_refund {
187 'crednum' => $self->crednum,
188 'refundnum' => $self->refundnum,
189 'amount' => $self->refund,
190 '_date' => $self->_date,
192 $error = $cust_credit_refund->insert;
194 $dbh->rollback if $oldAutoCommit;
197 #$self->custnum($cust_credit_refund->cust_credit->custnum);
198 } elsif ( $self->paynum ) {
199 my $cust_pay_refund = new FS::cust_pay_refund {
200 'paynum' => $self->paynum,
201 'refundnum' => $self->refundnum,
202 'amount' => $self->refund,
203 '_date' => $self->_date,
205 $error = $cust_pay_refund->insert;
207 $dbh->rollback if $oldAutoCommit;
213 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
221 Unless the closed flag is set, deletes this refund and all associated
222 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
228 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
230 local $SIG{HUP} = 'IGNORE';
231 local $SIG{INT} = 'IGNORE';
232 local $SIG{QUIT} = 'IGNORE';
233 local $SIG{TERM} = 'IGNORE';
234 local $SIG{TSTP} = 'IGNORE';
235 local $SIG{PIPE} = 'IGNORE';
237 my $oldAutoCommit = $FS::UID::AutoCommit;
238 local $FS::UID::AutoCommit = 0;
241 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
242 my $error = $cust_credit_refund->delete;
244 $dbh->rollback if $oldAutoCommit;
249 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
250 my $error = $cust_pay_refund->delete;
252 $dbh->rollback if $oldAutoCommit;
257 my $error = $self->SUPER::delete(@_);
259 $dbh->rollback if $oldAutoCommit;
263 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
269 =item replace OLD_RECORD
271 You can, but probably shouldn't modify refunds...
273 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
274 supplied, replaces this record. If there is an error, returns the error,
275 otherwise returns false.
281 return "Can't modify closed refund" if $self->closed =~ /^Y/i;
282 $self->SUPER::replace(@_);
287 Checks all fields to make sure this is a valid refund. If there is an error,
288 returns the error, otherwise returns false. Called by the insert method.
295 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
298 $self->ut_numbern('refundnum')
299 || $self->ut_numbern('custnum')
300 || $self->ut_money('refund')
301 || $self->ut_alphan('otaker')
302 || $self->ut_textn('reason')
303 || $self->ut_numbern('_date')
304 || $self->ut_textn('paybatch')
305 || $self->ut_enum('closed', [ '', 'Y' ])
307 return $error if $error;
309 my $method = $ignore_empty_reasonnum ? 'ut_foreign_keyn' : 'ut_foreign_key';
310 $error = $self->$method('reasonnum', 'reason', 'reasonnum');
311 return $error if $error;
313 return "refund must be > 0 " if $self->refund <= 0;
315 $self->_date(time) unless $self->_date;
317 return "unknown cust_main.custnum: ". $self->custnum
318 unless $self->crednum
319 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
321 $error = $self->payinfo_check;
322 return $error if $error;
327 =item cust_credit_refund
329 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
334 sub cust_credit_refund {
336 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
337 sort { $a->_date <=> $b->_date }
338 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
342 =item cust_pay_refund
344 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
349 sub cust_pay_refund {
351 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
352 sort { $a->_date <=> $b->_date }
353 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
359 Returns the amount of this refund that is still unapplied; which is
360 amount minus all credit applications (see L<FS::cust_credit_refund>) and
361 payment applications (see L<FS::cust_pay_refund>).
367 my $amount = $self->refund;
368 $amount -= $_->amount foreach ( $self->cust_credit_refund );
369 $amount -= $_->amount foreach ( $self->cust_pay_refund );
370 sprintf("%.2f", $amount );
381 Returns an SQL fragment to retreive the unapplied amount.
386 my ($class, $start, $end) = @_;
387 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
388 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
389 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
390 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
394 ( SELECT SUM(amount) FROM cust_credit_refund
395 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
396 $credit_start $credit_end )
400 ( SELECT SUM(amount) FROM cust_pay_refund
401 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
402 $pay_start $pay_end )
409 # Used by FS::Upgrade to migrate to a new database.
410 sub _upgrade_data { # class method
411 my ($class, %opts) = @_;
412 $class->_upgrade_reasonnum(%opts);
413 $class->_upgrade_otaker(%opts);
420 Delete and replace methods.
424 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.