class Atol::Request::PostDocument::Sell::Body

Public Class Methods

new(external_id:, phone: '', email: '', items:, config: nil) click to toggle source
# File lib/atol/request/post_document/sell/body.rb, line 10
def initialize(external_id:, phone: '', email: '', items:, config: nil)
  raise(Atol::EmptyClientContactError) if phone.empty? && email.empty?
  raise(Atol::EmptySellItemsError) if items.empty?

  @config = config || Atol.config
  @external_id = external_id
  @phone = phone
  @email = email
  @items = items
end

Public Instance Methods

to_h() click to toggle source
# File lib/atol/request/post_document/sell/body.rb, line 21
def to_h
  body.clone
end
to_json(*_args) click to toggle source
# File lib/atol/request/post_document/sell/body.rb, line 25
def to_json(*_args)
  body.to_json
end

Private Instance Methods

body() click to toggle source
# File lib/atol/request/post_document/sell/body.rb, line 31
def body
  body_template.tap do |result|
    receipt = result[:receipt]
    client = receipt[:client]
    result[:external_id] = @external_id
    client[:email] = @email unless @email.empty?
    client[:phone] = @phone unless @phone.empty?
    result[:service][:callback_url] = @config.callback_url if @config.callback_url

    receipt[:total] = receipt[:payments][0][:sum] = total
    receipt[:items] = @items
  end
end
body_template() click to toggle source
# File lib/atol/request/post_document/sell/body.rb, line 45
def body_template
  {
    receipt: {
      client: {},
      company: {
        inn: @config.inn.to_s,
        sno: @config.default_sno,
        payment_address: @config.payment_address,
        email: @config.company_email
      },
      items: [],
      payments: [
        {
          sum: 0,
          type: @config.default_payment_type
        }
      ]
    },
    service: {},
    timestamp: Time.now.strftime(Atol::TIMESTAMP_FORMAT)
  }
end
total() click to toggle source
# File lib/atol/request/post_document/sell/body.rb, line 68
def total
  @total ||= @items.inject(0) { |sum, item| sum += item[:sum] }
end