delete payments
[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            /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
186       #warn $error;
187       my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
188       $new->custnum($cust_bill->custnum);
189     } elsif ( $error ) {
190       $dbh->rollback if $oldAutoCommit;
191       return $error;
192     } else {
193       $new->custnum($cust_bill_pay->cust_bill->custnum);
194     }
195   } else {
196     die;
197   }
198
199   $error = $new->SUPER::replace($self);
200   if ( $error ) {
201     $dbh->rollback if $oldAutoCommit;
202     return $error;
203   }
204
205   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
206
207   '';
208
209
210 }
211
212 =item delete
213
214 Deletes this payment and all associated applications (see L<FS::cust_bill_pay>),
215 unless the closed flag is set.
216
217 =cut
218
219 sub delete {
220   my $self = shift;
221   return "Can't delete closed payment" if $self->closed =~ /^Y/i;
222
223   local $SIG{HUP} = 'IGNORE';
224   local $SIG{INT} = 'IGNORE';
225   local $SIG{QUIT} = 'IGNORE';
226   local $SIG{TERM} = 'IGNORE';
227   local $SIG{TSTP} = 'IGNORE';
228   local $SIG{PIPE} = 'IGNORE';
229
230   my $oldAutoCommit = $FS::UID::AutoCommit;
231   local $FS::UID::AutoCommit = 0;
232   my $dbh = dbh;
233
234   foreach my $cust_bill_pay ( $self->cust_bill_pay ) {
235     my $error = $cust_bill_pay->delete;
236     if ( $error ) {
237       $dbh->rollback if $oldAutoCommit;
238       return $error;
239     }
240   }
241
242   my $error = $self->SUPER::delete(@_);
243   if ( $error ) {
244     $dbh->rollback if $oldAutoCommit;
245     return $error;
246   }
247
248   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
249
250   '';
251
252 }
253
254 =item replace OLD_RECORD
255
256 Currently unimplemented (accounting reasons).
257
258 =cut
259
260 sub replace {
261    return "Can't (yet?) modify cust_pay records!";
262 }
263
264 =item check
265
266 Checks all fields to make sure this is a valid payment.  If there is an error,
267 returns the error, otherwise returns false.  Called by the insert method.
268
269 =cut
270
271 sub check {
272   my $self = shift;
273
274   my $error =
275     $self->ut_numbern('paynum')
276     || $self->ut_numbern('custnum')
277     || $self->ut_money('paid')
278     || $self->ut_numbern('_date')
279     || $self->ut_textn('paybatch')
280     || $self->ut_enum('closed', [ '', 'Y' ])
281   ;
282   return $error if $error;
283
284   return "paid must be > 0 " if $self->paid <= 0;
285
286   return "unknown cust_main.custnum: ". $self->custnum
287     unless $self->invnum
288            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
289
290   $self->_date(time) unless $self->_date;
291
292   $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
293   $self->payby($1);
294
295   #false laziness with cust_refund::check
296   if ( $self->payby eq 'CARD' ) {
297     my $payinfo = $self->payinfo;
298     $payinfo =~ s/\D//g;
299     $self->payinfo($payinfo);
300     if ( $self->payinfo ) {
301       $self->payinfo =~ /^(\d{13,16})$/
302         or return "Illegal (mistyped?) credit card number (payinfo)";
303       $self->payinfo($1);
304       validate($self->payinfo) or return "Illegal credit card number";
305       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
306     } else {
307       $self->payinfo('N/A');
308     }
309
310   } else {
311     $error = $self->ut_textn('payinfo');
312     return $error if $error;
313   }
314
315   ''; #no error
316
317 }
318
319 =item cust_bill_pay
320
321 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
322 payment.
323
324 =cut
325
326 sub cust_bill_pay {
327   my $self = shift;
328   sort { $a->_date <=> $b->_date }
329     qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
330   ;
331 }
332
333 =item unapplied
334
335 Returns the amount of this payment that is still unapplied; which is
336 paid minus all payment applications (see L<FS::cust_bill_pay>).
337
338 =cut
339
340 sub unapplied {
341   my $self = shift;
342   my $amount = $self->paid;
343   $amount -= $_->amount foreach ( $self->cust_bill_pay );
344   sprintf("%.2f", $amount );
345 }
346
347 =back
348
349 =head1 VERSION
350
351 $Id: cust_pay.pm,v 1.16 2002-02-07 22:29:34 ivan Exp $
352
353 =head1 BUGS
354
355 Delete and replace methods.
356
357 =head1 SEE ALSO
358
359 L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
360 base documentation.
361
362 =cut
363
364 1;
365