get rid of FS::SSH.pm (became Net::SSH and Net::SCP on CPAN)
[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; the transfer of money from a
33 customer.  FS::cust_pay inherits from FS::Record.  The following fields are
34 currently supported:
35
36 =over 4
37
38 =item paynum - primary key (assigned automatically for new payments)
39
40 =item invnum - Invoice (see L<FS::cust_bill>)
41
42 =item paid - Amount of this payment
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 paybatch - text field for tracking card processing
52
53 =back
54
55 =head1 METHODS
56
57 =over 4 
58
59 =item new HASHREF
60
61 Creates a new payment.  To add the payment to the databse, see L<"insert">.
62
63 =cut
64
65 sub table { 'cust_pay'; }
66
67 =item insert
68
69 Adds this payment to the databse, and updates the invoice (see
70 L<FS::cust_bill>).
71
72 =cut
73
74 sub insert {
75   my $self = shift;
76
77   my $error = $self->check;
78   return $error if $error;
79
80   my $old_cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
81   return "Unknown invnum" unless $old_cust_bill;
82
83   $self->SUPER::insert;
84 }
85
86 =item delete
87
88 Currently unimplemented (accounting reasons).
89
90 =cut
91
92 sub delete {
93   return "Can't (yet?) delete cust_pay records!";
94 }
95
96 =item replace OLD_RECORD
97
98 Currently unimplemented (accounting reasons).
99
100 =cut
101
102 sub replace {
103    return "Can't (yet?) modify cust_pay records!";
104 }
105
106 =item check
107
108 Checks all fields to make sure this is a valid payment.  If there is an error,
109 returns the error, otherwise returns false.  Called by the insert method.
110
111 =cut
112
113 sub check {
114   my $self = shift;
115
116   my $error;
117
118   $error =
119     $self->ut_numbern('paynum')
120     || $self->ut_number('invnum')
121     || $self->ut_money('paid')
122     || $self->ut_numbern('_date')
123   ;
124   return $error if $error;
125
126   $self->_date(time) unless $self->_date;
127
128   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
129   $self->payby($1);
130
131   if ( $self->payby eq 'CARD' ) {
132     my $payinfo = $self->payinfo;
133     $payinfo =~ s/\D//g;
134     $self->payinfo($payinfo);
135     if ( $self->payinfo ) {
136       $self->payinfo =~ /^(\d{13,16})$/
137         or return "Illegal (mistyped?) credit card number (payinfo)";
138       $self->payinfo($1);
139       validate($self->payinfo) or return "Illegal credit card number";
140       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
141     } else {
142       $self->payinfo('N/A');
143     }
144
145   } else {
146     $error = $self->ut_textn('payinfo');
147     return $error if $error;
148   }
149
150   $error = $self->ut_textn('paybatch');
151   return $error if $error;
152
153   ''; #no error
154
155 }
156
157 =back
158
159 =head1 VERSION
160
161 $Id: cust_pay.pm,v 1.3 2001-04-09 23:05:15 ivan Exp $
162
163 =head1 BUGS
164
165 Delete and replace methods.
166
167 =head1 SEE ALSO
168
169 L<FS::Record>, L<FS::cust_bill>, schema.html from the base documentation.
170
171 =cut
172
173 1;
174