calculate in_transit_payments correctly for partially complete batches, #37193
[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<payby>: the payment method; defaults to cust_main.payby
27
28 B<realtime>: runs this as a realtime payment instead of adding it to a 
29 batch.  Deprecated.
30
31 B<invnum>: sets cust_pay_batch.invnum.
32
33 B<address1>, B<address2>, B<city>, B<state>, B<zip>, B<country>: sets 
34 the billing address for the payment; defaults to the customer's billing
35 location.
36
37 B<payinfo>, B<paydate>, B<payname>: sets the payment account, expiration
38 date, and name; defaults to those fields in cust_main.
39
40 =cut
41
42 sub batch_card {
43   my ($self, %options) = @_;
44
45   my $amount;
46   if (exists($options{amount})) {
47     $amount = $options{amount};
48   }else{
49     $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
50   }
51   if ($amount <= 0) {
52     warn(sprintf("Customer balance %.2f - in transit amount %.2f is <= 0.\n",
53         $self->balance,
54         $self->in_transit_payments
55     ));
56     return;
57   }
58   
59   my $invnum = delete $options{invnum};
60   my $payby = $options{payby} || $self->payby;  #still dubious
61
62   if ($options{'realtime'}) {
63     return $self->realtime_bop( FS::payby->payby2bop($self->payby),
64                                 $amount,
65                                 %options,
66                               );
67   }
68
69   my $oldAutoCommit = $FS::UID::AutoCommit;
70   local $FS::UID::AutoCommit = 0;
71   my $dbh = dbh;
72
73   #this needs to handle mysql as well as Pg, like svc_acct.pm
74   #(make it into a common function if folks need to do batching with mysql)
75   $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
76     or return "Cannot lock pay_batch: " . $dbh->errstr;
77
78   my %pay_batch = (
79     'status' => 'O',
80     'payby'  => FS::payby->payby2payment($payby),
81   );
82   $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');
83
84   my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
85
86   unless ( $pay_batch ) {
87     $pay_batch = new FS::pay_batch \%pay_batch;
88     my $error = $pay_batch->insert;
89     if ( $error ) {
90       $dbh->rollback if $oldAutoCommit;
91       die "error creating new batch: $error\n";
92     }
93   }
94
95   my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
96       'batchnum' => $pay_batch->batchnum,
97       'custnum'  => $self->custnum,
98   } );
99
100   foreach (qw( address1 address2 city state zip country latitude longitude
101                payby payinfo paydate payname ))
102   {
103     $options{$_} = '' unless exists($options{$_});
104   }
105
106   my $loc = $self->bill_location;
107
108   my $cust_pay_batch = new FS::cust_pay_batch ( {
109     'batchnum' => $pay_batch->batchnum,
110     'invnum'   => $invnum || 0,                    # is there a better value?
111                                                    # this field should be
112                                                    # removed...
113                                                    # cust_bill_pay_batch now
114     'custnum'  => $self->custnum,
115     'last'     => $self->getfield('last'),
116     'first'    => $self->getfield('first'),
117     'address1' => $options{address1} || $loc->address1,
118     'address2' => $options{address2} || $loc->address2,
119     'city'     => $options{city}     || $loc->city,
120     'state'    => $options{state}    || $loc->state,
121     'zip'      => $options{zip}      || $loc->zip,
122     'country'  => $options{country}  || $loc->country,
123     'payby'    => $options{payby}    || $self->payby,
124     'payinfo'  => $options{payinfo}  || $self->payinfo,
125     'exp'      => $options{paydate}  || $self->paydate,
126     'payname'  => $options{payname}  || $self->payname,
127     'amount'   => $amount,                         # consolidating
128   } );
129   
130   $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
131     if $old_cust_pay_batch;
132
133   my $error;
134   if ($old_cust_pay_batch) {
135     $error = $cust_pay_batch->replace($old_cust_pay_batch)
136   } else {
137     $error = $cust_pay_batch->insert;
138   }
139
140   if ( $error ) {
141     $dbh->rollback if $oldAutoCommit;
142     die $error;
143   }
144
145   my $unapplied =   $self->total_unapplied_credits
146                   + $self->total_unapplied_payments
147                   + $self->in_transit_payments;
148   foreach my $cust_bill ($self->open_cust_bill) {
149     #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
150     my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
151       'invnum' => $cust_bill->invnum,
152       'paybatchnum' => $cust_pay_batch->paybatchnum,
153       'amount' => $cust_bill->owed,
154       '_date' => time,
155     };
156     if ($unapplied >= $cust_bill_pay_batch->amount){
157       $unapplied -= $cust_bill_pay_batch->amount;
158       next;
159     }else{
160       $cust_bill_pay_batch->amount(sprintf ( "%.2f", 
161                                    $cust_bill_pay_batch->amount - $unapplied ));      $unapplied = 0;
162     }
163     $error = $cust_bill_pay_batch->insert;
164     if ( $error ) {
165       $dbh->rollback if $oldAutoCommit;
166       die $error;
167     }
168   }
169
170   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
171   '';
172 }
173
174 =item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
175
176 Returns all batched payments (see L<FS::cust_pay_batch>) for this customer.
177
178 Optionally, a list or hashref of additional arguments to the qsearch call can
179 be passed.
180
181 =cut
182
183 sub cust_pay_batch {
184   my $self = shift;
185   my $opt = ref($_[0]) ? shift : { @_ };
186
187   #return $self->num_cust_statement unless wantarray || keys %$opt;
188
189   $opt->{'table'} = 'cust_pay_batch';
190   $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
191   $opt->{'hashref'}{'custnum'} = $self->custnum;
192   $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';
193
194   map { $_ } #behavior of sort undefined in scalar context
195     sort { $a->paybatchnum <=> $b->paybatchnum }
196       qsearch($opt);
197 }
198
199 =item in_transit_payments
200
201 Returns the total of requests for payments for this customer pending in 
202 batches in transit to the bank.  See L<FS::pay_batch> and L<FS::cust_pay_batch>
203
204 =cut
205
206 sub in_transit_payments {
207   my $self = shift;
208   my $in_transit_payments = 0;
209   foreach my $pay_batch ( qsearch('pay_batch', {
210     'status' => 'I',
211   } ) ) {
212     foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
213       'batchnum' => $pay_batch->batchnum,
214       'custnum' => $self->custnum,
215       'status'  => '',
216     } ) ) {
217       $in_transit_payments += $cust_pay_batch->amount;
218     }
219   }
220   sprintf( "%.2f", $in_transit_payments );
221 }
222
223 1;