SuiteBookingsController

mail@pastecode.io avatar
unknown
ruby
2 years ago
3.1 kB
0
Indexable
Never
class SuiteBookingsController < ApplicationController
  layout 'users_motel'

  before_action :check_user_session

  def index
    process_date_parameters

    @motel = Motel.find(params[:motel_id])
    authorize @motel, :show?

    authorize [:suite, :booking]
    # Bookings Scheduled
    @bookings_scheduled = @motel.bookings
                            .active
                            .not_rejected
                            .booking_type('scheduled')
                            .where('booking.booking_check_in >= ? AND booking.booking_check_in <= ?', @start_time, @end_time)
                            .order('booking.booking_check_in DESC')

    # Estas son la reservas vía Suite, si corresponde
    if suite_enabled? && @motel.has_suite?
      @suite_bookings = Suite::Booking
        .active
        .confirmed
        .not_foreign
        .includes(:addons)
        .includes(:price => { :room => :room_type }).references(:room)
        .includes(:price => :stay)
        .where('api_room.motel_id = ?', @motel.suite_id)
        .where('datetime_start >= ? AND datetime_start < ?', @start_time, @end_time)
        .order('api_booking.datetime_start DESC')

      @suite_bookings_set = Suite::BookingsSet.new(bookings: @suite_bookings)
    end

    @bookings = (@bookings_scheduled + (@suite_bookings))
    @scheduled_bookings_set = ScheduledBookingsSet.new(bookings: @bookings)

    respond_to do |format|
      format.html do
        if for_printing?
          layout = 'print'
        else
          layout = 'users_motel'
        end
        render template: 'suite_bookings/index', formats: [:html], layout: layout
      end
      format.js
      format.pdf do
        params[:_print] = 1
        content = render_to_string(template: 'suite_bookings/index', formats: [:html], layout: 'print')
        kit = PDFKit.new(content, page_size: 'Letter')
        pdf_start = @start_time.strftime('%Y-%m-%d')
        pdf_end = @end_time.strftime('%Y-%m-%d')
        filename = "motel-suite-bookings_#{@motel.id}_#{@motel.motel_name.parameterize}_#{pdf_start}_#{pdf_end}.pdf"
        send_data kit.to_pdf, filename: filename, type: 'application/pdf', disposition: 'attachment'
      end
    end
  end


  protected

  ##
  # Procesa parámetros start y end como si fueran
  # fechas (sin horas específicas)
  #
  def process_date_parameters
    # start
    if params[:start]
      begin
        @start_date = Date.parse(params[:start])
      rescue ArgumentError
      end
    end
    # Por defecto, start es el primer día del mes actual
    if !@start_date
      @start_date = (Time.zone.now).to_date
    end
    @start_time = @start_date.at_beginning_of_day

    # end
    if params[:end]
      begin
        @end_date = Date.parse(params[:end])
      rescue ArgumentError
      end
    end
    # Por defecto, end es el final del mes
    if !@end_date
      @end_date = (Time.zone.now.at_end_of_month).to_date
    end
    @end_time = @end_date.at_end_of_day
  end
end