Class: Economic::CurrentInvoice

Inherits:
Entity
  • Object
show all
Defined in:
lib/economic/current_invoice.rb

Overview

CurrentInvoices are invoices that are not yet booked. They are therefore not read-only.

API documentation: www.e-conomic.com/apidocs/Documentation/T_Economic_Api_ICurrentInvoice.html

Examples

# Create invoice for debtor:
invoice = debtor.current_invoices.build
invoice.date = Time.now
invoice.due_date = Time.now + 15
invoice.exchange_rate = 100
invoice.is_vat_included = false

# Add a line to the invoice
invoice_line = invoice.lines.build
invoice_line.description = 'Line on invoice'
invoice_line.unit_handle = { :number => 1 }
invoice_line.product_handle = { :number => 101 }
invoice_line.quantity = 12
invoice_line.unit_net_price = 19.95
invoice.lines << invoice_line

invoice.save

Instance Attribute Summary

Attributes inherited from Entity

#partial, #persisted, #session

Instance Method Summary collapse

Methods inherited from Entity

#get_data, has_properties, #id, #inspect, #number, #partial?, #persisted?, properties, properties_not_triggering_full_load, proxy, #proxy, soap_action, #update_properties

Constructor Details

#initialize(properties = {}) ⇒ CurrentInvoice

Returns a new instance of CurrentInvoice



31
32
33
# File 'lib/economic/current_invoice.rb', line 31

def initialize(properties = {})
  super
end

Instance Method Details

#build_soap_dataObject

Returns OrderedHash with the properties of CurrentInvoice in the correct order, camelcased and ready to be sent via SOAP



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/economic/current_invoice.rb', line 78

def build_soap_data
  data = ActiveSupport::OrderedHash.new

  data['Id'] = id
  data['DebtorHandle'] = debtor.handle.to_hash unless debtor.blank?
  data['DebtorName'] = debtor_name
  data['DebtorAddress'] = debtor_address unless debtor_address.blank?
  data['DebtorPostalCode'] = debtor_postal_code unless debtor_postal_code.blank?
  data['DebtorCity'] = debtor_city unless debtor_city.blank?
  data['DebtorCountry'] = debtor_country unless debtor_country.blank?
  data['Date'] = date.iso8601 unless date.blank?
  data['TermOfPaymentHandle'] = { 'Id' => term_of_payment_handle[:id] } unless term_of_payment_handle.blank?
  data['DueDate'] = due_date.iso8601 unless due_date.blank?
  data['CurrencyHandle'] = { 'Code' => currency_handle[:code] } unless currency_handle.blank?
  data['ExchangeRate'] = exchange_rate
  data['IsVatIncluded'] = is_vat_included
  data['LayoutHandle'] = { 'Id' => layout_handle[:id] } unless layout_handle.blank?
  data['DeliveryDate'] = delivery_date ? delivery_date.iso8601 : nil
  data['NetAmount'] = net_amount
  data['VatAmount'] = vat_amount
  data['GrossAmount'] = gross_amount
  data['Margin'] = margin
  data['MarginAsPercent'] = margin_as_percent

  return data
end

#debtorObject



35
36
37
38
# File 'lib/economic/current_invoice.rb', line 35

def debtor
  return nil if debtor_handle.blank?
  @debtor ||= session.debtors.find(debtor_handle)
end

#debtor=(debtor) ⇒ Object



40
41
42
43
# File 'lib/economic/current_invoice.rb', line 40

def debtor=(debtor)
  self.debtor_handle = debtor.handle
  @debtor = debtor
end

#debtor_handle=(handle) ⇒ Object



45
46
47
48
# File 'lib/economic/current_invoice.rb', line 45

def debtor_handle=(handle)
  @debtor = nil unless handle == @debtor_handle
  @debtor_handle = handle
end

#handleObject



50
51
52
# File 'lib/economic/current_invoice.rb', line 50

def handle
  Handle.new(:id => @id)
end

#initialize_defaultsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/economic/current_invoice.rb', line 59

def initialize_defaults
  self.id = 0
  self.date = Time.now
  self.term_of_payment_handle = nil
  self.due_date = nil
  self.currency_handle = nil
  self.exchange_rate = 100 # Why am _I_ inputting this?
  self.is_vat_included = nil
  self.layout_handle = nil
  self.delivery_date = nil
  self.net_amount = 0
  self.vat_amount = 0
  self.gross_amount = 0
  self.margin = 0
  self.margin_as_percent = 0 # Why do I have to input both Margin and MarginAsPercent? Shouldn't powerful Windows machines running ASP.NET be able to compute this?
end

#linesObject

Returns the current invoice lines of CurrentInvoice



55
56
57
# File 'lib/economic/current_invoice.rb', line 55

def lines
  @lines ||= CurrentInvoiceLineProxy.new(self)
end

#saveObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/economic/current_invoice.rb', line 105

def save
  result = super
  id = result[:id]

  self.lines.each do |invoice_line|
    invoice_line.session = session
    invoice_line.invoice = self
    invoice_line.save
  end
end