Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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
61   #false laziness with Billing_Realtime
62   my @cust_payby = qsearch({
63     'table'     => 'cust_payby',
64     'hashref'   => { 'custnum' => $self->custnum, },
65     'extra_sql' => " AND payby IN ( 'CARD', 'CHEK' ) ",
66     'order_by'  => 'ORDER BY weight ASC',
67   });
68
69   # batch can't try out every one like realtime, just use first one
70   my $cust_payby = $cust_payby[0] || $self; # somewhat dubious
71                                                    
72   my $payby = $options{payby} || $cust_payby->payby;
73
74   if ($options{'realtime'}) {
75     return $self->realtime_bop( FS::payby->payby2bop($payby),
76                                 $amount,
77                                 %options,
78                               );
79   }
80
81   my $oldAutoCommit = $FS::UID::AutoCommit;
82   local $FS::UID::AutoCommit = 0;
83   my $dbh = dbh;
84
85   #this needs to handle mysql as well as Pg, like svc_acct.pm
86   #(make it into a common function if folks need to do batching with mysql)
87   $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
88     or return "Cannot lock pay_batch: " . $dbh->errstr;
89
90   my %pay_batch = (
91     'status' => 'O',
92     'payby'  => FS::payby->payby2payment($payby),
93   );
94   $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');
95
96   my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
97
98   unless ( $pay_batch ) {
99     $pay_batch = new FS::pay_batch \%pay_batch;
100     my $error = $pay_batch->insert;
101     if ( $error ) {
102       $dbh->rollback if $oldAutoCommit;
103       die "error creating new batch: $error\n";
104     }
105   }
106
107   my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
108       'batchnum' => $pay_batch->batchnum,
109       'custnum'  => $self->custnum,
110   } );
111
112   foreach (qw( address1 address2 city state zip country latitude longitude
113                payby payinfo paydate payname ))
114   {
115     $options{$_} = '' unless exists($options{$_});
116   }
117
118   my $loc = $self->bill_location;
119
120   my $cust_pay_batch = new FS::cust_pay_batch ( {
121     'batchnum' => $pay_batch->batchnum,
122     'invnum'   => $invnum || 0,                    # is there a better value?
123                                                    # this field should be
124                                                    # removed...
125                                                    # cust_bill_pay_batch now
126     'custnum'  => $self->custnum,
127     'last'     => $self->getfield('last'),
128     'first'    => $self->getfield('first'),
129     'address1' => $options{address1} || $loc->address1,
130     'address2' => $options{address2} || $loc->address2,
131     'city'     => $options{city}     || $loc->city,
132     'state'    => $options{state}    || $loc->state,
133     'zip'      => $options{zip}      || $loc->zip,
134     'country'  => $options{country}  || $loc->country,
135     'payby'    => $options{payby}    || $cust_payby->payby,
136     'payinfo'  => $options{payinfo}  || $cust_payby->payinfo,
137     'exp'      => $options{paydate}  || $cust_payby->paydate,
138     'payname'  => $options{payname}  || $cust_payby->payname,
139     'amount'   => $amount,                         # consolidating
140   } );
141   
142   $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
143     if $old_cust_pay_batch;
144
145   my $error;
146   if ($old_cust_pay_batch) {
147     $error = $cust_pay_batch->replace($old_cust_pay_batch)
148   } else {
149     $error = $cust_pay_batch->insert;
150   }
151
152   if ( $error ) {
153     $dbh->rollback if $oldAutoCommit;
154     die $error;
155   }
156
157   my $unapplied =   $self->total_unapplied_credits
158                   + $self->total_unapplied_payments
159                   + $self->in_transit_payments;
160   foreach my $cust_bill ($self->open_cust_bill) {
161     #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
162     my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
163       'invnum' => $cust_bill->invnum,
164       'paybatchnum' => $cust_pay_batch->paybatchnum,
165       'amount' => $cust_bill->owed,
166       '_date' => time,
167     };
168     if ($unapplied >= $cust_bill_pay_batch->amount){
169       $unapplied -= $cust_bill_pay_batch->amount;
170       next;
171     }else{
172       $cust_bill_pay_batch->amount(sprintf ( "%.2f", 
173                                    $cust_bill_pay_batch->amount - $unapplied ));      $unapplied = 0;
174     }
175     $error = $cust_bill_pay_batch->insert;
176     if ( $error ) {
177       $dbh->rollback if $oldAutoCommit;
178       die $error;
179     }
180   }
181
182   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
183   '';
184 }
185
186 =item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
187
188 Returns all batched payments (see L<FS::cust_pay_batch>) for this customer.
189
190 Optionally, a list or hashref of additional arguments to the qsearch call can
191 be passed.
192
193 =cut
194
195 sub cust_pay_batch {
196   my $self = shift;
197   my $opt = ref($_[0]) ? shift : { @_ };
198
199   #return $self->num_cust_statement unless wantarray || keys %$opt;
200
201   $opt->{'table'} = 'cust_pay_batch';
202   $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
203   $opt->{'hashref'}{'custnum'} = $self->custnum;
204   $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';
205
206   map { $_ } #behavior of sort undefined in scalar context
207     sort { $a->paybatchnum <=> $b->paybatchnum }
208       qsearch($opt);
209 }
210
211 =item in_transit_payments
212
213 Returns the total of requests for payments for this customer pending in 
214 batches in transit to the bank.  See L<FS::pay_batch> and L<FS::cust_pay_batch>
215
216 =cut
217
218 sub in_transit_payments {
219   my $self = shift;
220   my $in_transit_payments = 0;
221   foreach my $pay_batch ( qsearch('pay_batch', {
222     'status' => 'I',
223   } ) ) {
224     foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
225       'batchnum' => $pay_batch->batchnum,
226       'custnum' => $self->custnum,
227       'status'  => '',
228     } ) ) {
229       $in_transit_payments += $cust_pay_batch->amount;
230     }
231   }
232   sprintf( "%.2f", $in_transit_payments );
233 }
234
235 1;