Files
sx-fc/app/models/order.rb
2017-04-04 12:18:17 +06:30

87 lines
2.1 KiB
Ruby

class Order < ApplicationRecord
before_create :set_order_date
before_save :update_products_and_quantity_count
belongs_to :customer
has_many :order_items, inverse_of: :order
has_many :dining_ins
#internal references attributes for business logic control
attr_accessor :items, :guest, :table_id, :new_booking, :booking_type
#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
if self.new_booking
booking = Booking.create({:dining_facility_id => :table_id, })
self.adding_line_items
self.save!
else
booking = Booking.find(self.booking_id)
#Add Order Table and Room relation afrer order creation
if booking.type = "Table"
#add to table_booking_order
tbo = TableBookingOrder.create({:table_booking => booking, :order => self})
elsif booking.type = "Room"
#add to room_booking_order
rbo = RoomBookingOrder.create({:table_booking => booking, :order => self})
end
end
end
#Main Method - to update order / add items
def modify
end
private
def validate_api_inputs
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) )
end
else
self.errors.add(:order_items, :blank, message: "Order items cannot be blank")
end
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