70d1b73149f1308442c624af0fda53cb921f1c77
[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( dbh );
7 use FS::cust_bill;
8 use FS::cust_bill_pay;
9 use FS::cust_main;
10
11 @ISA = qw( FS::Record );
12
13 =head1 NAME
14
15 FS::cust_pay - Object methods for cust_pay objects
16
17 =head1 SYNOPSIS
18
19   use FS::cust_pay;
20
21   $record = new FS::cust_pay \%hash;
22   $record = new FS::cust_pay { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::cust_pay object represents a payment; the transfer of money from a
35 customer.  FS::cust_pay inherits from FS::Record.  The following fields are
36 currently supported:
37
38 =over 4
39
40 =item paynum - primary key (assigned automatically for new payments)
41
42 =item custnum - customer (see L<FS::cust_main>)
43
44 =item paid - Amount of this payment
45
46 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
47 L<Time::Local> and L<Date::Parse> for conversion functions.
48
49 =item payby - `CARD' (credit cards), `BILL' (billing), or `COMP' (free)
50
51 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
52
53 =item paybatch - text field for tracking card processing
54
55 =back
56
57 =head1 METHODS
58
59 =over 4 
60
61 =item new HASHREF
62
63 Creates a new payment.  To add the payment to the databse, see L<"insert">.
64
65 =cut
66
67 sub table { 'cust_pay'; }
68
69 =item insert
70
71 Adds this payment to the database.
72
73 For backwards-compatibility and convenience, if the additional field invnum
74 is defined, an FS::cust_bill_pay record for the full amount of the payment
75 will be created.  In this case, custnum is optional.
76
77 =cut
78
79 sub insert {
80   my $self = shift;
81
82   local $SIG{HUP} = 'IGNORE';
83   local $SIG{INT} = 'IGNORE';
84   local $SIG{QUIT} = 'IGNORE';
85   local $SIG{TERM} = 'IGNORE';
86   local $SIG{TSTP} = 'IGNORE';
87   local $SIG{PIPE} = 'IGNORE';
88
89   my $oldAutoCommit = $FS::UID::AutoCommit;
90   local $FS::UID::AutoCommit = 0;
91   my $dbh = dbh;
92
93   my $error = $self->check;
94   return $error if $error;
95
96   if ( $self->invnum ) {
97     my $cust_bill_pay = new FS::cust_bill_pay {
98       'invnum' => $self->invnum,
99       'paynum' => $self->paynum,
100       'amount' => $self->paid,
101       '_date'  => $self->_date,
102     };
103     $error = $cust_bill_pay->insert;
104     if ( $error ) {
105       $dbh->rollback if $oldAutoCommit;
106       return $error;
107     }
108     warn $cust_bill_pay;
109     warn $cust_bill_pay->cust_bill;
110     warn $cust_bill_pay->cust_bill->custnum;
111     $self->custnum($cust_bill_pay->cust_bill->custnum);
112   }
113
114   $error = $self->SUPER::insert;
115   if ( $error ) {
116     $dbh->rollback if $oldAutoCommit;
117     return $error;
118   }
119
120   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
121
122   '';
123
124 }
125
126 sub upgrade_replace { #1.3.x->1.4.x
127   my $self = shift;
128
129   local $SIG{HUP} = 'IGNORE';
130   local $SIG{INT} = 'IGNORE';
131   local $SIG{QUIT} = 'IGNORE';
132   local $SIG{TERM} = 'IGNORE';
133   local $SIG{TSTP} = 'IGNORE';
134   local $SIG{PIPE} = 'IGNORE';
135
136   my $oldAutoCommit = $FS::UID::AutoCommit;
137   local $FS::UID::AutoCommit = 0;
138   my $dbh = dbh;
139
140   my $error = $self->check;
141   return $error if $error;
142
143   my %new = $self->hash;
144   my $new = FS::cust_pay->new(\%new);
145
146   if ( $self->invnum ) {
147     my $cust_bill_pay = new FS::cust_bill_pay {
148       'invnum' => $self->invnum,
149       'paynum' => $self->paynum,
150       'amount' => $self->paid,
151       '_date'  => $self->_date,
152     };
153     $error = $cust_bill_pay->insert;
154     if ( $error ) {
155       $dbh->rollback if $oldAutoCommit;
156       return $error;
157     }
158     $new->custnum($cust_bill_pay->cust_bill->custnum);
159   } else {
160     die;
161   }
162
163   $error = $new->SUPER::replace($self);
164   if ( $error ) {
165     $dbh->rollback if $oldAutoCommit;
166     return $error;
167   }
168
169   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
170
171   '';
172
173
174 }
175
176 =item delete
177
178 Currently unimplemented (accounting reasons).
179
180 =cut
181
182 sub delete {
183   return "Can't (yet?) delete cust_pay records!";
184 }
185
186 =item replace OLD_RECORD
187
188 Currently unimplemented (accounting reasons).
189
190 =cut
191
192 sub replace {
193    return "Can't (yet?) modify cust_pay records!";
194 }
195
196 =item check
197
198 Checks all fields to make sure this is a valid payment.  If there is an error,
199 returns the error, otherwise returns false.  Called by the insert method.
200
201 =cut
202
203 sub check {
204   my $self = shift;
205
206   my $error =
207     $self->ut_numbern('paynum')
208     || $self->ut_numbern('custnum')
209     || $self->ut_money('paid')
210     || $self->ut_numbern('_date')
211     || $self->ut_textn('paybatch')
212   ;
213   return $error if $error;
214
215   return "unknown cust_main.custnum: ". $self->custnum
216     unless $self->invnum
217            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
218
219   $self->_date(time) unless $self->_date;
220
221   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
222   $self->payby($1);
223
224   if ( $self->payby eq 'CARD' ) {
225     my $payinfo = $self->payinfo;
226     $payinfo =~ s/\D//g;
227     $self->payinfo($payinfo);
228     if ( $self->payinfo ) {
229       $self->payinfo =~ /^(\d{13,16})$/
230         or return "Illegal (mistyped?) credit card number (payinfo)";
231       $self->payinfo($1);
232       validate($self->payinfo) or return "Illegal credit card number";
233       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
234     } else {
235       $self->payinfo('N/A');
236     }
237
238   } else {
239     $error = $self->ut_textn('payinfo');
240     return $error if $error;
241   }
242
243   ''; #no error
244
245 }
246
247 =item cust_bill_pay
248
249 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
250 payment.
251
252 =cut
253
254 sub cust_bill_pay {
255   my $self = shift;
256   sort { $a->_date <=> $b->_date }
257     qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
258   ;
259 }
260
261 =item unapplied
262
263 Returns the amount of this payment that is still unapplied; which is
264 paid minus all payment applications (see L<FS::cust_bill_pay>).
265
266 =cut
267
268 sub unapplied {
269   my $self = shift;
270   my $amount = $self->paid;
271   $amount -= $_->amount foreach ( $self->cust_bill_pay );
272   sprintf("%.2f", $amount );
273 }
274
275 =back
276
277 =head1 VERSION
278
279 $Id: cust_pay.pm,v 1.6 2001-09-02 05:38:13 ivan Exp $
280
281 =head1 BUGS
282
283 Delete and replace methods.
284
285 =head1 SEE ALSO
286
287 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
288 base documentation.
289
290 =cut
291
292 1;
293