Sha256: a020db71abc08e615881bc90a4cd3467c94ac2ac6be95167d537699db92bb669

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Xdelivery
  module API
    class Sales < Base
      include Enumerable

      attr_accessor :sales

      COLUMNS = [
        :order_id, :recipient, :mobile, :email, :address,
        :items, :warehouse_items, :total_order_amount, :cash_on_delivery, :order_created_at,
        :note, :callback_url, :ref_id, :tag, :tracking_code
      ]

      INVOICE_COLUMNS = [
        :create_type, :email, :company_code, :donate_code, :device_id, :device, :items, :invoice_setting_id
      ]

      def add(params, invoice_params={})
        self.sales ||= []
        (params || {}).delete_if do |k, v|
          COLUMNS.include?(k) == false
        end
        invoice_params.delete_if do |k, v|
          INVOICE_COLUMNS.include?(k) == false
        end

        params.merge!(invoice: invoice_params) unless invoice_params.empty?

        sales.push(params)
        params
      end

      def [](index)
        sales[index]
      end

      def count
        sales.count
      end

      def create!
        response = post('/sales/batch.json')
        Response::Orders.new(response)
      end

      protected

      def post_data
        { import: { orders: sales } }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xdelivery-0.2.1 lib/xdelivery/api/sales.rb