class Reports::CardSaleTranController < BaseReportController   
  authorize_resource :class => false

  # GET /transactions/sales
  # GET /transactions/sales.json
  def index
    @payment_method = [["All Payments",''],["MPU Payment","mpu"], ["Visa Payment","visa"], 
                ["Master Payment","master"], ["JCB Payment","jcb"],["UnionPay Payment","cup"], 
                ["Alipay Payment","alipay"]]
    @sales = Sale.all

    payment_type = params[:payment_type]
    
    from, to  = get_date_range_from_params
    status = 'Approved'
    @shift_sale_range = ''
    @shift = ''

    if params[:shift_name].to_i != 0

      @shift_sale_range = CardSaleTran.get_by_shift_sale_by_card(from,to,status)

      @shift_sale = ShiftSale.find(params[:shift_name])
      if to.blank?  
        @shift = ShiftSale.where('shift_started_at = ? and shift_closed_at is NULL ',@shift_sale.shift_started_at)
      else
        if @shift_sale.shift_closed_at.blank?
          @shift = ShiftSale.where('shift_started_at = ? and shift_closed_at is NULL',@shift_sale.shift_started_at)
        else
          @shift = ShiftSale.where('shift_started_at = ? and shift_closed_at = ? ',@shift_sale.shift_started_at, @shift_sale.shift_closed_at)
        end
      end
    end    

    if from.nil? && to.nil? && payment_method.nil? && @shift.nil?
 
        @cardSales = CardSaleTran.where("status = 'Approved'").order("sale_id desc")
      
        @cardSales = Kaminari.paginate_array(@cardSales).page(params[:page]).per(20)
    else
        cardSale = CardSaleTran.searchReport(from,to,payment_type,@shift_sale_range,@shift)
        if cardSale.count > 0
           @cardSales = cardSale 
           @cardSales = Kaminari.paginate_array(@cardSales).page(params[:page]).per(20)
         else
           @cardSales = 0
         end 
    end

    @from = from
    @to   = to
    # get printer info
    @print_settings = PrintSetting.get_precision_delimiter()
    if @shift.present?
      @shift.each do |sh|
        @shift_from = sh.shift_started_at.nil? ? '-' : sh.shift_started_at.strftime("%e %b %I:%M%p")
        @shift_to = sh.shift_closed_at.nil? ? '-' : sh.shift_closed_at.strftime("%e %b %I:%M%p")
        @shift_data = sh
      end
    end

    respond_to do |format|
      format.html
      format.xls
    end

  end

end
