use unique tokens to prevent double-submission of payments in the web UI
[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   if ( $self->paybatch =~ /^webui-/ ) {
140     my @cust_pay = qsearch('cust_pay', {
141       'custnum' => $self->custnum,
142       'paybatch' => $self->paybatch,
143     } );
144     if ( scalar(@cust_pay) > 1 ) {
145       $dbh->rollback if $oldAutoCommit;
146       return "a payment with webui token ". $self->paybatch. " already exists";
147     }
148   }
149
150   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
151
152   #false laziness w/ cust_credit::insert
153   if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
154     my @errors = $cust_main->unsuspend;
155     #return 
156     # side-fx with nested transactions?  upstack rolls back?
157     warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
158          join(' / ', @errors)
159       if @errors;
160   }
161   #eslaf
162
163   '';
164
165 }
166
167 sub upgrade_replace { #1.3.x->1.4.x
168   my $self = shift;
169
170   local $SIG{HUP} = 'IGNORE';
171   local $SIG{INT} = 'IGNORE';
172   local $SIG{QUIT} = 'IGNORE';
173   local $SIG{TERM} = 'IGNORE';
174   local $SIG{TSTP} = 'IGNORE';
175   local $SIG{PIPE} = 'IGNORE';
176
177   my $oldAutoCommit = $FS::UID::AutoCommit;
178   local $FS::UID::AutoCommit = 0;
179   my $dbh = dbh;
180
181   my $error = $self->check;
182   return $error if $error;
183
184   my %new = $self->hash;
185   my $new = FS::cust_pay->new(\%new);
186
187   if ( $self->invnum ) {
188     my $cust_bill_pay = new FS::cust_bill_pay {
189       'invnum' => $self->invnum,
190       'paynum' => $self->paynum,
191       'amount' => $self->paid,
192       '_date'  => $self->_date,
193     };
194     $error = $cust_bill_pay->insert;
195     if ( $error =~ 
196            /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
197       #warn $error;
198       my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
199       $new->custnum($cust_bill->custnum);
200     } elsif ( $error ) {
201       $dbh->rollback if $oldAutoCommit;
202       return $error;
203     } else {
204       $new->custnum($cust_bill_pay->cust_bill->custnum);
205     }
206   } else {
207     die;
208   }
209
210   $error = $new->SUPER::replace($self);
211   if ( $error ) {
212     $dbh->rollback if $oldAutoCommit;
213     return $error;
214   }
215
216   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
217
218   '';
219
220
221 }
222
223 =item delete
224
225 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
226 unless the closed flag is set.
227
228 =cut
229
230 sub delete {
231   my $self = shift;
232   return "Can't delete closed payment" if $self->closed =~ /^Y/i;
233
234   local $SIG{HUP} = 'IGNORE';
235   local $SIG{INT} = 'IGNORE';
236   local $SIG{QUIT} = 'IGNORE';
237   local $SIG{TERM} = 'IGNORE';
238   local $SIG{TSTP} = 'IGNORE';
239   local $SIG{PIPE} = 'IGNORE';
240
241   my $oldAutoCommit = $FS::UID::AutoCommit;
242   local $FS::UID::AutoCommit = 0;
243   my $dbh = dbh;
244
245   foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
246     my $error = $cust_bill_pay->delete;
247     if ( $error ) {
248       $dbh->rollback if $oldAutoCommit;
249       return $error;
250     }
251   }
252
253   my $error = $self->SUPER::delete(@_);
254   if ( $error ) {
255     $dbh->rollback if $oldAutoCommit;
256     return $error;
257   }
258
259   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
260
261   '';
262
263 }
264
265 =item replace OLD_RECORD
266
267 Currently unimplemented (accounting reasons).
268
269 =cut
270
271 sub replace {
272    return "Can't (yet?) modify cust_pay records!";
273 }
274
275 =item check
276
277 Checks all fields to make sure this is a valid payment.  If there is an error,
278 returns the error, otherwise returns false.  Called by the insert method.
279
280 =cut
281
282 sub check {
283   my $self = shift;
284
285   my $error =
286     $self->ut_numbern('paynum')
287     || $self->ut_numbern('custnum')
288     || $self->ut_money('paid')
289     || $self->ut_numbern('_date')
290     || $self->ut_textn('paybatch')
291     || $self->ut_enum('closed', [ '', 'Y' ])
292   ;
293   return $error if $error;
294
295   return "paid must be > 0 " if $self->paid <= 0;
296
297   return "unknown cust_main.custnum: ". $self->custnum
298     unless $self->invnum
299            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
300
301   $self->_date(time) unless $self->_date;
302
303   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
304   $self->payby($1);
305
306   #false laziness with cust_refund::check
307   if ( $self->payby eq 'CARD' ) {
308     my $payinfo = $self->payinfo;
309     $payinfo =~ s/\D//g;
310     $self->payinfo($payinfo);
311     if ( $self->payinfo ) {
312       $self->payinfo =~ /^(\d{13,16})$/
313         or return "Illegal (mistyped?) credit card number (payinfo)";
314       $self->payinfo($1);
315       validate($self->payinfo) or return "Illegal credit card number";
316       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
317     } else {
318       $self->payinfo('N/A');
319     }
320
321   } else {
322     $error = $self->ut_textn('payinfo');
323     return $error if $error;
324   }
325
326   ''; #no error
327
328 }
329
330 =item cust_bill_pay
331
332 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
333 payment.
334
335 =cut
336
337 sub cust_bill_pay {
338   my $self = shift;
339   sort { $a->_date <=> $b->_date }
340     qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
341   ;
342 }
343
344 =item unapplied
345
346 Returns the amount of this payment that is still unapplied; which is
347 paid minus all payment applications (see L<FS::cust_bill_pay>).
348
349 =cut
350
351 sub unapplied {
352   my $self = shift;
353   my $amount = $self->paid;
354   $amount -= $_->amount foreach ( $self->cust_bill_pay );
355   sprintf("%.2f", $amount );
356 }
357
358 =back
359
360 =head1 VERSION
361
362 $Id: cust_pay.pm,v 1.17 2002-02-10 18:56:49 ivan Exp $
363
364 =head1 BUGS
365
366 Delete and replace methods.
367
368 =head1 SEE ALSO
369
370 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
371 base documentation.
372
373 =cut
374
375 1;
376