lokalebasen/rconomic

View on GitHub
lib/economic/debtor.rb

Summary

Maintainability
A
1 hr
Test Coverage
require 'economic/entity'

module Economic

  # Represents a debtor in E-conomic.
  #
  # Examples
  #
  #   # Find a debtor:
  #   debtor = economic.debtors.find(558)
  #
  #   # Creating a debtor:
  #   debtor = economic.debtors.build
  #   debtor.number = economic.debtors.next_available_number
  #   debtor.debtor_group_handle = { :number => 1 }
  #   debtor.name = 'Apple Inc'
  #   debtor.vat_zone = 'HomeCountry' # HomeCountry, EU, Abroad
  #   debtor.currency_handle = { :code => 'DKK' }
  #   debtor.price_group_handle = { :number => 1 }
  #   debtor.is_accessible = true
  #   debtor.ci_number = '12345678'
  #   debtor.term_of_payment_handle = { :id => 1 }
  #   debtor.vat_number = 12345678
  #   debtor.ean = 9780471117094
  #   debtor.layout_handle = { :id => 16 }
  #   debtor.save
  class Debtor < Entity
    has_properties :number,
      :debtor_group_handle,
      :name,
      :vat_zone,
      :currency_handle,
      :price_group_handle,
      :is_accessible,
      :ean,
      :public_entry_number,
      :email,
      :telephone_and_fax_number,
      :website,
      :address,
      :postal_code,
      :city,
      :country,
      :credit_maximum,
      :vat_number,
      :county,
      :ci_number,
      :term_of_payment_handle,
      :layout_handle,
      :attention_handle,
      :your_reference_handle,
      :our_reference_handle,
      :balance

    def handle
      @handle || Handle.new({:number => @number})
    end

    # Returns the Debtors contacts
    def contacts
      @contacts ||= DebtorContactProxy.new(self)
    end

    # Provides access to the current invoices for Debtor - ie invoices that
    # haven't yet been booked
    def current_invoices
      @current_invoices ||= CurrentInvoiceProxy.new(self)
    end

    def invoices
      return [] if self.handle.empty?
      @invoices ||= DebtorProxy.new(self).get_invoices(self.handle)
    end

    def orders
      return [] if self.handle.empty?
      @orders ||= DebtorProxy.new(self).get_orders(self.handle)
    end

    protected

    def fields
      to_hash = Proc.new { |handle| handle.to_hash }
      [
        ["Handle", :handle, to_hash, :required],
        ["Number", :handle, Proc.new { |h| h.number }, :required],
        ["DebtorGroupHandle", :debtor_group_handle, to_hash],
        ["Name", :name, nil, :required],
        ["VatZone", :vat_zone, nil, :required],
        ["CurrencyHandle", :currency_handle, to_hash],
        ["PriceGroupHandle", :price_group_handle, to_hash],
        ["IsAccessible", :is_accessible, nil, :required],
        ["Ean", :ean],
        ["PublicEntryNumber", :public_entry_number],
        ["Email", :email],
        ["TelephoneAndFaxNumber", :telephone_and_fax_number],
        ["Website", :website],
        ["Address", :address],
        ["PostalCode", :postal_code],
        ["City", :city],
        ["Country", :country],
        ["CreditMaximum", :credit_maximum],
        ["VatNumber", :vat_number],
        ["County", :county],
        ["CINumber", :ci_number],
        ["TermOfPaymentHandle", :term_of_payment_handle, to_hash],
        ["LayoutHandle", :layout_handle, to_hash],
        ["AttentionHandle", :attention_handle],
        ["YourReferenceHandle", :your_reference_handle],
        ["OurReferenceHandle", :our_reference_handle],
        ["Balance", :balance]
      ]
    end
  end
end