book closing schema changes
[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 =item closed - books closed flag, empty or `Y'
65
66 =back
67
68 =head1 METHODS
69
70 =over 4 
71
72 =item new HASHREF
73
74 Creates a new payment.  To add the payment to the databse, see L<"insert">.
75
76 =cut
77
78 sub table { 'cust_pay'; }
79
80 =item insert
81
82 Adds this payment to the database.
83
84 For backwards-compatibility and convenience, if the additional field invnum
85 is defined, an FS::cust_bill_pay record for the full amount of the payment
86 will be created.  In this case, custnum is optional.
87
88 =cut
89
90 sub insert {
91   my $self = shift;
92
93   local $SIG{HUP} = 'IGNORE';
94   local $SIG{INT} = 'IGNORE';
95   local $SIG{QUIT} = 'IGNORE';
96   local $SIG{TERM} = 'IGNORE';
97   local $SIG{TSTP} = 'IGNORE';
98   local $SIG{PIPE} = 'IGNORE';
99
100   my $oldAutoCommit = $FS::UID::AutoCommit;
101   local $FS::UID::AutoCommit = 0;
102   my $dbh = dbh;
103
104   if ( $self->invnum ) {
105     my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
106       or do {
107         $dbh->rollback if $oldAutoCommit;
108         return "Unknown cust_bill.invnum: ". $self->invnum;
109       };
110     $self->custnum($cust_bill->custnum );
111   }
112
113   my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
114   my $old_balance = $cust_main->balance;
115
116   my $error = $self->check;
117   return $error if $error;
118
119   $error = $self->SUPER::insert;
120   if ( $error ) {
121     $dbh->rollback if $oldAutoCommit;
122     return "error inserting $self: $error";
123   }
124
125   if ( $self->invnum ) {
126     my $cust_bill_pay = new FS::cust_bill_pay {
127       'invnum' => $self->invnum,
128       'paynum' => $self->paynum,
129       'amount' => $self->paid,
130       '_date'  => $self->_date,
131     };
132     $error = $cust_bill_pay->insert;
133     if ( $error ) {
134       $dbh->rollback if $oldAutoCommit;
135       return "error inserting $cust_bill_pay: $error";
136     }
137   }
138
139   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
140
141   #false laziness w/ cust_credit::insert
142   if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
143     my @errors = $cust_main->unsuspend;
144     #return 
145     # side-fx with nested transactions?  upstack rolls back?
146     warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
147          join(' / ', @errors)
148       if @errors;
149   }
150   #eslaf
151
152   '';
153
154 }
155
156 sub upgrade_replace { #1.3.x->1.4.x
157   my $self = shift;
158
159   local $SIG{HUP} = 'IGNORE';
160   local $SIG{INT} = 'IGNORE';
161   local $SIG{QUIT} = 'IGNORE';
162   local $SIG{TERM} = 'IGNORE';
163   local $SIG{TSTP} = 'IGNORE';
164   local $SIG{PIPE} = 'IGNORE';
165
166   my $oldAutoCommit = $FS::UID::AutoCommit;
167   local $FS::UID::AutoCommit = 0;
168   my $dbh = dbh;
169
170   my $error = $self->check;
171   return $error if $error;
172
173   my %new = $self->hash;
174   my $new = FS::cust_pay->new(\%new);
175
176   if ( $self->invnum ) {
177     my $cust_bill_pay = new FS::cust_bill_pay {
178       'invnum' => $self->invnum,
179       'paynum' => $self->paynum,
180       'amount' => $self->paid,
181       '_date'  => $self->_date,
182     };
183     $error = $cust_bill_pay->insert;
184     if ( $error ) {
185       $dbh->rollback if $oldAutoCommit;
186       return $error;
187     }
188     $new->custnum($cust_bill_pay->cust_bill->custnum);
189   } else {
190     die;
191   }
192
193   $error = $new->SUPER::replace($self);
194   if ( $error ) {
195     $dbh->rollback if $oldAutoCommit;
196     return $error;
197   }
198
199   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
200
201   '';
202
203
204 }
205
206 =item delete
207
208 Currently unimplemented (accounting reasons).
209
210 =cut
211
212 sub delete {
213   my $self = shift;
214   return "Can't delete closed payment" if $self->closed =~ /^Y/i;
215   $self->SUPER::delete(@_);
216 }
217
218 =item replace OLD_RECORD
219
220 Currently unimplemented (accounting reasons).
221
222 =cut
223
224 sub replace {
225    return "Can't (yet?) modify cust_pay records!";
226 }
227
228 =item check
229
230 Checks all fields to make sure this is a valid payment.  If there is an error,
231 returns the error, otherwise returns false.  Called by the insert method.
232
233 =cut
234
235 sub check {
236   my $self = shift;
237
238   my $error =
239     $self->ut_numbern('paynum')
240     || $self->ut_numbern('custnum')
241     || $self->ut_money('paid')
242     || $self->ut_numbern('_date')
243     || $self->ut_textn('paybatch')
244     || $self->ut_enum('closed', [ '', 'Y' ])
245   ;
246   return $error if $error;
247
248   return "paid must be > 0 " if $self->paid <= 0;
249
250   return "unknown cust_main.custnum: ". $self->custnum
251     unless $self->invnum
252            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
253
254   $self->_date(time) unless $self->_date;
255
256   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
257   $self->payby($1);
258
259   #false laziness with cust_refund::check
260   if ( $self->payby eq 'CARD' ) {
261     my $payinfo = $self->payinfo;
262     $payinfo =~ s/\D//g;
263     $self->payinfo($payinfo);
264     if ( $self->payinfo ) {
265       $self->payinfo =~ /^(\d{13,16})$/
266         or return "Illegal (mistyped?) credit card number (payinfo)";
267       $self->payinfo($1);
268       validate($self->payinfo) or return "Illegal credit card number";
269       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
270     } else {
271       $self->payinfo('N/A');
272     }
273
274   } else {
275     $error = $self->ut_textn('payinfo');
276     return $error if $error;
277   }
278
279   ''; #no error
280
281 }
282
283 =item cust_bill_pay
284
285 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
286 payment.
287
288 =cut
289
290 sub cust_bill_pay {
291   my $self = shift;
292   sort { $a->_date <=> $b->_date }
293     qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
294   ;
295 }
296
297 =item unapplied
298
299 Returns the amount of this payment that is still unapplied; which is
300 paid minus all payment applications (see L<FS::cust_bill_pay>).
301
302 =cut
303
304 sub unapplied {
305   my $self = shift;
306   my $amount = $self->paid;
307   $amount -= $_->amount foreach ( $self->cust_bill_pay );
308   sprintf("%.2f", $amount );
309 }
310
311 =back
312
313 =head1 VERSION
314
315 $Id: cust_pay.pm,v 1.14 2002-01-28 06:57:23 ivan Exp $
316
317 =head1 BUGS
318
319 Delete and replace methods.
320
321 =head1 SEE ALSO
322
323 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
324 base documentation.
325
326 =cut
327
328 1;
329