class Origami::PaymalController < BaseOrigamiController
 def index
  @sale_id = params[:sale_id]
  payment_method = params[:payment_method]
  @cashier_type   = params[:type]
  @membership_rebate_balance=0
  sale_data = Sale.find_by_sale_id(@sale_id)
  @receipt_no = sale_data.receipt_no
  @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  
  # @rounding_adj = sale_data.rounding_adjustment  

  @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
  @payment_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 = nil
              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

def create
 cash = params[:payment_amount]
 sale_id = params[:sale_id]
 transaction_ref = params[:transaction_ref]
 account_no = params[:account_no]
 puts params.to_json  
 if(Sale.exists?(sale_id))
   saleObj = Sale.find(sale_id)
   shop_details =  Shop.first 

	      # rounding adjustment
	      # if shop_details.is_rounding_adj
	      #   new_total    = Sale.get_rounding_adjustment(saleObj.grand_total)
	      #   rounding_adj = new_total-saleObj.grand_total
	      #   saleObj.update_attributes(grand_total: new_total,old_grand_total: saleObj.grand_total,rounding_adjustment:rounding_adj)        
	      # end

	      # saleObj = Sale.find(sale_id)
	      sale_payment = SalePayment.new
	      status, @sale,@membership_data = sale_payment.process_payment(saleObj, current_user, cash, "paymal",account_no)
        if status == true && @membership_data["status"] == true
          @out = true, "Success!"
        else
          @out =false, @membership_data["message"]
        end
      end
    end
end
