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::Record qw( qsearch qsearchs dbh );
11 use FS::cust_credit_refund;
12 use FS::cust_pay_refund;
15 @encrypted_fields = ('payinfo');
19 FS::cust_refund - Object method for cust_refund objects
25 $record = new FS::cust_refund \%hash;
26 $record = new FS::cust_refund { 'column' => 'value' };
28 $error = $record->insert;
30 $error = $new_record->replace($old_record);
32 $error = $record->delete;
34 $error = $record->check;
38 An FS::cust_refund represents a refund: the transfer of money to a customer;
39 equivalent to a negative payment (see L<FS::cust_pay>). FS::cust_refund
40 inherits from FS::Record. The following fields are currently supported:
46 primary key (assigned automatically for new refunds)
50 customer (see L<FS::cust_main>)
62 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
63 L<Time::Local> and L<Date::Parse> for conversion functions.
67 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
71 Payment Information (See L<FS::payinfo_Mixin> for data format)
75 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
79 text field for tracking card processing
83 order taker (see L<FS::access_user>
87 books closed flag, empty or `Y'
89 =item gatewaynum, processor, auth, order_number
91 Same as for L<FS::cust_pay>, but specifically the result of realtime
92 authorization of the refund.
102 Creates a new refund. To add the refund to the database, see L<"insert">.
106 sub table { 'cust_refund'; }
110 Adds this refund to the database.
112 For backwards-compatibility and convenience, if the additional field crednum is
113 defined, an FS::cust_credit_refund record for the full amount of the refund
114 will be created. Or (this time for convenience and consistancy), if the
115 additional field paynum is defined, an FS::cust_pay_refund record for the full
116 amount of the refund will be created. In both cases, custnum is optional.
123 local $SIG{HUP} = 'IGNORE';
124 local $SIG{INT} = 'IGNORE';
125 local $SIG{QUIT} = 'IGNORE';
126 local $SIG{TERM} = 'IGNORE';
127 local $SIG{TSTP} = 'IGNORE';
128 local $SIG{PIPE} = 'IGNORE';
130 my $oldAutoCommit = $FS::UID::AutoCommit;
131 local $FS::UID::AutoCommit = 0;
134 if ( $self->crednum ) {
135 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
137 $dbh->rollback if $oldAutoCommit;
138 return "Unknown cust_credit.crednum: ". $self->crednum;
140 $self->custnum($cust_credit->custnum);
141 } elsif ( $self->paynum ) {
142 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
144 $dbh->rollback if $oldAutoCommit;
145 return "Unknown cust_pay.paynum: ". $self->paynum;
147 $self->custnum($cust_pay->custnum);
150 my $error = $self->check;
151 return $error if $error;
153 $error = $self->SUPER::insert;
155 $dbh->rollback if $oldAutoCommit;
159 if ( $self->crednum ) {
160 my $cust_credit_refund = new FS::cust_credit_refund {
161 'crednum' => $self->crednum,
162 'refundnum' => $self->refundnum,
163 'amount' => $self->refund,
164 '_date' => $self->_date,
166 $error = $cust_credit_refund->insert;
168 $dbh->rollback if $oldAutoCommit;
171 #$self->custnum($cust_credit_refund->cust_credit->custnum);
172 } elsif ( $self->paynum ) {
173 my $cust_pay_refund = new FS::cust_pay_refund {
174 'paynum' => $self->paynum,
175 'refundnum' => $self->refundnum,
176 'amount' => $self->refund,
177 '_date' => $self->_date,
179 $error = $cust_pay_refund->insert;
181 $dbh->rollback if $oldAutoCommit;
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
195 Unless the closed flag is set, deletes this refund and all associated
196 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
202 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
204 local $SIG{HUP} = 'IGNORE';
205 local $SIG{INT} = 'IGNORE';
206 local $SIG{QUIT} = 'IGNORE';
207 local $SIG{TERM} = 'IGNORE';
208 local $SIG{TSTP} = 'IGNORE';
209 local $SIG{PIPE} = 'IGNORE';
211 my $oldAutoCommit = $FS::UID::AutoCommit;
212 local $FS::UID::AutoCommit = 0;
215 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
216 my $error = $cust_credit_refund->delete;
218 $dbh->rollback if $oldAutoCommit;
223 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
224 my $error = $cust_pay_refund->delete;
226 $dbh->rollback if $oldAutoCommit;
231 my $error = $self->SUPER::delete(@_);
233 $dbh->rollback if $oldAutoCommit;
237 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
243 =item replace OLD_RECORD
245 You can, but probably shouldn't modify refunds...
247 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
248 supplied, replaces this record. If there is an error, returns the error,
249 otherwise returns false.
255 return "Can't modify closed refund" if $self->closed =~ /^Y/i;
256 $self->SUPER::replace(@_);
261 Checks all fields to make sure this is a valid refund. If there is an error,
262 returns the error, otherwise returns false. Called by the insert method.
269 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
272 $self->ut_numbern('refundnum')
273 || $self->ut_numbern('custnum')
274 || $self->ut_money('refund')
275 || $self->ut_alphan('otaker')
276 || $self->ut_text('reason')
277 || $self->ut_numbern('_date')
278 || $self->ut_textn('paybatch')
279 || $self->ut_enum('closed', [ '', 'Y' ])
281 return $error if $error;
283 return "refund must be > 0 " if $self->refund <= 0;
285 $self->_date(time) unless $self->_date;
287 return "unknown cust_main.custnum: ". $self->custnum
288 unless $self->crednum
289 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
291 $error = $self->payinfo_check;
292 return $error if $error;
297 =item cust_credit_refund
299 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
304 sub cust_credit_refund {
306 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
307 sort { $a->_date <=> $b->_date }
308 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
312 =item cust_pay_refund
314 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
319 sub cust_pay_refund {
321 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
322 sort { $a->_date <=> $b->_date }
323 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
329 Returns the amount of this refund that is still unapplied; which is
330 amount minus all credit applications (see L<FS::cust_credit_refund>) and
331 payment applications (see L<FS::cust_pay_refund>).
337 my $amount = $self->refund;
338 $amount -= $_->amount foreach ( $self->cust_credit_refund );
339 $amount -= $_->amount foreach ( $self->cust_pay_refund );
340 sprintf("%.2f", $amount );
351 Returns an SQL fragment to retreive the unapplied amount.
356 my ($class, $start, $end) = @_;
357 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
358 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
359 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
360 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
364 ( SELECT SUM(amount) FROM cust_credit_refund
365 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
366 $credit_start $credit_end )
370 ( SELECT SUM(amount) FROM cust_pay_refund
371 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
372 $pay_start $pay_end )
379 # Used by FS::Upgrade to migrate to a new database.
380 sub _upgrade_data { # class method
381 my ($class, %opts) = @_;
382 $class->_upgrade_otaker(%opts);
389 Delete and replace methods.
393 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.