This commit was generated by cvs2svn to compensate for changes in r6252,
[freeside.git] / FS / FS / pay_batch.pm
index 54c370b..5448b03 100644 (file)
@@ -30,8 +30,8 @@ FS::pay_batch - Object methods for pay_batch records
 
 =head1 DESCRIPTION
 
 
 =head1 DESCRIPTION
 
-An FS::pay_batch object represents an example.  FS::pay_batch inherits from
-FS::Record.  The following fields are currently supported:
+An FS::pay_batch object represents an payment batch.  FS::pay_batch inherits
+from FS::Record.  The following fields are currently supported:
 
 =over 4
 
 
 =over 4
 
@@ -54,7 +54,7 @@ FS::Record.  The following fields are currently supported:
 
 =item new HASHREF
 
 
 =item new HASHREF
 
-Creates a new example.  To add the example to the database, see L<"insert">.
+Creates a new batch.  To add the batch to the database, see L<"insert">.
 
 Note that this stores the hash reference, not a distinct copy of the hash it
 points to.  You can ask the object for a copy with the I<hash> method.
 
 Note that this stores the hash reference, not a distinct copy of the hash it
 points to.  You can ask the object for a copy with the I<hash> method.
@@ -93,7 +93,7 @@ returns the error, otherwise returns false.
 
 =item check
 
 
 =item check
 
-Checks all fields to make sure this is a valid example.  If there is
+Checks all fields to make sure this is a valid batch.  If there is
 an error, returns the error, otherwise returns false.  Called by the insert
 and replace methods.
 
 an error, returns the error, otherwise returns false.  Called by the insert
 and replace methods.
 
@@ -137,7 +137,7 @@ sub set_status {
   $self->replace();
 }
 
   $self->replace();
 }
 
-=item import results OPTION => VALUE, ...
+=item import_results OPTION => VALUE, ...
 
 Import batch results.
 
 
 Import batch results.
 
@@ -145,7 +145,7 @@ Options are:
 
 I<filehandle> - open filehandle of results file.
 
 
 I<filehandle> - open filehandle of results file.
 
-I<format> - "csv-td_canada_trust-merchant_pc_batch", "csv-chase_canada-E-xactBatch" or "PAP"
+I<format> - "csv-td_canada_trust-merchant_pc_batch", "csv-chase_canada-E-xactBatch", "ach-spiritone", or "PAP"
 
 =cut
 
 
 =cut
 
@@ -333,6 +333,40 @@ sub import_results {
       0;
     };
 
       0;
     };
 
+  }elsif ( $format eq 'ach-spiritone' ) {
+
+    $filetype = "CSV";
+
+    @fields = (
+      '',            # Name
+      'paybatchnum', # ID: Number of the transaction
+      'aba',         # ABA Number for the transaction
+      'payinfo',     # Bank Account Number for the transaction
+      '',            # Transaction Type:  27 - debit
+      'paid',        # Amount:  Amount of the transaction.  Dollars and cents
+                     #          with decimal entered.
+      '',            # Default Transaction Type
+      '',            # Default Amount:  Dollars and cents with decimal entered.
+    );
+
+    $end_condition = sub {
+      '';
+    };
+
+    $hook = sub {
+      my $hash = shift;
+      $hash->{'_date'} = time;  # got a better one?
+      $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'aba'};
+    };
+
+    $approved_condition = sub {
+      1;
+    };
+
+    $declined_condition = sub {
+      0;
+    };
+
 
   } else {
     return "Unknown format $format";
 
   } else {
     return "Unknown format $format";
@@ -419,6 +453,20 @@ sub import_results {
 
       $new_cust_pay_batch->status('Approved');
 
 
       $new_cust_pay_batch->status('Approved');
 
+    } elsif ( &{$declined_condition}(\%hash) ) {
+
+      $new_cust_pay_batch->status('Declined');
+
+    }
+
+    my $error = $new_cust_pay_batch->replace($cust_pay_batch);
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "error updating status of paybatchnum $hash{'paybatchnum'}: $error\n";
+    }
+
+    if ( $new_cust_pay_batch->status =~ /Approved/i ) {
+
       my $cust_pay = new FS::cust_pay ( {
         'custnum'  => $custnum,
        'payby'    => $payby,
       my $cust_pay = new FS::cust_pay ( {
         'custnum'  => $custnum,
        'payby'    => $payby,
@@ -434,33 +482,38 @@ sub import_results {
   
       $cust_pay->cust_main->apply_payments;
 
   
       $cust_pay->cust_main->apply_payments;
 
-    } elsif ( &{$declined_condition}(\%hash) ) {
+    } elsif ( $new_cust_pay_batch->status =~ /Declined/i ) {
 
 
-      $new_cust_pay_batch->status('Declined');
+      #false laziness w/cust_main::collect
 
 
-      foreach my $part_bill_event ( due_events ( $new_cust_pay_batch,
-                                                 'DCLN',
-                                                '',
-                                                '') ) {
+      my $due_cust_event = $new_cust_pay_batch->cust_main->due_cust_event(
+        #'check_freq' => '1d', #?
+        'eventtable' => 'cust_pay_batch',
+        'objects'    => [ $new_cust_pay_batch ],
+      );
+      unless( ref($due_cust_event) ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $due_cust_event;
+      }
 
 
-        # don't run subsequent events if balance<=0
-        last if $cust_pay_batch->cust_main->balance <= 0;
+      foreach my $cust_event ( @$due_cust_event ) {
+        
+        #XXX lock event
+    
+        #re-eval event conditions (a previous event could have changed things)
+        next unless $cust_event->test_conditions;
 
 
-       if (my $error = $part_bill_event->do_event($new_cust_pay_batch)) {
+       if ( my $error = $cust_event->do_event() ) {
          # gah, even with transactions.
          # gah, even with transactions.
-         $dbh->commit if $oldAutoCommit; #well.
-         return $error;
+         #$dbh->commit if $oldAutoCommit; #well.
+         $dbh->rollback if $oldAutoCommit;
+          return $error;
        }
 
       }
 
     }
 
        }
 
       }
 
     }
 
-    my $error = $new_cust_pay_batch->replace($cust_pay_batch);
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return "error updating status of paybatchnum $hash{'paybatchnum'}: $error\n";
-    }
 
   }
   
 
   }