more udpates for the new world of unapplied stuff. yay.
[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
11 @ISA = qw( FS::Record );
12
13 =head1 NAME
14
15 FS::cust_refund - Object method for cust_refund objects
16
17 =head1 SYNOPSIS
18
19   use FS::cust_refund;
20
21   $record = new FS::cust_refund \%hash;
22   $record = new FS::cust_refund { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::cust_refund represents a refund: the transfer of money to a customer;
35 equivalent to a negative payment (see L<FS::cust_pay>).  FS::cust_refund
36 inherits from FS::Record.  The following fields are currently supported:
37
38 =over 4
39
40 =item refundnum - primary key (assigned automatically for new refunds)
41
42 =item refund - Amount of the refund
43
44 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
45 L<Time::Local> and L<Date::Parse> for conversion functions.
46
47 =item payby - `CARD' (credit cards), `BILL' (billing), or `COMP' (free)
48
49 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
50
51 =item paybatch - text field for tracking card processing
52
53 =item otaker - order taker (assigned automatically, see L<FS::UID>)
54
55 =back
56
57 =head1 METHODS
58
59 =over 4
60
61 =item new HASHREF
62
63 Creates a new refund.  To add the refund to the database, see L<"insert">.
64
65 =cut
66
67 sub table { 'cust_refund'; }
68
69 =item insert
70
71 Adds this refund to the database.
72
73 For backwards-compatibility and convenience, if the additional field crednum is
74 defined, an FS::cust_credit_refund record for the full amount of the refund
75 will be created.
76
77 =cut
78
79 sub insert {
80   my $self = shift;
81
82   local $SIG{HUP} = 'IGNORE';
83   local $SIG{INT} = 'IGNORE';
84   local $SIG{QUIT} = 'IGNORE';
85   local $SIG{TERM} = 'IGNORE';
86   local $SIG{TSTP} = 'IGNORE';
87   local $SIG{PIPE} = 'IGNORE';
88
89   my $oldAutoCommit = $FS::UID::AutoCommit;
90   local $FS::UID::AutoCommit = 0;
91   my $dbh = dbh;
92
93   my $error = $self->check;
94   return $error if $error;
95
96   $error = $self->SUPER::insert;
97   if ( $error ) {
98     $dbh->rollback if $oldAutoCommit;
99     return $error;
100   }
101
102   if ( $self->crednum ) {
103     my $cust_credit_refund = new FS::cust_credit_refund {
104       'cred' => $self->cred,
105       'refundnum' => $self->refundnum,
106       'amount' => $self->refund,
107       '_date' => $self->_date,
108     };
109     $error = $cust_bill_pay->insert;
110     if ( $error ) {
111       $dbh->rollback if $oldAutoCommit;
112       return $error;
113     }
114   }
115
116   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
117
118   '';
119
120 }
121
122 =item delete
123
124 Currently unimplemented (accounting reasons).
125
126 =cut
127
128 sub delete {
129   return "Can't (yet?) delete cust_refund records!";
130 }
131
132 =item replace OLD_RECORD
133
134 Currently unimplemented (accounting reasons).
135
136 =cut
137
138 sub replace {
139    return "Can't (yet?) modify cust_refund records!";
140 }
141
142 =item check
143
144 Checks all fields to make sure this is a valid refund.  If there is an error,
145 returns the error, otherwise returns false.  Called by the insert method.
146
147 =cut
148
149 sub check {
150   my $self = shift;
151
152   my $error =
153     $self->ut_number('refundnum')
154     || $self->ut_money('amount')
155     || $self->ut_numbern('_date')
156     || $self->ut_textn('paybatch')
157   ;
158   return $error if $error;
159
160   $self->_date(time) unless $self->_date;
161
162   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
163   $self->payby($1);
164
165   if ( $self->payby eq 'CARD' ) {
166     my $payinfo = $self->payinfo;
167     $self->payinfo($payinfo =~ s/\D//g);
168     if ( $self->payinfo ) {
169       $self->payinfo =~ /^(\d{13,16})$/
170         or return "Illegal (mistyped?) credit card number (payinfo)";
171       $self->payinfo($1);
172       validate($self->payinfo) or return "Illegal credit card number";
173       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
174     } else {
175       $self->payinfo('N/A');
176     }
177
178   } else {
179     $error = $self->ut_textn('payinfo');
180     return $error if $error;
181   }
182
183   $self->otaker(getotaker);
184
185   ''; #no error
186 }
187
188 =back
189
190 =head1 VERSION
191
192 $Id: cust_refund.pm,v 1.5 2001-09-02 01:27:11 ivan Exp $
193
194 =head1 BUGS
195
196 Delete and replace methods.
197
198 =head1 SEE ALSO
199
200 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.
201
202 =cut
203
204 1;
205