lib/aba.rb in aba-0.1.0 vs lib/aba.rb in aba-0.3.0

- old
+ new

@@ -3,29 +3,27 @@ require "aba/transaction" class Aba include Aba::Validations - attr_accessor :bsb, :account_number, :financial_institution, :user_name, :user_id, - :description, :process_at, :name_of_remitter, :transactions + attr_accessor :bsb, :financial_institution, :user_name, :user_id, :description, :process_at - validates_presence_of :bsb, :financial_institution, :user_name, :user_id, :description, :process_at - - validates_bsb :bsb + validates_bsb :bsb, allow_blank: true - validates_max_length :account_number, 9 - validates_max_length :financial_institution, 3 validates_max_length :user_name, 26 validates_max_length :user_id, 6 validates_max_length :description, 12 + validates_length :financial_institution, 3 + validates_length :process_at, 6 + def initialize(attrs = {}) attrs.each do |key, value| send("#{key}=", value) end - self.transactions = [] + @transactions = [] yield self if block_given? end def to_s @@ -37,84 +35,125 @@ # Batch control record output += "\r\n#{batch_control_record}" end + def add_transaction(transaction) + @transactions << transaction + end + private def descriptive_record # Record type + # Max: 1 + # Char position: 1 output = "0" - # Bank/State/Branch number of the funds account with a hyphen in the 4th character position. e.g. 013-999. - output += self.bsb + # Optional branch number of the funds account with a hyphen in the 4th character position + # Char position: 2-18 + # Max: 17 + # Blank filled + output += self.bsb.nil? ? " " * 17 : self.bsb.to_s.ljust(17) - # Funds account number. - output += self.account_number.to_s.ljust(9, " ") - - # Reserved - output += " " - # Sequence number + # Char position: 19-20 + # Max: 2 + # Zero padded output += "01" - # Must contain the bank mnemonic that is associated with the BSB of the funds account. e.g. ‘ANZ’. - output += self.financial_institution[0..2].to_s + # Name of user financial instituion + # Max: 3 + # Char position: 21-23 + output += self.financial_institution.to_s # Reserved + # Max: 7 + # Char position: 24-30 output += " " * 7 - # Name of User supplying File as advised by User's Financial Institution - output += self.user_name.to_s.ljust(26, " ") + # Name of User supplying File + # Char position: 31-56 + # Max: 26 + # Blank filled + output += self.user_name.to_s.ljust(26) - # Direct Entry User ID. + # Direct Entry User ID + # Char position: 57-62 + # Max: 6 + # Zero padded output += self.user_id.to_s.rjust(6, "0") - # Description of payments in the file (e.g. Payroll, Creditors etc.). - output += self.description.to_s.ljust(12, " ") + # Description of payments in the file (e.g. Payroll, Creditors etc.) + # Char position: 63-74 + # Max: 12 + # Blank filled + output += self.description.to_s.ljust(12) - # Date and time on which the payment is to be processed. - output += self.process_at.strftime("%d%m%y%H%M") + # Date on which the payment is to be processed + # Char position: 75-80 + # Max: 6 + output += self.process_at.rjust(6, "0") # Reserved - output += " " * 36 + # Max: 40 + # Char position: 81-120 + output += " " * 40 end def batch_control_record - # Record type - output = "7" - - # BSB Format Filler - output += "999-999" - - # Reserved - output += " " * 12 - net_total_amount = 0 credit_total_amount = 0 debit_total_amount = 0 @transactions.each do |t| net_total_amount += t.amount credit_total_amount += t.amount if t.amount > 0 debit_total_amount += t.amount if t.amount < 0 end - # Must equal the difference between File Credit & File Debit Total Amounts. Show in cents without punctuation + # Record type + # Max: 1 + # Char position: 1 + output = "7" + + # BSB Format Filler + # Max: 7 + # Char position: 2-8 + output += "999-999" + + # Reserved + # Max: 12 + # Char position: 9-20 + output += " " * 12 + + # Net total + # Max: 10 + # Char position: 21-30 output += net_total_amount.abs.to_s.rjust(10, "0") - # Batch Credit Total Amount + # Credit Total Amount + # Max: 10 + # Char position: 31-40 output += credit_total_amount.abs.to_s.rjust(10, "0") - # Batch Debit Total Amount + # Debit Total Amount + # Max: 10 + # Char position: 41-50 output += debit_total_amount.abs.to_s.rjust(10, "0") # Reserved + # Max: 24 + # Char position: 51-74 output += " " * 24 - # Batch Total Item Count + # Total Item Count + # Max: 6 + # Char position: 75-80 output += @transactions.size.to_s.rjust(6, "0") # Reserved + # Max: 40 + # Char position: 81-120 output += " " * 40 end -end \ No newline at end of file +end