Sha256: 10788eadf456ca6b7c273ffff70ed8551ef23a3063bf1951f199441c05688369

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module Skr

    # A GlAccount *(short for General Ledger Account)* is used
    # to define each class of items for
    # which money or the equivalent is spent or received.
    class GlAccount < Skr::Model

        # @private
        DEFAULT_ACCOUNTS = {}

        is_immutable except: [:name, :is_active]

        validates :name, :description, :presence => true

        # @!attribute description
        #   A short description of the GL Account

        # A future improvement would be to allow arbitrary account masks.  For now, keep it simple
        # with mandatory 4 characters + 2 char branch code
        validates :number, :presence => true, :numericality=>true, :length=>{ :is=>4 }

        # @return [GlAccount] the default account for the key from {Skr::Core::Configuration.default_gl_accounts}
        def self.default_for( lookup )
            number = Skr.config.default_gl_accounts[ lookup ]
            raise RuntimeError.new("Unkown GL default account lookup code: {lookup}") unless number
            DEFAULT_ACCOUNTS[ lookup ] ||= GlAccount.find_by_number( number )
        end

        # @return [String] the account number combined with location branch code
        def number_for_location( location )
            self.number + location.gl_branch_code
        end

        # @return [String] the account number combined with the default branch code
        def default_number
            self.number + Skr.config.default_branch_code
        end

        # @return [BigDecimal] The balance for the current period
        def trial_balance
            balance_for( GlPeriod.current )
        end

        # @return [String] the account number suitable for querying all branches
        def account_mask
            number + '%'
        end

        # @return [BigDecimal] the balance for a given period
        def balance_for( period, mask = self.account_mask )
            GlPosting.matching( period, account_mask ).sum(:amount)
        end

    end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stockor-0.1.9 lib/skr/models/gl_account.rb
stockor-0.1.8 lib/skr/models/gl_account.rb
stockor-0.1.7 lib/skr/models/gl_account.rb
stockor-0.1.5 lib/skr/models/gl_account.rb