Skip to content

Commit

Permalink
modify booking
Browse files Browse the repository at this point in the history
  • Loading branch information
CH1006 committed Sep 12, 2019
1 parent 0364437 commit bf00650
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Bundler/OrderedGems:
- 'Gemfile'

Metrics/LineLength:
Max: 135
Max: 120

Metrics/MethodLength:
Max: 20
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
protect_from_forgery
add_flash_types :success, :danger, :warning, :info
devise_group :user, contains: %i[admin member]
before_action :configure_permitted_parameters, if: :devise_controller?
Expand Down
27 changes: 12 additions & 15 deletions app/controllers/bookings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# frozen_string_literal: true

class BookingsController < ApplicationController
def new
return redirect_to root_path, warning: t(".warning") unless check_params_exists?

@booking = Booking.new
@room = Room.find params[:room_id]
@code_vouchers = @room.price.vouchers.map{|c| c.code}
@code_vouchers = @room.price.vouchers.map(&:code)
return redirect_to root_path, warning: t(".warning") unless valid_date?

check_valid_params
check_valid_guest
@night = ((@checkout - @checkin)/60/60/24).to_i.abs
@night = ((@checkout - @checkin) / 60 / 60 / 24).to_i.abs
end

def create
Expand All @@ -25,8 +29,8 @@ def create

def booking_params
params.require(:booking).permit :checkin, :checkout, :room_id, :number_guest,
:name_booking, :phone_booking, :email_booking, :name_booked, :phone_booked,
:email_booked, :country_code, :trip_purpose, :request, :intend_time, :voucher_id
:name_booking, :phone_booking, :email_booking, :name_booked, :phone_booked,
:email_booked, :country_code, :trip_purpose, :request, :intend_time, :voucher_id
end

def check_params_exists?
Expand All @@ -40,27 +44,20 @@ def check_valid_params
end

def check_valid_guest
if @room.guest < params[:guest].to_i.abs || !(params[:guest] =~ /\A[-+]?[0-9]+\z/)
return redirect_to root_path, warning: t(".warning")
if @room.guest < params[:guest].to_i.abs || params[:guest] !~ /\A[-+]?[0-9]+\z/
redirect_to root_path, warning: t(".warning")
end
end

def valid_date?
date_format = "%d/%m/%Y"
if DateTime.strptime(params[:checkin], date_format) && DateTime.strptime(params[:checkout], date_format)
return true
end
return true if DateTime.strptime(params[:checkin], date_format) && DateTime.strptime(params[:checkout], date_format)
rescue ArgumentError
return false
false
end

def save_booking
@booking.status = 0
if @booking.voucher_id.present?
@booking.total_price = (@booking.room.price.cost - ((@booking.room.price.cost * @booking.voucher.sale) / 100)) + @booking.room.price.cleaning_fee
else
@booking.total_price = (@booking.room.price.cost + @booking.room.price.cleaning_fee)
end
if @booking.save
redirect_to thank_payment_path
else
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class CommentsController < ApplicationController
before_action :load_room, only: %i(create destroy)
before_action :load_room, only: %i[create destroy]
before_action :load_comment, only: :destroy

def create
Expand All @@ -19,7 +21,7 @@ def destroy
if @comment.destroy
respond_to do |format|
format.html { redirect_to request.referrer }
format.js { }
format.js {}
end
else
flash[:danger] = t "controller.reviews.delete_review_fail"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ def index
def show
@favorite_space = FavoriteSpace.find(params[:id])
@room_favorite_space = @favorite_space.rooms
.includes(:price, :location, :room_images).page(params[:page]).per Settings.room_per
.includes(:price, :location, :room_images).page(params[:page]).per Settings.room_per
end
end
49 changes: 46 additions & 3 deletions app/controllers/mailpayment_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
# frozen_string_literal: true

class MailpaymentController < ApplicationController
before_action :load_booking, only: :edit
before_action :check_token, only: :edit
before_action :check_expire_time, only: :edit

