Sha256: 1b1cc9b7ada56cc1d7f5ad9e4ebedaa11494eb71a1996f1668cefa9e0ec51a87

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

module Effective
  class QbTicket < ActiveRecord::Base
    belongs_to :qb_request, optional: true # the current request. Only optional when set_all_orders_finished
    has_many :qb_requests
    has_many :orders, through: :qb_requests
    has_many :qb_logs

    STATES = ['Ready', 'Authenticated', 'Processing', 'Finished', 'ConnectionError', 'RequestError']

    effective_resource do
      username                  :string
      company_file_name         :string
      country                   :string

      qbxml_major_version       :string
      qbxml_minor_version       :string

      state                     :string   # , default: 'Ready'
      percent                   :integer

      hpc_response              :text
      connection_error_hresult  :text
      connection_error_message  :text
      last_error                :text

      timestamps
    end

    scope :deep, -> { includes(qb_requests: :order) }

    validates :state, inclusion: { in: STATES }

    def request_error!(error, atts={})
      self.error!(error, atts.reverse_merge(state: 'RequestError'))
    end

    # This is the entry point for a standard error.
    def error!(error, atts={})
      Effective::OrdersMailer.order_error(
        order: qb_request.try(:order),
        error: error,
        to: EffectiveQbSync.error_email,
        subject: "Quickbooks failed to synchronize order ##{qb_request.try(:order).try(:to_param) || 'unknown'}",
        template: 'qb_sync_error'
      ).deliver_now

      update!(atts.reverse_merge(last_error: error))
    end

    # persists a new log message to this ticket
    def log(message)
      qb_logs.create(message: message, qb_ticket: self)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
effective_qb_sync-1.3.5 app/models/effective/qb_ticket.rb
effective_qb_sync-1.3.4 app/models/effective/qb_ticket.rb
effective_qb_sync-1.3.3 app/models/effective/qb_ticket.rb