109 lines
2.7 KiB
Ruby
109 lines
2.7 KiB
Ruby
class Order < ApplicationRecord
|
|
before_create :set_order_date
|
|
before_save :update_products_and_quantity_count
|
|
|
|
belongs_to :customer
|
|
has_many :order_items, autosave: true , inverse_of: :order
|
|
|
|
#internal references attributes for business logic control
|
|
attr_accessor :items, :guest, :table_id, :new_booking, :booking_type, :employee_name
|
|
|
|
|
|
#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
|
|
booking = Booking.find(self.booking_id)
|
|
|
|
end
|
|
|
|
booking.save!
|
|
|
|
self.default_values
|
|
|
|
if self.save!
|
|
|
|
self.adding_line_items
|
|
|
|
#Add Order Table and Room relation afrer order creation
|
|
if booking.type = "TableBooking"
|
|
#add to table_booking_order
|
|
tbo = TableBookingOrder.create({:table_booking_id => booking.id, :order => self})
|
|
elsif booking.type = "RoomBooking"
|
|
#add to room_booking_order
|
|
rbo = RoomBookingOrder.create({:room_booking_id => booking.id, :order => self})
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
#Main Method - to update order / add items
|
|
def modify
|
|
|
|
end
|
|
|
|
def adding_line_items
|
|
|
|
if self.items
|
|
#loop to add all items to order
|
|
self.items.each do |item|
|
|
self.order_items.add( OrderItem.processs_item(item, self.id, self.employee_name) )
|
|
end
|
|
return true
|
|
else
|
|
self.errors.add(:order_items, :blank, message: "Order items cannot be blank")
|
|
return false
|
|
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
|
|
|
|
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 = item_count
|
|
self.quantity_count = quantity_count
|
|
# Counter number of quantity
|
|
end
|
|
|
|
#Process order items and send to order queue
|
|
def process_order_queue
|
|
#Send to background job for processing
|
|
OrderQueueProcessorJob.perform_later(:order_id => self.id)
|
|
end
|
|
end
|