class Origami::VoucherController < BaseOrigamiController
  def index
    @sale_id = params[:sale_id]
    @cashier_type   = params[:type]
    # limit voucher_amount
    sale_data = Sale.find_by_sale_id(@sale_id)
    total = sale_data.grand_total
    @vouchercount = 0
    others = 0

    @shop        = Shop.first 
    if @shop.is_rounding_adj
         new_total    = Sale.get_rounding_adjustment(sale_data.grand_total)
    else
        new_total    = sale_data.grand_total
    end
    @rounding_adj = new_total-sale_data.grand_total  
    
    sale_data.sale_payments.each do |sale_payment|
        if sale_payment.payment_method == "voucher"
          @vouchercount = @vouchercount + sale_payment.payment_amount
        else
          others = others + sale_payment.payment_amount
        end
    end
    @can_voucher = total - @vouchercount - others

  end

  def create
    cash = params[:amount]
    sale_id = params[:sale_id]
    voucher_no = params[:refnumber]
    if(Sale.exists?(sale_id))
      customer_data= Customer.find_by_customer_id(sale_data.customer_id)
      if customer_data
        membership_id = customer_data.membership_id
        membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
        if membership_setting.gateway_url
         # payment_data =MembershipAction.find_by_membership_type("get_account_balance") #need to modify here
         payment_data = PaymentMethodSetting.find_by_payment_method("Voucher")
          if payment_data.gateway_url
            campaign_type_id = payment_data.additional_parameters["campaign_type_id"]
            url = membership_setting.gateway_url.to_s + payment_data.gateway_url.to_s
            merchant_uid= payment_data.merchant_account_id
            auth_token = payment_data.auth_token.to_s
            # membership_data = SalePayment.get_paypar_account(url,membership_setting.auth_token,@membership_id,@campaign_type_id,merchant_uid,auth_token)
            # if membership_data["status"]==true
            # app_token: token,membership_id:membership_id,
            #           campaign_type_id:campaign_type_id,merchant_uid:merchant_uid,
            #           auth_token:auth_token
              begin
                response = HTTParty.get(url,
                        :body => { voucher_no: voucher_no,membership_id:membership_id
                                  }.to_json,
                        :headers => {
                                  'Content-Type' => 'application/json',
                                  'Accept' => 'application/json; version=3'
                        }, :timeout => 10
                  )
              rescue Net::OpenTimeout
                  response = { status: false }

              rescue OpenURI::HTTPError
                response = { status: false}

              rescue SocketError
                response = { status: false}
              end
            # end
          end
        end
      end
      if( response["status"]==true )
        saleObj = Sale.find(sale_id)
        sale_payment = SalePayment.new
        @status, @sale = sale_payment.process_payment(saleObj, current_user, cash, "voucher")
      end
    end
  end

end
