summaryrefslogtreecommitdiff
path: root/FS/FS/cust_main/Billing_Batch.pm
blob: c8292cd1b60957e0d5b140a3b090804b4d4c00ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package FS::cust_main::Billing_Batch;

use strict;
use vars qw( $conf );
use FS::Record qw( qsearch qsearchs dbh );
use FS::pay_batch;
use FS::cust_pay_batch;
use FS::cust_bill_pay_batch;

install_callback FS::UID sub { 
  $conf = new FS::Conf;
  #yes, need it for stuff below (prolly should be cached)
};

=item batch_card OPTION => VALUE...

Adds a payment for this invoice to the pending credit card batch (see
L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
runs the payment using a realtime gateway.

Options may include:

B<amount>: the amount to be paid; defaults to the customer's balance minus
any payments in transit.

B<realtime>: runs this as a realtime payment instead of adding it to a 
batch.  Deprecated.

B<invnum>: sets cust_pay_batch.invnum.

B<address1>, B<address2>, B<city>, B<state>, B<zip>, B<country>: sets 
the billing address for the payment; defaults to the customer's billing
location.

B<payby>, B<payinfo>, B<paydate>, B<payname>: sets the payment method, 
payment account, expiration date, and name; defaults to those fields 
in cust_main.

=cut

sub batch_card {
  my ($self, %options) = @_;

  my $amount;
  if (exists($options{amount})) {
    $amount = $options{amount};
  }else{
    $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
  }
  if ($amount <= 0) {
    warn(sprintf("Customer balance %.2f - in transit amount %.2f is <= 0.\n",
        $self->balance,
        $self->in_transit_payments
    ));
    return;
  }
  
  #my $invnum = delete $options{invnum};
  my $invnum = $options{invnum};

  #pay fields should all come from either cust_payby or options, not both
  #  in theory, could just pass payby, and use it to select cust_payby,
  #  but nothing currently needs that, so not implementing it now
  die "Incomplete payment details" 
    if  ($options{payby} || $options{payinfo} || $options{paydate} || $options{payname})
    && !($options{payby} && $options{payinfo} && $options{paydate} && $options{payname});

  #false laziness with Billing_Realtime
  my @cust_payby = $self->cust_payby('CARD','CHEK');

  # batch can't try out every one like realtime, just use first one
  my $cust_payby = $cust_payby[0];

  die "No customer payment info found"
    unless $options{payinfo} || $cust_payby;
                                                   
  my $payby = $options{payby} || $cust_payby->payby;

  if ($options{'realtime'}) {
    return $self->realtime_bop( FS::payby->payby2bop($payby),
                                $amount,
                                %options,
                              );
  }

  my $paycode= $options{paycode} || '';
  my $batch_type = "DEBIT";
  $batch_type = "CREDIT" if $paycode eq 'C';

  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  my $dbh = dbh;

  #this needs to handle mysql as well as Pg, like svc_acct.pm
  #(make it into a common function if folks need to do batching with mysql)
  $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
    or die "Cannot lock pay_batch: " . $dbh->errstr;

  my %pay_batch = (
    'status' => 'O',
    'payby'  => FS::payby->payby2payment($payby),
    'type'   => $batch_type,
  );
  $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');

  my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );

  unless ( $pay_batch ) {
    $pay_batch = new FS::pay_batch \%pay_batch;
    my $error = $pay_batch->insert;
    if ( $error ) {
      $dbh->rollback if $oldAutoCommit;
      die "error creating new batch: $error\n";
    }
  }

  my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
      'batchnum' => $pay_batch->batchnum,
      'custnum'  => $self->custnum,
  } );

  foreach (qw( address1 address2 city state zip country latitude longitude
               payby payinfo paydate payname paycode paytype ))
  {
    $options{$_} = '' unless exists($options{$_});
  }

  my $loc = $self->bill_location;

  my $cust_pay_batch = new FS::cust_pay_batch ( {
    'batchnum' => $pay_batch->batchnum,
    'invnum'   => $invnum || 0,                    # is there a better value?
                                                   # this field should be
                                                   # removed...
                                                   # cust_bill_pay_batch now
    'custnum'  => $self->custnum,
    'last'     => $self->getfield('last'),
    'first'    => $self->getfield('first'),
    'address1' => $options{address1} || $loc->address1,
    'address2' => $options{address2} || $loc->address2,
    'city'     => $options{city}     || $loc->city,
    'state'    => $options{state}    || $loc->state,
    'zip'      => $options{zip}      || $loc->zip,
    'country'  => $options{country}  || $loc->country,
    'payby'    => $options{payby}    || $cust_payby->payby,
    'payinfo'  => $options{payinfo}  || $cust_payby->payinfo,
    'paymask'  => ( $options{payinfo}
                      ? FS::payinfo_Mixin->mask_payinfo( $options{payby},
                                                         $options{payinfo} )
                      : $cust_payby->paymask
                  ),
    'exp'      => $options{paydate}  || $cust_payby->paydate,
    'payname'  => $options{payname}  || $cust_payby->payname,
    'paytype'  => $options{paytype}  || $cust_payby->{'Hash'}->{'paytype'},
    'amount'   => $amount,                         # consolidating
    'paycode'  => $options{paycode}  || '',
  } );
  
  $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
    if $old_cust_pay_batch;

  my $error;
  if ($old_cust_pay_batch) {
    $error = $cust_pay_batch->replace($old_cust_pay_batch)
  } else {
    $error = $cust_pay_batch->insert;
  }

  if ( $error ) {
    $dbh->rollback if $oldAutoCommit;
    #die $error;
    return $error; # e.g. "Illegal zip" ala RT#75998
  }

  if ($options{'processing-fee'} > 0) {
    my $pf_cust_pkg;
    my $processing_fee_text = 'Payment Processing Fee';

    unless ( $invnum ) { # probably from a payment screen
      # do we have any open invoices? pick earliest
      # uses the fact that cust_main->cust_bill sorts by date ascending
      my @open = $self->open_cust_bill;
      $invnum = $open[0]->invnum if scalar(@open);
    }

    unless ( $invnum ) {  # still nothing? pick last closed invoice
      # again uses fact that cust_main->cust_bill sorts by date ascending
      my @closed = $self->cust_bill;
      $invnum = $closed[$#closed]->invnum if scalar(@closed);
    }

    unless ( $invnum ) {
      # XXX: unlikely case - pre-paying before any invoices generated
      # what it should do is create a new invoice and pick it
      warn '\PROCESS FEE AND NO INVOICES PICKED TO APPLY IT!';
      return '';
    }

    my $pf_change_error = $self->charge({
            'amount'  => $options{'processing-fee'},
            'pkg'   => $processing_fee_text,
            'setuptax'  => 'Y',
            'cust_pkg_ref' => \$pf_cust_pkg,
    });

    if($pf_change_error) {
      warn 'Unable to add payment processing fee';
      return '';
    }

    $pf_cust_pkg->setup(time);
    my $pf_error = $pf_cust_pkg->replace;
    if($pf_error) {
      warn 'Unable to set setup time on cust_pkg for processing fee';
      # but keep going...
    }

    my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
    unless ( $cust_bill ) {
      warn "race condition + invoice deletion just happened";
      return '';
    }

    my $grand_pf_error =
      $cust_bill->add_cc_surcharge($pf_cust_pkg->pkgnum,$options{'processing-fee'});

    warn "cannot add Processing fee to invoice #$invnum: $grand_pf_error"
      if $grand_pf_error;
  }

  my $unapplied =   $self->total_unapplied_credits
                  + $self->total_unapplied_payments
                  + $self->in_transit_payments;
  foreach my $cust_bill ($self->open_cust_bill) {
    #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
    my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
      'invnum' => $cust_bill->invnum,
      'paybatchnum' => $cust_pay_batch->paybatchnum,
      'amount' => $cust_bill->owed,
      '_date' => time,
    };
    if ($unapplied >= $cust_bill_pay_batch->amount){
      $unapplied -= $cust_bill_pay_batch->amount;
      next;
    }else{
      $cust_bill_pay_batch->amount(sprintf ( "%.2f", 
                                   $cust_bill_pay_batch->amount - $unapplied ));      $unapplied = 0;
    }
    $error = $cust_bill_pay_batch->insert;
    if ( $error ) {
      $dbh->rollback if $oldAutoCommit;
      die $error;
    }
  }

  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
  '';
}

