Sha256: 47ae8da6f2aca8352ad347fe85c4c7502e9cd642c21cda55844afa414e8eee27

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require "einvoice/utils"

require "einvoice/neweb/model/base"
require "einvoice/neweb/model/contact"
require "einvoice/neweb/model/customer_defined"
require "einvoice/neweb/model/invoice_item"
require "einvoice/neweb/model/invoice"
require "einvoice/neweb/model/pre_invoice"
require "einvoice/neweb/model/seller_invoice"
require "einvoice/neweb/model/query"

require "einvoice/neweb/result"

module Einvoice
  module Neweb
    class Provider < Einvoice::Provider
      include Einvoice::Utils

      def issue(payload, options)
        case options[:type]
        when :seller_invoice
          action = "IN_SellerInvoiceS.action"
          invoice = Einvoice::Neweb::Model::SellerInvoice.new
        else
          action = "IN_PreInvoiceS.action"
          invoice = Einvoice::Neweb::Model::PreInvoice.new
        end

        invoice.from_json(payload.to_json)

        if invoice.valid?
          response = connection.post do |request|
            request.url endpoint_url || endpoint + action
            request.body = {
              storecode: client_id,
              xmldata: encode_xml(camelize(invoice.wrapped_payload))
            }
          end.body

          Einvoice::Neweb::Result.new(response)
        else
          Einvoice::Neweb::Result.new(invoice.errors)
        end
      end

      def query(payload, options)
        action = "IN_InvoiceMapS.action"
        query = Einvoice::Neweb::Model::Query.new
        query.from_json(payload.to_json)

        if query.valid?
          response = connection.post do |request|
            request.url endpoint_url || endpoint + action
            request.body = {
              storecode: client_id,
              xmldata: encode_xml(camelize(query.wrapped_payload))
            }
          end.body

          Einvoice::Neweb::Result.new(response)
        else
          Einvoice::Neweb::Result.new(query.errors)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
einvoice-1.0.0 lib/einvoice/neweb/provider.rb