initial checkin of module files for proper perl installation
[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.  FS::cust_refund inherits from
34 FS::Record.  The following fields are currently supported:
35
36 =over 4
37
38 =item refundnum - primary key (assigned automatically for new refunds)
39
40 =item crednum - Credit (see L<FS::cust_credit>)
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 otaker - order taker (assigned automatically, see L<FS::UID>)
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new refund.  To add the refund to the database, see L<"insert">.
62
63 =cut
64
65 sub table { 'cust_refund'; }
66
67 =item insert
68
69 Adds this refund to the database, and updates the credit (see
70 L<FS::cust_credit>).
71
72 =cut
73
74 sub insert {
75   my $self = shift;
76
77   my $error;
78
79   $error=$self->check;
80   return $error if $error;
81
82   my $old_cust_credit =
83     qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
84   return "Unknown crednum" unless $old_cust_credit;
85   my %hash = $old_cust_credit->hash;
86   $hash{credited} = sprintf("%.2f", $hash{credited} - $self->refund );
87   my($new_cust_credit) = new FS::cust_credit ( \%hash );
88
89   local $SIG{HUP} = 'IGNORE';
90   local $SIG{INT} = 'IGNORE';
91   local $SIG{QUIT} = 'IGNORE';
92   local $SIG{TERM} = 'IGNORE';
93   local $SIG{TSTP} = 'IGNORE';
94   local $SIG{PIPE} = 'IGNORE';
95
96   $error = $new_cust_credit->replace($old_cust_credit);
97   return "Error modifying cust_credit: $error" if $error;
98
99   $self->SUPER::insert;
100 }
101
102 =item delete
103
104 Currently unimplemented (accounting reasons).
105
106 =cut
107
108 sub delete {
109   return "Can't (yet?) delete cust_refund records!";
110 }
111
112 =item replace OLD_RECORD
113
114 Currently unimplemented (accounting reasons).
115
116 =cut
117
118 sub replace {
119    return "Can't (yet?) modify cust_refund records!";
120 }
121
122 =item check
123
124 Checks all fields to make sure this is a valid refund.  If there is an error,
125 returns the error, otherwise returns false.  Called by the insert method.
126
127 =cut
128
129 sub check {
130   my $self = shift;
131
132   my $error;
133
134   $error =
135     $self->ut_number('refundnum')
136     || $self->ut_number('crednum')
137     || $self->ut_money('amount')
138     || $self->ut_numbern('_date')
139   ;
140   return $error if $error;
141
142   $self->_date(time) unless $self->_date;
143
144   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
145   $self->payby($1);
146
147   if ( $self->payby eq 'CARD' ) {
148     my $payinfo = $self->payinfo;
149     $self->payinfo($payinfo =~ s/\D//g);
150     if ( $self->payinfo ) {
151       $self->payinfo =~ /^(\d{13,16})$/
152         or return "Illegal (mistyped?) credit card number (payinfo)";
153       $self->payinfo($1);
154       validate($self->payinfo) or return "Illegal credit card number";
155       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
156     } else {
157       $self->payinfo('N/A');
158     }
159
160   } else {
161     $error = $self->ut_textn('payinfo');
162     return $error if $error;
163   }
164
165   $self->otaker(getotaker);
166
167   ''; #no error
168 }
169
170 =back
171
172 =head1 VERSION
173
174 $Id: cust_refund.pm,v 1.1 1999-08-04 09:03:53 ivan Exp $
175
176 =head1 BUGS
177
178 Delete and replace methods.
179
180 =head1 SEE ALSO
181
182 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.
183
184 =cut
185
186 1;
187