=item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]

Returns all batched payments (see L<FS::cust_pay_batch>) for this customer.

Optionally, a list or hashref of additional arguments to the qsearch call can
be passed.

=cut

sub cust_pay_batch {
  my $self = shift;
  my $opt = ref($_[0]) ? shift : { @_ };

  #return $self->num_cust_statement unless wantarray || keys %$opt;

  $opt->{'table'} = 'cust_pay_batch';
  $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
  $opt->{'hashref'}{'custnum'} = $self->custnum;
  $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';

  map { $_ } #behavior of sort undefined in scalar context
    sort { $a->paybatchnum <=> $b->paybatchnum }
      qsearch($opt);
}

=item in_transit_payments

Returns the total of requests for payments for this customer pending in 
batches in transit to the bank.  See L<FS::pay_batch> and L<FS::cust_pay_batch>

=cut

sub in_transit_payments {
  my $self = shift;
  my $in_transit_payments = 0;
  foreach my $pay_batch ( qsearch('pay_batch', {
    'status' => 'I',
  } ) ) {
    foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
      'batchnum' => $pay_batch->batchnum,
      'custnum' => $self->custnum,
      'status'  => '',
    } ) ) {
      $in_transit_payments += $cust_pay_batch->amount;
    }
  }
  sprintf( "%.2f", $in_transit_payments );
}

1;