Sha256: 18a657b74930368b18c146d35163f2fa4eac91adfa46fda0c2546949980470f1

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

module Skr

    # Customers are Companies (or individuals) that purchase {Sku}s.
    # They have both a billing and shipping address,
    # a gl account that payments should be applied against, and a payment term.
    #
    class Customer < Skr::Model

        # Common code shared with {Vendor}
        include BusinessEntity

        belongs_to :gl_receivables_account, class_name: 'Skr::GlAccount', export: true

        has_many :sales_orders, inverse_of: :customer
        has_many :invoices,     inverse_of: :customer, listen: { save: :update_balance! }

        delegate_and_export :gl_receivables_account_number

        validates :gl_receivables_account, set: true

        # Updates the amount the customer owes, which is the sum of the amount unpaid on open invoices
        def update_balance!(*)
            update_attributes open_balance: invoices.open_for_customer(self)
                                                    .with_details.sum('details.total')
        end

      private

        def set_defaults
            super
            self.gl_receivables_account ||= GlAccount.default_for( :ar )
        end
    end

end # Skr module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stockor-0.1.7 lib/skr/models/customer.rb
stockor-0.1.5 lib/skr/models/customer.rb