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