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'
99 Creates a new refund. To add the refund to the database, see L<"insert">.
103 sub table { 'cust_refund'; }
107 Adds this refund to the database.
109 For backwards-compatibility and convenience, if the additional field crednum is
110 defined, an FS::cust_credit_refund record for the full amount of the refund
111 will be created. Or (this time for convenience and consistancy), if the
112 additional field paynum is defined, an FS::cust_pay_refund record for the full
113 amount of the refund will be created. In both cases, custnum is optional.
120 local $SIG{HUP} = 'IGNORE';
121 local $SIG{INT} = 'IGNORE';
122 local $SIG{QUIT} = 'IGNORE';
123 local $SIG{TERM} = 'IGNORE';
124 local $SIG{TSTP} = 'IGNORE';
125 local $SIG{PIPE} = 'IGNORE';
127 my $oldAutoCommit = $FS::UID::AutoCommit;
128 local $FS::UID::AutoCommit = 0;
131 if ( $self->crednum ) {
132 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
134 $dbh->rollback if $oldAutoCommit;
135 return "Unknown cust_credit.crednum: ". $self->crednum;
137 $self->custnum($cust_credit->custnum);
138 } elsif ( $self->paynum ) {
139 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
141 $dbh->rollback if $oldAutoCommit;
142 return "Unknown cust_pay.paynum: ". $self->paynum;
144 $self->custnum($cust_pay->custnum);
147 my $error = $self->check;
148 return $error if $error;
150 $error = $self->SUPER::insert;
152 $dbh->rollback if $oldAutoCommit;
156 if ( $self->crednum ) {
157 my $cust_credit_refund = new FS::cust_credit_refund {
158 'crednum' => $self->crednum,
159 'refundnum' => $self->refundnum,
160 'amount' => $self->refund,
161 '_date' => $self->_date,
163 $error = $cust_credit_refund->insert;
165 $dbh->rollback if $oldAutoCommit;
168 #$self->custnum($cust_credit_refund->cust_credit->custnum);
169 } elsif ( $self->paynum ) {
170 my $cust_pay_refund = new FS::cust_pay_refund {
171 'paynum' => $self->paynum,
172 'refundnum' => $self->refundnum,
173 'amount' => $self->refund,
174 '_date' => $self->_date,
176 $error = $cust_pay_refund->insert;
178 $dbh->rollback if $oldAutoCommit;
184 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
192 Unless the closed flag is set, deletes this refund and all associated
193 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
199 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
201 local $SIG{HUP} = 'IGNORE';
202 local $SIG{INT} = 'IGNORE';
203 local $SIG{QUIT} = 'IGNORE';
204 local $SIG{TERM} = 'IGNORE';
205 local $SIG{TSTP} = 'IGNORE';
206 local $SIG{PIPE} = 'IGNORE';
208 my $oldAutoCommit = $FS::UID::AutoCommit;
209 local $FS::UID::AutoCommit = 0;
212 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
213 my $error = $cust_credit_refund->delete;
215 $dbh->rollback if $oldAutoCommit;
220 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
221 my $error = $cust_pay_refund->delete;
223 $dbh->rollback if $oldAutoCommit;
228 my $error = $self->SUPER::delete(@_);
230 $dbh->rollback if $oldAutoCommit;
234 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
240 =item replace OLD_RECORD
242 You can, but probably shouldn't modify refunds...
244 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
245 supplied, replaces this record. If there is an error, returns the error,
246 otherwise returns false.
252 return "Can't modify closed refund" if $self->closed =~ /^Y/i;
253 $self->SUPER::replace(@_);
258 Checks all fields to make sure this is a valid refund. If there is an error,
259 returns the error, otherwise returns false. Called by the insert method.
266 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
269 $self->ut_numbern('refundnum')
270 || $self->ut_numbern('custnum')
271 || $self->ut_money('refund')
272 || $self->ut_alphan('otaker')
273 || $self->ut_text('reason')
274 || $self->ut_numbern('_date')
275 || $self->ut_textn('paybatch')
276 || $self->ut_enum('closed', [ '', 'Y' ])
278 return $error if $error;
280 return "refund must be > 0 " if $self->refund <= 0;
282 $self->_date(time) unless $self->_date;
284 return "unknown cust_main.custnum: ". $self->custnum
285 unless $self->crednum
286 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
288 $error = $self->payinfo_check;
289 return $error if $error;
294 =item cust_credit_refund
296 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
301 sub cust_credit_refund {
303 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
304 sort { $a->_date <=> $b->_date }
305 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
309 =item cust_pay_refund
311 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
316 sub cust_pay_refund {
318 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
319 sort { $a->_date <=> $b->_date }
320 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
326 Returns the amount of this refund that is still unapplied; which is
327 amount minus all credit applications (see L<FS::cust_credit_refund>) and
328 payment applications (see L<FS::cust_pay_refund>).
334 my $amount = $self->refund;
335 $amount -= $_->amount foreach ( $self->cust_credit_refund );
336 $amount -= $_->amount foreach ( $self->cust_pay_refund );
337 sprintf("%.2f", $amount );
348 Returns an SQL fragment to retreive the unapplied amount.
353 my ($class, $start, $end) = @_;
354 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
355 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
356 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
357 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
361 ( SELECT SUM(amount) FROM cust_credit_refund
362 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
363 $credit_start $credit_end )
367 ( SELECT SUM(amount) FROM cust_pay_refund
368 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
369 $pay_start $pay_end )
376 # Used by FS::Upgrade to migrate to a new database.
377 sub _upgrade_data { # class method
378 my ($class, %opts) = @_;
379 $class->_upgrade_otaker(%opts);
386 Delete and replace methods.
390 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.