251 lines
8.7 KiB
Ruby
251 lines
8.7 KiB
Ruby
class Order < ApplicationRecord
|
|
before_create :set_order_date
|
|
|
|
belongs_to :customer
|
|
has_many :order_items, autosave: true , inverse_of: :order
|
|
has_many :assigned_order_items
|
|
|
|
#internal references attributes for business logic control
|
|
attr_accessor :items, :guest, :table_id, :new_booking, :booking_type, :employee_name, :booking_id
|
|
|
|
|
|
#Main Controller method to create new order - validate all inputs and generate new order
|
|
# order_item : {
|
|
# order_item_code : "",
|
|
# item_instance_code : "",
|
|
# quantity : 0,
|
|
# option_values : [],
|
|
# sub_order_items : [],
|
|
# }
|
|
def generate
|
|
booking = nil
|
|
|
|
if self.new_booking
|
|
booking = Booking.create({:dining_facility_id => self.table_id,:type => "TableBooking",
|
|
:checkin_at => Time.now.utc, :checkin_by => self.employee_name,
|
|
:booking_status => "new" })
|
|
else
|
|
if (self.booking_id.to_i > 0 )
|
|
booking = Booking.find(self.booking_id)
|
|
end
|
|
|
|
end
|
|
|
|
booking.save!
|
|
self.default_values
|
|
|
|
if self.save!
|
|
|
|
self.adding_line_items
|
|
#Add Order Table and Room relation afrer order creation
|
|
BookingOrder.create({:booking_id => booking.id, :order => self})
|
|
|
|
#Send order to queue one it done!
|
|
process_order_queue
|
|
|
|
return true, booking
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
#Main Method - to update order / add items
|
|
def modify
|
|
|
|
end
|
|
|
|
def adding_line_items
|
|
|
|
if self.items
|
|
#re-order to
|
|
ordered_list = re_order_items(self.items)
|
|
|
|
#loop to add all items to order
|
|
self.items.each do |item|
|
|
menu_item = MenuItem.search_by_item_code(item[:item_instance_code])
|
|
|
|
#if (!menu_item.nil?)
|
|
Rails.logger.debug menu_item
|
|
set_order_items = nil
|
|
##If menu Item set item - must add child items to order as well, where price is only take from menu_item
|
|
if (menu_item[:type] == "SetMenuItem")
|
|
set_order_items
|
|
end
|
|
|
|
OrderItem.processs_item(menu_item[:item_code], menu_item[:name],
|
|
item[:quantity],menu_item[:price], item[:options], set_order_items, self.id,
|
|
self.employee_name)
|
|
|
|
#end
|
|
end
|
|
|
|
self.item_count = self.order_items.count
|
|
self.save!
|
|
|
|
return true
|
|
else
|
|
self.errors.add(:order_items, :blank, message: "Order items cannot be blank")
|
|
return false
|
|
end
|
|
|
|
end
|
|
|
|
def update_items_status_to_billed(items)
|
|
if (items.nil?)
|
|
##Update the order status to ensure that reflect the stage
|
|
self.order_items.each do |item|
|
|
item.order_item_status = "billed"
|
|
item.save
|
|
end
|
|
else
|
|
items.each do |item|
|
|
item.order_item_status = "billed"
|
|
item.save
|
|
end
|
|
end
|
|
end
|
|
|
|
def default_values
|
|
self.customer = Customer.find(1) if self.customer_id.nil?
|
|
self.source = "emenu" if self.source.nil?
|
|
self.order_type = "dine-in" if self.order_type.nil?
|
|
|
|
end
|
|
|
|
protected
|
|
def re_order_items(form_items) #reorder inputs items as parents and child
|
|
parent_id = Array.new
|
|
parents = Array.new
|
|
parents_and_children_items = Array.new
|
|
|
|
new_items_list = Array.new
|
|
|
|
form_items.each do |parent|
|
|
if !parent[:parent_order_item_id].nil?
|
|
if (!parent_id.include?(parent[:parent_order_item_id]))
|
|
parent_id.push(parent[:parent_order_item_id])
|
|
|
|
end
|
|
end
|
|
end
|
|
Rails.logger.debug "Parent Id count -> " + parent_id.count.to_s
|
|
|
|
|
|
parent_id.each do |pid|
|
|
form_items.each do |item|
|
|
Rails.logger.debug "Adding - Parents -> " + pid.to_s + " - " + item[:order_item_id].to_s
|
|
if (pid == item[:order_item_id])
|
|
parents.push(item)
|
|
end
|
|
end
|
|
end
|
|
Rails.logger.debug "Parents count -> " + parents.count.to_s
|
|
|
|
parents.each do |parent|
|
|
children = Array.new
|
|
form_items.each do |item|
|
|
if (parent[:order_item_id] == item[:parent_order_item_id] )
|
|
children.push(item)
|
|
#Items to remove for new list
|
|
parents_and_children_items.push(item)
|
|
end
|
|
end
|
|
parent[:sub_items] = children
|
|
end
|
|
Rails.logger.debug "Parent/children Items to remove -> " + parents_and_children_items.count.to_s
|
|
|
|
#Remove process items
|
|
#c = a.reject{ |e| b.include? e }
|
|
|
|
new_items_list = form_items - parents_and_children_items
|
|
|
|
|
|
Rails.logger.debug "New list count -> " + new_items_list.count.to_s
|
|
|
|
#Add parent to the list
|
|
#new_items_list = new_items_list + parents
|
|
|
|
Rails.logger.debug "Re-Order List (Parent)-"
|
|
|
|
Rails.logger.debug parents
|
|
|
|
Rails.logger.debug "Re-Order List -"
|
|
Rails.logger.debug new_items_list
|
|
|
|
return new_items_list
|
|
end
|
|
|
|
private
|
|
def validate_api_inputs
|
|
|
|
end
|
|
|
|
def set_order_date
|
|
self.date = Time.now.utc
|
|
end
|
|
|
|
#Update Items Count and Quantity changes whenever there is changes
|
|
def update_products_and_quantity_count
|
|
item_count = 0
|
|
quantity_count = 0
|
|
# Count number of different items
|
|
self.item_count = self.order_items.item_count
|
|
self.quantity_count = quantity_count
|
|
# Counter number of quantityf
|
|
end
|
|
|
|
#Process order items and send to order queue
|
|
def process_order_queue
|
|
#Send to background job for processing
|
|
OrderQueueProcessorJob.perform_later(self.id)
|
|
end
|
|
|
|
#Origami: Cashier : to view order type Table
|
|
def self.get_booking_order_table
|
|
booking_orders = Booking.select("sales.receipt_no,orders.status as order_status,bookings.id,sales.id as sale_id,dining_facilities.name as table_name")
|
|
.joins("left join booking_orders on booking_orders.booking_id = bookings.id")
|
|
.joins("left join dining_facilities on dining_facilities.id = bookings.dining_facility_id")
|
|
.joins("left join orders on orders.id = booking_orders.order_id")
|
|
.joins("left join sale_orders on sale_orders.order_id = orders.id")
|
|
.joins("left join sales on sales.id = sale_orders.sale_id")
|
|
.where("booking_orders.order_id IS NOT NULL and dining_facilities.type=? and dining_facilities.is_active=?",DiningFacility::TABLE_TYPE,true)
|
|
.group("bookings.id")
|
|
end
|
|
#Origami: Cashier : to view order type Room
|
|
def self.get_booking_order_rooms
|
|
booking_rooms = Booking.select("sales.receipt_no,orders.status as order_status,bookings.id,sales.id as sale_id,dining_facilities.name as room_name")
|
|
.joins("left join booking_orders on booking_orders.booking_id = bookings.id")
|
|
.joins("left join dining_facilities on dining_facilities.id = bookings.dining_facility_id")
|
|
.joins("left join orders on orders.id = booking_orders.order_id")
|
|
.joins("left join sale_orders on sale_orders.order_id = orders.id")
|
|
.joins("left join sales on sales.id = sale_orders.sale_id")
|
|
.where("booking_orders.order_id IS NOT NULL and dining_facilities.type=? and dining_facilities.is_active=?",DiningFacility::ROOM_TYPE,true)
|
|
.group("bookings.id")
|
|
end
|
|
#Origami: Cashier : to view orders
|
|
def self.get_orders
|
|
from = Time.now.beginning_of_day.utc
|
|
to = Time.now.end_of_day.utc
|
|
orders = Order.select("orders.id as order_id,sales.receipt_no,orders.status as order_status,bookings.id,sales.id as sale_id,dining_facilities.name as table_name")
|
|
.joins("left join booking_orders on booking_orders.order_id = orders.id
|
|
left join bookings on bookings.id = booking_orders.id
|
|
left join dining_facilities on dining_facilities.id = bookings.dining_facility_id
|
|
left join order_items on order_items.order_id = orders.id
|
|
left join sale_orders on sale_orders.order_id = orders.id
|
|
left join sales on sales.id = sale_orders.sale_id")
|
|
.where("dining_facilities.is_active=? and orders.date between ? and ?",true,from,to)
|
|
.group("orders.id")
|
|
|
|
# Booking.select("sales.receipt_no,orders.status as order_status,bookings.id,sales.id as sale_id,dining_facilities.name as table_name")
|
|
# .joins("left join booking_orders on booking_orders.booking_id = bookings.id")
|
|
# .joins("left join dining_facilities on dining_facilities.id = bookings.dining_facility_id")
|
|
# .joins("left join orders on orders.id = booking_orders.order_id")
|
|
# .joins("left join sale_orders on sale_orders.order_id = orders.id")
|
|
# .joins("left join sales on sales.id = sale_orders.sale_id")
|
|
# .where("booking_orders.order_id IS NOT NULL and dining_facilities.is_active=? and orders.date between ? and ?",true,from,to)
|
|
# .group("orders.id")
|
|
end
|
|
end
|