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

  def index

    from, to  = get_date_range_from_params
    @shift = ''
    if params[:shift_name].to_i != 0
            @shift = ShiftSale.find(params[:shift_name])
    end
    @sale_data = Sale.get_by_shiftsales(from,to,@shift)
    @from       = from
    @to         = to
    if @shift.present?

          @shift_from = @shift.shift_started_at.nil? ? '-' : @shift.shift_started_at.strftime("%e %b %I:%M%p")
          @shift_to = @shift.shift_closed_at.nil? ? '-' : @shift.shift_closed_at.strftime("%e %b %I:%M%p")
          @shift_data = @shift
    end
    # get printer info
    @print_settings = PrintSetting.get_precision_delimiter()
	  respond_to do |format|
      format.html
      format.xls
    end
  end

  def show
    from, to  = get_date_range_from_params

      @sale_data = Sale.get_by_shift_sale_by_item(from,to,Sale::SALE_STATUS_COMPLETED)

      date_arr = Array.new
      @sale_data.each do |sale|
        local_opening_date = sale.opening_date.nil? ? '-' : sale.opening_date.strftime("%e %b %I:%M%p")
        local_closing_date = sale.closing_date.nil? ? '-' : sale.closing_date.strftime("%e %b %I:%M%p")
        opening_date = sale.opening_date.nil? ? '-' : sale.opening_date.utc
        closing_date = sale.closing_date.nil? ? '-' : sale.closing_date.utc
        shift_id = sale.id.nil? ? '-' : sale.id
        str = {:shift_id => shift_id, :local_opening_date => local_opening_date, :local_closing_date => local_closing_date, :opening_date => opening_date, :closing_date => closing_date}
        date_arr.push(str)
      end

      out = {:status => 'ok', :message => date_arr}

      respond_to do |format|
        format.json { render json: out }
      end
  end

	def print_close_receipt
		shift_id = params[:id]
		@shift = ShiftSale.find_by_id(shift_id)
		shift_obj = ShiftSale.where('id =?',shift_id)

		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 = current_shop
		cashier_terminal = @shift.cashier_terminal

		@lookup = Lookup.shift_sale_items_lookup_value
		if @lookup.to_i == 1
			@sale_items    = Sale.get_shift_sale_items(@shift.id)
			@total_other_charges_info = ShiftSale.get_other_charges(@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)
		@payment_methods = PaymentMethodSetting.where("is_active='1'").pluck("payment_method")

		# 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)

		# printer.print_close_cashier(print_settings,cashier_terminal,@shift, nil, 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)
		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, @payment_methods)

		respond_to do |format|
			format.html { redirect_to '/en/reports/shiftsale/', notice: 'Printing Completed.'}
		end
	end
end
