cust_refund and cust_pay get custnums
[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   my $error = $self->check;
97   return $error if $error;
98
99   if ( $self->crednum ) {
100     my $cust_credit_refund = new FS::cust_credit_refund {
101       'cred'      => $self->cred,
102       'refundnum' => $self->refundnum,
103       'amount'    => $self->refund,
104       '_date'     => $self->_date,
105     };
106     $error = $cust_bill_pay->insert;
107     if ( $error ) {
108       $dbh->rollback if $oldAutoCommit;
109       return $error;
110     }
111     $self->custnum($cust_credit_refund->cust_credit->custnum);
112   }
113
114   $error = $self->SUPER::insert;
115   if ( $error ) {
116     $dbh->rollback if $oldAutoCommit;
117     return $error;
118   }
119
120   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
121
122   '';
123
124 }
125
126 =item delete
127
128 Currently unimplemented (accounting reasons).
129
130 =cut
131
132 sub delete {
133   return "Can't (yet?) delete cust_refund records!";
134 }
135
136 =item replace OLD_RECORD
137
138 Currently unimplemented (accounting reasons).
139
140 =cut
141
142 sub replace {
143    return "Can't (yet?) modify cust_refund records!";
144 }
145
146 =item check
147
148 Checks all fields to make sure this is a valid refund.  If there is an error,
149 returns the error, otherwise returns false.  Called by the insert method.
150
151 =cut
152
153 sub check {
154   my $self = shift;
155
156   my $error =
157     $self->ut_number('refundnum')
158     || $self->ut_number('custnum')
159     || $self->ut_money('amount')
160     || $self->ut_numbern('_date')
161     || $self->ut_textn('paybatch')
162   ;
163   return $error if $error;
164
165   $self->_date(time) unless $self->_date;
166
167   return "unknown cust_main.custnum: ". $self->custnum
168     unless $self->invnum 
169            ||  qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
170
171   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
172   $self->payby($1);
173
174   if ( $self->payby eq 'CARD' ) {
175     my $payinfo = $self->payinfo;
176     $self->payinfo($payinfo =~ s/\D//g);
177     if ( $self->payinfo ) {
178       $self->payinfo =~ /^(\d{13,16})$/
179         or return "Illegal (mistyped?) credit card number (payinfo)";
180       $self->payinfo($1);
181       validate($self->payinfo) or return "Illegal credit card number";
182       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
183     } else {
184       $self->payinfo('N/A');
185     }
186
187   } else {
188     $error = $self->ut_textn('payinfo');
189     return $error if $error;
190   }
191
192   $self->otaker(getotaker);
193
194   ''; #no error
195 }
196
197 =back
198
199 =head1 VERSION
200
201 $Id: cust_refund.pm,v 1.6 2001-09-02 02:46:55 ivan Exp $
202
203 =head1 BUGS
204
205 Delete and replace methods.
206
207 =head1 SEE ALSO
208
209 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.
210
211 =cut
212
213 1;
214