diff --git a/app/controllers/settings/promotions_controller.rb b/app/controllers/settings/promotions_controller.rb index ef972df9..af8bedc1 100644 --- a/app/controllers/settings/promotions_controller.rb +++ b/app/controllers/settings/promotions_controller.rb @@ -76,6 +76,17 @@ class Settings::PromotionsController < ApplicationController 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 + private # Use callbacks to share common setup or constraints between actions. def set_promotion diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index e7f53dcd..7ea5c3ad 100644 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -19,6 +19,7 @@ class MenuItem < ApplicationRecord 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 diff --git a/app/views/settings/promotions/_form.html.erb b/app/views/settings/promotions/_form.html.erb index 1e4a3f86..7ac413d0 100644 --- a/app/views/settings/promotions/_form.html.erb +++ b/app/views/settings/promotions/_form.html.erb @@ -49,32 +49,79 @@
-
<%= f.input :original_product,collection: MenuItem.order("name desc").pluck(:name,:item_code),input_html: { selected: 2 } %>
+ <% arr = MenuItem.active.order("name desc").pluck(:name,:item_code) %> + <% Product.order("name desc").pluck(:name,:item_code).each do |p| %> + <% arr.push(p) %> + <% end %> +
+ + + +
+ + <% sample = [] %> + <% if !@promotion.original_product.nil? %> + <% if !MenuItemInstance.find_by_item_instance_code(@promotion.original_product).nil? %> + <% sample = MenuItemInstance.where("item_instance_code=?",@promotion.original_product).pluck(:item_instance_name,:item_instance_code)%> + <% else %> + <% sample = Product.where("item_code=?",@promotion.original_product).pluck(:name,:item_code)%> + <% end %> + <% end %> +
<%= f.input :original_product,collection: sample %>
+
<%= f.input :min_qty %>

-
Item Code
+
Item Code
Min Qty
Net off
Net Price
-
Percentage
-
+
Percentage
+
<%= f.fields_for :promotion_products do |pro| %>
-
<%= pro.input :item_code, label: false,collection: MenuItem.order("name desc").pluck(:name,:item_code)%>
+ <% arr = MenuItem.active.order("name desc").pluck(:name,:item_code) %> + <% Product.order("name desc").pluck(:name,:item_code).each do |p| %> + <% arr.push(p) %> + <% end %> +
+ +
+ <% sample = [] %> +
<%= pro.input :item_code, :class => 'promoproduct', collection: sample,input_html: { selected: 2 }, label: false %>
<%= pro.input :min_qty , label: false%>
<%= pro.input :net_off , label: false %>
<%= pro.input :net_price , label: false %>
-
<%= pro.input :percentage , label: false %>
-
<%= pro.link_to_remove "X" %>
+
<%= pro.input :percentage , label: false %>
+
<%= pro.link_to_remove "X" %>
<% end %>
-
<%= f.link_to_add "Add Product", :promotion_products, :class => 'btn btn-primary' %>
+
<%= f.link_to_add "Add Product", :promotion_products, :class => 'btn btn-primary addProduct' %>
@@ -103,5 +150,56 @@ $(document).ready(function(){ datepicker:false, format:'H:m' }); + $("#promotion_original_product").select2(); + $(".item_code_place").select2(); + $(".item_code_place").on('change', function(event) { + var ajax_url = "<%= settings_find_item_instance_path %>"; + var item_code = this.value; + $.ajax({ + type: "GET", + url: ajax_url, + data: 'item_code=' + item_code, + success: function (result) { + $("#promotion_original_product").empty(); + var itemlist; + for (var i = 0; i < result.length; i++) { + itemlist += "" + } + $("#promotion_original_product").append(itemlist); + $("#promotion_original_product").select2(); + } + }); + }); + $(".promotion_promotion_products_item_code select").select2(); + $(".item_code_place1").select2(); + callforpromoproduct(); + $(".addProduct").on('click', function(event) { + setTimeout(function(){ + $(".promotion_promotion_products_item_code select").select2(); + callforpromoproduct(); + }, 0); + }); + function callforpromoproduct(){ + $(".item_code_place1").select2(); + $(".item_code_place1").on('change', function(event) { + id = $(this).parent().next().find("select").attr("id"); + var ajax_url = "<%= settings_find_item_instance_path %>"; + var item_code = this.value; + $.ajax({ + type: "GET", + url: ajax_url, + data: 'item_code=' + item_code, + success: function (result) { + $("#"+id).empty(); + var itemlist; + for (var i = 0; i < result.length; i++) { + itemlist += "" + } + $("#"+id).append(itemlist); + $("#"+id).select2(); + } + }); + }); + } }); diff --git a/config/routes.rb b/config/routes.rb index 1130bf36..b095e099 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -292,6 +292,7 @@ Rails.application.routes.draw do resources :promotions do resources :promotion_products end + get '/find_item_instance' => 'promotions#find_item_instance', as:'find_item_instance' # commission resources :commissions