fc9112b00efbfeb4a176390e6a2c14311f2cdf98
[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
93   $error = $new_cust_bill->replace($old_cust_bill);
94   return "Error modifying cust_bill: $error" if $error;
95
96   $self->SUPER::insert;
97 }
98
99 =item delete
100
101 Currently unimplemented (accounting reasons).
102
103 =cut
104
105 sub delete {
106   return "Can't (yet?) delete cust_pay records!";
107 }
108
109 =item replace OLD_RECORD
110
111 Currently unimplemented (accounting reasons).
112
113 =cut
114
115 sub replace {
116    return "Can't (yet?) modify cust_pay records!";
117 }
118
119 =item check
120
121 Checks all fields to make sure this is a valid payment.  If there is an error,
122 returns the error, otherwise returns false.  Called by the insert method.
123
124 =cut
125
126 sub check {
127   my $self = shift;
128
129   my $error;
130
131   $error =
132     $self->ut_numbern('paynum')
133     || $self->ut_number('invnum')
134     || $self->ut_money('paid')
135     || $self->ut_numbern('_date')
136   ;
137   return $error if $error;
138
139   $self->_date(time) unless $self->_date;
140
141   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
142   $self->payby($1);
143
144   if ( $self->payby eq 'CARD' ) {
145     my $payinfo = $self->payinfo;
146     $self->payinfo($payinfo =~ s/\D//g);
147     if ( $self->payinfo ) {
148       $self->payinfo =~ /^(\d{13,16})$/
149         or return "Illegal (mistyped?) credit card number (payinfo)";
150       $self->payinfo($1);
151       validate($self->payinfo) or return "Illegal credit card number";
152       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
153     } else {
154       $self->payinfo('N/A');
155     }
156
157   } else {
158     $error = $self->ut_textn('payinfo');
159     return $error if $error;
160   }
161
162   $error = $self->ut_textn('paybatch');
163   return $error if $error;
164
165   ''; #no error
166
167 }
168
169 =back
170
171 =head1 VERSION
172
173 $Id: cust_pay.pm,v 1.2 1998-12-29 11:59:43 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_bill>, schema.html from the base documentation.
182
183 =head1 HISTORY
184
185 ivan@voicenet.com 97-jul-1 - 25 - 29
186
187 new api ivan@sisd.com 98-mar-13
188
189 pod ivan@sisd.com 98-sep-21
190
191 $Log: cust_pay.pm,v $
192 Revision 1.2  1998-12-29 11:59:43  ivan
193 mostly properly OO, some work still to be done with svc_ stuff
194
195
196 =cut
197
198 1;
199