fix more bugs
[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 qsearch qsearchs );
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 = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
98       or do {
99         $dbh->rollback if $oldAutoCommit;
100         return "Unknown cust_bill.invnum: ". $self->invnum;
101       };
102     $self->custnum($cust_bill->custnum );
103   }
104
105   $error = $self->SUPER::insert;
106   if ( $error ) {
107     $dbh->rollback if $oldAutoCommit;
108     return "error inserting $self: $error";
109   }
110
111   if ( $self->invnum ) {
112     my $cust_bill_pay = new FS::cust_bill_pay {
113       'invnum' => $self->invnum,
114       'paynum' => $self->paynum,
115       'amount' => $self->paid,
116       '_date'  => $self->_date,
117     };
118     $error = $cust_bill_pay->insert;
119     if ( $error ) {
120       $dbh->rollback if $oldAutoCommit;
121       return "error inserting $cust_bill_pay: $error";
122     }
123   }
124
125   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
126
127   '';
128
129 }
130
131 sub upgrade_replace { #1.3.x->1.4.x
132   my $self = shift;
133
134   local $SIG{HUP} = 'IGNORE';
135   local $SIG{INT} = 'IGNORE';
136   local $SIG{QUIT} = 'IGNORE';
137   local $SIG{TERM} = 'IGNORE';
138   local $SIG{TSTP} = 'IGNORE';
139   local $SIG{PIPE} = 'IGNORE';
140
141   my $oldAutoCommit = $FS::UID::AutoCommit;
142   local $FS::UID::AutoCommit = 0;
143   my $dbh = dbh;
144
145   my $error = $self->check;
146   return $error if $error;
147
148   my %new = $self->hash;
149   my $new = FS::cust_pay->new(\%new);
150
151   if ( $self->invnum ) {
152     my $cust_bill_pay = new FS::cust_bill_pay {
153       'invnum' => $self->invnum,
154       'paynum' => $self->paynum,
155       'amount' => $self->paid,
156       '_date'  => $self->_date,
157     };
158     $error = $cust_bill_pay->insert;
159     if ( $error ) {
160       $dbh->rollback if $oldAutoCommit;
161       return $error;
162     }
163     $new->custnum($cust_bill_pay->cust_bill->custnum);
164   } else {
165     die;
166   }
167
168   $error = $new->SUPER::replace($self);
169   if ( $error ) {
170     $dbh->rollback if $oldAutoCommit;
171     return $error;
172   }
173
174   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
175
176   '';
177
178
179 }
180
181 =item delete
182
183 Currently unimplemented (accounting reasons).
184
185 =cut
186
187 sub delete {
188   return "Can't (yet?) delete cust_pay records!";
189 }
190
191 =item replace OLD_RECORD
192
193 Currently unimplemented (accounting reasons).
194
195 =cut
196
197 sub replace {
198    return "Can't (yet?) modify cust_pay records!";
199 }
200
201 =item check
202
203 Checks all fields to make sure this is a valid payment.  If there is an error,
204 returns the error, otherwise returns false.  Called by the insert method.
205
206 =cut
207
208 sub check {
209   my $self = shift;
210
211   my $error =
212     $self->ut_numbern('paynum')
213     || $self->ut_numbern('custnum')
214     || $self->ut_money('paid')
215     || $self->ut_numbern('_date')
216     || $self->ut_textn('paybatch')
217   ;
218   return $error if $error;
219
220   return "unknown cust_main.custnum: ". $self->custnum
221     unless $self->invnum
222            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
223
224   $self->_date(time) unless $self->_date;
225
226   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
227   $self->payby($1);
228
229   if ( $self->payby eq 'CARD' ) {
230     my $payinfo = $self->payinfo;
231     $payinfo =~ s/\D//g;
232     $self->payinfo($payinfo);
233     if ( $self->payinfo ) {
234       $self->payinfo =~ /^(\d{13,16})$/
235         or return "Illegal (mistyped?) credit card number (payinfo)";
236       $self->payinfo($1);
237       validate($self->payinfo) or return "Illegal credit card number";
238       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
239     } else {
240       $self->payinfo('N/A');
241     }
242
243   } else {
244     $error = $self->ut_textn('payinfo');
245     return $error if $error;
246   }
247
248   ''; #no error
249
250 }
251
252 =item cust_bill_pay
253
254 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
255 payment.
256
257 =cut
258
259 sub cust_bill_pay {
260   my $self = shift;
261   sort { $a->_date <=> $b->_date }
262     qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
263   ;
264 }
265
266 =item unapplied
267
268 Returns the amount of this payment that is still unapplied; which is
269 paid minus all payment applications (see L<FS::cust_bill_pay>).
270
271 =cut
272
273 sub unapplied {
274   my $self = shift;
275   my $amount = $self->paid;
276   $amount -= $_->amount foreach ( $self->cust_bill_pay );
277   sprintf("%.2f", $amount );
278 }
279
280 =back
281
282 =head1 VERSION
283
284 $Id: cust_pay.pm,v 1.7 2001-09-03 22:07:38 ivan Exp $
285
286 =head1 BUGS
287
288 Delete and replace methods.
289
290 =head1 SEE ALSO
291
292 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
293 base documentation.
294
295 =cut
296
297 1;
298