class Origami::RedeemPaymentsController < BaseOrigamiController
 	def index
 		@sale_id = params[:sale_id]
 		payment_method = params[:payment_method]
 		@membership_rebate_balance=0
 		sale_data = Sale.find_by_sale_id(@sale_id)

    # limit redeem_amount
    # rebate_prices = SaleItem.calculate_food_beverage(sale_data.sale_items)
    # nonrebate_prices = sale_data.total_amount - rebate_prices
    # @payparcount = 0
    # others = 0
    # sale_data.sale_payments.each do |sale_payment|
    #     if sale_payment.payment_method == "paypar"
    #       @payparcount = @payparcount + sale_payment.payment_amount
    #     else
    #       others = others + sale_payment.payment_amount
    #     end
    # end
    # non_rebate_exceed = others - (nonrebate_prices +  sale_data.total_tax)
    # if non_rebate_exceed < 0
    #   @redeem_prices = rebate_prices - @payparcount
    # else
    #   @redeem_prices = rebate_prices - @payparcount -non_rebate_exceed
    # end

    @payparcount = 0
    others = 0
    sale_data.sale_payments.each do |sale_payment|
        if sale_payment.payment_method == "paypar"
          @payparcount = @payparcount + sale_payment.payment_amount
        else
          others = others + sale_payment.payment_amount
        end
    end
    @redeem_prices = sale_data.grand_total - @payparcount -others


 		if sale_data
 			if sale_data.customer_id
 				customer_data= Customer.find_by_customer_id(sale_data.customer_id)
 				if customer_data
 					@membership_id = customer_data.membership_id
 					if !@membership_id.nil?
 						membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
 						if membership_setting.gateway_url
 							member_actions =MembershipAction.find_by_membership_type("get_account_balance")
 							if member_actions.gateway_url
 								@campaign_type_id = member_actions.additional_parameter["campaign_type_id"]
 								url = membership_setting.gateway_url.to_s + member_actions.gateway_url.to_s
 								merchant_uid= member_actions.merchant_account_id
 								auth_token = member_actions.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
		 							@membership_rebate_balance=membership_data["balance"]
		 							@out = true, @membership_rebate_balance,@membership_id
		 						end
		 					else
		 						@out =false,0
 							end
 						else
 							@out = false,0
 						end


 					else
 						@out = false, 0
 					end
 				else
 					@out = false, 0
 				end
 			else
 				@out = false, 0
 			end
 		else
 			@out = false, 0
 		end
 		@out = false, 0
 	end
end
