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::UID qw(getotaker);
9 use FS::Record qw( qsearch qsearchs dbh );
12 use FS::cust_credit_refund;
13 use FS::cust_pay_refund;
18 $me = '[ FS::cust_refund ]';
21 $ignore_empty_reasonnum = 0;
23 @encrypted_fields = ('payinfo');
24 sub nohistory_fields { ('payinfo'); }
28 FS::cust_refund - Object method for cust_refund objects
34 $record = new FS::cust_refund \%hash;
35 $record = new FS::cust_refund { 'column' => 'value' };
37 $error = $record->insert;
39 $error = $new_record->replace($old_record);
41 $error = $record->delete;
43 $error = $record->check;
47 An FS::cust_refund represents a refund: the transfer of money to a customer;
48 equivalent to a negative payment (see L<FS::cust_pay>). FS::cust_refund
49 inherits from FS::Record. The following fields are currently supported:
55 primary key (assigned automatically for new refunds)
59 customer (see L<FS::cust_main>)
67 Text stating the reason for the refund ( deprecated )
71 Reason (see L<FS::reason>)
75 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
76 L<Time::Local> and L<Date::Parse> for conversion functions.
80 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
84 Payment Information (See L<FS::payinfo_Mixin> for data format)
88 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
92 text field for tracking card processing
96 order taker (see L<FS::access_user>
100 books closed flag, empty or `Y'
102 =item gatewaynum, processor, auth, order_number
104 Same as for L<FS::cust_pay>, but specifically the result of realtime
105 authorization of the refund.
115 Creates a new refund. To add the refund to the database, see L<"insert">.
119 sub table { 'cust_refund'; }
123 Adds this refund to the database.
125 For backwards-compatibility and convenience, if the additional field crednum is
126 defined, an FS::cust_credit_refund record for the full amount of the refund
127 will be created. Or (this time for convenience and consistancy), if the
128 additional field paynum is defined, an FS::cust_pay_refund record for the full
129 amount of the refund will be created. In both cases, custnum is optional.
134 my ($self, %options) = @_;
136 local $SIG{HUP} = 'IGNORE';
137 local $SIG{INT} = 'IGNORE';
138 local $SIG{QUIT} = 'IGNORE';
139 local $SIG{TERM} = 'IGNORE';
140 local $SIG{TSTP} = 'IGNORE';
141 local $SIG{PIPE} = 'IGNORE';
143 my $oldAutoCommit = $FS::UID::AutoCommit;
144 local $FS::UID::AutoCommit = 0;
147 unless ($self->reasonnum) {
149 if ( $self->get('reason') ) {
150 my $reason = FS::reason->new_or_existing(
151 reason => $self->get('reason'),
153 type => 'Refund reason',
156 return "failed to add refund reason: $@";
158 $self->set('reasonnum', $reason->get('reasonnum'));
159 $self->set('reason', '');
163 if ( $self->crednum ) {
164 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
166 $dbh->rollback if $oldAutoCommit;
167 return "Unknown cust_credit.crednum: ". $self->crednum;
169 $self->custnum($cust_credit->custnum);
170 } elsif ( $self->paynum ) {
171 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
173 $dbh->rollback if $oldAutoCommit;
174 return "Unknown cust_pay.paynum: ". $self->paynum;
176 $self->custnum($cust_pay->custnum);
179 my $error = $self->check;
180 return $error if $error;
182 $error = $self->SUPER::insert;
184 $dbh->rollback if $oldAutoCommit;
188 if ( $self->crednum ) {
189 my $cust_credit_refund = new FS::cust_credit_refund {
190 'crednum' => $self->crednum,
191 'refundnum' => $self->refundnum,
192 'amount' => $self->refund,
193 '_date' => $self->_date,
195 $error = $cust_credit_refund->insert;
197 $dbh->rollback if $oldAutoCommit;
200 #$self->custnum($cust_credit_refund->cust_credit->custnum);
201 } elsif ( $self->paynum ) {
202 my $cust_pay_refund = new FS::cust_pay_refund {
203 'paynum' => $self->paynum,
204 'refundnum' => $self->refundnum,
205 'amount' => $self->refund,
206 '_date' => $self->_date,
208 $error = $cust_pay_refund->insert;
210 $dbh->rollback if $oldAutoCommit;
216 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
224 Unless the closed flag is set, deletes this refund and all associated
225 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
231 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
233 local $SIG{HUP} = 'IGNORE';
234 local $SIG{INT} = 'IGNORE';
235 local $SIG{QUIT} = 'IGNORE';
236 local $SIG{TERM} = 'IGNORE';
237 local $SIG{TSTP} = 'IGNORE';
238 local $SIG{PIPE} = 'IGNORE';
240 my $oldAutoCommit = $FS::UID::AutoCommit;
241 local $FS::UID::AutoCommit = 0;
244 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
245 my $error = $cust_credit_refund->delete;
247 $dbh->rollback if $oldAutoCommit;
252 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
253 my $error = $cust_pay_refund->delete;
255 $dbh->rollback if $oldAutoCommit;
260 my $error = $self->SUPER::delete(@_);
262 $dbh->rollback if $oldAutoCommit;
266 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
272 =item replace OLD_RECORD
274 You can, but probably shouldn't modify refunds...
276 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
277 supplied, replaces this record. If there is an error, returns the error,
278 otherwise returns false.
284 return "Can't modify closed refund" if $self->closed =~ /^Y/i;
285 $self->SUPER::replace(@_);
290 Checks all fields to make sure this is a valid refund. If there is an error,
291 returns the error, otherwise returns false. Called by the insert method.
298 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
301 $self->ut_numbern('refundnum')
302 || $self->ut_numbern('custnum')
303 || $self->ut_money('refund')
304 || $self->ut_alphan('otaker')
305 || $self->ut_textn('reason')
306 || $self->ut_numbern('_date')
307 || $self->ut_textn('paybatch')
308 || $self->ut_enum('closed', [ '', 'Y' ])
309 || $self->ut_foreign_keyn('source_paynum', 'cust_pay', 'paynum')
311 return $error if $error;
313 my $method = $ignore_empty_reasonnum ? 'ut_foreign_keyn' : 'ut_foreign_key';
314 $error = $self->$method('reasonnum', 'reason', 'reasonnum');
315 return $error if $error;
317 return "refund must be > 0 " if $self->refund <= 0;
319 $self->_date(time) unless $self->_date;
321 return "unknown cust_main.custnum: ". $self->custnum
322 unless $self->crednum
323 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
325 $error = $self->payinfo_check;
326 return $error if $error;
331 =item cust_credit_refund
333 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
338 sub cust_credit_refund {
340 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
341 sort { $a->_date <=> $b->_date }
342 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
346 =item cust_pay_refund
348 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
353 sub cust_pay_refund {
355 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
356 sort { $a->_date <=> $b->_date }
357 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
363 Returns the amount of this refund that is still unapplied; which is
364 amount minus all credit applications (see L<FS::cust_credit_refund>) and
365 payment applications (see L<FS::cust_pay_refund>).
371 my $amount = $self->refund;
372 $amount -= $_->amount foreach ( $self->cust_credit_refund );
373 $amount -= $_->amount foreach ( $self->cust_pay_refund );
374 sprintf("%.2f", $amount );
377 =item send_receipt HASHREF | OPTION => VALUE ...
379 Sends a payment receipt for this payment.
381 refund_receipt_msgnum must be configured.
389 Customer (FS::cust_main) object (for efficiency).
399 my $opt = ref($_[0]) ? shift : { @_ };
401 my $cust_main = $opt->{'cust_main'} || $self->cust_main;
403 my $conf = new FS::Conf;
405 my $msgnum = $conf->config('refund_receipt_msgnum', $cust_main->agentnum);
406 return "No refund_receipt_msgnum configured" unless $msgnum;
408 my $msg_template = qsearchs('msg_template',{ msgnum => $msgnum});
409 return "Could not load template"
410 unless $msg_template;
412 my $queue = new FS::queue {
413 'job' => 'FS::Misc::process_send_email',
414 'custnum' => $cust_main->custnum,
416 my $error = $queue->insert(
417 FS::msg_template->by_key($msgnum)->prepare(
418 'cust_main' => $cust_main,
421 'msgtype' => 'receipt', # override msg_template's default
435 Returns an SQL fragment to retreive the unapplied amount.
440 my ($class, $start, $end) = @_;
441 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
442 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
443 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
444 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
448 ( SELECT SUM(amount) FROM cust_credit_refund
449 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
450 $credit_start $credit_end )
454 ( SELECT SUM(amount) FROM cust_pay_refund
455 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
456 $pay_start $pay_end )
465 Returns the text of the associated reason (see L<FS::reason>) for this credit.
470 my ($self, $value, %options) = @_;
473 my $typenum = $options{'reason_type'};
475 my $oldAutoCommit = $FS::UID::AutoCommit; # this should already be in
476 local $FS::UID::AutoCommit = 0; # a transaction if it matters
478 if ( defined( $value ) ) {
479 my $hashref = { 'reason' => $value };
480 $hashref->{'reason_type'} = $typenum if $typenum;
481 my $addl_from = "LEFT JOIN reason_type ON ( reason_type = typenum ) ";
482 my $extra_sql = " AND reason_type.class='F'";
484 $reason = qsearchs( { 'table' => 'reason',
485 'hashref' => $hashref,
486 'addl_from' => $addl_from,
487 'extra_sql' => $extra_sql,
490 if (!$reason && $typenum) {
491 $reason = new FS::reason( { 'reason_type' => $typenum,
495 my $error = $reason->insert;
497 warn "error inserting reason: $error\n";
502 $self->reasonnum($reason ? $reason->reasonnum : '') ;
503 warn "$me reason used in set mode with non-existant reason -- clearing"
506 $reason = qsearchs( 'reason', { 'reasonnum' => $self->reasonnum } );
508 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
510 ( $reason ? $reason->reason : '' ).
511 ( $self->addlinfo ? ' '.$self->addlinfo : '' );
514 # Used by FS::Upgrade to migrate to a new database.
515 sub _upgrade_data { # class method
516 my ($class, %opts) = @_;
517 $class->_upgrade_reasonnum(%opts);
518 $class->_upgrade_otaker(%opts);
525 Delete and replace methods.
529 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.