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