Class: Stay::Booking

Inherits:
ApplicationRecord show all
Defined in:
app/models/stay/booking.rb

Constant Summary collapse

PAYMENT_STATES =
%w[failed paid]
STATUSES =
%w[booking_request invoice_sent confirmed canceled completed].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_association(auth_object = nil) ⇒ Object



69
70
71
# File 'app/models/stay/booking.rb', line 69

def self.ransackable_association(auth_object = nil)
  %w[user]
end

.ransackable_attributes(auth_object = nil) ⇒ Object



65
66
67
# File 'app/models/stay/booking.rb', line 65

def self.ransackable_attributes(auth_object = nil)
  %w[number]
end

Instance Method Details

#add_rooms_and_calculate(selected_rooms, booking_params, property) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/stay/booking.rb', line 125

def add_rooms_and_calculate(selected_rooms, booking_params, property)
  total_price = 0
  total_guests = 0

  selected_rooms.each do |room_id|
    room = property.rooms.find(room_id.to_i)
    number_of_guests = booking_params[:bookings][room_id][:number_of_guests].to_i
    price = room.price_per_month * (check_out_date - check_in_date).to_i

    line_items.build(room: room, quantity: number_of_guests, price: price)

    total_price += price
    total_guests += number_of_guests
  end

  self.total = total_price
  self.number_of_guests = total_guests
end

#after_cancelObject



99
100
101
102
103
# File 'app/models/stay/booking.rb', line 99

def after_cancel
  payments.completed.each(&:cancel!)
  send_cancel_email
  update(status: "canceled", canceled_at: Time.current)
end

#associate_user!(user, override_email = true) ⇒ Object

Associates the specified user with the booking.



106
107
108
109
110
111
112
113
114
115
# File 'app/models/stay/booking.rb', line 106

def associate_user!(user, override_email = true)
  self.user           = user
  self.email          = user.email if override_email
  self.created_by   ||= user
  changes = slice(:user_id, :email, :created_by_id)

  # immediately persist the changes we just made, but don't use save
  # since we might have an invalid address associated
  self.class.unscoped.where(id: self).update_all(changes)
end

#booking_completed?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/stay/booking.rb', line 91

def booking_completed?
  completed?
end

#booking_completed_atObject



95
96
97
# File 'app/models/stay/booking.rb', line 95

def booking_completed_at
  update_columns(completed_at: Time.current)
end

#booking_room_countObject



192
193
194
# File 'app/models/stay/booking.rb', line 192

def booking_room_count
  self.update_column(:room_count, self.rooms.count)
end

#calculate_city_feeObject



171
172
173
# File 'app/models/stay/booking.rb', line 171

def calculate_city_fee
  property.city_fee || 0
end

#calculate_cleaning_feeObject



167
168
169
# File 'app/models/stay/booking.rb', line 167

def calculate_cleaning_fee
  property.cleaning_fee || 0
end

#calculate_item_totalObject



157
158
159
# File 'app/models/stay/booking.rb', line 157

def calculate_item_total
  property.shared_property ? line_items.sum(:price) : property.price_per_month
end

#calculate_tax_totalObject



161
162
163
164
165
# File 'app/models/stay/booking.rb', line 161

def calculate_tax_total
  property.property_taxes
          .uniq { |property_tax| property_tax.tax_id }
          .sum(&:value)
end

#calculate_totalsObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/stay/booking.rb', line 175

def calculate_totals
  item_total = calculate_item_total || 0
  invoice_total = invoice&.total || 0
  tax_total = calculate_tax_total
  extra_guest_total = extra_guest_amount
  cleaning_fee = calculate_cleaning_fee
  city_fee = calculate_city_fee
  total_amount = item_total + invoice_total + tax_total  + extra_guest_total + cleaning_fee + city_fee

  update_columns(
    item_total: item_total,
    total: invoice_total,
    total_amount: total_amount
  ) if item_total != self.item_total || invoice_total != self.total || total_amount != self.total_amount
end

#canceled_by(user) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'app/models/stay/booking.rb', line 81

def canceled_by(user)
  transaction do
    cancel!
    update_columns(
      canceler_id: user.id,
      canceled_at: Time.current
    )
  end
end

#ensure_store_presenceObject



144
145
146
# File 'app/models/stay/booking.rb', line 144

def ensure_store_presence
  self.store ||= Stay::Store.default
end

#extra_guest_amountObject



148
149
150
151
152
153
154
155
# File 'app/models/stay/booking.rb', line 148

def extra_guest_amount
  return 0 unless property&.allow_extra_guest
  return 0 unless property&.guest_number
  return 0 unless property.allow_extra_guest && number_of_guests > property.guest_number

  extra_guest = number_of_guests - property.guest_number
  property.per_extra_guest_amount.to_f * extra_guest
end

#generate_numberObject



117
118
119
120
121
122
123
# File 'app/models/stay/booking.rb', line 117

def generate_number
  loop do
    numeric_part = rand.to_s[2..11]
    self.number = "S#{numeric_part}"
    break unless self.class.exists?(number: number)
  end
end

#invoice_deleted?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/stay/booking.rb', line 77

def invoice_deleted?
  invoice.nil?
end

#update_payment_statusObject



73
74
75
# File 'app/models/stay/booking.rb', line 73

def update_payment_status
  update_columns(payment_state: "paid") if payments.exists?(state: "paid")
end