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 Detected credit card type, if appropriate; autodetected.
91 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
95 text field for tracking card processing
99 order taker (see L<FS::access_user>
103 books closed flag, empty or `Y'
105 =item gatewaynum, processor, auth, order_number
107 Same as for L<FS::cust_pay>, but specifically the result of realtime
108 authorization of the refund.
118 Creates a new refund. To add the refund to the database, see L<"insert">.
122 sub table { 'cust_refund'; }
126 Adds this refund to the database.
128 For backwards-compatibility and convenience, if the additional field crednum is
129 defined, an FS::cust_credit_refund record for the full amount of the refund
130 will be created. Or (this time for convenience and consistancy), if the
131 additional field paynum is defined, an FS::cust_pay_refund record for the full
132 amount of the refund will be created. In both cases, custnum is optional.
137 my ($self, %options) = @_;
139 local $SIG{HUP} = 'IGNORE';
140 local $SIG{INT} = 'IGNORE';
141 local $SIG{QUIT} = 'IGNORE';
142 local $SIG{TERM} = 'IGNORE';
143 local $SIG{TSTP} = 'IGNORE';
144 local $SIG{PIPE} = 'IGNORE';
146 my $oldAutoCommit = $FS::UID::AutoCommit;
147 local $FS::UID::AutoCommit = 0;
150 if (!$self->reasonnum) {
151 my $reason_text = $self->get('reason')
152 or return "reason text or existing reason required";
153 my $reason_type = $options{'reason_type'}
154 or return "reason type required";
157 my $reason = FS::reason->new_or_existing(
158 reason => $reason_text,
159 type => $reason_type,
163 $dbh->rollback if $oldAutoCommit;
164 return "failed to set refund reason: $@";
166 $self->set('reasonnum', $reason->reasonnum);
169 $self->setfield('reason', '');
171 if ( $self->crednum ) {
172 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
174 $dbh->rollback if $oldAutoCommit;
175 return "Unknown cust_credit.crednum: ". $self->crednum;
177 $self->custnum($cust_credit->custnum);
178 } elsif ( $self->paynum ) {
179 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
181 $dbh->rollback if $oldAutoCommit;
182 return "Unknown cust_pay.paynum: ". $self->paynum;
184 $self->custnum($cust_pay->custnum);
187 my $error = $self->check;
188 return $error if $error;
190 $error = $self->SUPER::insert;
192 $dbh->rollback if $oldAutoCommit;
196 if ( $self->crednum ) {
197 my $cust_credit_refund = new FS::cust_credit_refund {
198 'crednum' => $self->crednum,
199 'refundnum' => $self->refundnum,
200 'amount' => $self->refund,
201 '_date' => $self->_date,
203 $error = $cust_credit_refund->insert;
205 $dbh->rollback if $oldAutoCommit;
208 #$self->custnum($cust_credit_refund->cust_credit->custnum);
209 } elsif ( $self->paynum ) {
210 my $cust_pay_refund = new FS::cust_pay_refund {
211 'paynum' => $self->paynum,
212 'refundnum' => $self->refundnum,
213 'amount' => $self->refund,
214 '_date' => $self->_date,
216 $error = $cust_pay_refund->insert;
218 $dbh->rollback if $oldAutoCommit;
224 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
232 Unless the closed flag is set, deletes this refund and all associated
233 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
239 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
241 local $SIG{HUP} = 'IGNORE';
242 local $SIG{INT} = 'IGNORE';
243 local $SIG{QUIT} = 'IGNORE';
244 local $SIG{TERM} = 'IGNORE';
245 local $SIG{TSTP} = 'IGNORE';
246 local $SIG{PIPE} = 'IGNORE';
248 my $oldAutoCommit = $FS::UID::AutoCommit;
249 local $FS::UID::AutoCommit = 0;
252 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
253 my $error = $cust_credit_refund->delete;
255 $dbh->rollback if $oldAutoCommit;
260 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
261 my $error = $cust_pay_refund->delete;
263 $dbh->rollback if $oldAutoCommit;
268 my $error = $self->SUPER::delete(@_);
270 $dbh->rollback if $oldAutoCommit;
274 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
280 =item replace OLD_RECORD
282 You can, but probably shouldn't modify refunds...
284 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
285 supplied, replaces this record. If there is an error, returns the error,
286 otherwise returns false.
292 return "Can't modify closed refund"
293 if $self->closed =~ /^Y/i && !$FS::payinfo_Mixin::allow_closed_replace;
294 $self->SUPER::replace(@_);
299 Checks all fields to make sure this is a valid refund. If there is an error,
300 returns the error, otherwise returns false. Called by the insert method.
307 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
310 $self->ut_numbern('refundnum')
311 || $self->ut_numbern('custnum')
312 || $self->ut_money('refund')
313 || $self->ut_alphan('otaker')
314 || $self->ut_textn('reason')
315 || $self->ut_numbern('_date')
316 || $self->ut_textn('paybatch')
317 || $self->ut_enum('closed', [ '', 'Y' ])
318 || $self->ut_foreign_keyn('source_paynum', 'cust_pay', 'paynum')
320 return $error if $error;
322 my $method = $ignore_empty_reasonnum ? 'ut_foreign_keyn' : 'ut_foreign_key';
323 $error = $self->$method('reasonnum', 'reason', 'reasonnum');
324 return $error if $error;
326 return "refund must be > 0 " if $self->refund <= 0;
328 $self->_date(time) unless $self->_date;
330 return "unknown cust_main.custnum: ". $self->custnum
331 unless $self->crednum
332 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
334 $error = $self->payinfo_check;
335 return $error if $error;
340 =item cust_credit_refund
342 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
347 sub cust_credit_refund {
349 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
350 sort { $a->_date <=> $b->_date }
351 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
355 =item cust_pay_refund
357 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
362 sub cust_pay_refund {
364 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
365 sort { $a->_date <=> $b->_date }
366 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
372 Returns the amount of this refund that is still unapplied; which is
373 amount minus all credit applications (see L<FS::cust_credit_refund>) and
374 payment applications (see L<FS::cust_pay_refund>).
380 my $amount = $self->refund;
381 $amount -= $_->amount foreach ( $self->cust_credit_refund );
382 $amount -= $_->amount foreach ( $self->cust_pay_refund );
383 sprintf("%.2f", $amount );
386 =item send_receipt HASHREF | OPTION => VALUE ...
388 Sends a payment receipt for this payment.
390 refund_receipt_msgnum must be configured.
398 Customer (FS::cust_main) object (for efficiency).
408 my $opt = ref($_[0]) ? shift : { @_ };
410 my $cust_main = $opt->{'cust_main'} || $self->cust_main;
412 my $conf = new FS::Conf;
414 my $msgnum = $conf->config('refund_receipt_msgnum', $cust_main->agentnum);
415 return "No refund_receipt_msgnum configured" unless $msgnum;
417 my $msg_template = qsearchs('msg_template',{ msgnum => $msgnum});
418 return "Could not load template"
419 unless $msg_template;
421 my $cust_msg = $msg_template->prepare(
422 'cust_main' => $cust_main,
424 'msgtype' => 'receipt',
426 return 'Error preparing message' unless $cust_msg;
427 my $error = $cust_msg->insert;
428 return $error if $error;
430 my $queue = new FS::queue {
431 'job' => 'FS::cust_msg::process_send',
432 'custnum' => $cust_main->custnum,
434 $error = $queue->insert( $cust_msg->custmsgnum );
447 Returns an SQL fragment to retreive the unapplied amount.
452 my ($class, $start, $end) = @_;
453 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
454 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
455 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
456 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
460 ( SELECT SUM(amount) FROM cust_credit_refund
461 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
462 $credit_start $credit_end )
466 ( SELECT SUM(amount) FROM cust_pay_refund
467 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
468 $pay_start $pay_end )
475 sub _upgrade_schema {
476 #my ($class, %opts) = @_;
478 my $sql = 'UPDATE cust_refund SET source_paynum = NULL
479 WHERE source_paynum IS NOT NULL
480 AND NOT EXISTS ( SELECT 1 FROM cust_pay
481 WHERE paynum = cust_refund.source_paynum )
483 my $sth = dbh->prepare($sql) or die dbh->errstr;
484 $sth->execute or die $sth->errstr;
488 # Used by FS::Upgrade to migrate to a new database.
489 sub _upgrade_data { # class method
490 my ($class, %opts) = @_;
491 $class->_upgrade_reasonnum(%opts);
492 $class->_upgrade_otaker(%opts);
494 local $ignore_empty_reasonnum = 1;
495 $class->upgrade_set_cardtype;
502 Delete and replace methods.
506 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.