From dd95b29d7dfdd1a2cc9f3295dd67d4d32c200bd3 Mon Sep 17 00:00:00 2001 From: Yan Date: Thu, 29 Jun 2017 19:09:01 +0630 Subject: [PATCH] control order to logout --- app/controllers/api/orders_controller.rb | 37 +++++++++++++----------- app/models/dining_facility.rb | 10 +++++++ app/models/order.rb | 2 +- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index e3184ecc..817c7eea 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -68,10 +68,10 @@ class Api::OrdersController < Api::ApiController if booking.dining_facility_id.to_i == params[:table_id].to_i && booking.booking_status != 'moved' if !booking.sale_id.nil? sale_status = check_order_with_booking(booking) - puts "WWwwWWWWWWww" - puts sale_status + puts "WWwwWWWWWWww" + puts sale_status if sale_status - return false , @message = "bill requested" + return return_json_status_with_code(400, "bill requested") end else @order.new_booking = false @@ -79,43 +79,45 @@ class Api::OrdersController < Api::ApiController end else sale_status = check_order_with_table(params[:table_id]) - puts "OOOOOOOOO" - puts sale_status + puts "OOOOOOOOO" + puts sale_status if sale_status - return false , @message = "bill requested" + return return_json_status_with_code(400, "bill requested") end end end #booking exists else sale_status = check_order_with_table(params[:table_id]) - puts "MMMMMMMM" - puts sale_status + puts "MMMMMMMM" + puts sale_status if sale_status - return false , @message = "bill requested" + # return false , @message = "bill requested" + return return_json_status_with_code(400, "bill requested") end end @status, @booking = @order.generate end - # render json for http status code 202 - def return_json_status_with_code(code, msg, booking_id) + # render json for http status code + def return_json_status_with_code(code, msg) render status: code, json: { - message: msg, - booking_id: booking_id + message: msg }.to_json end def check_order_with_table(table_id) table = DiningFacility.find(table_id) if table - booking = table.get_current_booking + booking = table.get_moved_booking puts booking if booking - if booking.sale.sale_status == "completed" || booking.sale.sale_status == "billed" + if !booking.sale_id.nil? + if booking.sale.sale_status == "completed" || booking.sale.sale_status == "new" @order.new_booking = true return true - else + end + else @order.new_booking = false @order.booking_id = booking.booking_id return false @@ -124,8 +126,9 @@ class Api::OrdersController < Api::ApiController end end + # this can always true def check_order_with_booking(booking) - if booking.sale.sale_status == "completed" || booking.sale.sale_status == "billed" + if booking.sale.sale_status == "completed" || booking.sale.sale_status == "new" @order.new_booking = true return true else diff --git a/app/models/dining_facility.rb b/app/models/dining_facility.rb index 0e10307b..510da53d 100644 --- a/app/models/dining_facility.rb +++ b/app/models/dining_facility.rb @@ -39,6 +39,16 @@ class DiningFacility < ApplicationRecord end end + def get_moved_booking + booking = Booking.where("dining_facility_id = #{self.id} and booking_status ='moved' and checkin_at between '#{DateTime.now.utc - 5.hours}' and '#{DateTime.now.utc}' and checkout_at is null").limit(1) + + if booking.count > 0 then + return booking[0] + else + return nil + end + end + def get_new_booking # query for new # if status diff --git a/app/models/order.rb b/app/models/order.rb index 29267965..ed5f7cbc 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -54,7 +54,7 @@ class Order < ApplicationRecord end - return false, @message = "booking fail" + return false end