1 package FS::cust_main::Billing_Batch;
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::cust_pay_batch;
8 use FS::cust_bill_pay_batch;
10 install_callback FS::UID sub {
12 #yes, need it for stuff below (prolly should be cached)
15 =item batch_card OPTION => VALUE...
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.
23 B<amount>: the amount to be paid; defaults to the customer's balance minus
24 any payments in transit.
26 B<realtime>: runs this as a realtime payment instead of adding it to a
29 B<invnum>: sets cust_pay_batch.invnum.
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
35 B<payby>, B<payinfo>, B<paydate>, B<payname>: sets the payment method,
36 payment account, expiration date, and name; defaults to those fields
42 my ($self, %options) = @_;
45 if (exists($options{amount})) {
46 $amount = $options{amount};
48 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
51 warn(sprintf("Customer balance %.2f - in transit amount %.2f is <= 0.\n",
53 $self->in_transit_payments
58 #my $invnum = delete $options{invnum};
59 my $invnum = $options{invnum};
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});
68 #false laziness with Billing_Realtime
69 my @cust_payby = $self->cust_payby('CARD','CHEK');
71 # batch can't try out every one like realtime, just use first one
72 my $cust_payby = $cust_payby[0];
74 die "No customer payment info found"
75 unless $options{payinfo} || $cust_payby;
77 my $payby = $options{payby} || $cust_payby->payby;
79 if ($options{'realtime'}) {
80 return $self->realtime_bop( FS::payby->payby2bop($payby),
86 my $paycode= $options{paycode} || '';
87 my $batch_type = "DEBIT";
88 $batch_type = "CREDIT" if $paycode eq 'C';
90 my $oldAutoCommit = $FS::UID::AutoCommit;
91 local $FS::UID::AutoCommit = 0;
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 die "Cannot lock pay_batch: " . $dbh->errstr;
101 'payby' => FS::payby->payby2payment($payby),
102 'type' => $batch_type,
104 $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');
106 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
108 unless ( $pay_batch ) {
109 $pay_batch = new FS::pay_batch \%pay_batch;
110 my $error = $pay_batch->insert;
112 $dbh->rollback if $oldAutoCommit;
113 die "error creating new batch: $error\n";
117 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
118 'batchnum' => $pay_batch->batchnum,
119 'custnum' => $self->custnum,
122 foreach (qw( address1 address2 city state zip country latitude longitude
123 payby payinfo paydate payname paycode paytype ))
125 $options{$_} = '' unless exists($options{$_});
128 my $loc = $self->bill_location;
130 my $cust_pay_batch = new FS::cust_pay_batch ( {
131 'batchnum' => $pay_batch->batchnum,
132 'invnum' => $invnum || 0, # is there a better value?
133 # this field should be
135 # cust_bill_pay_batch now
136 'custnum' => $self->custnum,
137 'last' => $self->getfield('last'),
138 'first' => $self->getfield('first'),
139 'address1' => $options{address1} || $loc->address1,
140 'address2' => $options{address2} || $loc->address2,
141 'city' => $options{city} || $loc->city,
142 'state' => $options{state} || $loc->state,
143 'zip' => $options{zip} || $loc->zip,
144 'country' => $options{country} || $loc->country,
145 'payby' => $options{payby} || $cust_payby->payby,
146 'payinfo' => $options{payinfo} || $cust_payby->payinfo,
147 'paymask' => ( $options{payinfo}
148 ? FS::payinfo_Mixin->mask_payinfo( $options{payby},
150 : $cust_payby->paymask
152 'exp' => $options{paydate} || $cust_payby->paydate,
153 'payname' => $options{payname} || $cust_payby->payname,
154 'paytype' => $options{paytype} || $cust_payby->{'Hash'}->{'paytype'},
155 'amount' => $amount, # consolidating
156 'paycode' => $options{paycode} || '',
159 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
160 if $old_cust_pay_batch;
163 if ($old_cust_pay_batch) {
164 $error = $cust_pay_batch->replace($old_cust_pay_batch)
166 $error = $cust_pay_batch->insert;
170 $dbh->rollback if $oldAutoCommit;
172 return $error; # e.g. "Illegal zip" ala RT#75998
175 if ($options{'processing-fee'} > 0) {
177 my $processing_fee_text = 'Payment Processing Fee';
179 unless ( $invnum ) { # probably from a payment screen
180 # do we have any open invoices? pick earliest
181 # uses the fact that cust_main->cust_bill sorts by date ascending
182 my @open = $self->open_cust_bill;
183 $invnum = $open[0]->invnum if scalar(@open);
186 unless ( $invnum ) { # still nothing? pick last closed invoice
187 # again uses fact that cust_main->cust_bill sorts by date ascending
188 my @closed = $self->cust_bill;
189 $invnum = $closed[$#closed]->invnum if scalar(@closed);
193 # XXX: unlikely case - pre-paying before any invoices generated
194 # what it should do is create a new invoice and pick it
195 warn '\PROCESS FEE AND NO INVOICES PICKED TO APPLY IT!';
199 my $pf_change_error = $self->charge({
200 'amount' => $options{'processing-fee'},
201 'pkg' => $processing_fee_text,
203 'cust_pkg_ref' => \$pf_cust_pkg,
206 if($pf_change_error) {
207 warn 'Unable to add payment processing fee';
211 $pf_cust_pkg->setup(time);
212 my $pf_error = $pf_cust_pkg->replace;
214 warn 'Unable to set setup time on cust_pkg for processing fee';
218 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
219 unless ( $cust_bill ) {
220 warn "race condition + invoice deletion just happened";
225 $cust_bill->add_cc_surcharge($pf_cust_pkg->pkgnum,$options{'processing-fee'});
227 warn "cannot add Processing fee to invoice #$invnum: $grand_pf_error"
231 my $unapplied = $self->total_unapplied_credits
232 + $self->total_unapplied_payments
233 + $self->in_transit_payments;
234 foreach my $cust_bill ($self->open_cust_bill) {
235 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
236 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
237 'invnum' => $cust_bill->invnum,
238 'paybatchnum' => $cust_pay_batch->paybatchnum,
239 'amount' => $cust_bill->owed,
242 if ($unapplied >= $cust_bill_pay_batch->amount){
243 $unapplied -= $cust_bill_pay_batch->amount;
246 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
247 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
249 $error = $cust_bill_pay_batch->insert;
251 $dbh->rollback if $oldAutoCommit;
256 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
260 =item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
262 Returns all batched payments (see L<FS::cust_pay_batch>) for this customer.
264 Optionally, a list or hashref of additional arguments to the qsearch call can
271 my $opt = ref($_[0]) ? shift : { @_ };
273 #return $self->num_cust_statement unless wantarray || keys %$opt;
275 $opt->{'table'} = 'cust_pay_batch';
276 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
277 $opt->{'hashref'}{'custnum'} = $self->custnum;
278 $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';
280 map { $_ } #behavior of sort undefined in scalar context
281 sort { $a->paybatchnum <=> $b->paybatchnum }
285 =item in_transit_payments
287 Returns the total of requests for payments for this customer pending in
288 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
292 sub in_transit_payments {
294 my $in_transit_payments = 0;
295 foreach my $pay_batch ( qsearch('pay_batch', {
298 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
299 'batchnum' => $pay_batch->batchnum,
300 'custnum' => $self->custnum,
303 $in_transit_payments += $cust_pay_batch->amount;
306 sprintf( "%.2f", $in_transit_payments );