RT# 34134 - updated select invoice to actually pay the invoice being selected not...
[freeside.git] / FS / FS / cust_main / Billing_Batch.pm
1 package FS::cust_main::Billing_Batch;
2
3 use strict;
4 use vars qw( $conf );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::pay_batch;
7 use FS::cust_pay_batch;
8 use FS::cust_bill_pay_batch;
9
10 install_callback FS::UID sub { 
11   $conf = new FS::Conf;
12   #yes, need it for stuff below (prolly should be cached)
13 };
14
15 =item batch_card OPTION => VALUE...
16
17 Adds a payment for this invoice to the pending credit card batch (see
18 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
19 runs the payment using a realtime gateway.
20
21 Options may include:
22
23 B<amount>: the amount to be paid; defaults to the customer's balance minus
24 any payments in transit.
25
26 B<realtime>: runs this as a realtime payment instead of adding it to a 
27 batch.  Deprecated.
28
29 B<invnum>: sets cust_pay_batch.invnum.
30
31 B<address1>, B<address2>, B<city>, B<state>, B<zip>, B<country>: sets 
32 the billing address for the payment; defaults to the customer's billing
33 location.
34
35 B<payby>, B<payinfo>, B<paydate>, B<payname>: sets the payment method, 
36 payment account, expiration date, and name; defaults to those fields 
37 in cust_main.
38
39 =cut
40
41 sub batch_card {
42   my ($self, %options) = @_;
43
44   my $amount;
45   if (exists($options{amount})) {
46     $amount = $options{amount};
47   }else{
48     $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
49   }
50   if ($amount <= 0) {
51     warn(sprintf("Customer balance %.2f - in transit amount %.2f is <= 0.\n",
52         $self->balance,
53         $self->in_transit_payments
54     ));
55     return;
56   }
57   
58   #my $invnum = delete $options{invnum};
59   my $invnum = $options{invnum};
60
61   #pay fields should all come from either cust_payby or options, not both
62   #  in theory, could just pass payby, and use it to select cust_payby,
63   #  but nothing currently needs that, so not implementing it now
64   die "Incomplete payment details" 
65     if  ($options{payby} || $options{payinfo} || $options{paydate} || $options{payname})
66     && !($options{payby} && $options{payinfo} && $options{paydate} && $options{payname});
67
68   #false laziness with Billing_Realtime
69   my @cust_payby = $self->cust_payby('CARD','CHEK');
70
71   # batch can't try out every one like realtime, just use first one
72   my $cust_payby = $cust_payby[0];
73
74   die "No customer payment info found"
75     unless $options{payinfo} || $cust_payby;
76                                                    
77   my $payby = $options{payby} || $cust_payby->payby;
78
79   if ($options{'realtime'}) {
80     return $self->realtime_bop( FS::payby->payby2bop($payby),
81                                 $amount,
82                                 %options,
83                               );
84   }
85
86   my $oldAutoCommit = $FS::UID::AutoCommit;
87   local $FS::UID::AutoCommit = 0;
88   my $dbh = dbh;
89
90   #this needs to handle mysql as well as Pg, like svc_acct.pm
91   #(make it into a common function if folks need to do batching with mysql)
92   $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
93     or die "Cannot lock pay_batch: " . $dbh->errstr;
94
95   my %pay_batch = (
96     'status' => 'O',
97     'payby'  => FS::payby->payby2payment($payby),
98   );
99   $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');
100
101   my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
102
103   unless ( $pay_batch ) {
104     $pay_batch = new FS::pay_batch \%pay_batch;
105     my $error = $pay_batch->insert;
106     if ( $error ) {
107       $dbh->rollback if $oldAutoCommit;
108       die "error creating new batch: $error\n";
109     }
110   }
111
112   my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
113       'batchnum' => $pay_batch->batchnum,
114       'custnum'  => $self->custnum,
115   } );
116
117   foreach (qw( address1 address2 city state zip country latitude longitude
118                payby payinfo paydate payname paycode paytype ))
119   {
120     $options{$_} = '' unless exists($options{$_});
121   }
122
123   my $loc = $self->bill_location;
124
125   my $cust_pay_batch = new FS::cust_pay_batch ( {
126     'batchnum' => $pay_batch->batchnum,
127     'invnum'   => $invnum || 0,                    # is there a better value?
128                                                    # this field should be
129                                                    # removed...
130                                                    # cust_bill_pay_batch now
131     'custnum'  => $self->custnum,
132     'last'     => $self->getfield('last'),
133     'first'    => $self->getfield('first'),
134     'address1' => $options{address1} || $loc->address1,
135     'address2' => $options{address2} || $loc->address2,
136     'city'     => $options{city}     || $loc->city,
137     'state'    => $options{state}    || $loc->state,
138     'zip'      => $options{zip}      || $loc->zip,
139     'country'  => $options{country}  || $loc->country,
140     'payby'    => $options{payby}    || $cust_payby->payby,
141     'payinfo'  => $options{payinfo}  || $cust_payby->payinfo,
142     'paymask'  => ( $options{payinfo}
143                       ? FS::payinfo_Mixin->mask_payinfo( $options{payby},
144                                                          $options{payinfo} )
145                       : $cust_payby->paymask
146                   ),
147     'exp'      => $options{paydate}  || $cust_payby->paydate,
148     'payname'  => $options{payname}  || $cust_payby->payname,
149     'paytype'  => $options{paytype}  || $cust_payby->paytype,
150     'amount'   => $amount,                         # consolidating
151     'paycode'  => $options{paycode}  || '',
152   } );
153   
154   $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
155     if $old_cust_pay_batch;
156
157   my $error;
158   if ($old_cust_pay_batch) {
159     $error = $cust_pay_batch->replace($old_cust_pay_batch)
160   } else {
161     $error = $cust_pay_batch->insert;
162   }
163
164   if ( $error ) {
165     $dbh->rollback if $oldAutoCommit;
166     #die $error;
167     return $error; # e.g. "Illegal zip" ala RT#75998
168   }
169
170   my $unapplied =   $self->total_unapplied_credits
171                   + $self->total_unapplied_payments
172                   + $self->in_transit_payments;
173   foreach my $cust_bill ($self->open_cust_bill) {
174     #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
175     my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
176       'invnum' => $cust_bill->invnum,
177       'paybatchnum' => $cust_pay_batch->paybatchnum,
178       'amount' => $cust_bill->owed,
179       '_date' => time,
180     };
181     if ($unapplied >= $cust_bill_pay_batch->amount){
182       $unapplied -= $cust_bill_pay_batch->amount;
183       next;
184     }else{
185       $cust_bill_pay_batch->amount(sprintf ( "%.2f", 
186                                    $cust_bill_pay_batch->amount - $unapplied ));      $unapplied = 0;
187     }
188     $error = $cust_bill_pay_batch->insert;
189     if ( $error ) {
190       $dbh->rollback if $oldAutoCommit;
191       die $error;
192     }
193   }
194
195   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
196   '';
197 }
198
199 =item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
200
201 Returns all batched payments (see L<FS::cust_pay_batch>) for this customer.
202
203 Optionally, a list or hashref of additional arguments to the qsearch call can
204 be passed.
205
206 =cut
207
208 sub cust_pay_batch {
209   my $self = shift;
210   my $opt = ref($_[0]) ? shift : { @_ };
211
212   #return $self->num_cust_statement unless wantarray || keys %$opt;
213
214   $opt->{'table'} = 'cust_pay_batch';
215   $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
216   $opt->{'hashref'}{'custnum'} = $self->custnum;
217   $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';
218
219   map { $_ } #behavior of sort undefined in scalar context
220     sort { $a->paybatchnum <=> $b->paybatchnum }
221       qsearch($opt);
222 }
223
224 =item in_transit_payments
225
226 Returns the total of requests for payments for this customer pending in 
227 batches in transit to the bank.  See L<FS::pay_batch> and L<FS::cust_pay_batch>
228
229 =cut
230
231 sub in_transit_payments {
232   my $self = shift;
233   my $in_transit_payments = 0;
234   foreach my $pay_batch ( qsearch('pay_batch', {
235     'status' => 'I',
236   } ) ) {
237     foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
238       'batchnum' => $pay_batch->batchnum,
239       'custnum' => $self->custnum,
240       'status'  => '',
241     } ) ) {
242       $in_transit_payments += $cust_pay_batch->amount;
243     }
244   }
245   sprintf( "%.2f", $in_transit_payments );
246 }
247
248 1;