jeff's on a bugfinding roll here, thanks!
[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( 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), `BILL' (billing), or `COMP' (free)
51
52 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
53
54 =item paybatch - text field for tracking card processing
55
56 =item otaker - order taker (assigned automatically, see L<FS::UID>)
57
58 =back
59
60 =head1 METHODS
61
62 =over 4
63
64 =item new HASHREF
65
66 Creates a new refund.  To add the refund to the database, see L<"insert">.
67
68 =cut
69
70 sub table { 'cust_refund'; }
71
72 =item insert
73
74 Adds this refund to the database.
75
76 For backwards-compatibility and convenience, if the additional field crednum is
77 defined, an FS::cust_credit_refund record for the full amount of the refund
78 will be created.  In this case, custnum is optional.
79
80 =cut
81
82 sub insert {
83   my $self = shift;
84
85   local $SIG{HUP} = 'IGNORE';
86   local $SIG{INT} = 'IGNORE';
87   local $SIG{QUIT} = 'IGNORE';
88   local $SIG{TERM} = 'IGNORE';
89   local $SIG{TSTP} = 'IGNORE';
90   local $SIG{PIPE} = 'IGNORE';
91
92   my $oldAutoCommit = $FS::UID::AutoCommit;
93   local $FS::UID::AutoCommit = 0;
94   my $dbh = dbh;
95
96   if ( $self->crednum ) {
97     my $cust_credit = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
98       or do {
99         $dbh->rollback if $oldAutoCommit;
100         return "Unknown cust_credit.crednum: ". $self->crednum;
101       };
102     $self->custnum($cust_credit->custnum);
103   }
104
105   my $error = $self->check;
106   return $error if $error;
107
108   $error = $self->SUPER::insert;
109   if ( $error ) {
110     $dbh->rollback if $oldAutoCommit;
111     return $error;
112   }
113
114   if ( $self->crednum ) {
115     my $cust_credit_refund = new FS::cust_credit_refund {
116       'crednum'   => $self->crednum,
117       'refundnum' => $self->refundnum,
118       'amount'    => $self->refund,
119       '_date'     => $self->_date,
120     };
121     $error = $cust_credit_refund->insert;
122     if ( $error ) {
123       $dbh->rollback if $oldAutoCommit;
124       return $error;
125     }
126     #$self->custnum($cust_credit_refund->cust_credit->custnum);
127   }
128
129
130   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
131
132   '';
133
134 }
135
136 sub upgrade_replace { #1.3.x->1.4.x
137   my $self = shift;
138
139   local $SIG{HUP} = 'IGNORE';
140   local $SIG{INT} = 'IGNORE';
141   local $SIG{QUIT} = 'IGNORE';
142   local $SIG{TERM} = 'IGNORE';
143   local $SIG{TSTP} = 'IGNORE';
144   local $SIG{PIPE} = 'IGNORE';
145
146   my $oldAutoCommit = $FS::UID::AutoCommit;
147   local $FS::UID::AutoCommit = 0;
148   my $dbh = dbh;
149
150   my $error = $self->check;
151   return $error if $error;
152
153   my %new = $self->hash;
154   my $new = FS::cust_refund->new(\%new);
155
156   if ( $self->crednum ) {
157     my $cust_credit_refund = new FS::cust_credit_refund {
158       'crednum'   => $self->crednum,
159       'refundnum' => $self->refundnum,
160       'amount'    => $self->refund,
161       '_date'     => $self->_date,
162     };
163     $error = $cust_credit_refund->insert;
164     if ( $error ) {
165       $dbh->rollback if $oldAutoCommit;
166       return $error;
167     }
168     $new->custnum($cust_credit_refund->cust_credit->custnum);
169   } else {
170     die;
171   }
172
173   $error = $new->SUPER::replace($self);
174   if ( $error ) {
175     $dbh->rollback if $oldAutoCommit;
176     return $error;
177   }
178
179   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
180
181   '';
182
183 }
184
185 =item delete
186
187 Currently unimplemented (accounting reasons).
188
189 =cut
190
191 sub delete {
192   return "Can't (yet?) delete cust_refund records!";
193 }
194
195 =item replace OLD_RECORD
196
197 Currently unimplemented (accounting reasons).
198
199 =cut
200
201 sub replace {
202    return "Can't (yet?) modify cust_refund records!";
203 }
204
205 =item check
206
207 Checks all fields to make sure this is a valid refund.  If there is an error,
208 returns the error, otherwise returns false.  Called by the insert method.
209
210 =cut
211
212 sub check {
213   my $self = shift;
214
215   my $error =
216     $self->ut_number('refundnum')
217     || $self->ut_numbern('custnum')
218     || $self->ut_money('refund')
219     || $self->ut_numbern('_date')
220     || $self->ut_textn('paybatch')
221   ;
222   return $error if $error;
223
224   return "refund must be > 0 " if $self->refund == 0;
225
226   $self->_date(time) unless $self->_date;
227
228   return "unknown cust_main.custnum: ". $self->custnum
229     unless $self->crednum 
230            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
231
232   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
233   $self->payby($1);
234
235   #false laziness with cust_pay::check
236   if ( $self->payby eq 'CARD' ) {
237     my $payinfo = $self->payinfo;
238     $payinfo =~ s/\D//g;
239     $self->payinfo($payinfo);
240     if ( $self->payinfo ) {
241       $self->payinfo =~ /^(\d{13,16})$/
242         or return "Illegal (mistyped?) credit card number (payinfo)";
243       $self->payinfo($1);
244       validate($self->payinfo) or return "Illegal credit card number";
245       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
246     } else {
247       $self->payinfo('N/A');
248     }
249
250   } else {
251     $error = $self->ut_textn('payinfo');
252     return $error if $error;
253   }
254
255   $self->otaker(getotaker);
256
257   ''; #no error
258 }
259
260 =back
261
262 =head1 VERSION
263
264 $Id: cust_refund.pm,v 1.13 2002-01-24 11:52:02 ivan Exp $
265
266 =head1 BUGS
267
268 Delete and replace methods.
269
270 =head1 SEE ALSO
271
272 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.
273
274 =cut
275
276 1;
277