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');
20 FS::cust_refund - Object method for cust_refund objects
26 $record = new FS::cust_refund \%hash;
27 $record = new FS::cust_refund { 'column' => 'value' };
29 $error = $record->insert;
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
39 An FS::cust_refund represents a refund: the transfer of money to a customer;
40 equivalent to a negative payment (see L<FS::cust_pay>). FS::cust_refund
41 inherits from FS::Record. The following fields are currently supported:
47 primary key (assigned automatically for new refunds)
51 customer (see L<FS::cust_main>)
63 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
64 L<Time::Local> and L<Date::Parse> for conversion functions.
68 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
72 Payment Information (See L<FS::payinfo_Mixin> for data format)
76 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
80 text field for tracking card processing
84 order taker (see L<FS::access_user>
88 books closed flag, empty or `Y'
90 =item gatewaynum, processor, auth, order_number
92 Same as for L<FS::cust_pay>, but specifically the result of realtime
93 authorization of the refund.
103 Creates a new refund. To add the refund to the database, see L<"insert">.
107 sub table { 'cust_refund'; }
111 Adds this refund to the database.
113 For backwards-compatibility and convenience, if the additional field crednum is
114 defined, an FS::cust_credit_refund record for the full amount of the refund
115 will be created. Or (this time for convenience and consistancy), if the
116 additional field paynum is defined, an FS::cust_pay_refund record for the full
117 amount of the refund will be created. In both cases, custnum is optional.
124 local $SIG{HUP} = 'IGNORE';
125 local $SIG{INT} = 'IGNORE';
126 local $SIG{QUIT} = 'IGNORE';
127 local $SIG{TERM} = 'IGNORE';
128 local $SIG{TSTP} = 'IGNORE';
129 local $SIG{PIPE} = 'IGNORE';
131 my $oldAutoCommit = $FS::UID::AutoCommit;
132 local $FS::UID::AutoCommit = 0;
135 if ( $self->crednum ) {
136 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
138 $dbh->rollback if $oldAutoCommit;
139 return "Unknown cust_credit.crednum: ". $self->crednum;
141 $self->custnum($cust_credit->custnum);
142 } elsif ( $self->paynum ) {
143 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
145 $dbh->rollback if $oldAutoCommit;
146 return "Unknown cust_pay.paynum: ". $self->paynum;
148 $self->custnum($cust_pay->custnum);
151 my $error = $self->check;
152 return $error if $error;
154 $error = $self->SUPER::insert;
156 $dbh->rollback if $oldAutoCommit;
160 if ( $self->crednum ) {
161 my $cust_credit_refund = new FS::cust_credit_refund {
162 'crednum' => $self->crednum,
163 'refundnum' => $self->refundnum,
164 'amount' => $self->refund,
165 '_date' => $self->_date,
167 $error = $cust_credit_refund->insert;
169 $dbh->rollback if $oldAutoCommit;
172 #$self->custnum($cust_credit_refund->cust_credit->custnum);
173 } elsif ( $self->paynum ) {
174 my $cust_pay_refund = new FS::cust_pay_refund {
175 'paynum' => $self->paynum,
176 'refundnum' => $self->refundnum,
177 'amount' => $self->refund,
178 '_date' => $self->_date,
180 $error = $cust_pay_refund->insert;
182 $dbh->rollback if $oldAutoCommit;
188 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
196 Unless the closed flag is set, deletes this refund and all associated
197 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
203 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
205 local $SIG{HUP} = 'IGNORE';
206 local $SIG{INT} = 'IGNORE';
207 local $SIG{QUIT} = 'IGNORE';
208 local $SIG{TERM} = 'IGNORE';
209 local $SIG{TSTP} = 'IGNORE';
210 local $SIG{PIPE} = 'IGNORE';
212 my $oldAutoCommit = $FS::UID::AutoCommit;
213 local $FS::UID::AutoCommit = 0;
216 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
217 my $error = $cust_credit_refund->delete;
219 $dbh->rollback if $oldAutoCommit;
224 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
225 my $error = $cust_pay_refund->delete;
227 $dbh->rollback if $oldAutoCommit;
232 my $error = $self->SUPER::delete(@_);
234 $dbh->rollback if $oldAutoCommit;
238 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
244 =item replace OLD_RECORD
246 You can, but probably shouldn't modify refunds...
248 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
249 supplied, replaces this record. If there is an error, returns the error,
250 otherwise returns false.
256 return "Can't modify closed refund" if $self->closed =~ /^Y/i;
257 $self->SUPER::replace(@_);
262 Checks all fields to make sure this is a valid refund. If there is an error,
263 returns the error, otherwise returns false. Called by the insert method.
270 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
273 $self->ut_numbern('refundnum')
274 || $self->ut_numbern('custnum')
275 || $self->ut_money('refund')
276 || $self->ut_alphan('otaker')
277 || $self->ut_text('reason')
278 || $self->ut_numbern('_date')
279 || $self->ut_textn('paybatch')
280 || $self->ut_enum('closed', [ '', 'Y' ])
282 return $error if $error;
284 return "refund must be > 0 " if $self->refund <= 0;
286 $self->_date(time) unless $self->_date;
288 return "unknown cust_main.custnum: ". $self->custnum
289 unless $self->crednum
290 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
292 $error = $self->payinfo_check;
293 return $error if $error;
298 =item cust_credit_refund
300 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
305 sub cust_credit_refund {
307 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
308 sort { $a->_date <=> $b->_date }
309 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
313 =item cust_pay_refund
315 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
320 sub cust_pay_refund {
322 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
323 sort { $a->_date <=> $b->_date }
324 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
330 Returns the amount of this refund that is still unapplied; which is
331 amount minus all credit applications (see L<FS::cust_credit_refund>) and
332 payment applications (see L<FS::cust_pay_refund>).
338 my $amount = $self->refund;
339 $amount -= $_->amount foreach ( $self->cust_credit_refund );
340 $amount -= $_->amount foreach ( $self->cust_pay_refund );
341 sprintf("%.2f", $amount );
352 Returns an SQL fragment to retreive the unapplied amount.
357 my ($class, $start, $end) = @_;
358 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
359 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
360 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
361 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
365 ( SELECT SUM(amount) FROM cust_credit_refund
366 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
367 $credit_start $credit_end )
371 ( SELECT SUM(amount) FROM cust_pay_refund
372 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
373 $pay_start $pay_end )
380 # Used by FS::Upgrade to migrate to a new database.
381 sub _upgrade_data { # class method
382 my ($class, %opts) = @_;
383 $class->_upgrade_otaker(%opts);
390 Delete and replace methods.
394 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.