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};
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});
67 #false laziness with Billing_Realtime
68 my @cust_payby = $self->cust_payby('CARD','CHEK');
70 # batch can't try out every one like realtime, just use first one
71 my $cust_payby = $cust_payby[0];
73 die "No customer payment info found"
74 unless $options{payinfo} || $cust_payby;
76 my $payby = $options{payby} || $cust_payby->payby;
78 if ($options{'realtime'}) {
79 return $self->realtime_bop( FS::payby->payby2bop($payby),
85 my $oldAutoCommit = $FS::UID::AutoCommit;
86 local $FS::UID::AutoCommit = 0;
89 #this needs to handle mysql as well as Pg, like svc_acct.pm
90 #(make it into a common function if folks need to do batching with mysql)
91 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
92 or return "Cannot lock pay_batch: " . $dbh->errstr;
96 'payby' => FS::payby->payby2payment($payby),
98 $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');
100 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
102 unless ( $pay_batch ) {
103 $pay_batch = new FS::pay_batch \%pay_batch;
104 my $error = $pay_batch->insert;
106 $dbh->rollback if $oldAutoCommit;
107 die "error creating new batch: $error\n";
111 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
112 'batchnum' => $pay_batch->batchnum,
113 'custnum' => $self->custnum,
116 foreach (qw( address1 address2 city state zip country latitude longitude
117 payby payinfo paydate payname ))
119 $options{$_} = '' unless exists($options{$_});
122 my $loc = $self->bill_location;
124 my $cust_pay_batch = new FS::cust_pay_batch ( {
125 'batchnum' => $pay_batch->batchnum,
126 'invnum' => $invnum || 0, # is there a better value?
127 # this field should be
129 # cust_bill_pay_batch now
130 'custnum' => $self->custnum,
131 'last' => $self->getfield('last'),
132 'first' => $self->getfield('first'),
133 'address1' => $options{address1} || $loc->address1,
134 'address2' => $options{address2} || $loc->address2,
135 'city' => $options{city} || $loc->city,
136 'state' => $options{state} || $loc->state,
137 'zip' => $options{zip} || $loc->zip,
138 'country' => $options{country} || $loc->country,
139 'payby' => $options{payby} || $cust_payby->payby,
140 'payinfo' => $options{payinfo} || $cust_payby->payinfo,
141 'exp' => $options{paydate} || $cust_payby->paydate,
142 'payname' => $options{payname} || $cust_payby->payname,
143 'paytype' => $options{paytype} || $cust_payby->paytype,
144 'amount' => $amount, # consolidating
147 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
148 if $old_cust_pay_batch;
151 if ($old_cust_pay_batch) {
152 $error = $cust_pay_batch->replace($old_cust_pay_batch)
154 $error = $cust_pay_batch->insert;
158 $dbh->rollback if $oldAutoCommit;
162 my $unapplied = $self->total_unapplied_credits
163 + $self->total_unapplied_payments
164 + $self->in_transit_payments;
165 foreach my $cust_bill ($self->open_cust_bill) {
166 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
167 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
168 'invnum' => $cust_bill->invnum,
169 'paybatchnum' => $cust_pay_batch->paybatchnum,
170 'amount' => $cust_bill->owed,
173 if ($unapplied >= $cust_bill_pay_batch->amount){
174 $unapplied -= $cust_bill_pay_batch->amount;
177 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
178 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
180 $error = $cust_bill_pay_batch->insert;
182 $dbh->rollback if $oldAutoCommit;
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
191 =item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
193 Returns all batched payments (see L<FS::cust_pay_batch>) for this customer.
195 Optionally, a list or hashref of additional arguments to the qsearch call can
202 my $opt = ref($_[0]) ? shift : { @_ };
204 #return $self->num_cust_statement unless wantarray || keys %$opt;
206 $opt->{'table'} = 'cust_pay_batch';
207 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
208 $opt->{'hashref'}{'custnum'} = $self->custnum;
209 $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';
211 map { $_ } #behavior of sort undefined in scalar context
212 sort { $a->paybatchnum <=> $b->paybatchnum }
216 =item in_transit_payments
218 Returns the total of requests for payments for this customer pending in
219 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
223 sub in_transit_payments {
225 my $in_transit_payments = 0;
226 foreach my $pay_batch ( qsearch('pay_batch', {
229 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
230 'batchnum' => $pay_batch->batchnum,
231 'custnum' => $self->custnum,
234 $in_transit_payments += $cust_pay_batch->amount;
237 sprintf( "%.2f", $in_transit_payments );