class Origami::ShiftsController < BaseOrigamiController
  def index
  end

  def show
    @cashier_type = params[:type]
    @shift = ShiftSale.current_open_shift(current_user.id)
    #for bank integration
    bank_integration = Lookup.collection_of('bank_integration')
    @bank_integration = 0
    if !bank_integration[0].nil?
      @bank_integration = bank_integration[0][1]
    end

  end

  def new
    @float = Lookup.where('lookup_type=?','float_value')
    @terminal = CashierTerminal.available
  end

  def create
    opening_balance = params[:opening_balance]
    cashier_terminal_param = params[:cashier_terminal]

    # Multiple Cashier
    cashier_terminal = CashierTerminal.find(cashier_terminal_param)
    if cashier_terminal.is_currently_login
      respond_to do |format|
        format.html { redirect_to settings_order_queue_stations_url, notice: 'Cashier Terminal already signin!' }
      end
    end

    # Update Cashier Terminal
    cashier_terminal.is_currently_login = 1
    cashier_terminal.save

    @shift_sale = ShiftSale.new
    @shift_sale.create(opening_balance,cashier_terminal_param, current_user)
  end



  def update_shift
    closing_balance = params[:closing_balance]
    shift_id = params[:shift_id]
    @shift = ShiftSale.find_by_id(shift_id)
    if @shift
      @shift.shift_closed_at = DateTime.now.utc
      @shift.closing_balance = closing_balance.to_f
      @shift.save

      # Multiple Cashier
      cashier_terminal = @shift.cashier_terminal
      cashier_terminal.is_currently_login = 0
      cashier_terminal.save

      #add shift_sale_id to card_settle_trans
       bank_integration = Lookup.find_by_lookup_type('bank_integration')
      if !bank_integration.nil?
        card_settle_trans = CardSettleTran.select('id').where(['shift_sale_id IS NULL and status IS NOT NULL'])

        card_settle_trans.each do |data|
          card_sett_trans = CardSettleTran.find(data.id)
          card_sett_trans.shift_sale_id = @shift.id
          card_sett_trans.save()
        end
      end

      if ENV["SERVER_MODE"] != "cloud" #no print in cloud server



        # if !close_cashier_print[0].nil?
        #   @close_cashier_print = close_cashier_print[0][1]
        # end
        close_cashier_pdf = Lookup.collection_of("print_settings")
        unique_code = "CloseCashierPdf"

        if !close_cashier_pdf.empty?
          close_cashier_pdf.each do |close_cashier|
            if close_cashier[0] == 'CloseCashierCustomisePdf'
              if close_cashier[1] == '1'
                unique_code="CloseCashierCustomisePdf"
              else
                unique_code="CloseCashierPdf"
              end
            end
          end
        end
        shop_details = shop_detail
        #get tax
        shift_obj      = ShiftSale.where('id =?',@shift.id)
        sale_items = ''
        @lookup = Lookup.shift_sale_items_lookup_value
        if @lookup.to_i == 1
          @sale_items    = Sale.get_shift_sale_items(@shift.id)
          other_charges = Sale.get_other_charges()
          @total_other_charges_info = other_charges.where("sales.shift_sale_id IN (?) and sale_status='completed'",@shift)
        end
        @sale_taxes    = Sale.get_separate_tax(shift_obj,from=nil,to=nil,type='')
        @total_waste   = Sale.get_total_waste(shift_id).sum(:grand_total)
        @total_spoile  = Sale.get_total_spoile(shift_id).sum(:grand_total)
        #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)
        @total_dinein               = ShiftSale.get_total_dinein(@shift).total_dinein_amount
        @total_takeway              = ShiftSale.get_total_takeway(@shift).total_takeway_amount
        @total_other_charges        = ShiftSale.get_total_other_charges(@shift).total_other_charges_amount
        @total_credit_payments      = ShiftSale.get_shift_sales_with_credit_payment(shift_id).total_credit_payments

        # get printer info
        print_settings = PrintSetting.find_by_unique_code(unique_code)
        printer = Printer::CashierStationPrinter.new(print_settings)

        # print close cashier setting
        close_cashier_print = Lookup.collection_of('close_cashier_print')
          if close_cashier_print.empty?
            @settings_lookup = Lookup.new(lookup_type: "close_cashier_print", name: "CloseCashierPrint", value: "1")
            @settings_lookup.save
          end
          find_close_cashier_print = Lookup.collection_of('close_cashier_print')
          if find_close_cashier_print[0][1].to_i > 0
            printer.print_close_cashier(print_settings,cashier_terminal,@shift, @sale_items, @total_other_charges_info, shop_details,@sale_taxes,@other_payment,@total_amount_by_account,@total_discount_by_account,@total_member_discount,@total_dinein,@total_takeway,@total_other_charges,@total_waste,@total_spoile,@total_credit_payments)
          end
      end
    end
    Employee.logout(session[:session_token])
    session[:session_token] = nil
  end

  def edit
  end

  def sale_summary
    @shift = ShiftSale.current_open_shift(current_user.id)

    # @shift = ShiftSale.find_by_id(shift_id)
    if @shift
      #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)
    end

  end

end
