Files
sx-fc/app/controllers/settings/menus_controller.rb
2018-11-16 14:51:40 +06:30

173 lines
6.2 KiB
Ruby
Executable File

class Settings::MenusController < ApplicationController
load_and_authorize_resource except: [:create]
before_action :set_settings_menu, only: [:show, :edit, :update, :destroy]
# GET /settings/menus
# GET /settings/menus.json
def index
@settings_menus = Menu.all.page(params[:page]).per(10)
respond_to do |format|
format.html
format.xlsx
end
end
# GET /settings/menus/1
# GET /settings/menus/1.json
def show
@settings_menu_categories = @settings_menu.menu_categories.page(params[:page]).per(10)
end
# GET /settings/menus/new
def new
@settings_menu = Menu.new
end
# GET /settings/menus/1/edit
def edit
end
# POST /settings/menus
# POST /settings/menus.json
def create
@settings_menu = Menu.new(settings_menu_params)
@settings_menu.created_by = current_login_employee.name
respond_to do |format|
if @settings_menu.save
format.html { redirect_to settings_menus_path, notice: 'Menu was successfully created.' }
format.json { render :show, status: :created, location: @settings_menu }
else
format.html { render :new }
format.json { render json: @settings_menu.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /settings/menus/1
# PATCH/PUT /settings/menus/1.json
def update
respond_to do |format|
if @settings_menu.update(settings_menu_params)
format.html { redirect_to settings_menus_path, notice: 'Menu was successfully updated.' }
format.json { render :show, status: :ok, location: @settings_menu }
else
format.html { render :edit }
format.json { render json: @settings_menu.errors, status: :unprocessable_entity }
end
end
end
# DELETE /settings/menus/1
# DELETE /settings/menus/1.json
def destroy
# @settings_menu.destroy
abc = Menu.destroyMenu(@settings_menu)
# @settings_menu_item_set.destroy
flash[:notice] = 'Menu was successfully destroyed.'
render :json => {:status=> "Success", :url => settings_menus_path }.to_json
# respond_to do |format|
# format.html { redirect_to settings_menus_path, notice: 'Menu was successfully destroyed.' }
# format.json { head :no_content }
# end
end
def export
@settings_menus = Menu.all.page(params[:page]).per(10)
menu = Menu.find(params[:id])
p = Axlsx::Package.new
wb = p.workbook
wb.styles do |s|
time_format = wb.styles.add_style :format_code => 'hh:mm:ss'
title = s.add_style :fg_color => "004586",
:b => true,
:sz => 12
wrap_text = s.add_style :sz => 11,
:alignment => { :horizontal => :left,:vertical => :center ,
:wrap_text => true}
header_text = s.add_style :fg_color=> "090909",
:b => true,
:sz => 12,
:border => { :style => :thin, :color => "00" },
:alignment => { :horizontal => :left,
:vertical => :center ,
:header_text => true}
# Menu Sheet
wb.add_worksheet(name: menu.name,is_ordering: menu.is_ordering) do |sheet|
sheet.add_row ["Name",menu.name,"is_ordering",menu.is_ordering], :style=>title
sheet.add_row
sheet.add_row ["Category Code", "Category Name", "Item Code", "Item Name", "Account", "Item AltName", "Taxable", "Attributes", "Options", "Instance Code", "Instance Name", "Instance Attribute", "Price", "Is Default", "Image Path"], :style=>header_text
menu.menu_categories.each do |mc|
mc.menu_items.each do |mi|
attributes = ""
i=0
if mi.item_attributes.length > 0
mi.item_attributes.each do |mia|
attribute = MenuItemAttribute.find(mia)
if i == mi.item_attributes.length - 1
attributes = attributes + attribute.name
else
attributes = attributes + attribute.name + ","
end
i = i + 1
end
end
image_path = ""
if mi.image_path
image_path = mi.image_path.to_s.remove("/image/menu_images/")
end
options = ""
i=0
if mi.item_options.length > 0
mi.item_options.each do |mio|
option = MenuItemOption.find(mio)
if i == mi.item_options.length - 1
options = options + option.name
else
options = options + option.name + ","
end
i = i + 1
end
end
mi.menu_item_instances.each do |mii|
attribute = ""
if mii.item_attributes.length > 0
attribute = MenuItemAttribute.find(mii.item_attributes[0]).name
end
sheet.add_row [ mc.code, mc.name, mi.item_code, mi.name, mi.account.title, mi.alt_name, mi.taxable, attributes, options, mii.item_instance_code, mii.item_instance_name, attribute, mii.price, mii.is_default, image_path], :style=>wrap_text
end
end
end
end
end
directory_name = "public/menus"
Dir.mkdir(directory_name) unless File.exists?(directory_name)
file_path = directory_name +"/" + menu.name + ".xlsx"
p.serialize(file_path)
render :json => { status: true, path: file_path }
end
def import
if params[:file]
status = Menu.import(params[:file], current_user.name)
redirect_to settings_menus_path, notice: status
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_settings_menu
@settings_menu = Menu.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def settings_menu_params
params.require(:menu).permit(:name, :is_active, :is_ordering, :valid_days, :valid_time_to, :valid_time_from)
end
end