Flatten Batches

File creation honors the order and number of Batch's that are created by the client. This is not optimized for the smallest file size and cost, that could be sent to the Federal Reserve for processing.

Flattening batches is a post file creation process that takes an input of an ACH.File and returns an ACH.File which have optimized the file by flattening (merging) Batch Headers that have the same information and appending all Entries into a single Batch.

Note: FlattenBatches (via FlattenBatches()) are only in ACH v1.2.0 and later.

Creating Flattened Batches

An ACH File supports calling FlattenBatches

// open a file for reading. Any io.Reader Can be used
f, err := os.Open(filepath.Join("test", "testdata", "flattenBatchesOneBatchHeader.ach"))

if err != nil {
    t.Fatal(err)
}
r := NewReader(f)
achFile, err := r.Read()
if err != nil {
    t.Fatalf("Issue reading file: %+v \n", err)
}

of, err := achFile.FlattenBatches()

if err != nil {
    t.Fatalf("Could not flatten the file: %+v \n", err)
}

if err := of.Validate(); err != nil {
    t.Fatalf("Flattened file did not validate: %+v \n", err)
}

// write the file to std out. Anything io.Writer
w := ach.NewWriter(os.Stdout)
if err := w.Write(of); err != nil {
    log.Fatalf("Unexpected error: %s\n", err)
}

w.Flush()