def create
@booking = Booking.includes(:room).find(params[:id])
PaymentMailer.payment_booking(@booking).deliver_now
flash[:notice] = t "messages.notice.mailpayment.sent_mail", email: @booking.email_booking.to_s
@booking = Booking.includes(:room).find_by(id: params[:id])
if @booking&.Pending?
@booking.create_booking_digest
PaymentMailer.payment_booking(@booking).deliver_now
flash[:notice] = t "messages.notice.mailpayment.sent_mail", email: @booking.email_booking.to_s
else
flash[:danger] = t "messages.failed.mailpayment.sent_mail"
end
redirect_to manager_bookings_path
end

def edit
if @booking.Pending?
redirect_to @booking.paypal_url(payment_booking_path(@booking))
else
flash[:danger] = t "messages.failed.mailpayment.paymented"
redirect_to root_path
end
end

private

def check_token
return if @booking.booking_digest == params[:id]

flash[:danger] = t "messages.failed.mailpayment.token"
redirect_to root_path
end

def load_booking
@booking = Booking.includes(:room).find_by(id: params[:booking])
return if @booking

flash[:danger] = t "messages.failed.mailpayment.sent_mail"
redirect_to root_path
end

def check_expire_time
return unless @booking.booking_expired?

@booking.destroy
flash[:danger] = t "messages.failed.mailpayment.expire"
redirect_to root_path
end
end
1 change: 0 additions & 1 deletion app/controllers/manager/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Manager
class BaseController < ApplicationController
layout "manager"
protect_from_forgery with: :exception
before_action :authenticate_admin!
end
end
2 changes: 1 addition & 1 deletion app/controllers/manager/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def room_params
params.require(:room).permit :name, :address, :rate_point, :description, :code_room,
:favorite_space_id, :guest, :type_room,
:acreage, :bed_room, :area_id, :bath_room, :location_id,
utility_ids: [], room_images_attributes: [:id, :room_id, :image, :_destroy]
utility_ids: [], room_images_attributes: %i[id room_id image _destroy]
end

def load_room
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def show
@rooms = []
@room = Room.find params[:id]
user = @room.user
rooms = user.rooms.map(&:id).reject{|r| r == @room.id}
rooms.each{|rooms| @rooms << Room.find(rooms)}
rooms = user.rooms.map(&:id).reject { |r| r == @room.id }
rooms.each { |rooms| @rooms << Room.find(rooms) }
@comment = @room.comments.build
@comments = @room.comments.newest
@average_comment = @room.comments.average(:rate) if @room.comments.present?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index

def show
@items = @location + @area + @room
render json: @items
render json: @items.sort
end

private
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ def avatar_admin(admin)
end
end

def format_time time
def format_time(time)
time.strftime("%m/%d/%Y")
end

def load_limit_image object
def load_limit_image(object)
return object.room_images.limit(1)[0].image.url if object.room_images.present?

"location_2.png"
end
end
33 changes: 31 additions & 2 deletions app/models/booking.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

class Booking < ApplicationRecord
before_save :downcase_email, :add_total_price

belongs_to :room
belongs_to :voucher, optional: true

validates :checkin, :checkout, :room_id, :number_guest, :name_booking,
:phone_booking, :email_booking, presence: true
:phone_booking, :email_booking, presence: true

enum status: %i[Pending Approve]

Expand Down Expand Up @@ -33,11 +35,38 @@ def paypal_url(_return_path)
"#{Settings.paypal_host}/cgi-bin/webscr?" + values.to_query
end

private
# Returns a random token.
def self.new_token
SecureRandom.urlsafe_base64
end

def create_booking_digest
update_attributes(booking_digest: Booking.new_token,
booking_sent_at: Time.zone.now)
end

def booking_expired?
booking_sent_at < 1.hours.ago
end

def checkin_date_after_checkout_date
return if checkin.blank? || checkout.blank?

errors.add(:checkout, "must be after the start date") if checkout < checkin
end

private

def add_total_price
total_price = (room.price.cost * ((checkout - checkin) / 86_400))
self.total_price = if voucher_id.present?
(total_price - ((total_price * voucher.sale) / 100)) + room.price.cleaning_fee
else
total_price + room.price.cleaning_fee
end
end

