require "net/http"
class Api::LoaderService::LoadDataController < Api::ApiController
  skip_before_action  :authenticate

  def get_sale_data_rage
    load_time_start = params[:load_time]
    load_time_end = params[:load_time_end]
    unless load_time_start.nil? || load_time_end.nil?
      @sale_data = Sale.get_load_sale_range(load_time_start.to_datetime,load_time_end.to_datetime)
        if !@sale_data.empty?
          @out = {general_status: true, data: @sale_data}
        else
          @out = {general_status: false, data: "Data is empty."}
        end
    else
      @out = {general_status: false, data: "load_time is missing."}
    end
    render :json => @out
  end

  # SFTP for BreadTalk Start

  # Detail Sale
  def get_detail_sale_data
    data = params['data']
    transaction_date = data[:transaction_date].to_s
    detail_sale_data = SaleItem.get_detail_sale_data(transaction_date)
    json = detail_sale_data.to_json
    trans_count = JSON.parse(json).count
    unless detail_sale_data.empty?
      out = { :status => "success", :transaction_count => trans_count, :data => detail_sale_data }
    else
      out = { :status => "fail", :data => "Data is empty" }
    end
    respond_to do |format|
      format.json {render json: out }
    end
  end

  # Tender sale
  def get_tender_sale_data
    data = params['data']
    transaction_date = data['transaction_date'].to_s
    tender_sale_data = Sale.get_tender_sale_data(transaction_date)
    json = tender_sale_data.to_json
    trans_count = JSON.parse(json).count
		unless tender_sale_data.empty?
			out = { :status => "success", :transaction_count => trans_count, :data => tender_sale_data }
		else
			out = { :status => "fail", :data => "Data is empty" }
		end
		respond_to do |format|
			format.json { render json: out }
		end
  end

  # Daily_Sale summary
  def get_daily_sale_data
    data = params['data']
		transaction_date = data['transaction_date'].to_s
		daily_sale_data = Sale.get_daily_sale_data(transaction_date)
		unless daily_sale_data.empty?
			out = { :status => "success", :data => daily_sale_data}
		else
			out = { :status => "fail", :data => "Data is empty"}
		end
		respond_to do |format|
			format.json { render json: out }
		end
  end

  # Check Sale Data
  def get_check_sale_data
    data = params['data']
    transaction_date = data['transaction_date'].to_s
    check_sale_data = Sale.get_check_sale_data(transaction_date)
    json = check_sale_data.to_json
    trans_count = JSON.parse(json).count
    unless check_sale_data.empty?
      out = { :status => "success", :transaction_count => trans_count, :data => check_sale_data}
    else
      out = { :status => "fail", :data => "Data is empty"}
    end
    respond_to do |format|
      format.json { render json: out }
    end
  end

  # SFTP for BreadTalk End
end
