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