module Economic::SoapMethods

Public Instance Methods

all() click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 17
def all
  result = Economic::SoapAPI.call(
    soap_method_names[:all][:method],
  )

  ids = result_array(result[soap_method_names[:all][:handle]]).map { |record|
    record[:id].to_i
  }

  find_all_records(ids)
end
build_line_data_array(order) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 179
def build_line_data_array(order)
  arr = [soap_method_names[:create_lines][:line_data_handle] => []]

  order.lines.each do |line|
    arr.first[soap_method_names[:create_lines][:line_data_handle]].push(
      {
        :Handle => {
          Id: order.id_key,
          Number: line.line_number, # must be sent - does not have to fit with the next linenumber
        },
        :Id => order.id_key,
        :Number => line.line_number, # must be sent - does not have to fit with the next linenumber
        soap_method_names[:create_lines][:model_handle] => {
          Id: order.id_key,
        },
        :Description => line.description,
        :DeliveryDate => line.delivery.delivery_date,
        :UnitHandle => {Number: line.unit.unit_number},
        :ProductHandle => {Number: line.product.product_number},
        :Quantity => line.quantity,
        :UnitNetPrice => line.unit_net_price,
        :DiscountAsPercent => line.discount_percentage,
        :UnitCostPrice => line.unit_cost_price,
        :TotalNetAmount => line.total_net_amount, # TODO: Should this not be sent?
        :TotalMargin => line.margin_in_base_currency,
        :MarginAsPercent => line.margin_percentage,
      }
    )
  end

  arr
end
create_lines(model) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 118
def create_lines(model)
  Economic::SoapAPI.call(
    soap_method_names[:create_lines][:method], message: {
      dataArray: build_line_data_array(model),
    }
  )
end
destroy(id) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 95
def destroy(id)
  # The SoapAPI raises an exception if the record you try to delete is missing

  result = Economic::SoapAPI.call(
    soap_method_names[:destroy][:method],
    message: {soap_method_names[:destroy][:handle] => {Id: id}}
  )

  true
rescue Savon::SOAPFault
  false
end
find(id) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 8
def find(id)
  result = Economic::SoapAPI.call(
    soap_method_names[:find][:method],
    message: {soap_method_names[:find][:handle] => {Id: id}}
  )

  model.build_from_soap_api(result)
end
find_all_lines(invoice_id, line_ids) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 126
def find_all_lines(invoice_id, line_ids)
  result = Economic::SoapAPI.call(
    soap_method_names[:find_all_lines][:method],
    message: {soap_method_names[:find_all_lines][:handle] => line_ids_array(invoice_id, line_ids, handle: soap_method_names[:find_all_lines][:line_handle])}
  )

  result_array(result[soap_method_names[:find_all_lines][:data]]).map do |data|
    Economic::Line.build_from_soap_api(data)
  end
end
find_all_records(invoice_ids) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 137
def find_all_records(invoice_ids)
  result = Economic::SoapAPI.call(
    soap_method_names[:find_all_records][:method],
    message: {
      entityHandles: ids_array(invoice_ids, handle: soap_method_names[:find_all_records][:handle]),
    }
  )

  result[soap_method_names[:find_all_records][:data]].map do |data|
    model.build_from_soap_api(data)
  end
end
find_lines(invoice_id) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 80
def find_lines(invoice_id)
  result = Economic::SoapAPI.call(
    soap_method_names[:find_lines][:method],
    message: {soap_method_names[:find_lines][:handle] => {Id: invoice_id}}
  )

  return if result.nil?

  line_ids = result_array(result[soap_method_names[:find_lines][:line_handle]]).map { |line|
    line[:number].to_i
  }

  find_all_lines(invoice_id, line_ids)
end
ids_array(invoice_ids, handle:) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 165
def ids_array(invoice_ids, handle:)
  arr = [handle => []]

  invoice_ids.each do |invoice_id|
    arr.first[handle].push(
      {
        Id: invoice_id,
      }
    )
  end

  arr
end
is_vat_included?(model) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 212
def is_vat_included?(model)
  model.vat_amount != 0
end
line_ids_array(order_id, line_ids, handle:) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 150
def line_ids_array(order_id, line_ids, handle:)
  arr = [handle => []]

  line_ids.each do |line_id|
    arr.first[handle].push(
      {
        Id: order_id,
        Number: line_id,
      }
    )
  end

  arr
end
result_array(result) click to toggle source

Since the soap api returns hashes, if there is a single record, and arrays, if there are more, this method wraps a response in an array, if it is not already.

# File lib/economic/concerns/soap_methods.rb, line 112
def result_array(result)
  return result if result.is_a? Array

  [result]
end
send(model) click to toggle source
# File lib/economic/concerns/soap_methods.rb, line 29
def send(model)
  # There is a current_invoice_create_from_data_array method for bulk creation of invoices as well, but bulk
  # creation is not supported by the e-conomic REST api or any other REST api. To avoid implementing
  # functionality that will be removed once e-conomic finish the REST api, we only use the standard create end
  # point

  result = Economic::SoapAPI.call(
    soap_method_names[:send][:method], message: {
      data: {
        Handle: {
          Id: model.id_key,
        },
        DebtorHandle: {
          Number: model.customer.customer_number,
        },
        DebtorName: model.customer.name,
        Date: to_iso8601z(model.date.to_datetime),
        TermOfPaymentHandle: {
          Id: model.payment_terms.payment_terms_number,
        },
        DueDate: to_iso8601z(model.due_date.to_datetime),
        CurrencyHandle: {
          Code: model.currency,
        },
        ExchangeRate: model.exchange_rate,
        IsVatIncluded: is_vat_included?(model),
        LayoutHandle: {
          Id: model.layout.layout_number,
        },
        DeliveryAddress: model.delivery.address,
        DeliveryPostalCode: model.delivery.zip,
        DeliveryCity: model.delivery.city,
        DeliveryCountry: model.delivery.country,
        DeliveryDate: to_iso8601z(model.delivery.delivery_date.to_datetime),
        Heading: model.notes.heading,
        NetAmount: model.net_amount,
        VatAmount: model.vat_amount,
        GrossAmount: model.gross_amount,
        Margin: model.margin_in_base_currency,
        MarginAsPercent: model.margin_percentage,
      },
    }
  )

  model.id_key = result[:id]

  create_lines(model) if model.lines.any?

  find(result[:id].to_i)
end