Sha256: fe229be66ff26241994fd772b67aa54ff6d708ef98f27554bbcdc9d1c2aa6dc0

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 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 {Customer} and {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.invoice_total')
        end

      private

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

end # Skr module

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stockor-0.5.0 lib/skr/models/customer.rb
stockor-0.4.0 lib/skr/models/customer.rb
stockor-0.3.0 lib/skr/models/customer.rb
stockor-0.2 lib/skr/models/customer.rb