1 package FS::cust_refund;
4 use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
6 use vars qw( @encrypted_fields );
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;
16 @encrypted_fields = ('payinfo');
17 sub nohistory_fields { ('payinfo'); }
21 FS::cust_refund - Object method for cust_refund objects
27 $record = new FS::cust_refund \%hash;
28 $record = new FS::cust_refund { 'column' => 'value' };
30 $error = $record->insert;
32 $error = $new_record->replace($old_record);
34 $error = $record->delete;
36 $error = $record->check;
40 An FS::cust_refund represents a refund: the transfer of money to a customer;
41 equivalent to a negative payment (see L<FS::cust_pay>). FS::cust_refund
42 inherits from FS::Record. The following fields are currently supported:
48 primary key (assigned automatically for new refunds)
52 customer (see L<FS::cust_main>)
64 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
65 L<Time::Local> and L<Date::Parse> for conversion functions.
69 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
73 Payment Information (See L<FS::payinfo_Mixin> for data format)
77 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
81 text field for tracking card processing
85 order taker (see L<FS::access_user>
89 books closed flag, empty or `Y'
91 =item gatewaynum, processor, auth, order_number
93 Same as for L<FS::cust_pay>, but specifically the result of realtime
94 authorization of the refund.
104 Creates a new refund. To add the refund to the database, see L<"insert">.
108 sub table { 'cust_refund'; }
112 Adds this refund to the database.
114 For backwards-compatibility and convenience, if the additional field crednum is
115 defined, an FS::cust_credit_refund record for the full amount of the refund
116 will be created. Or (this time for convenience and consistancy), if the
117 additional field paynum is defined, an FS::cust_pay_refund record for the full
118 amount of the refund will be created. In both cases, custnum is optional.
125 local $SIG{HUP} = 'IGNORE';
126 local $SIG{INT} = 'IGNORE';
127 local $SIG{QUIT} = 'IGNORE';
128 local $SIG{TERM} = 'IGNORE';
129 local $SIG{TSTP} = 'IGNORE';
130 local $SIG{PIPE} = 'IGNORE';
132 my $oldAutoCommit = $FS::UID::AutoCommit;
133 local $FS::UID::AutoCommit = 0;
136 if ( $self->crednum ) {
137 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
139 $dbh->rollback if $oldAutoCommit;
140 return "Unknown cust_credit.crednum: ". $self->crednum;
142 $self->custnum($cust_credit->custnum);
143 } elsif ( $self->paynum ) {
144 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
146 $dbh->rollback if $oldAutoCommit;
147 return "Unknown cust_pay.paynum: ". $self->paynum;
149 $self->custnum($cust_pay->custnum);
152 my $error = $self->check;
153 return $error if $error;
155 $error = $self->SUPER::insert;
157 $dbh->rollback if $oldAutoCommit;
161 if ( $self->crednum ) {
162 my $cust_credit_refund = new FS::cust_credit_refund {
163 'crednum' => $self->crednum,
164 'refundnum' => $self->refundnum,
165 'amount' => $self->refund,
166 '_date' => $self->_date,
168 $error = $cust_credit_refund->insert;
170 $dbh->rollback if $oldAutoCommit;
173 #$self->custnum($cust_credit_refund->cust_credit->custnum);
174 } elsif ( $self->paynum ) {
175 my $cust_pay_refund = new FS::cust_pay_refund {
176 'paynum' => $self->paynum,
177 'refundnum' => $self->refundnum,
178 'amount' => $self->refund,
179 '_date' => $self->_date,
181 $error = $cust_pay_refund->insert;
183 $dbh->rollback if $oldAutoCommit;
189 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
197 Unless the closed flag is set, deletes this refund and all associated
198 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
204 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
206 local $SIG{HUP} = 'IGNORE';
207 local $SIG{INT} = 'IGNORE';
208 local $SIG{QUIT} = 'IGNORE';
209 local $SIG{TERM} = 'IGNORE';
210 local $SIG{TSTP} = 'IGNORE';
211 local $SIG{PIPE} = 'IGNORE';
213 my $oldAutoCommit = $FS::UID::AutoCommit;
214 local $FS::UID::AutoCommit = 0;
217 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
218 my $error = $cust_credit_refund->delete;
220 $dbh->rollback if $oldAutoCommit;
225 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
226 my $error = $cust_pay_refund->delete;
228 $dbh->rollback if $oldAutoCommit;
233 my $error = $self->SUPER::delete(@_);
235 $dbh->rollback if $oldAutoCommit;
239 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
245 =item replace OLD_RECORD
247 You can, but probably shouldn't modify refunds...
249 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
250 supplied, replaces this record. If there is an error, returns the error,
251 otherwise returns false.
257 return "Can't modify closed refund" if $self->closed =~ /^Y/i;
258 $self->SUPER::replace(@_);
263 Checks all fields to make sure this is a valid refund. If there is an error,
264 returns the error, otherwise returns false. Called by the insert method.
271 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
274 $self->ut_numbern('refundnum')
275 || $self->ut_numbern('custnum')
276 || $self->ut_money('refund')
277 || $self->ut_alphan('otaker')
278 || $self->ut_text('reason')
279 || $self->ut_numbern('_date')
280 || $self->ut_textn('paybatch')
281 || $self->ut_enum('closed', [ '', 'Y' ])
283 return $error if $error;
285 return "refund must be > 0 " if $self->refund <= 0;
287 $self->_date(time) unless $self->_date;
289 return "unknown cust_main.custnum: ". $self->custnum
290 unless $self->crednum
291 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
293 $error = $self->payinfo_check;
294 return $error if $error;
299 =item cust_credit_refund
301 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
306 sub cust_credit_refund {
308 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
309 sort { $a->_date <=> $b->_date }
310 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
314 =item cust_pay_refund
316 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
321 sub cust_pay_refund {
323 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
324 sort { $a->_date <=> $b->_date }
325 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
331 Returns the amount of this refund that is still unapplied; which is
332 amount minus all credit applications (see L<FS::cust_credit_refund>) and
333 payment applications (see L<FS::cust_pay_refund>).
339 my $amount = $self->refund;
340 $amount -= $_->amount foreach ( $self->cust_credit_refund );
341 $amount -= $_->amount foreach ( $self->cust_pay_refund );
342 sprintf("%.2f", $amount );
353 Returns an SQL fragment to retreive the unapplied amount.
358 my ($class, $start, $end) = @_;
359 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
360 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
361 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
362 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
366 ( SELECT SUM(amount) FROM cust_credit_refund
367 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
368 $credit_start $credit_end )
372 ( SELECT SUM(amount) FROM cust_pay_refund
373 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
374 $pay_start $pay_end )
381 # Used by FS::Upgrade to migrate to a new database.
382 sub _upgrade_data { # class method
383 my ($class, %opts) = @_;
384 $class->_upgrade_otaker(%opts);
391 Delete and replace methods.
395 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.