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 Detected credit card type, if appropriate; autodetected.
92 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
96 text field for tracking card processing
100 order taker (see L<FS::access_user>
104 books closed flag, empty or `Y'
106 =item gatewaynum, processor, auth, order_number
108 Same as for L<FS::cust_pay>, but specifically the result of realtime
109 authorization of the refund.
119 Creates a new refund. To add the refund to the database, see L<"insert">.
123 sub table { 'cust_refund'; }
127 Adds this refund to the database.
129 For backwards-compatibility and convenience, if the additional field crednum is
130 defined, an FS::cust_credit_refund record for the full amount of the refund
131 will be created. Or (this time for convenience and consistancy), if the
132 additional field paynum is defined, an FS::cust_pay_refund record for the full
133 amount of the refund will be created. In both cases, custnum is optional.
138 my ($self, %options) = @_;
140 local $SIG{HUP} = 'IGNORE';
141 local $SIG{INT} = 'IGNORE';
142 local $SIG{QUIT} = 'IGNORE';
143 local $SIG{TERM} = 'IGNORE';
144 local $SIG{TSTP} = 'IGNORE';
145 local $SIG{PIPE} = 'IGNORE';
147 my $oldAutoCommit = $FS::UID::AutoCommit;
148 local $FS::UID::AutoCommit = 0;
151 unless ($self->reasonnum) {
153 if ( $self->get('reason') ) {
154 my $reason = FS::reason->new_or_existing(
155 reason => $self->get('reason'),
157 type => 'Refund reason',
160 return "failed to add refund reason: $@";
162 $self->set('reasonnum', $reason->get('reasonnum'));
163 $self->set('reason', '');
167 if ( $self->crednum ) {
168 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
170 $dbh->rollback if $oldAutoCommit;
171 return "Unknown cust_credit.crednum: ". $self->crednum;
173 $self->custnum($cust_credit->custnum);
174 } elsif ( $self->paynum ) {
175 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
177 $dbh->rollback if $oldAutoCommit;
178 return "Unknown cust_pay.paynum: ". $self->paynum;
180 $self->custnum($cust_pay->custnum);
183 my $error = $self->check;
184 return $error if $error;
186 $error = $self->SUPER::insert;
188 $dbh->rollback if $oldAutoCommit;
192 if ( $self->crednum ) {
193 my $cust_credit_refund = new FS::cust_credit_refund {
194 'crednum' => $self->crednum,
195 'refundnum' => $self->refundnum,
196 'amount' => $self->refund,
197 '_date' => $self->_date,
199 $error = $cust_credit_refund->insert;
201 $dbh->rollback if $oldAutoCommit;
204 #$self->custnum($cust_credit_refund->cust_credit->custnum);
205 } elsif ( $self->paynum ) {
206 my $cust_pay_refund = new FS::cust_pay_refund {
207 'paynum' => $self->paynum,
208 'refundnum' => $self->refundnum,
209 'amount' => $self->refund,
210 '_date' => $self->_date,
212 $error = $cust_pay_refund->insert;
214 $dbh->rollback if $oldAutoCommit;
220 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
228 Unless the closed flag is set, deletes this refund and all associated
229 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
235 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
237 local $SIG{HUP} = 'IGNORE';
238 local $SIG{INT} = 'IGNORE';
239 local $SIG{QUIT} = 'IGNORE';
240 local $SIG{TERM} = 'IGNORE';
241 local $SIG{TSTP} = 'IGNORE';
242 local $SIG{PIPE} = 'IGNORE';
244 my $oldAutoCommit = $FS::UID::AutoCommit;
245 local $FS::UID::AutoCommit = 0;
248 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
249 my $error = $cust_credit_refund->delete;
251 $dbh->rollback if $oldAutoCommit;
256 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
257 my $error = $cust_pay_refund->delete;
259 $dbh->rollback if $oldAutoCommit;
264 my $error = $self->SUPER::delete(@_);
266 $dbh->rollback if $oldAutoCommit;
270 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
276 =item replace OLD_RECORD
278 You can, but probably shouldn't modify refunds...
280 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
281 supplied, replaces this record. If there is an error, returns the error,
282 otherwise returns false.
288 return "Can't modify closed refund"
289 if $self->closed =~ /^Y/i && !$FS::payinfo_Mixin::allow_closed_replace;
290 $self->SUPER::replace(@_);
295 Checks all fields to make sure this is a valid refund. If there is an error,
296 returns the error, otherwise returns false. Called by the insert method.
303 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
306 $self->ut_numbern('refundnum')
307 || $self->ut_numbern('custnum')
308 || $self->ut_money('refund')
309 || $self->ut_alphan('otaker')
310 || $self->ut_textn('reason')
311 || $self->ut_numbern('_date')
312 || $self->ut_textn('paybatch')
313 || $self->ut_enum('closed', [ '', 'Y' ])
314 || $self->ut_foreign_keyn('source_paynum', 'cust_pay', 'paynum')
316 return $error if $error;
318 my $method = $ignore_empty_reasonnum ? 'ut_foreign_keyn' : 'ut_foreign_key';
319 $error = $self->$method('reasonnum', 'reason', 'reasonnum');
320 return $error if $error;
322 return "refund must be > 0 " if $self->refund <= 0;
324 $self->_date(time) unless $self->_date;
326 return "unknown cust_main.custnum: ". $self->custnum
327 unless $self->crednum
328 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
330 $error = $self->payinfo_check;
331 return $error if $error;
336 =item cust_credit_refund
338 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
343 sub cust_credit_refund {
345 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
346 sort { $a->_date <=> $b->_date }
347 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
351 =item cust_pay_refund
353 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
358 sub cust_pay_refund {
360 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
361 sort { $a->_date <=> $b->_date }
362 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
368 Returns the amount of this refund that is still unapplied; which is
369 amount minus all credit applications (see L<FS::cust_credit_refund>) and
370 payment applications (see L<FS::cust_pay_refund>).
376 my $amount = $self->refund;
377 $amount -= $_->amount foreach ( $self->cust_credit_refund );
378 $amount -= $_->amount foreach ( $self->cust_pay_refund );
379 sprintf("%.2f", $amount );
382 =item send_receipt HASHREF | OPTION => VALUE ...
384 Sends a payment receipt for this payment.
386 refund_receipt_msgnum must be configured.
394 Customer (FS::cust_main) object (for efficiency).
404 my $opt = ref($_[0]) ? shift : { @_ };
406 my $cust_main = $opt->{'cust_main'} || $self->cust_main;
408 my $conf = new FS::Conf;
410 my $msgnum = $conf->config('refund_receipt_msgnum', $cust_main->agentnum);
411 return "No refund_receipt_msgnum configured" unless $msgnum;
413 my $msg_template = qsearchs('msg_template',{ msgnum => $msgnum});
414 return "Could not load template"
415 unless $msg_template;
417 my $queue = new FS::queue {
418 'job' => 'FS::Misc::process_send_email',
419 'custnum' => $cust_main->custnum,
421 my $error = $queue->insert(
422 FS::msg_template->by_key($msgnum)->prepare(
423 'cust_main' => $cust_main,
426 'msgtype' => 'receipt', # override msg_template's default
440 Returns an SQL fragment to retreive the unapplied amount.
445 my ($class, $start, $end) = @_;
446 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
447 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
448 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
449 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
453 ( SELECT SUM(amount) FROM cust_credit_refund
454 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
455 $credit_start $credit_end )
459 ( SELECT SUM(amount) FROM cust_pay_refund
460 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
461 $pay_start $pay_end )
470 Returns the text of the associated reason (see L<FS::reason>) for this credit.
475 my ($self, $value, %options) = @_;
478 my $typenum = $options{'reason_type'};
480 my $oldAutoCommit = $FS::UID::AutoCommit; # this should already be in
481 local $FS::UID::AutoCommit = 0; # a transaction if it matters
483 if ( defined( $value ) ) {
484 my $hashref = { 'reason' => $value };
485 $hashref->{'reason_type'} = $typenum if $typenum;
486 my $addl_from = "LEFT JOIN reason_type ON ( reason_type = typenum ) ";
487 my $extra_sql = " AND reason_type.class='F'";
489 $reason = qsearchs( { 'table' => 'reason',
490 'hashref' => $hashref,
491 'addl_from' => $addl_from,
492 'extra_sql' => $extra_sql,
495 if (!$reason && $typenum) {
496 $reason = new FS::reason( { 'reason_type' => $typenum,
500 my $error = $reason->insert;
502 warn "error inserting reason: $error\n";
507 $self->reasonnum($reason ? $reason->reasonnum : '') ;
508 warn "$me reason used in set mode with non-existant reason -- clearing"
511 $reason = qsearchs( 'reason', { 'reasonnum' => $self->reasonnum } );
513 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
515 ( $reason ? $reason->reason : '' ).
516 ( $self->addlinfo ? ' '.$self->addlinfo : '' );
519 # Used by FS::Upgrade to migrate to a new database.
520 sub _upgrade_data { # class method
521 my ($class, %opts) = @_;
522 $class->_upgrade_reasonnum(%opts);
523 $class->_upgrade_otaker(%opts);
525 local $ignore_empty_reasonnum = 1;
527 # don't set paycardtype until 4.x
528 #$class->upgrade_set_cardtype;
535 Delete and replace methods.
539 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.