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