add custnum to invoice template vars
[freeside.git] / FS / FS / cust_bill.pm
index 1801b12..e5e741c 100644 (file)
@@ -294,7 +294,7 @@ sub cust_suspend_if_balance_over {
   if ( $cust_main->total_owed_date($self->_date) < $amount ) {
     return ();
   } else {
-    $cust_main->suspend;
+    $cust_main->suspend(@_);
   }
 }
 
@@ -1303,10 +1303,11 @@ sub realtime_bop {
 
 }
 
-=item batch_card
+=item batch_card OPTION => VALUE...
 
 Adds a payment for this invoice to the pending credit card batch (see
-L<FS::cust_pay_batch>).
+L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
+runs the payment using a realtime gateway.
 
 =cut
 
@@ -1318,20 +1319,28 @@ sub batch_card {
   return '' unless $amount > 0;
   
   if ($options{'realtime'}) {
-    return $cust_main->realtime_bop ( $FS::payby::payby2bop{$cust_main->payby}, $amount,
-      %options,
-    );
+    return $cust_main->realtime_bop( FS::payby->payby2bop($cust_main->payby),
+                                     $amount,
+                                     %options,
+                                   );
   }
 
   my $oldAutoCommit = $FS::UID::AutoCommit;
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
-  my $pay_batch = qsearchs('pay_batch', {'status' => 'O'});
+  $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
+    or return "Cannot lock pay_batch: " . $dbh->errstr;
+
+  my %pay_batch = (
+    'status' => 'O',
+    'payby'  => FS::payby->payby2payment($cust_main->payby),
+  );
+
+  my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
 
   unless ( $pay_batch ) {
-    $pay_batch = new FS::pay_batch;
-    $pay_batch->setfield('status' => 'O');
+    $pay_batch = new FS::pay_batch \%pay_batch;
     my $error = $pay_batch->insert;
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
@@ -1340,26 +1349,29 @@ sub batch_card {
   }
 
   my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
-      'batchnum' => $pay_batch->getfield('batchnum'),
-      'custnum'  => $cust_main->getfield('custnum'),
+      'batchnum' => $pay_batch->batchnum,
+      'custnum'  => $cust_main->custnum,
   } );
 
   my $cust_pay_batch = new FS::cust_pay_batch ( {
-    'batchnum' => $pay_batch->getfield('batchnum'),
+    'batchnum' => $pay_batch->batchnum,
     'invnum'   => $self->getfield('invnum'),       # is there a better value?
-    'custnum'  => $cust_main->getfield('custnum'),
+                                                   # this field should be
+                                                  # removed...
+                                                  # cust_bill_pay_batch now
+    'custnum'  => $cust_main->custnum,
     'last'     => $cust_main->getfield('last'),
     'first'    => $cust_main->getfield('first'),
-    'address1' => $cust_main->getfield('address1'),
-    'address2' => $cust_main->getfield('address2'),
-    'city'     => $cust_main->getfield('city'),
-    'state'    => $cust_main->getfield('state'),
-    'zip'      => $cust_main->getfield('zip'),
-    'country'  => $cust_main->getfield('country'),
+    'address1' => $cust_main->address1,
+    'address2' => $cust_main->address2,
+    'city'     => $cust_main->city,
+    'state'    => $cust_main->state,
+    'zip'      => $cust_main->zip,
+    'country'  => $cust_main->country,
     'payby'    => $cust_main->payby,
     'payinfo'  => $cust_main->payinfo,
-    'exp'      => $cust_main->getfield('paydate'),
-    'payname'  => $cust_main->getfield('payname'),
+    'exp'      => $cust_main->paydate,
+    'payname'  => $cust_main->payname,
     'amount'   => $amount,                          # consolidating
   } );
   
@@ -1380,7 +1392,7 @@ sub batch_card {
 
   my $unapplied = $cust_main->total_credited + $cust_main->total_unapplied_payments + $cust_main->in_transit_payments;
   foreach my $cust_bill ($cust_main->open_cust_bill) {
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+    #$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,
@@ -1616,12 +1628,14 @@ sub print_text {
 
   #setup template variables
   package FS::cust_bill::_template; #!
-  use vars qw( $invnum $date $page $total_pages @address $overdue @buf $agent );
+  use vars qw( $custnum $invnum $date $agent @address $overdue
+               $page $total_pages @buf );
 
+  $custnum = $self->custnum;
   $invnum = $self->invnum;
   $date = $self->_date;
-  $page = 1;
   $agent = $self->cust_main->agent->agent;
+  $page = 1;
 
   if ( $FS::cust_bill::invoice_lines ) {
     $total_pages =
@@ -1754,6 +1768,7 @@ sub print_latex {
   }
 
   my %invoice_data = (
+    'custnum'      => $self->custnum,
     'invnum'       => $self->invnum,
     'date'         => time2str('%b %o, %Y', $self->_date),
     'today'        => time2str('%b %o, %Y', $today),
@@ -2154,6 +2169,7 @@ sub print_html {
     or die 'While compiling ' . $templatefile . ': ' . $Text::Template::ERROR;
 
   my %invoice_data = (
+    'custnum'      => $self->custnum,
     'invnum'       => $self->invnum,
     'date'         => time2str('%b&nbsp;%o,&nbsp;%Y', $self->_date),
     'today'        => time2str('%b %o, %Y', $today),