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

#id, #number, #partial, #persisted, #session

Instance Method Summary collapse

Methods inherited from Entity

#==, default_values, defaults, #destroy, #get, #get_data, #handle=, handle_writer, has_properties, #inspect, key, #partial?, #persisted?, properties, properties_not_triggering_full_load, property_reader, property_writer, proxy, #proxy, #update_properties

Constructor Details

#initialize(properties = {}) ⇒ CurrentInvoice

Returns a new instance of CurrentInvoice.



65
66
67
# File 'lib/economic/current_invoice.rb', line 65

def initialize(properties = {})
  super
end

Instance Method Details

#attentionObject



69
70
71
72
# File 'lib/economic/current_invoice.rb', line 69

def attention
  return nil if attention_handle.nil?
  @attention ||= session.contacts.find(attention_handle)
end

#attention=(contact) ⇒ Object



74
75
76
77
# File 'lib/economic/current_invoice.rb', line 74

def attention=(contact)
  self.attention_handle = contact.handle
  @attention = contact
end

#attention_handle=(handle) ⇒ Object



79
80
81
82
# File 'lib/economic/current_invoice.rb', line 79

def attention_handle=(handle)
  @attention = nil unless handle == @attention_handle
  @attention_handle = handle
end

#bookObject

Books a current invoice. An invoice number greater than all other invoice numbers will be assigned to the resulting Economic::Invoice.

Returns the resulting Economic::Invoice object



88
89
90
91
92
93
# File 'lib/economic/current_invoice.rb', line 88

def book
  response = request(:book, "currentInvoiceHandle" => handle.to_hash)

  # Find the created Invoice
  session.invoices.find(response[:number])
end

#book_with_number(number) ⇒ Object

Books a current invoice. The given number will be assigned to the resulting Economic::Invoice.

Returns the resulting Economic::Invoice object



99
100
101
102
103
104
105
106
107
108
# File 'lib/economic/current_invoice.rb', line 99

def book_with_number(number)
  response = request(
    :book_with_number,
    "currentInvoiceHandle" => handle.to_hash,
    "number" => number
  )

  # Find the created Invoice
  session.invoices.find(response[:number])
end

#debtorObject



110
111
112
113
# File 'lib/economic/current_invoice.rb', line 110

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

#debtor=(debtor) ⇒ Object



115
116
117
118
# File 'lib/economic/current_invoice.rb', line 115

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

#debtor_handle=(handle) ⇒ Object



120
121
122
123
# File 'lib/economic/current_invoice.rb', line 120

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

#handleObject



125
126
127
# File 'lib/economic/current_invoice.rb', line 125

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

#linesObject



129
130
131
# File 'lib/economic/current_invoice.rb', line 129

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

#saveObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/economic/current_invoice.rb', line 133

def save
  lines = self.lines

  result = super
  self.id = result[:id].to_i

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