oops, don't comment out &swapuid in &adminsuidsetup!
[freeside.git] / site_perl / cust_pay.pm
1 package FS::cust_pay;
2
3 use strict;
4 use vars qw( @ISA );
5 use Business::CreditCard;
6 use FS::Record qw( qsearchs );
7 use FS::cust_bill;
8
9 @ISA = qw( FS::Record );
10
11 =head1 NAME
12
13 FS::cust_pay - Object methods for cust_pay objects
14
15 =head1 SYNOPSIS
16
17   use FS::cust_pay;
18
19   $record = new FS::cust_pay \%hash;
20   $record = new FS::cust_pay { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::cust_pay object represents a payment.  FS::cust_pay inherits from
33 FS::Record.  The following fields are currently supported:
34
35 =over 4
36
37 =item paynum - primary key (assigned automatically for new payments)
38
39 =item invnum - Invoice (see L<FS::cust_bill>)
40
41 =item paid - Amount of this payment
42
43 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
44 L<Time::Local> and L<Date::Parse> for conversion functions.
45
46 =item payby - `CARD' (credit cards), `BILL' (billing), or `COMP' (free)
47
48 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
49
50 =item paybatch - text field for tracking card processing
51
52 =back
53
54 =head1 METHODS
55
56 =over 4 
57
58 =item new HASHREF
59
60 Creates a new payment.  To add the payment to the databse, see L<"insert">.
61
62 =cut
63
64 sub table { 'cust_pay'; }
65
66 =item insert
67
68 Adds this payment to the databse, and updates the invoice (see
69 L<FS::cust_bill>).
70
71 =cut
72
73 sub insert {
74   my $self = shift;
75
76   my $error;
77
78   $error = $self->check;
79   return $error if $error;
80
81   my $old_cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
82   return "Unknown invnum" unless $old_cust_bill;
83   my %hash = $old_cust_bill->hash;
84   $hash{'owed'} = sprintf("%.2f", $hash{owed} - $self->paid );
85   my $new_cust_bill = new FS::cust_bill ( \%hash );
86
87   local $SIG{HUP} = 'IGNORE';
88   local $SIG{INT} = 'IGNORE';
89   local $SIG{QUIT} = 'IGNORE';
90   local $SIG{TERM} = 'IGNORE';
91   local $SIG{TSTP} = 'IGNORE';
92   local $SIG{PIPE} = 'IGNORE';
93
94   $error = $new_cust_bill->replace($old_cust_bill);
95   return "Error modifying cust_bill: $error" if $error;
96
97   $self->SUPER::insert;
98 }
99
100 =item delete
101
102 Currently unimplemented (accounting reasons).
103
104 =cut
105
106 sub delete {
107   return "Can't (yet?) delete cust_pay records!";
108 }
109
110 =item replace OLD_RECORD
111
112 Currently unimplemented (accounting reasons).
113
114 =cut
115
116 sub replace {
117    return "Can't (yet?) modify cust_pay records!";
118 }
119
120 =item check
121
122 Checks all fields to make sure this is a valid payment.  If there is an error,
123 returns the error, otherwise returns false.  Called by the insert method.
124
125 =cut
126
127 sub check {
128   my $self = shift;
129
130   my $error;
131
132   $error =
133     $self->ut_numbern('paynum')
134     || $self->ut_number('invnum')
135     || $self->ut_money('paid')
136     || $self->ut_numbern('_date')
137   ;
138   return $error if $error;
139
140   $self->_date(time) unless $self->_date;
141
142   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
143   $self->payby($1);
144
145   if ( $self->payby eq 'CARD' ) {
146     my $payinfo = $self->payinfo;
147     $self->payinfo($payinfo =~ s/\D//g);
148     if ( $self->payinfo ) {
149       $self->payinfo =~ /^(\d{13,16})$/
150         or return "Illegal (mistyped?) credit card number (payinfo)";
151       $self->payinfo($1);
152       validate($self->payinfo) or return "Illegal credit card number";
153       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
154     } else {
155       $self->payinfo('N/A');
156     }
157
158   } else {
159     $error = $self->ut_textn('payinfo');
160     return $error if $error;
161   }
162
163   $error = $self->ut_textn('paybatch');
164   return $error if $error;
165
166   ''; #no error
167
168 }
169
170 =back
171
172 =head1 VERSION
173
174 $Id: cust_pay.pm,v 1.3 1999-01-25 12:26:11 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_bill>, schema.html from the base documentation.
183
184 =head1 HISTORY
185
186 ivan@voicenet.com 97-jul-1 - 25 - 29
187
188 new api ivan@sisd.com 98-mar-13
189
190 pod ivan@sisd.com 98-sep-21
191
192 $Log: cust_pay.pm,v $
193 Revision 1.3  1999-01-25 12:26:11  ivan
194 yet more mod_perl stuff
195
196 Revision 1.2  1998/12/29 11:59:43  ivan
197 mostly properly OO, some work still to be done with svc_ stuff
198
199
200 =cut
201
202 1;
203