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'
98 Creates a new refund. To add the refund to the database, see L<"insert">.
102 sub table { 'cust_refund'; }
106 Adds this refund to the database.
108 For backwards-compatibility and convenience, if the additional field crednum is
109 defined, an FS::cust_credit_refund record for the full amount of the refund
110 will be created. Or (this time for convenience and consistancy), if the
111 additional field paynum is defined, an FS::cust_pay_refund record for the full
112 amount of the refund will be created. In both cases, custnum is optional.
119 local $SIG{HUP} = 'IGNORE';
120 local $SIG{INT} = 'IGNORE';
121 local $SIG{QUIT} = 'IGNORE';
122 local $SIG{TERM} = 'IGNORE';
123 local $SIG{TSTP} = 'IGNORE';
124 local $SIG{PIPE} = 'IGNORE';
126 my $oldAutoCommit = $FS::UID::AutoCommit;
127 local $FS::UID::AutoCommit = 0;
130 if ( $self->crednum ) {
131 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
133 $dbh->rollback if $oldAutoCommit;
134 return "Unknown cust_credit.crednum: ". $self->crednum;
136 $self->custnum($cust_credit->custnum);
137 } elsif ( $self->paynum ) {
138 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
140 $dbh->rollback if $oldAutoCommit;
141 return "Unknown cust_pay.paynum: ". $self->paynum;
143 $self->custnum($cust_pay->custnum);
146 my $error = $self->check;
147 return $error if $error;
149 $error = $self->SUPER::insert;
151 $dbh->rollback if $oldAutoCommit;
155 if ( $self->crednum ) {
156 my $cust_credit_refund = new FS::cust_credit_refund {
157 'crednum' => $self->crednum,
158 'refundnum' => $self->refundnum,
159 'amount' => $self->refund,
160 '_date' => $self->_date,
162 $error = $cust_credit_refund->insert;
164 $dbh->rollback if $oldAutoCommit;
167 #$self->custnum($cust_credit_refund->cust_credit->custnum);
168 } elsif ( $self->paynum ) {
169 my $cust_pay_refund = new FS::cust_pay_refund {
170 'paynum' => $self->paynum,
171 'refundnum' => $self->refundnum,
172 'amount' => $self->refund,
173 '_date' => $self->_date,
175 $error = $cust_pay_refund->insert;
177 $dbh->rollback if $oldAutoCommit;
183 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
191 Unless the closed flag is set, deletes this refund and all associated
192 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
198 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
200 local $SIG{HUP} = 'IGNORE';
201 local $SIG{INT} = 'IGNORE';
202 local $SIG{QUIT} = 'IGNORE';
203 local $SIG{TERM} = 'IGNORE';
204 local $SIG{TSTP} = 'IGNORE';
205 local $SIG{PIPE} = 'IGNORE';
207 my $oldAutoCommit = $FS::UID::AutoCommit;
208 local $FS::UID::AutoCommit = 0;
211 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
212 my $error = $cust_credit_refund->delete;
214 $dbh->rollback if $oldAutoCommit;
219 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
220 my $error = $cust_pay_refund->delete;
222 $dbh->rollback if $oldAutoCommit;
227 my $error = $self->SUPER::delete(@_);
229 $dbh->rollback if $oldAutoCommit;
233 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
239 =item replace OLD_RECORD
241 You can, but probably shouldn't modify refunds...
243 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
244 supplied, replaces this record. If there is an error, returns the error,
245 otherwise returns false.
251 return "Can't modify closed refund" if $self->closed =~ /^Y/i;
252 $self->SUPER::replace(@_);
257 Checks all fields to make sure this is a valid refund. If there is an error,
258 returns the error, otherwise returns false. Called by the insert method.
265 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
268 $self->ut_numbern('refundnum')
269 || $self->ut_numbern('custnum')
270 || $self->ut_money('refund')
271 || $self->ut_alphan('otaker')
272 || $self->ut_text('reason')
273 || $self->ut_numbern('_date')
274 || $self->ut_textn('paybatch')
275 || $self->ut_enum('closed', [ '', 'Y' ])
277 return $error if $error;
279 return "refund must be > 0 " if $self->refund <= 0;
281 $self->_date(time) unless $self->_date;
283 return "unknown cust_main.custnum: ". $self->custnum
284 unless $self->crednum
285 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
287 $error = $self->payinfo_check;
288 return $error if $error;
293 =item cust_credit_refund
295 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
300 sub cust_credit_refund {
302 map { $_ } #return $self->num_cust_credit_refund unless wantarray;
303 sort { $a->_date <=> $b->_date }
304 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
308 =item cust_pay_refund
310 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
315 sub cust_pay_refund {
317 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
318 sort { $a->_date <=> $b->_date }
319 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
325 Returns the amount of this refund that is still unapplied; which is
326 amount minus all credit applications (see L<FS::cust_credit_refund>) and
327 payment applications (see L<FS::cust_pay_refund>).
333 my $amount = $self->refund;
334 $amount -= $_->amount foreach ( $self->cust_credit_refund );
335 $amount -= $_->amount foreach ( $self->cust_pay_refund );
336 sprintf("%.2f", $amount );
347 Returns an SQL fragment to retreive the unapplied amount.
352 my ($class, $start, $end) = @_;
353 my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
354 my $credit_end = $end ? "AND cust_credit_refund._date > $end" : '';
355 my $pay_start = $start ? "AND cust_pay_refund._date <= $start" : '';
356 my $pay_end = $end ? "AND cust_pay_refund._date > $end" : '';
360 ( SELECT SUM(amount) FROM cust_credit_refund
361 WHERE cust_refund.refundnum = cust_credit_refund.refundnum
362 $credit_start $credit_end )
366 ( SELECT SUM(amount) FROM cust_pay_refund
367 WHERE cust_refund.refundnum = cust_pay_refund.refundnum
368 $pay_start $pay_end )
375 # Used by FS::Upgrade to migrate to a new database.
376 sub _upgrade_data { # class method
377 my ($class, %opts) = @_;
378 $class->_upgrade_otaker(%opts);
385 Delete and replace methods.
389 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.