213 lines
7.2 KiB
Ruby
Executable File
213 lines
7.2 KiB
Ruby
Executable File
class MenuItem < ApplicationRecord
|
|
# before_create :generate_menu_item_code
|
|
|
|
belongs_to :menu_category, :optional => true
|
|
has_many :menu_item_instances
|
|
has_many :commissions
|
|
has_many :product_commissions
|
|
|
|
# belongs_to :parent, :class_name => "MenuItem", foreign_key: "menu_item_id", :optional => true
|
|
# has_many :children, :class_name => "MenuItem", foreign_key: "menu_item_id"
|
|
belongs_to :account
|
|
|
|
has_many :menu_item_sets
|
|
has_many :item_sets, through: :menu_item_sets
|
|
|
|
validates_presence_of :item_code, :name, :type, :min_qty,:account_id
|
|
validates_uniqueness_of :item_code
|
|
|
|
default_scope { order('item_code asc') }
|
|
|
|
scope :simple_menu_item, -> { where(type: 'SimpleMenuItem') }
|
|
scope :set_menu_item, -> { where(type: 'SetMenuItem') }
|
|
scope :active, -> { where(is_available: true) }
|
|
|
|
# Item Image Uploader
|
|
mount_uploader :image_path, MenuItemImageUploader
|
|
|
|
def self.collection
|
|
MenuItem.select("id, name").map { |e| [e.name, e.id] }
|
|
end
|
|
|
|
# Work with item_code = item_instance_code
|
|
def self.search_by_item_code(item_code)
|
|
menu_item_hash = Hash.new
|
|
mt_instance = MenuItemInstance.find_by_item_instance_code(item_code)
|
|
if (!mt_instance.nil?)
|
|
menu_item = MenuItem.find(mt_instance.menu_item_id)
|
|
menu_item_hash[:type] = menu_item.type
|
|
menu_item_hash[:account_id] = menu_item.account_id
|
|
menu_item_hash[:item_code] = menu_item.item_code
|
|
menu_item_hash[:item_instance_code] = mt_instance.item_instance_code
|
|
menu_item_hash[:name] = menu_item.name.to_s + " - " + mt_instance.item_instance_name.to_s
|
|
menu_item_hash[:alt_name] = menu_item.alt_name.to_s # + " - " + mt_instance.item_instance_name.to_s
|
|
menu_item_hash[:price] = mt_instance.price
|
|
menu_item_hash[:promotion_price] = mt_instance.promotion_price
|
|
menu_item_hash[:is_on_promotion] = mt_instance.is_on_promotion
|
|
menu_item_hash[:is_available] = mt_instance.is_available
|
|
menu_item_hash[:taxable] = menu_item.taxable
|
|
|
|
return menu_item_hash
|
|
end
|
|
|
|
return nil
|
|
end
|
|
|
|
def self.deleteRecursive(menu_item)
|
|
# find the sub menu item of current item
|
|
sub_menu_items = MenuItem.where("id=?",menu_item.id)
|
|
if sub_menu_items.length != 0
|
|
sub_menu_items.each do |subitem|
|
|
# if deleteRecursive(subitem)
|
|
# end
|
|
end
|
|
# find the instances of current menu item
|
|
instances = MenuItemInstance.where("menu_item_id=?",menu_item.id)
|
|
instances.each do |instance|
|
|
instance.destroy
|
|
end
|
|
menu_item.destroy
|
|
return true
|
|
else
|
|
instances = MenuItemInstance.where("menu_item_id=?",menu_item.id)
|
|
instances.each do |instance|
|
|
instance.destroy
|
|
end
|
|
menu_item.destroy
|
|
return false
|
|
end
|
|
|
|
end
|
|
|
|
def self.get_items(menu)
|
|
item = Array.new
|
|
if menu.menu_items
|
|
menu.menu_items do |item|
|
|
if item.is_available
|
|
# Format for attributes json
|
|
attr_format = []
|
|
# Format for attributes json
|
|
if item.item_attributes.count > 0
|
|
item.item_attributes.each do|attr_id|
|
|
menu_attr = MenuItemAttribute.find(attr_id)
|
|
if attr_format.count == 0
|
|
attr_format.push({ type: menu_attr.attribute_type, values: [menu_attr.name] })
|
|
next
|
|
end
|
|
|
|
attr_format.each do |af|
|
|
if menu_attr.attribute_type.in? attr_format.map {|k| k[:type]}
|
|
if menu_attr.attribute_type == af[:type]
|
|
af[:values].push(menu_attr.name)
|
|
end
|
|
else
|
|
new_attr = {type: menu_attr.attribute_type, values: [ menu_attr.name ] }
|
|
attr_format.push(new_attr)
|
|
break
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
# Format for option json
|
|
opt_format = []
|
|
# Format for attributes json
|
|
if item.item_options.count > 0
|
|
item.item_options.each do|opt|
|
|
menu_opt = MenuItemOption.find(opt)
|
|
if opt_format.count == 0
|
|
opt_format.push({ type: menu_opt.option_type, values: [menu_opt.name] })
|
|
next
|
|
end
|
|
|
|
opt_format.each do |of|
|
|
if menu_opt.option_type.in? opt_format.map {|k| k[:type]}
|
|
if menu_opt.option_type == of[:type]
|
|
of[:values].push(menu_opt.name)
|
|
end
|
|
else
|
|
new_opt = {type: menu_opt.option_type, values: [ menu_opt.name ] }
|
|
opt_format.push(new_opt)
|
|
break
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
#Menu Item Information
|
|
menu_items.id item.id
|
|
menu_items.code item.item_code
|
|
menu_items.name item.name
|
|
menu_items.alt_name item.alt_name
|
|
menu_items.image item.image_path.url
|
|
menu_items.description item.description
|
|
menu_items.information item.information
|
|
menu_items.type item.type
|
|
menu_items.account_id item.account_id
|
|
menu_items.min_qty item.min_qty
|
|
menu_items.is_available item.is_available
|
|
menu_items.is_sub_item item.is_sub_item
|
|
menu_items.unit item.unit
|
|
|
|
# Item Sets of Menu Item
|
|
item_sets = Array.new()
|
|
item.item_sets do |its|
|
|
sets.id its.id
|
|
sets.name its.name
|
|
sets.alt_name its.alt_name
|
|
sets.min_selectable_qty its.min_selectable_qty
|
|
sets.max_selectable_qty its.max_selectable_qty
|
|
instances = Array.new()
|
|
its.menu_item_instances do |i|
|
|
instan.id i.id
|
|
instances.push(instan)
|
|
end
|
|
item_sets.push(sets)
|
|
end
|
|
|
|
menu_items.attributes attr_format
|
|
menu_items.options opt_format
|
|
|
|
instances = Array new()
|
|
item.menu_item_instances do |is|
|
|
if is.is_available
|
|
# Convert id to name for attributes
|
|
instance_attr = []
|
|
|
|
is.item_attributes.each do |ia|
|
|
# mItemAttr = MenuItemAttribute.find(is)
|
|
# instance_attr.push(ia)
|
|
mItemAttr = MenuItemAttribute.find(ia).name
|
|
instance_attr.push(mItemAttr)
|
|
end
|
|
|
|
instance.id is.id
|
|
instance.code is.item_instance_code
|
|
instance.name is.item_instance_name
|
|
instance.price is.price
|
|
instance.is_available is.is_available
|
|
instance.is_default is.is_default
|
|
instance.is_on_promotion is.is_on_promotion
|
|
instance.promotion_price is.promotion_price
|
|
instance.values instance_attr
|
|
# json.item_sets is.item_sets
|
|
end
|
|
instances.push(instance)
|
|
end
|
|
end
|
|
item.push(menu_items)
|
|
end
|
|
return item
|
|
end
|
|
end
|
|
|
|
|
|
|
|
# private
|
|
|
|
# def generate_menu_item_code
|
|
# self.item_code = SeedGenerator.generate_code(self.class.name, "I")
|
|
# end
|
|
|
|
end
|