1 package FS::cust_refund;
4 use vars qw( @ISA @encrypted_fields );
5 use Business::CreditCard;
6 use FS::UID qw(getotaker);
7 use FS::Record qw( qsearch qsearchs dbh );
8 use FS::cust_main_Mixin;
9 use FS::payinfo_transaction_Mixin;
11 use FS::cust_credit_refund;
12 use FS::cust_pay_refund;
15 @ISA = qw( FS::payinfo_transaction_Mixin FS::cust_main_Mixin FS::Record );
17 @encrypted_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:
46 =item refundnum - primary key (assigned automatically for new refunds)
48 =item custnum - customer (see L<FS::cust_main>)
50 =item refund - Amount of the refund
52 =item reason - Reason for the refund
54 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
55 L<Time::Local> and L<Date::Parse> for conversion functions.
57 =item payby - Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
59 =item payinfo - Payment Information (See L<FS::payinfo_Mixin> for data format)
61 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
63 =item paybatch - text field for tracking card processing
65 =item otaker - order taker (assigned automatically, see L<FS::UID>)
67 =item closed - books closed flag, empty or `Y'
77 Creates a new refund. To add the refund to the database, see L<"insert">.
81 sub table { 'cust_refund'; }
85 Adds this refund to the database.
87 For backwards-compatibility and convenience, if the additional field crednum is
88 defined, an FS::cust_credit_refund record for the full amount of the refund
89 will be created. Or (this time for convenience and consistancy), if the
90 additional field paynum is defined, an FS::cust_pay_refund record for the full
91 amount of the refund will be created. In both cases, custnum is optional.
98 local $SIG{HUP} = 'IGNORE';
99 local $SIG{INT} = 'IGNORE';
100 local $SIG{QUIT} = 'IGNORE';
101 local $SIG{TERM} = 'IGNORE';
102 local $SIG{TSTP} = 'IGNORE';
103 local $SIG{PIPE} = 'IGNORE';
105 my $oldAutoCommit = $FS::UID::AutoCommit;
106 local $FS::UID::AutoCommit = 0;
109 if ( $self->crednum ) {
110 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
112 $dbh->rollback if $oldAutoCommit;
113 return "Unknown cust_credit.crednum: ". $self->crednum;
115 $self->custnum($cust_credit->custnum);
116 } elsif ( $self->paynum ) {
117 my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
119 $dbh->rollback if $oldAutoCommit;
120 return "Unknown cust_pay.paynum: ". $self->paynum;
122 $self->custnum($cust_pay->custnum);
125 my $error = $self->check;
126 return $error if $error;
128 $error = $self->SUPER::insert;
130 $dbh->rollback if $oldAutoCommit;
134 if ( $self->crednum ) {
135 my $cust_credit_refund = new FS::cust_credit_refund {
136 'crednum' => $self->crednum,
137 'refundnum' => $self->refundnum,
138 'amount' => $self->refund,
139 '_date' => $self->_date,
141 $error = $cust_credit_refund->insert;
143 $dbh->rollback if $oldAutoCommit;
146 #$self->custnum($cust_credit_refund->cust_credit->custnum);
147 } elsif ( $self->paynum ) {
148 my $cust_pay_refund = new FS::cust_pay_refund {
149 'paynum' => $self->paynum,
150 'refundnum' => $self->refundnum,
151 'amount' => $self->refund,
152 '_date' => $self->_date,
154 $error = $cust_pay_refund->insert;
156 $dbh->rollback if $oldAutoCommit;
162 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
170 Unless the closed flag is set, deletes this refund and all associated
171 applications (see L<FS::cust_credit_refund> and L<FS::cust_pay_refund>).
177 return "Can't delete closed refund" if $self->closed =~ /^Y/i;
179 local $SIG{HUP} = 'IGNORE';
180 local $SIG{INT} = 'IGNORE';
181 local $SIG{QUIT} = 'IGNORE';
182 local $SIG{TERM} = 'IGNORE';
183 local $SIG{TSTP} = 'IGNORE';
184 local $SIG{PIPE} = 'IGNORE';
186 my $oldAutoCommit = $FS::UID::AutoCommit;
187 local $FS::UID::AutoCommit = 0;
190 foreach my $cust_credit_refund ( $self->cust_credit_refund ) {
191 my $error = $cust_credit_refund->delete;
193 $dbh->rollback if $oldAutoCommit;
198 foreach my $cust_pay_refund ( $self->cust_pay_refund ) {
199 my $error = $cust_pay_refund->delete;
201 $dbh->rollback if $oldAutoCommit;
206 my $error = $self->SUPER::delete(@_);
208 $dbh->rollback if $oldAutoCommit;
212 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
218 =item replace OLD_RECORD
220 Modifying a refund? Well, don't say I didn't warn you.
226 $self->SUPER::replace(@_);
231 Checks all fields to make sure this is a valid refund. If there is an error,
232 returns the error, otherwise returns false. Called by the insert method.
239 $self->otaker(getotaker) unless ($self->otaker);
242 $self->ut_numbern('refundnum')
243 || $self->ut_numbern('custnum')
244 || $self->ut_money('refund')
245 || $self->ut_alpha('otaker')
246 || $self->ut_text('reason')
247 || $self->ut_numbern('_date')
248 || $self->ut_textn('paybatch')
249 || $self->ut_enum('closed', [ '', 'Y' ])
251 return $error if $error;
253 return "refund must be > 0 " if $self->refund <= 0;
255 $self->_date(time) unless $self->_date;
257 return "unknown cust_main.custnum: ". $self->custnum
258 unless $self->crednum
259 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
261 $error = $self->payinfo_check;
262 return $error if $error;
267 =item cust_credit_refund
269 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
274 sub cust_credit_refund {
276 sort { $a->_date <=> $b->_date }
277 qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
281 =item cust_pay_refund
283 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
288 sub cust_pay_refund {
290 sort { $a->_date <=> $b->_date }
291 qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
297 Returns the amount of this refund that is still unapplied; which is
298 amount minus all credit applications (see L<FS::cust_credit_refund>) and
299 payment applications (see L<FS::cust_pay_refund>).
305 my $amount = $self->refund;
306 $amount -= $_->amount foreach ( $self->cust_credit_refund );
307 $amount -= $_->amount foreach ( $self->cust_pay_refund );
308 sprintf("%.2f", $amount );
319 Returns an SQL fragment to retreive the unapplied amount.
328 ( SELECT SUM(amount) FROM cust_credit_refund
329 WHERE cust_refund.refundnum = cust_credit_refund.refundnum )
333 ( SELECT SUM(amount) FROM cust_pay_refund
334 WHERE cust_refund.refundnum = cust_pay_refund.refundnum )
345 Delete and replace methods.
349 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.