documentation updates from jason
[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;
78
79   $error = $self->check;
80   return $error if $error;
81
82   my $old_cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
83   return "Unknown invnum" unless $old_cust_bill;
84   my %hash = $old_cust_bill->hash;
85   $hash{'owed'} = sprintf("%.2f", $hash{owed} - $self->paid );
86   my $new_cust_bill = new FS::cust_bill ( \%hash );
87
88   local $SIG{HUP} = 'IGNORE';
89   local $SIG{INT} = 'IGNORE';
90   local $SIG{QUIT} = 'IGNORE';
91   local $SIG{TERM} = 'IGNORE';
92   local $SIG{TSTP} = 'IGNORE';
93   local $SIG{PIPE} = 'IGNORE';
94
95   $error = $new_cust_bill->replace($old_cust_bill);
96   return "Error modifying cust_bill: $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_pay 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_pay records!";
119 }
120
121 =item check
122
123 Checks all fields to make sure this is a valid payment.  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_numbern('paynum')
135     || $self->ut_number('invnum')
136     || $self->ut_money('paid')
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     $payinfo =~ s/\D//g;
149     $self->payinfo($payinfo);
150     if ( $self->payinfo ) {
151       $self->payinfo =~ /^(\d{13,16})$/
152         or return "Illegal (mistyped?) credit card number (payinfo)";
153       $self->payinfo($1);
154       validate($self->payinfo) or return "Illegal credit card number";
155       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
156     } else {
157       $self->payinfo('N/A');
158     }
159
160   } else {
161     $error = $self->ut_textn('payinfo');
162     return $error if $error;
163   }
164
165   $error = $self->ut_textn('paybatch');
166   return $error if $error;
167
168   ''; #no error
169
170 }
171
172 =back
173
174 =head1 VERSION
175
176 $Id: cust_pay.pm,v 1.2 2001-02-11 17:17:39 ivan Exp $
177
178 =head1 BUGS
179
180 Delete and replace methods.
181
182 =head1 SEE ALSO
183
184 L<FS::Record>, L<FS::cust_bill>, schema.html from the base documentation.
185
186 =cut
187
188 1;
189