rubyritas / ach

Helper for building ACH files in Ruby
MIT License
111 stars 75 forks source link

Add Balancing Entry Detail rows in order to construct a Balanced ACH file #57

Closed samgranieri closed 3 years ago

samgranieri commented 3 years ago

At CityBase, one of our clients asked us to transfer NACHA files with a balancing entry detail row, aka a balanced ACH file

What is a balanced ACH file, and how does it differ from how we (and this gem) generate ACH files now?

I'll simply quote from this site's documentation

It is similar to the regular NACHA file layout, except it includes an additional line for the balancing amount for the sum of each transaction.

I've also relied on https://www.treasurysoftware.com/ACH/ACH-File-Offset-Record-Balanced.aspx for documentation on how to construct an balanced ACH file

You would create a balancing entry detail row like this

ACH::BalancingEntryDetail.new.tap do |entry|
  entry.transaction_code = ACH::CHECKING_DEBIT
  entry.routing_number = '111111111'
  entry.account_number = '22222222222'
  entry.amount = batch.entries.inject(0){|sum, entry| sum + entry.amount}
  entry.account_description = 'OFFSET'
  entry.origin_routing_number = '33333333'
  entry.trace_number = 1
end

I've modified batch.rb to check to see if the last entry in a batch is a balanced entry, and to alter the credits and debit totals accordingly in the control row.

Here's an example of a balanced ACH file

101 999999999 6666666662011031627A094101BANK NAME              BANK NAME                      
5200COMPANY NAME                        1123456789PPDDESCRIPTIO201103201104   1777777770000001
62211111111122222222222      0000000101EMPLOYEE NAME  EMPLOYEE NAME           0000000000000001
62211111111122222222222      0000000101EMPLOYEE NAME  EMPLOYEE NAME           0000000000000001
62211111111122222222222      0000000101EMPLOYEE NAME  EMPLOYEE NAME           0000000000000001
62211111111122222222222      0000000101EMPLOYEE NAME  EMPLOYEE NAME           0000000000000001
62711111111122222222222      0000000404               OFFSET                  0333333330000001
820000000500555555550000000004040000000004041123456789                         777777770000001
9000001000001000000050055555555000000000404000000000404                                       
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
jm81 commented 3 years ago

@samgranieri Thanks! I've pushed version 0.5.16 to rubygems which includes this PR