don't generate invoices for COMP customers
[freeside.git] / FS / FS / cust_refund.pm
1 package FS::cust_refund;
2
3 use strict;
4 use vars qw( @ISA );
5 use Business::CreditCard;
6 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::UID qw(getotaker);
8 use FS::cust_credit;
9 use FS::cust_credit_refund;
10 use FS::cust_pay_refund;
11 use FS::cust_main;
12
13 @ISA = qw( FS::Record );
14
15 =head1 NAME
16
17 FS::cust_refund - Object method for cust_refund objects
18
19 =head1 SYNOPSIS
20
21   use FS::cust_refund;
22
23   $record = new FS::cust_refund \%hash;
24   $record = new FS::cust_refund { 'column' => 'value' };
25
26   $error = $record->insert;
27
28   $error = $new_record->replace($old_record);
29
30   $error = $record->delete;
31
32   $error = $record->check;
33
34 =head1 DESCRIPTION
35
36 An FS::cust_refund represents a refund: the transfer of money to a customer;
37 equivalent to a negative payment (see L<FS::cust_pay>).  FS::cust_refund
38 inherits from FS::Record.  The following fields are currently supported:
39
40 =over 4
41
42 =item refundnum - primary key (assigned automatically for new refunds)
43
44 =item custnum - customer (see L<FS::cust_main>)
45
46 =item refund - Amount of the refund
47
48 =item reason - Reason for the refund
49
50 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
51 L<Time::Local> and L<Date::Parse> for conversion functions.
52
53 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
54 `LECB' (Phone bill billing), `BILL' (billing), or `COMP' (free)
55
56 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
57
58 =item paybatch - text field for tracking card processing
59
60 =item otaker - order taker (assigned automatically, see L<FS::UID>)
61
62 =item closed - books closed flag, empty or `Y'
63
64 =back
65
66 =head1 METHODS
67
68 =over 4
69
70 =item new HASHREF
71
72 Creates a new refund.  To add the refund to the database, see L<"insert">.
73
74 =cut
75
76 sub table { 'cust_refund'; }
77
78 =item insert
79
80 Adds this refund to the database.
81
82 For backwards-compatibility and convenience, if the additional field crednum is
83 defined, an FS::cust_credit_refund record for the full amount of the refund
84 will be created.  Or (this time for convenience and consistancy), if the
85 additional field paynum is defined, an FS::cust_pay_refund record for the full
86 amount of the refund will be created.  In both cases, custnum is optional.
87
88 =cut
89
90 sub insert {
91   my $self = shift;
92
93   local $SIG{HUP} = 'IGNORE';
94   local $SIG{INT} = 'IGNORE';
95   local $SIG{QUIT} = 'IGNORE';
96   local $SIG{TERM} = 'IGNORE';
97   local $SIG{TSTP} = 'IGNORE';
98   local $SIG{PIPE} = 'IGNORE';
99
100   my $oldAutoCommit = $FS::UID::AutoCommit;
101   local $FS::UID::AutoCommit = 0;
102   my $dbh = dbh;
103
104   if ( $self->crednum ) {
105     my $cust_credit = qsearchs('cust_credit', { 'crednum' => $self->crednum } )
106       or do {
107         $dbh->rollback if $oldAutoCommit;
108         return "Unknown cust_credit.crednum: ". $self->crednum;
109       };
110     $self->custnum($cust_credit->custnum);
111   } elsif ( $self->paynum ) {
112     my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } )
113       or do {
114         $dbh->rollback if $oldAutoCommit;
115         return "Unknown cust_pay.paynum: ". $self->paynum;
116       };
117     $self->custnum($cust_pay->custnum);
118   }
119
120   my $error = $self->check;
121   return $error if $error;
122
123   $error = $self->SUPER::insert;
124   if ( $error ) {
125     $dbh->rollback if $oldAutoCommit;
126     return $error;
127   }
128
129   if ( $self->crednum ) {
130     my $cust_credit_refund = new FS::cust_credit_refund {
131       'crednum'   => $self->crednum,
132       'refundnum' => $self->refundnum,
133       'amount'    => $self->refund,
134       '_date'     => $self->_date,
135     };
136     $error = $cust_credit_refund->insert;
137     if ( $error ) {
138       $dbh->rollback if $oldAutoCommit;
139       return $error;
140     }
141     #$self->custnum($cust_credit_refund->cust_credit->custnum);
142   } elsif ( $self->paynum ) {
143     my $cust_pay_refund = new FS::cust_pay_refund {
144       'paynum'    => $self->paynum,
145       'refundnum' => $self->refundnum,
146       'amount'    => $self->refund,
147       '_date'     => $self->_date,
148     };
149     $error = $cust_pay_refund->insert;
150     if ( $error ) {
151       $dbh->rollback if $oldAutoCommit;
152       return $error;
153     }
154   }
155
156
157   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
158
159   '';
160
161 }
162
163 =item delete
164
165 Currently unimplemented (accounting reasons).
166
167 =cut
168
169 sub delete {
170   my $self = shift;
171   return "Can't delete closed refund" if $self->closed =~ /^Y/i;
172   $self->SUPER::delete(@_);
173 }
174
175 =item replace OLD_RECORD
176
177 Currently unimplemented (accounting reasons).
178
179 =cut
180
181 sub replace {
182    return "Can't (yet?) modify cust_refund records!";
183 }
184
185 =item check
186
187 Checks all fields to make sure this is a valid refund.  If there is an error,
188 returns the error, otherwise returns false.  Called by the insert method.
189
190 =cut
191
192 sub check {
193   my $self = shift;
194
195   my $error =
196     $self->ut_numbern('refundnum')
197     || $self->ut_numbern('custnum')
198     || $self->ut_money('refund')
199     || $self->ut_text('reason')
200     || $self->ut_numbern('_date')
201     || $self->ut_textn('paybatch')
202     || $self->ut_enum('closed', [ '', 'Y' ])
203   ;
204   return $error if $error;
205
206   return "refund must be > 0 " if $self->refund <= 0;
207
208   $self->_date(time) unless $self->_date;
209
210   return "unknown cust_main.custnum: ". $self->custnum
211     unless $self->crednum 
212            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
213
214   $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP)$/ or return "Illegal payby";
215   $self->payby($1);
216
217   #false laziness with cust_pay::check
218   if ( $self->payby eq 'CARD' ) {
219     my $payinfo = $self->payinfo;
220     $payinfo =~ s/\D//g;
221     $self->payinfo($payinfo);
222     if ( $self->payinfo ) {
223       $self->payinfo =~ /^(\d{13,16})$/
224         or return "Illegal (mistyped?) credit card number (payinfo)";
225       $self->payinfo($1);
226       validate($self->payinfo) or return "Illegal credit card number";
227       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
228     } else {
229       $self->payinfo('N/A');
230     }
231
232   } else {
233     $error = $self->ut_textn('payinfo');
234     return $error if $error;
235   }
236
237   $self->otaker(getotaker);
238
239   $self->SUPER::check;
240 }
241
242 =item cust_credit_refund
243
244 Returns all applications to credits (see L<FS::cust_credit_refund>) for this
245 refund.
246
247 =cut
248
249 sub cust_credit_refund {
250   my $self = shift;
251   sort { $a->_date <=> $b->_date }
252     qsearch( 'cust_credit_refund', { 'refundnum' => $self->refundnum } )
253   ;
254 }
255
256 =item cust_pay_refund
257
258 Returns all applications to payments (see L<FS::cust_pay_refund>) for this
259 refund.
260
261 =cut
262
263 sub cust_pay_refund {
264   my $self = shift;
265   sort { $a->_date <=> $b->_date }
266     qsearch( 'cust_pay_refund', { 'refundnum' => $self->refundnum } )
267   ;
268 }
269
270 =item unapplied
271
272 Returns the amount of this refund that is still unapplied; which is
273 amount minus all credit applications (see L<FS::cust_credit_refund>) and
274 payment applications (see L<FS::cust_pay_refund>).
275
276 =cut
277
278 sub unapplied {
279   my $self = shift;
280   my $amount = $self->refund;
281   $amount -= $_->amount foreach ( $self->cust_credit_refund );
282   $amount -= $_->amount foreach ( $self->cust_pay_refund );
283   sprintf("%.2f", $amount );
284 }
285
286
287
288 =item payinfo_masked
289
290 Returns a "masked" payinfo field with all but the last four characters replaced
291 by 'x'es.  Useful for displaying credit cards.
292
293 =cut
294
295
296 sub payinfo_masked {
297   my $self = shift;
298   my $payinfo = $self->payinfo;
299   'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
300 }
301
302
303 =back
304
305 =head1 BUGS
306
307 Delete and replace methods.  payinfo_masked false laziness with cust_main.pm
308 and cust_pay.pm
309
310 =head1 SEE ALSO
311
312 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.
313
314 =cut
315
316 1;
317