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