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 );
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'
97 Creates a new refund. To add the refund to the database, see L<"insert">.
101 sub table { 'cust_refund'; }
105 Adds this refund to the database.
107 For backwards-compatibility and convenience, if the additional field crednum is
108 defined, an FS::cust_credit_refund record for the full amount of the refund
109 will be created. Or (this time for convenience and consistancy), if the
110 additional field paynum is defined, an FS::cust_pay_refund record for the full
111 amount of the refund will be created. In both cases, custnum is optional.
118 local $SIG{HUP} = 'IGNORE';
119 local $SIG{INT} = 'IGNORE';
120 local $SIG{QUIT} = 'IGNORE';
121 local $SIG{TERM} = 'IGNORE';
122 local $SIG{TSTP} = 'IGNORE';
123 local $SIG{PIPE} = 'IGNORE';
125 my $oldAutoCommit = $FS::UID::AutoCommit;
126 local $FS::UID::AutoCommit = 0;
129 if ( $self->crednum ) {
130 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
132 $dbh->rollback if $oldAutoCommit;
133 return "Unknown cust_credit.crednum: ". $self->crednum;
135 $self->custnum($cust_credit->custnum);
136 } elsif ( $self->paynum ) {
137 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
139 $dbh->rollback if $oldAutoCommit;
140 return "Unknown cust_pay.paynum: ". $self->paynum;
142 $self->custnum($cust_pay->custnum);
145 my $error = $self->check;
146 return $error if $error;
148 $error = $self->SUPER::insert;
150 $dbh->rollback if $oldAutoCommit;
154 if ( $self->crednum ) {
155 my $cust_credit_refund = new FS::cust_credit_refund {
156 'crednum' => $self->crednum,
157 'refundnum' => $self->refundnum,
158 'amount' => $self->refund,
159 '_date' => $self->_date,
161 $error = $cust_credit_refund->insert;
163 $dbh->rollback if $oldAutoCommit;
166 #$self->custnum($cust_credit_refund->cust_credit->custnum);
167 } elsif ( $self->paynum ) {
168 my $cust_pay_refund = new FS::cust_pay_refund {
169 'paynum' => $self->paynum,
170 'refundnum' => $self->refundnum,
171 'amount' => $self->refund,
172 '_date' => $self->_date,
174 $error = $cust_pay_refund->insert;
176 $dbh->rollback if $oldAutoCommit;
182 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
190 Unless the closed flag is set, deletes this refund and all associated
191 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
197 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
199 local $SIG{HUP} = 'IGNORE';
200 local $SIG{INT} = 'IGNORE';
201 local $SIG{QUIT} = 'IGNORE';
202 local $SIG{TERM} = 'IGNORE';
203 local $SIG{TSTP} = 'IGNORE';
204 local $SIG{PIPE} = 'IGNORE';
206 my $oldAutoCommit = $FS::UID::AutoCommit;
207 local $FS::UID::AutoCommit = 0;
210 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
211 my $error = $cust_credit_refund->delete;
213 $dbh->rollback if $oldAutoCommit;
218 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
219 my $error = $cust_pay_refund->delete;
221 $dbh->rollback if $oldAutoCommit;
226 my $error = $self->SUPER::delete(@_);
228 $dbh->rollback if $oldAutoCommit;
232 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
238 =item replace OLD_RECORD
240 Modifying a refund? Well, don't say I didn't warn you.
246 $self->SUPER::replace(@_);
251 Checks all fields to make sure this is a valid refund. If there is an error,
252 returns the error, otherwise returns false. Called by the insert method.
259 $self->otaker(getotaker) unless $self->otaker;
262 $self->ut_numbern('refundnum')
263 || $self->ut_numbern('custnum')
264 || $self->ut_money('refund')
265 || $self->ut_alphan('otaker')
266 || $self->ut_text('reason')
267 || $self->ut_numbern('_date')
268 || $self->ut_textn('paybatch')
269 || $self->ut_enum('closed', [ '', 'Y' ])
271 return $error if $error;
273 return "refund must be > 0 " if $self->refund <= 0;
275 $self->_date(time) unless $self->_date;
277 return "unknown cust_main.custnum: ". $self->custnum
278 unless $self->crednum
279 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
281 $error = $self->payinfo_check;
282 return $error if $error;
287 =item cust_credit_refund
289 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
294 sub cust_credit_refund {
296 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
297 sort { $a->_date <=> $b->_date }
298 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
302 =item cust_pay_refund
304 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
309 sub cust_pay_refund {
311 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
312 sort { $a->_date <=> $b->_date }
313 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
319 Returns the amount of this refund that is still unapplied; which is
320 amount minus all credit applications (see L<FS::cust_credit_refund>) and
321 payment applications (see L<FS::cust_pay_refund>).
327 my $amount = $self->refund;
328 $amount -= $_->amount foreach ( $self->cust_credit_refund );
329 $amount -= $_->amount foreach ( $self->cust_pay_refund );
330 sprintf("%.2f", $amount );
341 Returns an SQL fragment to retreive the unapplied amount.
350 ( SELECT SUM(amount) FROM cust_credit_refund
351 WHERE cust_refund.refundnum = cust_credit_refund.refundnum )
355 ( SELECT SUM(amount) FROM cust_pay_refund
356 WHERE cust_refund.refundnum = cust_pay_refund.refundnum )
363 # Used by FS::Upgrade to migrate to a new database.
364 sub _upgrade_data { # class method
365 my ($class, %opts) = @_;
366 $class->_upgrade_otaker(%opts);
373 Delete and replace methods.
377 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.