mostly properly OO, some work still to be done with svc_ stuff
[freeside.git] / site_perl / 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
95   $error = $new_cust_credit->replace($old_cust_credit);
96   return "Error modifying cust_credit: $error" if $error;
97
98   $self->SUPER::insert;
99 }
100
101 =item delete
102
103 Currently unimplemented (accounting reasons).
104
105 =cut
106
107 sub delete {
108   return "Can't (yet?) delete cust_refund records!";
109 }
110
111 =item replace OLD_RECORD
112
113 Currently unimplemented (accounting reasons).
114
115 =cut
116
117 sub replace {
118    return "Can't (yet?) modify cust_refund records!";
119 }
120
121 =item check
122
123 Checks all fields to make sure this is a valid refund.  If there is an error,
124 returns the error, otherwise returns false.  Called by the insert method.
125
126 =cut
127
128 sub check {
129   my $self = shift;
130
131   my $error;
132
133   $error =
134     $self->ut_number('refundnum')
135     || $self->ut_number('crednum')
136     || $self->ut_money('amount')
137     || $self->ut_numbern('_date')
138   ;
139   return $error if $error;
140
141   $self->_date(time) unless $self->_date;
142
143   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
144   $self->payby($1);
145
146   if ( $self->payby eq 'CARD' ) {
147     my $payinfo = $self->payinfo;
148     $self->payinfo($payinfo =~ s/\D//g);
149     if ( $self->payinfo ) {
150       $self->payinfo =~ /^(\d{13,16})$/
151         or return "Illegal (mistyped?) credit card number (payinfo)";
152       $self->payinfo($1);
153       validate($self->payinfo) or return "Illegal credit card number";
154       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
155     } else {
156       $self->payinfo('N/A');
157     }
158
159   } else {
160     $error = $self->ut_textn('payinfo');
161     return $error if $error;
162   }
163
164   $self->otaker(getotaker);
165
166   ''; #no error
167 }
168
169 =back
170
171 =head1 VERSION
172
173 $Id: cust_refund.pm,v 1.2 1998-12-29 11:59:46 ivan Exp $
174
175 =head1 BUGS
176
177 Delete and replace methods.
178
179 =head1 SEE ALSO
180
181 L<FS::Record>, L<FS::cust_credit>, schema.html from the base documentation.
182
183 =head1 HISTORY
184
185 ivan@sisd.com 98-mar-18
186
187 ->create had wrong tablename ivan@sisd.com 98-jun-16
188 (finish me!)
189
190 pod and finish up ivan@sisd.com 98-sep-21
191
192 $Log: cust_refund.pm,v $
193 Revision 1.2  1998-12-29 11:59:46  ivan
194 mostly properly OO, some work still to be done with svc_ stuff
195
196
197 =cut
198
199 1;
200