class Origami::JunctionPayController < BaseOrigamiController

  def index
    @sale_id = params[:sale_id]
    @cashier_type   = params[:type]
    # limit jcb_amount
    sale_data = Sale.find_by_sale_id(@sale_id)
    total = sale_data.grand_total
    @junction_pay_count = 0
    others = 0
    @cashier_id = current_user.emp_id

    @payment_method_setting_nav = PaymentMethodSetting.all
    if current_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 == "JunctionPay"
          @junction_pay_count = @junction_pay_count + sale_payment.payment_amount
        else
          others = others + sale_payment.payment_amount
        end
    end
    @can_junction_pay = total - @junction_pay_count - 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

  def create
    gift_amount = params[:gift_amount]
    voucher_amount = params[:voucher_amount]
    voucher_no = params[:voucher_no]
    sale_id = params[:sale_id]

    # Gift card or Voucher classified and add for payment_reference
    remarks = ''
    cash = (gift_amount.to_f + voucher_amount.to_f)
    if gift_amount.to_f > 0
      remarks = "Junciton Gift Card Payment. RefNo-" + voucher_no
    else
      remarks = "Junciton Voucher Payment. RefNo-" + voucher_no
    end

    if(Sale.exists?(sale_id))
      saleObj = Sale.find(sale_id)

      # 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 = sale_payment.process_payment(saleObj, current_user, cash, "JunctionPay", remarks)
    end
  end

end
