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