Sha256: 03ad03cf9d87500d7586c6fc7aea08a8c27b4fcf9ec8cecea3933ef238da61ab

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

module Bankserv
  
  class Credit < ActiveRecord::Base
    extend Eft

    serialize :response
    scope :unprocessed, where(status: "new")

    belongs_to :bank_account, :foreign_key => 'bankserv_bank_account_id'
    belongs_to :request, :foreign_key => 'bankserv_request_id'
    
    after_create :generate_internal_user_ref
    
    def generate_internal_user_ref
      self.internal_user_ref = "CREDIT#{id}"
      save!
    end
    
    def self.for_reference(reference)
      self.where(:user_ref => reference)
    end
    
    def self.for_internal_reference(reference)
      self.where(:internal_user_ref => reference)
    end
    
    def new?
      status == "new"
    end
    
    def pending?
      status == "pending"
    end
    
    def error?
      status == "error"
    end
    
    def completed?
      status == "completed"
    end
    
    def standard?
      record_type == "standard"
    end
    
    def contra?
      record_type == "contra"
    end
    
    def formatted_user_ref
      if contra?
        Configuration.client_abbreviated_name.ljust(10, ' ') << "CONTRA#{user_ref}"
      else
        Configuration.client_abbreviated_name.ljust(10, ' ') << user_ref
      end
    end
    
    def contra_bank_details
      if self.standard?
        Credit.where(record_type: "contra", batch_id: self.batch_id, status: "new").first.bank_account
      else
        self.bank_account
      end
    end
    
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bankserv-0.2.3 lib/bankserv/credit.rb
bankserv-0.2.2 lib/bankserv/credit.rb
bankserv-0.2.1 lib/bankserv/credit.rb
bankserv-0.1.9 lib/bankserv/credit.rb
bankserv-0.1.8 lib/bankserv/credit.rb