Files
sx-fc/app/controllers/settings/promotions_controller.rb
2017-09-06 17:01:33 +06:30

120 lines
4.0 KiB
Ruby

class Settings::PromotionsController < ApplicationController
before_action :set_promotion, only: [:show, :edit, :update, :destroy]
# GET /promotions
# GET /promotions.json
def index
@promotions = Promotion.all.order("created_at asc")
end
# GET /promotions/1
# GET /promotions/1.json
def show
end
# GET /promotions/new
def new
@promotion = Promotion.new
@promotion.promo_start_date = DateTime.now
@promotion.promo_end_date = DateTime.now
end
# GET /promotions/1/edit
def edit
end
# POST /promotions
# POST /promotions.json
def create
@promotion = Promotion.new(promotion_params)
@promotion.created_by = current_login_employee.id
@promotion.promo_start_hour = @promotion.promo_start_hour.to_datetime.advance(hours: +6, minutes: +30)
@promotion.promo_end_hour = @promotion.promo_end_hour.to_datetime.advance(hours: +6, minutes: +30)
respond_to do |format|
if @promotion.save
promo_pros = @promotion.promotion_products
promo_pros.each do |promo_pro|
promo_pro.save
end
format.html { redirect_to settings_promotions_path, notice: 'Promotion was successfully created.' }
format.json { render :show, status: :created, location: @promotion }
else
format.html { render :new }
format.json { render json: @promotion.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /promotions/1
# PATCH/PUT /promotions/1.json
def update
respond_to do |format|
if @promotion.update(promotion_params)
@promotion.promo_start_hour = @promotion.promo_start_hour.to_datetime.advance(hours: +6, minutes: +30)
@promotion.promo_end_hour = @promotion.promo_end_hour.to_datetime.advance(hours: +6, minutes: +30)
@promotion.save
promo_pros = @promotion.promotion_products
promo_pros.each do |promo_pro|
promo_pro.save
end
format.html { redirect_to settings_promotions_path, notice: 'Promotion was successfully updated.' }
format.json { render :show, status: :ok, location: @promotion }
else
format.html { render :edit }
format.json { render json: @promotion.errors, status: :unprocessable_entity }
end
end
end
# DELETE /promotions/1
# DELETE /promotions/1.json
def destroy
@promotion.promotion_products.each do |pp|
pp.destroy
end
@promotion.destroy
respond_to do |format|
format.html { redirect_to settings_promotions_path, notice: 'Promotion was successfully destroyed.' }
format.json { head :no_content }
end
end
def find_item_instance
item = MenuItem.find_by_item_code(params[:item_code])
if item.nil?
product = Product.where("item_code = ?",params[:item_code]).pluck(:name,:item_code)
render json: product
else
menu_instance = MenuItemInstance.where("menu_item_id = ?",item.id).pluck(:item_instance_name,:item_instance_code)
render json: menu_instance
end
end
def find_parent_item
res = []
item = MenuItemInstance.find_by_item_instance_code(params[:item_instance_code])
if item.nil?
product = Product.where("item_code = ?",params[:item_instance_code]).pluck(:name,:item_code)
res.push(product.name)
res.push(product.item_code)
else
# menu_item = item.menu_item.pluck(:name,:item_code)
res.push(item.item_instance_name)
res.push(item.menu_item.item_code)
end
render json: res
end
private
# Use callbacks to share common setup or constraints between actions.
def set_promotion
@promotion = Promotion.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def promotion_params
params.require(:promotion).permit(:promo_code, :promo_start_date, :promo_end_date, :promo_start_hour,:promo_end_hour ,:promo_day, :promo_type,:original_product ,:min_qty ,:created_by,
:promotion_products_attributes => [:id,:item_code, :min_qty, :net_off, :net_price, :percentage, :_destroy])
end
end