Files
sx-fc/app/controllers/api/orders_controller.rb
2018-05-25 17:44:36 +06:30

267 lines
8.9 KiB
Ruby
Executable File

class Api::OrdersController < Api::ApiController
# skip_before_action :authenticate
#Description
# This API show current order details
# Input Params - order_id
def show
order = Order.find(params[:order_id])
order.order_items
end
def get_order
order = Order.find(params[:order_id])
order.order_items
end
# API - This api will retrive current booking for android with table or room id
def view_orders
booking_id = params[:booking_id]
table_id = params[:table_id]
if Booking.exists?(booking_id)
booking = Booking.find(booking_id)
if booking
if booking.dining_facility_id.to_i == table_id.to_i
if booking.booking_status == 'assign'
if booking.sale_id.nil?
@booking = booking
end
end
else
table = DiningFacility.find(table_id)
@booking = table.get_booking
end
end
else
table = DiningFacility.find(table_id)
@booking = table.get_booking
end
end
# Description
# This API allow new order creation
# Input Params
# order_source [* default - emenu] | table_id | booking_id [table_booking_id & Room_booking_id] (*require for Dine-In) | order_type [* Default - Dine-in]
# | guest_info (optional) | customer_id (* Default assigned to WALK-IN)
# order_items {[item_code, item_instance_code , qty, option, variants]}
# Output Params
# Status [Success | Error | System Error] , order_id, error_message (*)
def create
Rails.logger.debug "Order Source - " + params[:order_source].to_s
Rails.logger.debug "Table ID - " + params[:table_id].to_s
if checkin_checkout_time(params[:booking_id])
#for extratime
is_extra_time = false
extra_time = ''
params[:order_items].each { |i|
i["item_instance_code"] = i["item_instance_code"].downcase.to_s
if i["item_instance_code"].include? "ext"
is_extra_time = true
arr_exts = i["item_instance_code"].split("_")
if arr_exts[1].match(/^(\d)+$/)
time = arr_exts[1].to_i*60*i["quantity"].to_i
extra_time = Time.at(time)
end
end
}
#end extra time
puts params[:order_items]
puts "ORDER ITEMS"
# begin
@order = Order.new
@order.source = params[:order_source]
@order.order_type = params[:order_type]
@order.customer_id = params[:customer_id] == ""? "CUS-000000000001" : params[:customer_id] # for no customer id from mobile
@order.items = params[:order_items]
@order.guest = params[:guest_info]
@order.table_id = params[:table_id] # this is dining facilities's id
@order.new_booking = true
@order.waiters = current_login_employee.name
@order.employee_name = current_login_employee.name
@order.is_extra_time = is_extra_time
@order.extra_time = extra_time
#Create Table Booking or Room Booking
if !params["booking_id"].nil?
# check booking id is already completed.
booking = Booking.find(params[:booking_id])
if booking
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)
if sale_status
return return_json_status_with_code(400, "bill requested")
end
else
@order.new_booking = false
@order.booking_id = params[:booking_id]
end
else
sale_status = check_order_with_table(params[:table_id])
if sale_status
return return_json_status_with_code(400, "bill requested")
end
end
end #booking exists
else
sale_status = check_order_with_table(params[:table_id])
if sale_status
# return false , @message = "bill requested"
return return_json_status_with_code(400, "bill requested")
end
end
@status, @booking = @order.generate
if @status && @booking
Order.process_order_queue(@order.order_id,@order.table_id,@order.source)
end
# # for parallel order
# remoteIP = ""
# begin
# @status, @booking = @order.generate
# remoteIP = request.remote_ip
# end while request.remote_ip != remoteIP
else
return return_json_status_with_code(406, "Checkout time is over!")
end
end
# render json for http status code
def return_json_status_with_code(code, msg)
render status: code, json: {
message: msg
# booking_id: booking_id
}.to_json
end
def check_order_with_table(table_id)
table = DiningFacility.find(table_id)
if table
booking = table.get_current_booking
# puts booking
if booking
if !booking.sale_id.nil?
if booking.sale.sale_status == "completed" || booking.sale.sale_status == "new"
@order.new_booking = true
return false
end
else
@order.new_booking = false
@order.booking_id = booking.booking_id
return false
end
end
end
end
# this can always true
def check_order_with_booking(booking)
if booking.sale.sale_status == "completed" || booking.sale.sale_status == "new"
@order.new_booking = true
return false
else
@order.new_booking = false
@order.booking_id = params[:booking_id]
return false
end
end
# Description
# This API - allow order to add new items to existing orders, does not allow you to remove confirm items
# Update customer info, Guest Info
# Input Params
# order_id , order_items {[item_code, item_instance_code , qty, option, variants]}
# def update
# end
def order_params
params.permits(:order_source, :booking_id,:order_type,
:customer_id,:guest_info, :table_id, :room_id,
order_items: [:item_code, :qty, :option, :remark])
end
#checked checkin and checkout time
def checkin_checkout_time(booking_id)
status = true
if !booking_id.nil?
today = Time.now.utc
booking = Booking.find(booking_id)
if !booking.nil?
if !booking.checkout_at.nil?
checkout_time = booking.checkout_at.utc
if checkout_time <= today
status = false
end
end
end
end
return status
end
def update
Rails.logger.debug "Booking ID - " + params[:booking_id].to_s
if checkin_checkout_time(params[:booking_id])
Rails.logger.debug "Order Item ID - " + params[:order_item_id].to_s
order_items_id = params[:order_item_id]
qty_weight = params[:quantity].to_f
remarks = params[:remark]
order_item = OrderItem.find(order_items_id)
before_updated_qty = order_item.qty
order_item.item_order_by = current_login_employee.name
order_item.qty = qty_weight
order_item.remark = remarks
order_item.save
if ENV["SERVER_MODE"] != "cloud" #no print in cloud server
# print
assigned_item = AssignedOrderItem.find_by_instance_code(order_item.item_instance_code)
assigned_items = AssignedOrderItem.where("item_code='" + assigned_item.item_code + "' AND " + "order_id='" + assigned_item.order_id + "'")
if !assigned_items.nil?
assigned_items.each do |assign_item|
# order queue stations
oqs = assign_item.order_queue_station
printer = PrintSetting.all
unique_code="OrderItemPdf"
if !order_slim_pdf.empty?
if !printer.empty?
printer.each do |printer_setting|
if printer_setting.unique_code == 'OrderItemPdf'
unique_code="OrderItemPdf"
elsif printer_setting.unique_code == 'OrderItemSlimPdf'
unique_code="OrderItemSlimPdf"
elsif printer_setting.unique_code == 'OrderSetItemPdf'
unique_code="OrderSetItemPdf"
elsif printer_setting.unique_code == 'OrderItemSlimCustomisePdf'
unique_code="OrderItemSlimCustomisePdf"
elsif printer_setting.unique_code == 'OrderItemCustomisePdf'
unique_code="OrderItemCustomisePdf"
end
end
end
end
print_settings=PrintSetting.find_by_unique_code(unique_code)
order_queue_printer= Printer::OrderQueuePrinter.new(print_settings)
order_queue_printer.print_order_item(print_settings, oqs, order_item.order_id, order_items_id, print_status=" (Cancelled)", before_updated_qty )
end
end
end
return return_json_status_with_code(200, "updated successfully!")
else
return return_json_status_with_code(406, "Checkout time is over!")
end
end
end