Class: TFSO::Invoice

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/tfso/invoice.rb

Constant Summary collapse

URL =
'https://api.24sevenoffice.com/Economy/InvoiceOrder/V001/InvoiceService.asmx?wsdl'

Instance Method Summary collapse

Methods included from Helpers

#ensure_authenticated, #intialize_savon_client, #savon_client, #session_id, #session_id=

Constructor Details

#initialize(auth) ⇒ Invoice

Returns a new instance of Invoice.



8
9
10
11
12
# File 'lib/tfso/invoice.rb', line 8

def initialize(auth)
  ensure_authenticated(auth)
  self.session_id = auth.session_id
  intialize_savon_client
end

Instance Method Details

#create(order_attributes) ⇒ Object



44
45
46
47
# File 'lib/tfso/invoice.rb', line 44

def create(order_attributes)
  response = savon_client.call(:save_invoices, message: {invoices: [{InvoiceOrder: order_attributes}] }, cookies: @cookies)
  response.body[:save_invoices_response][:save_invoices_result][:invoice_order]
end

#find(search_params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tfso/invoice.rb', line 14

def find(search_params)
  response = savon_client.call(:get_invoices, message: {searchParams: search_params, invoiceReturnProperties: {string: ['OrderId', 'InvoiceId', 'CustomerId', 'OrderStatus', 'DateOrdered', 'DateInvoiced', 'PaymentTime', 'CustomerReferenceNo', 'IncludeVAT', 'YourReference', 'OrderTotalIncVat', 'OrderTotalVat', 'InvoiceTitle', 'InvoiceText', 'Paid', 'Currency', 'PaymentMethodId', 'PaymentAmount', 'TypeOfSaleId', 'Distributor', 'DistributionMethod', 'InvoiceEmailAddress']}, rowReturnProperties: {string: ['ProductId', 'VatRate', 'Price', 'Name', 'DiscountRate', 'Quantity']} }, cookies: @cookies)
  result = response.body[:get_invoices_response][:get_invoices_result]
  if result
    if result[:invoice_order].class == Hash
      [result[:invoice_order]]
    else
      result[:invoice_order]
    end
  else
    []
  end
end

#find_by_invoice_id(id) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/tfso/invoice.rb', line 28

def find_by_invoice_id(id)
  if invoice = find(InvoiceIds: {int: [id]})
    invoice.first
  else
    false
  end
end

#find_by_order_id(id) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/tfso/invoice.rb', line 36

def find_by_order_id(id)
  if invoice = find(OrderIds: {int: [id]})
    invoice.first
  else
    false
  end
end

#transform_attributes(invoice, items) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tfso/invoice.rb', line 49

def transform_attributes(invoice, items)
  invoice.compact!
  items.compact!
  state = case invoice[:state]
  when :invoice
    'Invoiced'
  when :draft
    'Offer'
  else
    invoice[:state]
  end
  invoice_info = {
    OrderStatus: state,
    DateOrdered: Time.zone.today.iso8601,
    CustomerId: invoice[:customer_id],
    CustomerName: invoice[:customer_name],
    CustomerOrgNo: invoice[:customer_gov_no],
    InvoiceEmailAddress: invoice[:customer_email],
    InvoiceRows: []
  }
  invoice_info[:TypeOfSaleId] = invoice[:type_of_sale_id]
  invoice_info[:PaymentTime] = invoice[:payment_time]
  if invoice[:currency]
    invoice_info[:Currency] = {
      Symbol: invoice[:currency],
      Rate: invoice[:currency_rate].to_f,
    }
  end
  invoice_info[:InvoiceTitle] = invoice[:title] if invoice[:title]
  invoice_info[:InvoiceText] = invoice[:description] if invoice[:description]
  invoice_info[:YourReference] = invoice[:order_ref]

  invoice_info[:Distributor] = invoice[:distributor] if invoice[:distributor]
  invoice_info[:DistributionMethod] = invoice[:distribution_method] if invoice[:distribution_method]

  invoice[:Addresses] = {
    Invoice: {
      :Street => invoice[:customer_street],
      :PostalCode => invoice[:customer_postal_code],
      :PostalArea => invoice[:customer_city],
      :State => invoice[:customer_state],
      :Country => invoice[:customer_country_code]
    }
  }
  if items.any?
    invoiceRows = []
    items.each {|item|
      if item[:type] == :text
        invoiceRows << {:InvoiceRow => {
          Type: 'Text',
          Name: item[:name]
        }}
      else
        invoiceRows << {:InvoiceRow => {
          Type: 'Normal',
          ProductId: item[:product_id],
          Name: item[:name],
          Price: item[:price].to_f,
          Quantity: item[:quantity].to_f
        }}
      end
    }
    invoice_info[:InvoiceRows] = invoiceRows
  end
  invoice_info
end