def downcase_email
self.email_booking = email_booking.downcase
end
end
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Comment < ApplicationRecord
belongs_to :user
belongs_to :room
Expand Down
6 changes: 6 additions & 0 deletions app/views/payment_mailer/invoice_payment.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ table.body border="0" cellpadding="0" cellspacing="0" role="presentation"
= t ".sale"
.col-7
= number_to_percentage(@booking.voucher.sale, precision: 0)
.row
.col-2
.col-3 style="font-weight: bold;"
= t ".clean"
.col-7
= number_to_currency(@room.price.cleaning_fee)
.row
.col-2
.col-3 style="font-weight: bold;"
Expand Down
8 changes: 7 additions & 1 deletion app/views/payment_mailer/payment_booking.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ table.body border="0" cellpadding="0" cellspacing="0" role="presentation"
= t ".sale"
.col-7
= number_to_percentage(@booking.voucher.sale, precision: 0)
.row
.col-2
.col-3 style="font-weight: bold;"
= t ".clean"
.col-7
= number_to_currency(@room.price.cleaning_fee)
.row
.col-2
.col-3 style="font-weight: bold;"
Expand Down Expand Up @@ -75,7 +81,7 @@ table.body border="0" cellpadding="0" cellspacing="0" role="presentation"
p = t ".end_mail"
/! END MAIN CONTENT AREA
= link_to t(".payment_now"),
@booking.paypal_url(payment_booking_url(@booking)),
check_payment_url(id: @booking.booking_digest, booking: @booking.id),
class: "btn btn-primary", style: "text-decoration: none;"
/! END CENTERED WHITE CONTAINER
/! START FOOTER
Expand Down
2 changes: 1 addition & 1 deletion app/views/trends/_trend.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
h2.extra-bold.p--giant= link_to trend.name, trend_path(trend), class: "trend-title"
p = trend.description
.row
= render trend.rooms.limit Settings.trend_limit
= render trend.rooms.includes(:trend_rooms, :room_images, :location, :price, :likes, :favorite_space).limit Settings.trend_limit
8 changes: 8 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ en:
failed:
admins:
create: "Create failed!"
mailpayment:
sent_mail: "Not find booking!"
paymented: "Booking paymented!"
token: "Token not true!"
expire: "Payment expired"
notice:
mailpayment:
sent_mail: "Sent a mail to %{email}"

manager:
rooms:
create_room: "Thank you to create room"
Expand Down Expand Up @@ -167,6 +173,7 @@ en:
room_name: "Room Name:"
cost: "Cost:"
sale: "Sale:"
clean: "Clean fee:"
total_price: "Total price:"
checkin: "Check in:"
checkout: "Check out:"
Expand All @@ -184,6 +191,7 @@ en:
room_name: "Room name:"
price: "Price:"
sale: "Sale:"
clean: "Clean fee:"
total_price: "Total price:"
checkin: "Check in:"
checkout: "Check out:"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
get "/favorite_spaces/:id", to: "home#show"
get "/autocomplete", to: "search#show"
post "/mailpayment/:id", to: "mailpayment#create", as: :mailpayment
get "/paypal/:id", to: "mailpayment#edit", as: :check_payment
post "/payment_update", to: "payment_booking#create", as: :payment_bookings
get "/payment_booking/:id", to: "payment_booking#show", as: :payment_booking
get "/payment_booking", to: "payment_booking#index", as: :thank_payment
Expand Down
2 changes: 1 addition & 1 deletion config/settings/development.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
paypal_host: https://www.sandbox.paypal.com
app_host: http://514e8bcd.ngrok.io
app_host: http://3fa1c2d5.ngrok.io
email: bookinghomestay@merchant.com
2 changes: 2 additions & 0 deletions db/migrate/20190903025307_create_bookings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def change
t.text :request
t.time :intend_time
t.decimal :total_price
t.string :booking_digest
t.datetime :booking_sent_at

t.timestamps
end
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
t.text "request"
t.time "intend_time"
t.decimal "total_price", precision: 10
t.string "booking_digest"
t.datetime "booking_sent_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "notification_params"
Expand Down
4 changes: 3 additions & 1 deletion spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'rails_helper'
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Comment, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
Expand Down

0 comments on commit bf00650

Please sign in to comment.