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" if $self->closed =~ /^Y/i;
289 $self->SUPER::replace(@_);
294 Checks all fields to make sure this is a valid refund. If there is an error,
295 returns the error, otherwise returns false. Called by the insert method.
302 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
305 $self->ut_numbern('refundnum')
306 || $self->ut_numbern('custnum')
307 || $self->ut_money('refund')
308 || $self->ut_alphan('otaker')
309 || $self->ut_textn('reason')
310 || $self->ut_numbern('_date')
311 || $self->ut_textn('paybatch')
312 || $self->ut_enum('closed', [ '', 'Y' ])
313 || $self->ut_foreign_keyn('source_paynum', 'cust_pay', 'paynum')
315 return $error if $error;
317 my $method = $ignore_empty_reasonnum ? 'ut_foreign_keyn' : 'ut_foreign_key';
318 $error = $self->$method('reasonnum', 'reason', 'reasonnum');
319 return $error if $error;
321 return "refund must be > 0 " if $self->refund <= 0;
323 $self->_date(time) unless $self->_date;
325 return "unknown cust_main.custnum: ". $self->custnum
326 unless $self->crednum
327 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
329 $error = $self->payinfo_check;
330 return $error if $error;
335 =item cust_credit_refund
337 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
342 sub cust_credit_refund {
344 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
345 sort { $a->_date <=> $b->_date }
346 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
350 =item cust_pay_refund
352 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
357 sub cust_pay_refund {
359 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
360 sort { $a->_date <=> $b->_date }
361 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
367 Returns the amount of this refund that is still unapplied; which is
368 amount minus all credit applications (see L<FS::cust_credit_refund>) and
369 payment applications (see L<FS::cust_pay_refund>).
375 my $amount = $self->refund;
376 $amount -= $_->amount foreach ( $self->cust_credit_refund );
377 $amount -= $_->amount foreach ( $self->cust_pay_refund );
378 sprintf("%.2f", $amount );
381 =item send_receipt HASHREF | OPTION => VALUE ...
383 Sends a payment receipt for this payment.
385 refund_receipt_msgnum must be configured.
393 Customer (FS::cust_main) object (for efficiency).
403 my $opt = ref($_[0]) ? shift : { @_ };
405 my $cust_main = $opt->{'cust_main'} || $self->cust_main;
407 my $conf = new FS::Conf;
409 my $msgnum = $conf->config('refund_receipt_msgnum', $cust_main->agentnum);
410 return "No refund_receipt_msgnum configured" unless $msgnum;
412 my $msg_template = qsearchs('msg_template',{ msgnum => $msgnum});
413 return "Could not load template"
414 unless $msg_template;
416 my $queue = new FS::queue {
417 'job' => 'FS::Misc::process_send_email',
418 'custnum' => $cust_main->custnum,
420 my $error = $queue->insert(
421 FS::msg_template->by_key($msgnum)->prepare(
422 'cust_main' => $cust_main,
425 'msgtype' => 'receipt', # override msg_template's default
439 Returns an SQL fragment to retreive the unapplied amount.
444 my ($class, $start, $end) = @_;
445 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
446 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
447 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
448 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
452 ( SELECT SUM(amount) FROM cust_credit_refund
453 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
454 $credit_start $credit_end )
458 ( SELECT SUM(amount) FROM cust_pay_refund
459 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
460 $pay_start $pay_end )
469 Returns the text of the associated reason (see L<FS::reason>) for this credit.
474 my ($self, $value, %options) = @_;
477 my $typenum = $options{'reason_type'};
479 my $oldAutoCommit = $FS::UID::AutoCommit; # this should already be in
480 local $FS::UID::AutoCommit = 0; # a transaction if it matters
482 if ( defined( $value ) ) {
483 my $hashref = { 'reason' => $value };
484 $hashref->{'reason_type'} = $typenum if $typenum;
485 my $addl_from = "LEFT JOIN reason_type ON ( reason_type = typenum ) ";
486 my $extra_sql = " AND reason_type.class='F'";
488 $reason = qsearchs( { 'table' => 'reason',
489 'hashref' => $hashref,
490 'addl_from' => $addl_from,
491 'extra_sql' => $extra_sql,
494 if (!$reason && $typenum) {
495 $reason = new FS::reason( { 'reason_type' => $typenum,
499 my $error = $reason->insert;
501 warn "error inserting reason: $error\n";
506 $self->reasonnum($reason ? $reason->reasonnum : '') ;
507 warn "$me reason used in set mode with non-existant reason -- clearing"
510 $reason = qsearchs( 'reason', { 'reasonnum' => $self->reasonnum } );
512 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
514 ( $reason ? $reason->reason : '' ).
515 ( $self->addlinfo ? ' '.$self->addlinfo : '' );
518 sub _upgrade_schema {
519 #my ($class, %opts) = @_;
521 my $sql = 'UPDATE cust_refund SET source_paynum = NULL
522 WHERE source_paynum IS NOT NULL
523 AND NOT EXISTS ( SELECT 1 FROM cust_pay
524 WHERE paynum = cust_refund.source_paynum )
526 my $sth = dbh->prepare($sql) or die dbh->errstr;
527 $sth->execute or die $sth->errstr;
531 # Used by FS::Upgrade to migrate to a new database.
532 sub _upgrade_data { # class method
533 my ($class, %opts) = @_;
534 $class->_upgrade_reasonnum(%opts);
535 $class->_upgrade_otaker(%opts);
537 local $ignore_empty_reasonnum = 1;
539 # don't set paycardtype until 4.x
540 #$class->upgrade_set_cardtype;
547 Delete and replace methods.
551 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.