initial checkin of module files for proper perl installation
[freeside.git] / FS / FS / 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     $payinfo =~ s/\D//g;
148     $self->payinfo($payinfo);
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   $error = $self->ut_textn('paybatch');
165   return $error if $error;
166
167   ''; #no error
168
169 }
170
171 =back
172
173 =head1 VERSION
174
175 $Id: cust_pay.pm,v 1.1 1999-08-04 09:03:53 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_bill>, schema.html from the base documentation.
184
185 =cut
186
187 1;
188