class Transactions::ShiftSalesController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_transactions_shift_sale, only: [:show, :edit, :update, :destroy]
  before_action :check_user

  def index

    filter     = params[:filter]
    from       = params[:from]
    to         = params[:to]

    if filter.nil? && from.nil? && to.nil?
        @shift_sales = ShiftSale.all.order("id desc") 
        @shift_sales = Kaminari.paginate_array(@shift_sales).page(params[:page]).per(20)
    else
        shift_sale = ShiftSale.search(filter,from,to)
        if shift_sale.count > 0
           @shift_sales = shift_sale 
           @shift_sales = Kaminari.paginate_array(@shift_sales).page(params[:page]).per(20)
         else
           @shift_sales = 0
         end    
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @shift_sales }
    end

  end

  # GET /transactions/shift_sales/1
  # GET /transactions/shift_sales/1.json
  def show
   
    @shift = ShiftSale.find(params[:id])

      #get tax
      shift_obj      = ShiftSale.where('id =?',@shift.id)
      @sale_taxes    = Sale.get_separate_tax(shift_obj,from=nil,to=nil,type='')
      #other payment details for mpu or visa like card
      @other_payment = ShiftSale.get_by_shift_other_payment(@shift)

      # Calculate price_by_accounts
      @total_amount_by_account   = ShiftSale.calculate_total_price_by_accounts(@shift,'amount')
      @total_discount_by_account = ShiftSale.calculate_total_price_by_accounts(@shift,'discount')
      @total_member_discount     = ShiftSale.get_total_member_discount(@shift)

       

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @shift }
    end
  end

  def check_user
    if current_user.nil?
      redirect_to root_path
    end
  end
  
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_transactions_shift_sale
      @transactions_shift_sale = ShiftSale.find(params[:id])
    end
end
