class Origami::OthersPaymentsController < BaseOrigamiController
  def index
  	@membership_rebate_balance = 0
  	@sale_id = params[:sale_id]
  	@cashier_type   = params[:type]
  	path = request.fullpath
  	if path.include? ("credit_payment")
  		@payment_method_setting = PaymentMethodSetting.where("is_active='1'")
  	else
  		@payment_method_setting = PaymentMethodSetting.all
  	end

  	@rebate = MembershipSetting.find_by_rebate(1)
  	@sale_data = Sale.find_by_sale_id(@sale_id)
  	# @sale_id = params[:sale_id]
  	# sale_data = Sale.find_by_sale_id(@sale_id)

 #  	if sale_data.customer_id
 #  		customer_data= Customer.find_by_customer_id(sale_data.customer_id)
	#   	@membership_id = customer_data.membership_id
	#   	if !@membership_id.nil?
	#   		membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
	# 	  	membership_data = SalePayment.get_paypar_account(membership_setting.gateway_url,membership_setting.auth_token,@membership_id)
	# 	  	if membership_data["status"]==true
	# 	  		membership_account_data = membership_data["data"];
	# 	  		membership_account_data.each do |acc_data|
	# 	  			if acc_data["accountable_type"] == "REBATEACCOUNT"
	# 	  				@membership_rebate_balance=acc_data["balance"]
	# 	  			else
	# 	  				@membership_rebate_balance = 0
	# 	  			end
	# 	  		end
	# 	  	else
	# 	  		@membership_rebate_balance = 0
	# 	  	end
	# 	end
	# else

 #  	end
  end

  def new
    @sale_id = params[:sale_id]
    @cashier_type   = params[:type]
    payment_methods = PaymentMethodSetting.pluck(:payment_method)
    if @payment_method = payment_methods.find { |m| m.parameterize == params[:method] }
      path = request.fullpath
      # limit master_amount
      if path.include? ("credit_payment")
        sale_data = Sale.get_sale_data_for_other_payment_credit(@sale_id)
      else
        sale_data = Sale.find_by_sale_id(@sale_id)
      end
      total = 0
      @shop        = Shop.current_shop
      @paid_amount = 0
      @can_paid = 0
      @rounding_adj = 0
      @member_discount = 0
      @sub_total       = 0
      @membership_id   = nil
      @receipt_no      = nil
      if !sale_data.nil?
        total = sale_data.grand_total

        others = 0

        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

        if path.include? ("credit_payment")
          sale_payment_data = SalePayment.get_sale_payment_for_credit(sale_data)
        else
          sale_payment_data = sale_data.sale_payments
        end

        sale_payment_data.each do |sale_payment|
          if sale_payment.payment_method == @payment_method.parameterize
            @paid_amount = @paid_amount + sale_payment.payment_amount
          else
            others = others + sale_payment.payment_amount
          end
        end
        @can_paid = total - @paid_amount - others
        @member_discount = MembershipSetting.find_by_discount(1)
        @sub_total       = sale_data.total_amount
        @membership_id   = sale_data.customer.membership_id
        #for bank integration
        @receipt_no      = sale_data.receipt_no
      end
      bank_integration = Lookup.collection_of('bank_integration')
      @bank_integration = 0
      if !bank_integration[0].nil?
        @bank_integration = bank_integration[0][1]
      end
    else
      if path.include? ("credit_payment")
        redirect_to "sale/#{@sale_id}/#{@cashier_type}/payment/others_payment"
      else
        redirect_to "sale/#{@sale_id}/#{@cashier_type}/credit_payment/others_payment"
      end
    end
  end

  def create
    cash = params[:amount]
    sale_id = params[:sale_id]
    ref_no  = params[:ref_no]
    payment_method = params[:method]
    if PaymentMethodSetting.pluck(:payment_method).map(&:parameterize).include? payment_method
      if saleObj = Sale.find(sale_id)
        shop_details =  Shop.current_shop

        path = request.fullpath
        payment_for = false
        if path.include? ("credit_payment")
          payment_for = true
        end
        sale_payment = SalePayment.new
        @status, @sale = sale_payment.process_payment(saleObj, current_user, cash, payment_method, ref_no, payment_for)
      end
    end
  end

end
