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