diff --git a/app/controllers/api/check_in_process_controller.rb b/app/controllers/api/check_in_process_controller.rb index fe98cf2a..3ac8a8af 100644 --- a/app/controllers/api/check_in_process_controller.rb +++ b/app/controllers/api/check_in_process_controller.rb @@ -46,58 +46,84 @@ class Api::CheckInProcessController < Api::ApiController def check_in_process if params[:dining_id] - dining_facility = DiningFacility.find(params[:dining_id]) + dining_facility = DiningFacility.find(params[:dining_id]) if dining_facility.status == "available" - lookup_checkout_time = Lookup.collection_of("checkout_time") - - if !lookup_checkout_time.empty? - today = Time.now.utc.getlocal - checkout_at = Time.now.utc.getlocal - - lookup_checkout_time.each do |checkout_time| - arr_time = checkout_time[0].split("-") - start_time = Time.parse(arr_time[0].strip).utc.getlocal.strftime("%H:%M%p") - end_time = Time.parse(arr_time[1].strip).utc.getlocal.strftime("%H:%M%p") - if start_time <= today.strftime("%H:%M%p") && today.strftime("%H:%M%p") <= end_time - checkout_at = checkout_at + (checkout_time[1]).to_i.minutes + if params[:checkin_time] + if dining_facility.check_time(params[:checkin_time], "checkin") + booking = dining_facility.get_current_booking + if booking.nil? + checkin_at = Time.parse(params[:checkin_time]).utc + booking = Booking.create({:dining_facility_id => params[:dining_id],:type => "TableBooking", + :checkin_by=>current_login_employee.name,:checkin_at => checkin_at,:checkout_at =>nil, :booking_status => "assign", :reserved_at => nil, :reserved_by => nil }) + if booking.save! + dining_facility.status = "occupied" + dining_facility.save! + render :json => { :status => true, :booking_id => booking.booking_id, :checkin_at => booking.checkin_at.utc.getlocal.strftime("%Y-%m-%d %H:%M:%S"), :message => "Check-in success" } + else + render :json => { :status => false, :error_message => "Booking does not create successfully!" } + end + else + render :json => { :status => false, :error_message => "Booking already exist!" } end - end - - if checkout_at.strftime("%Y-%m-%d %H:%M%p") > today.strftime("%Y-%m-%d %H:%M%p") - # if dining_facility.type == "Table" - # type = "TableBooking" - # else - # type = "RoomBooking" - # end - - booking = Booking.create({:dining_facility_id => params[:dining_id],:type => "TableBooking", - :checkin_by=>current_login_employee.name,:checkin_at => Time.now.utc,:checkout_at =>checkout_at, :booking_status => "assign", :reserved_at => checkout_at, :reserved_by => current_login_employee.name }) - if booking.save! - dining_facility.status = "occupied" - dining_facility.save! - end - terminal = DiningFacility.find_by_id(booking.dining_facility_id) - cashier_terminal = CashierTerminal.find_by_id(terminal.zone_id) - - if ENV["SERVER_MODE"] != "cloud" #no print in cloud server - - unique_code = "CheckInOutPdf" - printer = PrintSetting.find_by_unique_code(unique_code) - - # print when complete click - order_queue_printer = Printer::OrderQueuePrinter.new(printer) - - if !printer.nil? - order_queue_printer.print_check_in_out(printer, cashier_terminal, booking, dining_facility) - end - end - render :json => { :status => true, :checkout_at => booking.checkout_at.utc.getlocal.strftime("%Y-%m-%d %H:%M") } else - render :json => { :status => true } + render :json => { :status => false, :error_message => "Checkin time not available!" } end - else - render :json => { :status => true } + booking = dining_facility.get_current_checkout_booking + if booking.nil? + lookup_checkout_time = Lookup.collection_of("checkout_time") + + if !lookup_checkout_time.empty? + today = Time.now.utc.getlocal + checkout_at = Time.now.utc.getlocal + + lookup_checkout_time.each do |checkout_time| + arr_time = checkout_time[0].split("-") + start_time = Time.parse(arr_time[0].strip).utc.getlocal.strftime("%H:%M%p") + end_time = Time.parse(arr_time[1].strip).utc.getlocal.strftime("%H:%M%p") + if start_time <= today.strftime("%H:%M%p") && today.strftime("%H:%M%p") <= end_time + checkout_at = checkout_at + (checkout_time[1]).to_i.minutes + end + end + + if checkout_at.strftime("%Y-%m-%d %H:%M%p") > today.strftime("%Y-%m-%d %H:%M%p") + # if dining_facility.type == "Table" + # type = "TableBooking" + # else + # type = "RoomBooking" + # end + + booking = Booking.create({:dining_facility_id => params[:dining_id],:type => "TableBooking", + :checkin_by=>current_login_employee.name,:checkin_at => Time.now.utc,:checkout_at =>checkout_at, :booking_status => "assign", :reserved_at => checkout_at, :reserved_by => current_login_employee.name }) + if booking.save! + dining_facility.status = "occupied" + dining_facility.save! + end + terminal = DiningFacility.find_by_id(booking.dining_facility_id) + cashier_terminal = CashierTerminal.find_by_id(terminal.zone_id) + + if ENV["SERVER_MODE"] != "cloud" #no print in cloud server + + unique_code = "CheckInOutPdf" + printer = PrintSetting.find_by_unique_code(unique_code) + + # print when complete click + order_queue_printer = Printer::OrderQueuePrinter.new(printer) + + if !printer.nil? + order_queue_printer.print_check_in_out(printer, cashier_terminal, booking, dining_facility) + end + end + render :json => { :status => true, :checkout_at => booking.checkout_at.utc.getlocal.strftime("%Y-%m-%d %H:%M") } + else + render :json => { :status => true } + end + else + render :json => { :status => true } + end + else + render :json => { :status => false, :error_message => "Booking already exist!" } + end end else error_message = "#{dining_facility.type} is not available!" @@ -136,6 +162,26 @@ class Api::CheckInProcessController < Api::ApiController end end + def check_out_process + if !params[:booking_id].nil? && !params[:checkout_time].nil? + booking = Booking.find(params[:booking_id]) + dining_facility = DiningFacility.find(booking.dining_facility_id) + if dining_facility.check_time(params[:checkout_time], booking, "checkout") + checkout_time = Time.parse(params[:checkout_time].strip).utc.getlocal + booking.checkout_at = checkout_time + booking.reserved_at = checkout_time + booking.reserved_by = current_login_employee.name + if booking.save! + render :json => { :status => true, :checkin_at => booking.checkin_at.utc.getlocal.strftime("%Y-%m-%d %H:%M:%S"), :checkout_at => booking.checkout_at.utc.getlocal.strftime("%Y-%m-%d %H:%M:%S"), :message => "Checkout success." } + end + else + render :json => { :status => false, :error_message => "Checkout time not available!" } + end + else + render :json => { :status => false, :error_message => "booking_id and checkout_time are required!" } + end + end + private def check_in_process_params params.permit(:dining_id,:booking_id,:time) diff --git a/app/models/dining_facility.rb b/app/models/dining_facility.rb index 739e15d7..e2a0b9a3 100755 --- a/app/models/dining_facility.rb +++ b/app/models/dining_facility.rb @@ -179,5 +179,24 @@ class DiningFacility < ApplicationRecord end end end + + def check_time(time, booking = nil, type) + status = true + today = Time.now.utc + check_time = Time.parse(time.strip).utc + if type.downcase == "checkin" + if check_time < today + status = false + end + else + if !booking.nil? + checkin_at = booking.checkin_at.utc + if check_time <= checkin_at + status = false + end + end + end + return status + end end diff --git a/app/models/sale.rb b/app/models/sale.rb index 9096adcf..7df97840 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -165,7 +165,9 @@ class Sale < ApplicationRecord booking.sale_id = self.id if !booking.checkout_at.nil? + if Time.parse(booking.checkout_at).utc < Time.now.utc booking.checkout_at = Time.now.utc.getlocal + end else booking.checkout_at = Time.now.utc.getlocal end diff --git a/app/views/api/restaurant/zones/index.json.jbuilder b/app/views/api/restaurant/zones/index.json.jbuilder index 4a8666b4..d9bff6be 100755 --- a/app/views/api/restaurant/zones/index.json.jbuilder +++ b/app/views/api/restaurant/zones/index.json.jbuilder @@ -6,21 +6,39 @@ if @zones #List all tables json.tables zone.tables do |table| if table.is_active + current_booking = table.get_current_booking json.id table.id json.name table.name json.status table.status json.zone_id table.zone_id #Add this zone_id to keep data structure consistance - json.current_booking table.get_current_booking.booking_id rescue "" + if !current_booking.nil? + json.current_booking current_booking.booking_id + json.checkin_at Time.parse(current_booking.checkin_at.strftime("%Y-%m-%d %H:%M:%S")).utc.getlocal.strftime("%Y-%m-%d %H:%M:%S") + json.checkout_at current_booking.checkout_at ? Time.parse(current_booking.checkout_at.strftime("%Y-%m-%d %H:%M:%S")).utc.getlocal.strftime("%Y-%m-%d %H:%M:%S") : "" + else + json.current_booking "" + json.checkin_at "" + json.checkout_at "" + end end end json.rooms zone.rooms do |room| if room.is_active + current_booking = room.get_current_booking json.id room.id json.name room.name json.status room.status json.zone_id room.zone_id #Add this zone_id to keep data structure consistance - json.current_booking room.get_current_booking.booking_id rescue "" + if !current_booking.nil? + json.current_booking current_booking.booking_id + json.checkin_at Time.parse(current_booking.checkin_at.strftime("%Y-%m-%d %H:%M:%S")).utc.getlocal.strftime("%Y-%m-%d %H:%M:%S") + json.checkout_at current_booking.checkout_at ? Time.parse(current_booking.checkout_at.strftime("%Y-%m-%d %H:%M:%S")).utc.getlocal.strftime("%Y-%m-%d %H:%M:%S") : "" + else + json.current_booking "" + json.checkin_at "" + json.checkout_at "" + end end end end @@ -28,21 +46,39 @@ if @zones else #list all tables and rooms with out zones json.tables @all_tables do |table| if table.is_active + current_booking = table.get_current_booking json.id table.id json.name table.name json.status table.status json.zone_id table.zone_id #Add this zone_id to keep data structure consistance - json.current_booking table.get_current_booking.booking_id rescue "" + if !current_booking.nil? + json.current_booking current_booking.booking_id + json.checkin_at Time.parse(current_booking.checkin_at.strftime("%Y-%m-%d %H:%M:%S")).utc.getlocal.strftime("%Y-%m-%d %H:%M:%S") + json.checkout_at current_booking.checkout_at ? Time.parse(current_booking.checkout_at.strftime("%Y-%m-%d %H:%M:%S")).utc.getlocal.strftime("%Y-%m-%d %H:%M:%S") : "" + else + json.current_booking "" + json.checkin_at "" + json.checkout_at "" + end end end json.rooms @all_rooms do |room| if room.is_active + current_booking = room.get_current_booking json.id room.id json.name room.name json.status room.status json.zone_id room.zone_id #Add this zone_id to keep data structure consistance - json.current_booking room.get_current_booking.booking_id rescue "" + if !current_booking.nil? + json.current_booking current_booking.booking_id + json.checkin_at Time.parse(current_booking.checkin_at.strftime("%Y-%m-%d %H:%M:%S")).utc.getlocal.strftime("%Y-%m-%d %H:%M:%S") + json.checkout_at current_booking.checkout_at ? Time.parse(current_booking.checkout_at.strftime("%Y-%m-%d %H:%M:%S")).utc.getlocal.strftime("%Y-%m-%d %H:%M:%S") : "" + else + json.current_booking "" + json.checkin_at "" + json.checkout_at "" + end end end end diff --git a/config/routes.rb b/config/routes.rb index dd9660c8..e68fc76d 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -82,6 +82,7 @@ scope "(:locale)", locale: /en|mm/ do post "call_waiter" => "call_waiters#index" get "survey/:id" => "survey#index" post "survey/:id" => "survey#create" + post "check_out" => "check_in_process#check_out_process" end #order and reservation api